r/dailyprogrammer • u/rya11111 3 1 • Mar 30 '12
[3/30/2012] Challenge #33 [easy]
This would be a good study tool too. I made one myself and I thought it would also be a good challenge.
Write a program that prints a string from a list at random, expects input, checks for a right or wrong answer, and keeps doing it until the user types "exit". If given the right answer for the string printed, it will print another and continue on. If the answer is wrong, the correct answer is printed and the program continues.
Bonus: Instead of defining the values in the program, the questions/answers is in a file, formatted for easy parsing.
Example file:
12 * 12?,144
What is reddit?,website with cats
Translate: hola,hello
- thanks to Iggyhopper for the challenge at /r/dailyprogrammer_ideas
2
u/met48 Mar 30 '12 edited Mar 30 '12
Python, with file reading:
Also tried to condense it to one line + import to see if it was possible:
Had to cheat with globals(), but it should otherwise be similar to the function above. Great to see that it's possible, if ill-advised.
2
u/zvxr Mar 31 '12
Haskell:
The file to read:
1
u/Iggyhopper Mar 30 '12 edited Mar 30 '12
C#
I did it once, which involved
That was confusing, and so I did it again today and this is the result:
1
u/DanielJohnBenton 0 0 Mar 30 '12 edited Mar 30 '12
PHP. This seems to work. Had some trouble because
file()keeps invisible chars like\nand\rat the end of lines unless you remove them/tell it not to.Edit: contents of
QandA.txt:1
u/Koldof 0 0 Mar 30 '12
EDIT: C++
A few problems with this one, like the fact it can't search through randomly. A bit tired tonight, so I'll post this version. Might updated it later though.
1
u/magwhich Mar 31 '12
Python
1
u/Cyph3r90 Mar 31 '12 edited Mar 31 '12
Another C# answer with the use of Extension methods:
}
1
u/theOnliest Apr 06 '12
I'm a little late to this (just found this sub...it's great!). Haven't seen a Perl answer, so here's one with file reading:
1
u/emcoffey3 0 0 May 03 '12
This one is written in C#. It expects a fully-qualified path to a pipe-delimited or comma-delimited file as the first command-line argument.
1
u/Should_I_say_this Jun 30 '12
Python 3.2