Daily Programming Challenge:

Programming (no configs, no support)
Forum rules
Only original work ;)
User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Daily Programming Challenge:

Unread post by DebianJoe » Tue Dec 02, 2014 11:14 am

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!

Image
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. :)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
harveyhunt
Haxxor
Posts: 125
Joined: Mon Jul 07, 2014 3:06 am
Contact:

Re: Daily Programming Challenge:

Unread post by harveyhunt » Tue Dec 02, 2014 2:51 pm

An embarassingly ugly one liner in python - I might neaten this up when I get back from uni. :-)

Code: Select all

sum(list(map(lambda x: int(x) ** 2, input("Enter sides: ").split()))) ** 0.5
Edit: This is python 3 and you should separate side lengths with a space.

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Daily Programming Challenge:

Unread post by pidsley » Tue Dec 02, 2014 6:23 pm

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:

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
This one's almost a function, for part two:

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
And here's part one again, especially for Joe:

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)) 

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: Daily Programming Challenge:

Unread post by GekkoP » Tue Dec 02, 2014 7:31 pm

My solution is a blatant Clojure-version of a Scheme example from SICP. So I guess I'm cheating...

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)))
Here's in action:

Code: Select all

user> (hypotenuse 3 4)
5.000023178253949
user> (right-triangle? 3 4 5)
true
user> (right-triangle? 5 4 3)
false
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).

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Tue Dec 02, 2014 8:14 pm

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.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Daily Programming Challenge:

Unread post by pidsley » Tue Dec 02, 2014 8:16 pm

^ 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. ;)

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Tue Dec 02, 2014 8:49 pm

pidsley 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. ;)
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. :D
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Daily Programming Challenge:

Unread post by pidsley » Tue Dec 02, 2014 8:56 pm

^ 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.

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Tue Dec 02, 2014 9:29 pm

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"

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: 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.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Daily Programming Challenge:

Unread post by pidsley » Tue Dec 02, 2014 11:53 pm

DebianJoe wrote:Edit: Coming soon "Learn Haskell in < 1 hour: Professor Pidsley" :)
More like "Learn just enough Haskell to make it look like you almost know what you are doing" ;) But it's a start.

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Wed Dec 03, 2014 6:31 am

Image

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)))))
Usage: (fizzbuzz 1)
May do it in ghostlisp later.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Wed Dec 03, 2014 8:40 am

zazen23 wrote:goto10 goto20 goto30 goto^n
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".
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Wed Dec 03, 2014 9:41 am

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)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: Daily Programming Challenge:

Unread post by GekkoP » Wed Dec 03, 2014 10:08 am

FizzBuzz

Code: Select all

(map #(cond (zero? (mod % 3)) "Fizz"
            (zero? (mod % 5)) "Buzz")
     (range 1 101))

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Wed Dec 03, 2014 11:06 am

@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.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: Daily Programming Challenge:

Unread post by GekkoP » Wed Dec 03, 2014 11:20 am

^ I love anonymous functions, but I still have to get used to them. Plus, map in Clojure is really a powerful item.

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Daily Programming Challenge:

Unread post by pidsley » Wed Dec 03, 2014 5:52 pm

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.

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');}}
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.

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Wed Dec 03, 2014 11:57 pm

@ 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.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Thu Dec 04, 2014 7:04 am

Today/Tomorrow's Challenge:
Image

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
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Thu Dec 04, 2014 8:50 am

Chicken Scheme solution - Gandalf Mode:

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"))))
Scrot or it didn't happen?
2014-12-04-014659_1024x768_scrot.png
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.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

Post Reply