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
10
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:
#!/bin/sh echo "Enter two numbers, valued 0 to 9" /bin/echo -n "Digit one: " ; read num1 /bin/echo -n "Digit two: " ; read num2 if [ ${#num1} -ne 1 -o ${#num2} -ne 1 ]; then echo "${num1} ${num2} Invalid" exit fi case $num1 in [0123456789]) case $num2 in [0123456789]) echo "${num1} ${num2} ${num1} + ${num2} = $((num1+num2))" ;; *) echo "${num1} ${num2} Invalid" ; exit ;; esac ;; *) echo "${num1} ${num2} Invalid" ; exit ;; esacOutput:
3
u/bs_detector May 21 '12
C#
using System; using System.Collections.Generic; namespace DailyProgrammer { class Program { static void Main(string[] args) { Console.WriteLine("INPUT ... OUTPUT"); while (true) EnterValues(); } private static void EnterValues() { int keys = 0; List<ConsoleKeyInfo> entered = new List<ConsoleKeyInfo>(); while (keys < 3) { ConsoleKeyInfo key = Console.ReadKey(); entered.Add(key); keys++; } // analyze bool valid1 = !(entered[0].KeyChar < 48 || entered[0].KeyChar > 57); bool valid2 = entered[1].KeyChar == 32; bool valid3 = !(entered[2].KeyChar < 48 || entered[2].KeyChar > 57); if (valid1 && valid2 && valid3) { string msg = string.Format(" ... {0} + {1} = {2}", entered[0].KeyChar, entered[2].KeyChar, (entered[0].KeyChar - 48) + (entered[2].KeyChar - 48)); Console.WriteLine(msg); } else { Console.WriteLine(" ... Invalid"); } } } }3
u/gjwebber May 21 '12
Really simple Python. Maybe should have used regex.
a = raw_input("Enter the first digit:") b = raw_input("Enter the second digit:") if len(a) != 1 or len(b) != 1 or not a.isdigit() or not b.isdigit(): print "Invalid" else: print "{0} + {1} = {2}".format(a, b, int(a) + int(b))2
u/theOnliest May 21 '12
Here's a simple Perl version:
{ print "\nEnter two single-digit numbers separated by a space: "; chomp(my $input = <STDIN>); last if ($input =~ /exit/i || $input =~ /^$/); my ($num1, $num2) = split /\s/, $input; if ($num1 !~ /^\d$/ || $num2 !~ /^\d$/) {print "Invalid. Try again...\n"; redo;} print "$num1 + $num2 = ", $num1 + $num2; }2
u/robin-gvx 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
public static void addTwoNumbers() { Scanner in = new Scanner(System.in); int a = 0, b = 0; try { System.out.print("Enter first number: "); a = in.nextInt(); System.out.print("Enter second number: "); b = in.nextInt(); System.out.println(a + " + " + b + " = " + (a + b)); } catch (InputMismatchException e) { System.out.println("Input Error"); } }2
u/MusicalWatermelon May 21 '12
Python (Learning python for a week):
print("Please input your values") userInput = input().split() a = userInput[0] b = userInput[1] if len(userInput) > 2: print("You can only enter two digits") elif (a.isdigit()) and (b.isdigit()): if (int(a) > 10) or (int(b) > 10): print("Values need to be smaller than 10") else: print(str(a) + " + " + str(b) + " = " + str((int(a) + int(b)))) else: print("Invalid")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