Daily Programming Challenge:
Forum rules
Only original work ;)
Only original work ;)
Daily Programming Challenge:
Fuck, I'm so sick of WM's and init wars and editor wars and language wars. So, in order to not fall off of the face of the planet (again), I propose that we start some algorithm writing!
Rules:
1). Don't cheat. Kill/Yanking someone else's code is for pussies. I won't double-check you, but you're a bitch if you cheat.
2). Doesn't matter what language you use, but some languages include functions that automate too much. Abstracting is to be avoided if possible.
3). I won't make a request that I'm not pretty damn sure I can do. If you want, you can challenge me to do the task. If I can't do it, I'll admit to being a scrub.
4). Constructive criticism can be helpful, but is often unappreciated. If a program meets the requirements, then it works. If you can do it better, then do it, and simply talk about how yours is totally bad-ass and awesome. Leave the other person out of it.
5). Have fun! This is meant to be like sudoku for hackers.
-----Challenge #1-----
Scripting Guru Mode - Write a function/set-of-functions that displays the length of the hypotenuse of a right triangle given the other two sides.
Gandalf Mode - Write a function/set-of-functions that given 3 sides of a triangle determines if it is a right triangle within <0.0001 (foo_measurement_unit) accuracy, DO NOT IMPORT MATH LIBS/HEADERS. Write all of the dependent functions using only comparison and +,-,/,* logic. Post "You shall not pass" at the end of your code block. :)
Rules:
1). Don't cheat. Kill/Yanking someone else's code is for pussies. I won't double-check you, but you're a bitch if you cheat.
2). Doesn't matter what language you use, but some languages include functions that automate too much. Abstracting is to be avoided if possible.
3). I won't make a request that I'm not pretty damn sure I can do. If you want, you can challenge me to do the task. If I can't do it, I'll admit to being a scrub.
4). Constructive criticism can be helpful, but is often unappreciated. If a program meets the requirements, then it works. If you can do it better, then do it, and simply talk about how yours is totally bad-ass and awesome. Leave the other person out of it.
5). Have fun! This is meant to be like sudoku for hackers.
-----Challenge #1-----
Scripting Guru Mode - Write a function/set-of-functions that displays the length of the hypotenuse of a right triangle given the other two sides.
Gandalf Mode - Write a function/set-of-functions that given 3 sides of a triangle determines if it is a right triangle within <0.0001 (foo_measurement_unit) accuracy, DO NOT IMPORT MATH LIBS/HEADERS. Write all of the dependent functions using only comparison and +,-,/,* logic. Post "You shall not pass" at the end of your code block. :)
- harveyhunt
- Haxxor
- Posts: 125
- Joined: Mon Jul 07, 2014 3:06 am
- Contact:
Re: Daily Programming Challenge:
An embarassingly ugly one liner in python - I might neaten this up when I get back from uni. :-)
Edit: This is python 3 and you should separate side lengths with a space.
Code: Select all
sum(list(map(lambda x: int(x) ** 2, input("Enter sides: ").split()))) ** 0.5
Re: Daily Programming Challenge:
Nice topic idea Joe. I have been looking for an excuse to play with some different languages, and brush up skills on others.
Not really a function, but works for part one:
This one's almost a function, for part two:
And here's part one again, especially for Joe:
Not really a function, but works for part one:
Code: Select all
program hypotenuse
implicit none
real :: a,b,h
print *, "side one?"
read *, a
print *, "side two?"
read *, b
h = sqrt(a*a + b*b)
print *, "hypotenuse is:", h
end
Code: Select all
subroutine isright(a,b,h,c)
implicit none
real :: a,b,h,ht,hr,c
ht = a*a + b*b
hr = h*h
c = ht - hr
if (c < 0) c = -c
print *, "confidence: ", c
if (c < 0.0001) print *, "probably a right triangle"
print *, "you shall not pass!"
end subroutine isright
Code: Select all
f x y = sqrt(x*x + y*y)
main = do
putStrLn "side one?"
a <- getLine
putStrLn "side two?"
b <- getLine
print (f (read a) (read b))
Re: Daily Programming Challenge:
My solution is a blatant Clojure-version of a Scheme example from SICP. So I guess I'm cheating...
Here's in action:
Ok, sorry about that. Promise I'll come up with my own solutions for the next challenges. I think I'll stick with Clojure, because I need to practice it (so thanks for the topic).
Code: Select all
(defn abs [n]
(cond (> n 0) n
(= n 0) 0
:else (- n)))
(defn average [a b]
(/ (+ a b) 2))
(defn improve [guess n]
(average guess (/ n guess)))
(defn square [n]
(* n n))
(defn sum-of-squares [a b]
(+ (square a) (square b)))
(defn good-enough? [guess n]
(< (abs (- (square guess) n)) 0.001))
(defn square-root [n]
(letfn [(square-root-iter [guess n]
(if (good-enough? guess n)
guess
(square-root-iter (improve guess n)
n)))]
(square-root-iter 1.0 n)))
(defn hypotenuse [s1 s2]
(square-root (sum-of-squares s1 s2)))
(defn right-triangle? [s1 s2 s3]
(= (sum-of-squares s1 s2) (square s3)))
Code: Select all
user> (hypotenuse 3 4)
5.000023178253949
user> (right-triangle? 3 4 5)
true
user> (right-triangle? 5 4 3)
false
Re: Daily Programming Challenge:
A script one-liner and double Gandalf posts! :) I am so happy with these. Just to play along I'll try to do something silly and fun.
Re: Daily Programming Challenge:
^ Speaking of silly, did you notice I did Haskell just for you? I also did them in C, but I won't expose those to ridicule. My C skills are weak at this point. I am not afraid of people laughing at my Fortran and Haskell. ;)
Re: Daily Programming Challenge:
Yes, yes I did. Still smiling like a dumbass over it. You pick up Haskell before or after I started joking about you learning Haskell? Because if it's after, that's a scary grasp on the language already. :Dpidsley wrote:^ Speaking of silly, did you notice I did Haskell just for you? I also did them in C, but I won't expose those to ridicule. My C skills are weak at this point. I am not afraid of people laughing at my Fortran and Haskell. ;)
Re: Daily Programming Challenge:
^ After! It was a very painful hour or two this morning.
It helps if I have a task, a goal, whatever you want to call it, to help me learn. So thanks for this topic.
It helps if I have a task, a goal, whatever you want to call it, to help me learn. So thanks for this topic.
Re: Daily Programming Challenge:
You are certainly welcome for the thread. Freaking "Haskell in one morning...." nuts!
Here, here's my fun one "Pretend to be Clojure pretending to be Scheme, Ghostlisp Version"
Edit: Coming soon "Learn Haskell in < 1 hour: Professor Pidsley" :)
Edit #2: Obviously, this example works like GekkoP's. hypotenuse, right-triangle....you know, really, we should both have written a reduction via "if" or "cond" statements that allows right-triangle checking to work regardless of the input order...oh well.
Here, here's my fun one "Pretend to be Clojure pretending to be Scheme, Ghostlisp Version"
Code: Select all
;; Backend to fake GekkoP's work so
;; we can "recursively steal from SICP"
(def {defn} (\ { f b} {
def (car f) (\ (cdr f) b)
}))
(defn {abs x} {
if (< x 0) {* -1 x} {x}})
(defn {average a b} {/ (+ a b) 2})
(defn {improve guess n} {average guess (/ n guess)})
(defn {square n} {* n n})
(defn {sum-of-squares a b} {+ (* a a) (* b b)})
(defn {good-enough guess n}
{< (abs (- (square guess) n)) 0.000001})
(defn {square-root-iter guess n}
{if (good-enough guess x)
{guess}
{square-root-iter (improve guess x) x}})
(defn {sqrt x} {square-root-iter 1.0 x})
(defn {hypotenuse s1 s2} {sqrt (sum-of-squares s1 s2)})
(defn {right-triangle a b c}
{if (< (- (abs (sum-of-squares a b)) (abs (square c))) 0.0001)
{print "you shall not PASS"}
{print "false"}})
Edit #2: Obviously, this example works like GekkoP's. hypotenuse, right-triangle....you know, really, we should both have written a reduction via "if" or "cond" statements that allows right-triangle checking to work regardless of the input order...oh well.
Re: Daily Programming Challenge:
More like "Learn just enough Haskell to make it look like you almost know what you are doing" ;) But it's a start.DebianJoe wrote:Edit: Coming soon "Learn Haskell in < 1 hour: Professor Pidsley" :)
Re: Daily Programming Challenge:
Tomorrow/Today's challenge.
Where would any exercise in trivial coding be without a FizzBuzz? That being said, it's a pretty well known fact that about 90% of applicants will fail at creating a working FizzBuzz during the interview process (...ask me why I know this.-_-) Anyhow, that's today's challenge:
Normal Mode: Make a fizzbuzz representation that works from 1-100.
Gandalf Mode: You have transcended the mortal trappings of such nonsense as line-breaks, sane variable naming, standards, and commenting. Do the above in the most obfuscated one-liner possible. You don't care if you get the job, your code is self-satisfaction incarnate and your future-self must suffer to reach nirvana. :)
---chicken scheme gandalf mode---
Code: Select all
(define (fizzbuzz n)(if (< n 101)(begin (cond ((and (= (remainder n 5) 0) (= (remainder n 3) 0)) (print "FizzBuzz"))((= (remainder n 5) 0) (print "Buzz"))((= (remainder n 3) 0) (print "Fizz"))(else (print n)))(fizzbuzz (+ n 1)))))
May do it in ghostlisp later.
Re: Daily Programming Challenge:
Arrr, ye foul demon of the name "Goto," so we meet again. Back! Back I say. Back to the burning hells to which ye were banished along with your hideous brother "Gosub".zazen23 wrote:goto10 goto20 goto30 goto^n
Re: Daily Programming Challenge:
Python one-liner, string concatenation variation:
Code: Select all
for n in range(1,101): print("fizz"*(n%3==0) + "buzz"*(n%5==0) or n)
Re: Daily Programming Challenge:
FizzBuzz
Code: Select all
(map #(cond (zero? (mod % 3)) "Fizz"
(zero? (mod % 5)) "Buzz")
(range 1 101))
Re: Daily Programming Challenge:
@Zazen: So you use the Vice emulator alone, or do you still have a C64 around? I had a TRS-80, and I loved that thing. I can still hear the sound of loading programs from the cassette. "RrrrRRRRrrRRRrrRRRRRR" :D
@GP: <.< >.>....that's a really clean way to get it done.
I guess as GekkoP says he needs Clojure work, I'll stick to Chicken because I am madly in love with the inline-C compiling with it. Ghosty is just a little TOO primitive for some cases yet. I did write most of the fizzbuzz in it, but holy hell, if you don't import the stdlib, you end up having to write out "nil" and "default" cases, the 'cond' statement, etc, from scratch.
@GP: <.< >.>....that's a really clean way to get it done.
I guess as GekkoP says he needs Clojure work, I'll stick to Chicken because I am madly in love with the inline-C compiling with it. Ghosty is just a little TOO primitive for some cases yet. I did write most of the fizzbuzz in it, but holy hell, if you don't import the stdlib, you end up having to write out "nil" and "default" cases, the 'cond' statement, etc, from scratch.
Re: Daily Programming Challenge:
^ I love anonymous functions, but I still have to get used to them. Plus, map in Clojure is really a powerful item.
Re: Daily Programming Challenge:
You guys and your fancy code. Very impressive. This is not fancy, but it works. That's pretty much how I do everything in my life, so I'm OK with it.
I have failed miserably at Haskell today. I can get close, but then I get some errors I don't understand, and reading about them just makes me more confused. I will never be cool. So sad.
zazen23 seems to have cornered the market on ancient language, so I won't post my Fortran77 solution.
Code: Select all
main(){int a,b,c;for(c=1;c<101;c++){a=(c%3==0);b=(c%5==0);if(a)printf("%s","fizz");if(b)printf("%s","buzz");if(!a&&!b)printf("%i",c);putchar('\n');}}
zazen23 seems to have cornered the market on ancient language, so I won't post my Fortran77 solution.
Re: Daily Programming Challenge:
@ Pidsley, that's good stuff. Fancy code or perfectly concise solutions aren't my purpose for doing this at all. That's precisely the reason for my choice of Scheme for my solutions (as opposed to Python 2). You ever watch the Kung Fu movies with the unlikely hero that goes to train in the mountains with a crazy hermit and then by practicing a form that he/she is weak in day in and day out...he eventually has some kind of mystic enlightenment and is able to defeat the gang of thugs with some technique that he's been practicing alone for months? Yep, that's what I hope to accomplish. Getting better at something that I'm weak in by doing it a lot. (That being said, Haskell is like reading Egyptian Hieroglyphs to me, I can admire the pictures, but I've got very little idea of what is being said.)
@Zazen: RIP C64, you will be missed...in a nostalgic way, like 8-track tapes.
@Zazen: RIP C64, you will be missed...in a nostalgic way, like 8-track tapes.
Re: Daily Programming Challenge:
Today/Tomorrow's Challenge:
Normal Mode - Design and implement a function, or input accepting program that given a 2 choice set, determines the winner in a game of Rock/Paper/Scissors.
Gandalf Mode - Do the above, but with Rock/Paper/Scissors/Lizard/Spock. Try to avoid writing single-part compares with 'if' statements. (I know how this works mathematically, just need to figure out how to do it with my chosen language...very possible.)
+ Scissors cut paper
+ Paper covers rock
+ Rock crushes lizard
+ Lizard poisons Spock
+ Spock smashes scissors
+ Scissors decapitate lizard
+ Lizard eats paper
+ Paper disproves Spock
+ Spock vaporizes rock
+ And as it's always been, Rock crushes scissors
Normal Mode - Design and implement a function, or input accepting program that given a 2 choice set, determines the winner in a game of Rock/Paper/Scissors.
Gandalf Mode - Do the above, but with Rock/Paper/Scissors/Lizard/Spock. Try to avoid writing single-part compares with 'if' statements. (I know how this works mathematically, just need to figure out how to do it with my chosen language...very possible.)
+ Scissors cut paper
+ Paper covers rock
+ Rock crushes lizard
+ Lizard poisons Spock
+ Spock smashes scissors
+ Scissors decapitate lizard
+ Lizard eats paper
+ Paper disproves Spock
+ Spock vaporizes rock
+ And as it's always been, Rock crushes scissors
Re: Daily Programming Challenge:
Chicken Scheme solution - Gandalf Mode:
Scrot or it didn't happen?
Fun fact that I did not know. Chicken specifically will allow you to set (imperatively) a global declaration by value alone (set!). The compiler or interpreter will bitch at you, but it will still do it. (Note: the bottom portion of code in the scrot was my next-to-working attempt...and I did the set! in the interpreter. I attempted using let for local assignment, but I'm pretty sure I messed up the syntax for that. It is (let ([x 2]))...right?)
Edit: Removed the redundant 'game' call from code.
Code: Select all
(define (game x y)
(cond
((eqv? x 'rock)(set! a 0))
((eqv? x 'spock)(set! a 1))
((eqv? x 'paper)(set! a 2))
((eqv? x 'lizard)(set! a 3))
((eqv? x 'scissors)(set! a 4))
(else (print "try again, use lowercase")))
(cond
((eqv? y 'rock)(set! b 0))
((eqv? y 'spock)(set! b 1))
((eqv? y 'paper)(set! b 2))
((eqv? y 'lizard)(set! b 3))
((eqv? y 'scissors)(set! b 4)))
(who_won? a b))
(define (who_won? a b)
(set! result (modulo (- a b) 5))
(cond
((= result 0) (print "It's a tie"))
((or (= result 1) (= result 2)) (print "Player 1 Wins"))
(else (print "player 2 wins"))))
Edit: Removed the redundant 'game' call from code.