r/dailyprogrammer • u/rya11111 3 1 • May 21 '12
[5/21/2012] Challenge #55 [intermediate]
Write a program that will allow the user to enter two characters. The program will validate the characters to make sure they are in the range '0' to '9'. The program will display their sum. The output should look like this.
INPUT .... OUTPUT
3 6 ........ 3 + 6 = 9
4 9 ........ 4 + 9 = 13
0 9 ........ 0 + 9 = 9
g 6 ........ Invalid
7 h ........ Invalid
- thanks to frenulem for the challenge at /r/dailyprogrammer_ideas .. please ignore the dots :D .. it was messing with the formatting actually
8
Upvotes
10
u/AlienRaper May 21 '12
I don't know how you come up with the difficulty measure for these.
3
u/totallygeek May 21 '12 edited May 21 '12
Bash:
Output:
3
u/bs_detector May 21 '12
C#
3
u/gjwebber 0 0 May 21 '12
Really simple Python. Maybe should have used regex.
2
u/theOnliest May 21 '12
Here's a simple Perl version:
2
u/robin-gvx 0 2 May 21 '12
The output doesn't really match, but meh:
The second version add ASCII codes, so
5 + 5 = :,d9 + 9 = BandA + B = S. It can calculate any sum of a+b for 0<=a<=5 and 0<=b<=5 and (a<5 or b<5).2
u/[deleted] May 21 '12
Seems pretty unnecessary to worry about input of only 0 - 9
2
u/MusicalWatermelon May 21 '12
Python (Learning python for a week):
2
u/[deleted] May 21 '12
The key part of the challenge was actually for the program to recognise that the input is a character or a larger number, in this case you could just use
but it's a little more complex to recognise characters
2
u/drb226 0 0 May 22 '12
Possibility of failure mingled with
IO? Sounds like a job forMaybeT!Learning time: if the first character you type in is invalid, it won't even wait for you to type in the second. Can you see why it behaves this way? (hint: do you know what
guarddoes? do you know how "do" notation is desugared? do you understand howrunMaybeTworks?) What does this tell you about theMonadinstance ofMaybeT?Note that this is why
MaybeTworks so nicely as a way of "throwing" exceptions;Nothingmeans "an exception occurred", whileJust foomeans "no exception,foois the result". If you need to keep track of which exception occurred, then useErrorT. You can think ofMaybeTin the type signature as being akin to the Java annotationthrows Exception.2
u/[deleted] May 22 '12
PHP / HTML
Live : http://www.aj13.net/dp521
*edit: fixed an error
1
u/cchockley May 22 '12
c++
EDIT: 4 space formatting fail...
1
u/brbpizzatime May 22 '12
Potentially cheating with Java:
1
u/TaiGat May 22 '12
Ruby:
1
u/scootstah May 22 '12
PHP:
1
u/crawphish May 22 '12
Python:
1
u/SwimmingPastaDevil 0 0 May 22 '12
Python: In python, I think a function call returns
Falseby default ifTrueconditions are not met. Am i correct here ?1
u/PenguinKenny May 22 '12 edited May 22 '12
Visual Basic
Output:
1
u/CarNiBore May 22 '12
JavaScript
1
u/Ozera 0 0 May 23 '12
1
u/Medicalizawhat May 24 '12
C:
1
u/loonybean 0 0 Jun 16 '12
Python:
0
u/[deleted] May 22 '12
[deleted] — view deleted comment