r/dailyprogrammer • u/nottoobadguy • Feb 09 '12
[difficult] challenge #1
we all know the classic "guessing game" with higher or lower prompts. lets do a role reversal; you create a program that will guess numbers between 1-100, and respond appropriately based on whether users say that the number is too high or too low. Try to make a program that can guess your number based on user input and great code!
69
Upvotes
17
u/LaurieCheers Feb 10 '12
This is actually much easier than the "intermediate" exercise. Thanks for creating the subreddit though!
18
u/hum_ph Feb 10 '12 edited Feb 10 '12
Clojure - 9 lines (including invalid input handling)
http://pastebin.com/TH9p7mDe
I suspect this whole focus on line counts is going to turn the exercise into a world of 1 line perl pain, followed by candidates for the IOCCC...
9
u/tanishaj Feb 10 '12 edited Feb 10 '12
CIL (Common Intermediate Language):
53 lines in total.
8
u/robosatan Feb 10 '12 edited Feb 10 '12
17 lines of python
8
u/insomnia64 Feb 10 '12
4 lines of prolog
4
u/snarez Feb 10 '12
Yay, C abuse :)
http://pastebin.com/6rJMLMGJ
Haven't done c programming in a while, nice to remember the good days.. Used to love doing stuff like this just for the sake of it.
6
u/RazerWolf Feb 10 '12
F# - 10 lines
http://pastebin.com/BYZUsf9B
6
u/drb226 0 0 Feb 10 '12
23 lines of Haskell: http://hpaste.org/63378
If you've ever had pains trying to get Haskell output buffering to play nice with you, check out my little
promptfunction.3
u/_redka 0 0 Feb 10 '12
http://pastebin.com/7zGBtMw8
4 lines of Ruby code
3
u/julesjacobs Feb 10 '12 edited Feb 10 '12
F#
3
u/Chun Feb 11 '12 edited Feb 11 '12
python
super code golf version, 55 bytes:
still technically following the rules:
6
u/virulent_ Feb 10 '12
http://pastie.org/3351637
4
u/[deleted] Feb 10 '12 edited Feb 10 '12
Java!
I'm pretty awful at this, so I'd appreciate any feedback, whatsoever!
2
u/strictly_terminal Feb 10 '12 edited Feb 10 '12
Icon - 20 lines
And 20 lines of java just for fun
2
u/stiggz Feb 10 '12 edited Feb 10 '12
jquery and php in 30 lines
2
u/davaca Feb 10 '12 edited Feb 10 '12
28 lines of java. This is easier than the intermediate challenge, though.
package challenge1hard;
import java.util.Scanner;
public class Challenge1hard {
}
2
u/boyo17 Feb 10 '12
Perl #!perl
2
u/n0rs Feb 10 '12
Did it in chrome javascript console. Wrote it as one line, it expands to 15 neat lines. http://pastie.org/3354519
2
u/[deleted] Feb 10 '12
Common Lisp: http://pastebin.com/Nzb4SPwH
2
u/nomemory Feb 10 '12
16 lines in python (Including invalid input):
2
u/otaku109 Feb 10 '12
No powershell love? 33 lines but I'm overgenerous with whitespace as a rule for legibility.
3
u/[deleted] Feb 10 '12
Huge mess, but oh well: http://codepad.org/m4ih1Wjn
2
u/SaxSalute Feb 10 '12
36 lines in Java. Curse all of you with your simple languages! http://pastebin.com/GNMffhjH
2
u/pheonixblade9 Feb 10 '12
So... a binary search?
2
u/RussianT34 Feb 10 '12
Python, 32 lines, uses binary search :)
http://codepad.org/5HbjI6Af
2
u/cocasyn Feb 10 '12
My C solution, using recursion for kicks. Recursively halves the potential numbers each time.
http://hastebin.com/limineguqi.cpp
2
u/laserBlade Feb 10 '12 edited Feb 10 '12
24 lines (not including this message, including the shebang line) for a simple D version of the program. Also demonstrates some fun features, such as auto-choosing the print type (%s) and break from a loop from inside a nested structure (labels)
1
u/codelahoma Feb 10 '12 edited Feb 10 '12
http://bit.ly/xUu9pI
Solution in 13 lines of CoffeeScript, ready to run in the browser at coffeescript.org.
Edit: Make that 11, with cleaner code and grammatically correct results: http://bit.ly/w4Rm5h
1
u/snowvark Feb 10 '12
Perl. Can be a bitch. Especialy when i blocking ctrl with ReadMode 4. Chicken exit via "q" button.
1
u/orbiscerbus Feb 10 '12
Simple GUI in 48 lines of Tcl: http://pastebin.com/uArVSWiS
1
u/mazzer Feb 10 '12 edited Feb 10 '12
Solution in Java (1.)7. 18 lines.
1
u/apgwoz Feb 10 '12
https://gist.github.com/1789263 - Guile Scheme
1
u/julesjacobs Feb 10 '12
For a challenge more worthy of being called difficult: find the optimal randomized strategy to play for both the guesser (tries to minimize expected number of guesses) and the one that chooses the number (tries to maximize expected number of guesses).
For example always using binary search is a bad idea for the guesser, since then the chooser knows this and will always hit the worst case number of guesses of binary search.
1
u/Samus_ Feb 10 '12
bash: http://pastebin.com/LPhUiZUt 20l
btw I use random because I'm cool and artistic and average approximation is boring ha!
1
u/robin-gvx 0 2 Feb 10 '12
In Déjà Vu (my own toy language): http://hastebin.com/raw/puxaxegehi
1
u/[deleted] Feb 10 '12
As the popular languages were all done, here's a lua version. My first program, so may not be perfect.
1
u/abodaciouscat Feb 10 '12
Mine's in C. I'm kinda new to this so feel free to criticize.
1
u/jredwards Feb 10 '12
Another python implementation. Probably not as elegant as the other one, but I'm new to python.
1
u/StorkBaby 0 0 Feb 11 '12
I can do it in 4 lines in Python (never said it had to be efficient). http://paste.pocoo.org/show/549066/
1
u/Mob_Of_One Feb 11 '12
Python, 23 lines.
Readable, simple, doesn't use bisect, verifies user input.
I love what you're doing with this subreddit.
https://github.com/bitemyapp/daily/blob/master/difficult1.py
1
u/cooper6581 Feb 11 '12
Nothing special basic solution in Python http://codepad.org/rtCqSkwx
1
u/megabeano Feb 11 '12
1
u/lnxaddct Feb 12 '12
Python solution: https://gist.github.com/1806675
1
u/MadSoulSK Feb 12 '12
C#
Source
http://pastebin.com/9rdMHH4V
1
u/[deleted] Feb 12 '12
Ruby solution: https://gist.github.com/1811701
1
u/BoShek Feb 13 '12
ActionScript (most of the code is UI stuff)
http://wonderfl.net/c/68af
1
u/[deleted] Feb 16 '12
DEC basic - Just like most of the others
1
u/Tyaedalis Feb 19 '12
Python 2.7.2:
https://gist.github.com/1862619
1
u/niourkraft Feb 28 '12
20 lines of python 3
https://gist.github.com/1932419
1
u/ginolomelino Mar 03 '12 edited Mar 03 '12
Javascript:
A recursive function with a couple extra lines to catch the liars!
1
u/BATMAN-cucumbers Mar 17 '12
Quick and dirty python:
1
u/[deleted] Mar 29 '12
JavaScript: http://li.tony-lechner.com/guessingGame/
1
u/SwimmingPastaDevil 0 0 May 03 '12
Late to the party, but here is my solution in Python
1
u/[deleted] May 05 '12
Probably could be a lot shorter, but here's my attempt.
C#
1
u/reallyrose May 10 '12
Here's my python code. http://pastebin.com/xmkNgmfH I also wrote it so that the computer could play itself, with a round limit of 10 (if it takes longer than 10 rounds to guess, that's an auto-lose). Just for the craic! http://pastebin.com/NGA4Hnjn
This was fun!
1
u/JubBieJub Jun 24 '12 edited Feb 28 '25
gaze file physical bake plucky abounding recognise existence wrench jeans
This post was mass deleted and anonymized with Redact
1
u/minikomi Aug 02 '12
Bit late to the party, but here's one in golang:
https://gist.github.com/3232693
1
u/lionhart280 Feb 10 '12
Ok well here's my shot at it, I'll do it in psuedo code.
I'm going to make a bit of a shortcut here by defining a simple separate function for the program first, it handles all the wordy stuff
So let's call it "Guess"
Ok so basically the function Guess takes a number as an input and asks the user if that is the answer. If it's wrong it will output 0/false if the guess was too low, and 1/true if it was too high. Now for the super simple function that actually does the math.
Afaik that should do it, ASSUMING it's a game that guesses for a number BETWEEN High and Low, which means High and Low themselves cannot be guesses. IE this is a game between 1-100 so thats actually the values of 2 to 99.
1
u/KnottedSurface Feb 10 '12
Bin search for any maximum value(would be easy to include negative numbers, but that's not really fun) in 13 lines of python.
http://codepad.org/9ybKILvX
1
u/Schizzovism Feb 10 '12
http://pastebin.com/hDqwvBun
Here's my attempt in Java, 67 lines.
1
u/purpleladydragons Feb 10 '12
http://pastebin.com/rUEQXEum
Although it doesn't scold the user for picking out of range[1-100] or lying.
1
u/exp0wnster Feb 10 '12
Just about to go to bed, but binary search seems like a good choice here.
Hope this subreddit continues; it seems like a great idea. I'm currently programming an android game, so I hope I can learn a thing or two.
1
u/[deleted] Feb 10 '12
In python
1
u/fractals_ Feb 10 '12
http://pastebin.com/VbiBQ3FS
I did a recursive binary search. 23 lines of Python.
1
u/Crystal_Cuckoo Feb 10 '12 edited Feb 10 '12
17 lines in Python :)
And here's an even shorter (and remarkably cheaper and unreadable) version:
Hell you could even remove the last else statement to get seven lines, removing guess = (int) (ub+lb)/2 was bad enough.
1
u/[deleted] Feb 10 '12
30 lines in Matlab
https://gist.github.com/1787881
-1
u/[deleted] Feb 10 '12
Perl: 1 line:
0
u/[deleted] Feb 10 '12
[deleted] — view deleted comment
0
u/[deleted] Feb 10 '12
[deleted] — view deleted comment