Web chat with granny - Validating number in perl


) puts(1,"no solve24 routine\n") else call_proc(r_solve24,) end if else = eval(equation, rset) if error! Otherwise you are repeatedly asked for an expression until it evaluates to 24 Note: you cannot form multiple digit numbers from the supplied digits,so an answer of 12 12 when given 1, 2, 2, and 1 would not be allowed.

arg^-1) & ( convert Division To Multiplication = a z . The dynamic query parser is used to evaluate the expression.

= 0 then random_numbers.add(number)end # Check if used `numbers` exist in the `random_numbers` createdfun numbers_exists(random_numbers, numbers: Array[Int]): Bool do for number in numbers do if not random_numbers.count(number) = numbers.count(number) then return false end return trueend # Remove `numbers` when they are usedfun remove_numbers(random_numbers, numbers: Array[Int]) do for number in numbers do random_numbers.remove(number)end # Check if the mathematical `operation` is validfun check(operation: String): Bool do var previous_char: nullable Char = null var next_char: nullable Char = null var next_1_char: nullable Char = null for i in operation.length.times do var current_char = operation[i] if i 1 1 do var numbers = new Array[Int] var operands = new Array[Char] print "numbers: " random_numbers.join(", ") operation = gets if check(operation) then exportation(operation, numbers, operands) if numbers_exists(random_numbers, numbers) then calculation(random_numbers, numbers, operands) remove_numbers(random_numbers, numbers) else print "NUMBERS ERROR! " endend if random_numbers.has(24) then print "CONGRATULATIONS" else print "YOU LOSE"" The 24 Game Given any four digits in the range 1 to 9, which may have repetitions, Using just the , -, *, and / operators; and the possible use of brackets, (), show how to make an answer of 24. (*) Classic Rexx accepts assignment without an expression (x=;), oo Rexx does not.

] make "done true ] [ (print (sentence quoted [Incorrect \(got ] testval quoted [instead of 24\).])) ] ] ] ]]]] bye Solution for 3 8 9 5? Using only these digits: 3 8 9 5 and these operators: * / - (and parentheses as needed), enter an arithmetic expression which evaluates to exactly 24. s n1=$r(9) 1, n2=$r(9) 1, n3=$r(9) 1, n4=$r(9) 1 ; save a copy of them so that we can keep restarting ; if the user gets it wrong s s1=n1,s2=n2,s3=n3,s4=n4Question s (numcount,opcount,lbrackcount,rbrackcount)=0 ; restart with the random numbers already found s n1=s1,n2=s2,n3=s3,n4=s4 w !

] [ show_help digits ] [ifelse [expression = "q] [ print "Bye! ] [ make "digits [] ] [ make "exprchars ` expression make "allowed (sentence digits operators) ifelse (member? c allowed] exprchars)) [ (print quoted [Illegal character in input.]) ] [ catch "error [ make "syntax_error true make "testval (run expression) make "syntax_error false ] ifelse syntax_error [ (print quoted [Invalid expression.]) ] [ ifelse (testval = 24) [ print quoted [You win! 24Game k number, operator, bracket ; generate 4 random numbers each between 1 & 9 ; duplicates allowed!

print "success:"success" failure:"failure" total:"(success failure) as int let success = 0, failure = 0. our nat seed = 0xc6f31 (: default seed when /dev/urandom doesn't exist :) let urandom = fopen "/dev/urandom" "r" if urandom isn't nil fread &seed size of seed 1 urandom fclose urandom Cfunc srandom seed seed = (Cfunc random) as nat for each (val int d) from 0 to 3 digits[d] = '1' (seed % 9) seed /= 9 let digits be an array of 4 byte .: show random digits :. Exe) '( - * /)) (link "Using illegal operator(s)") ) ) ) (loop (setq Numbers (make (do 4 (link (rand 1 9))))) (prinl "Please enter a Lisp expression using (, ), , -, *, / and " (glue ", " Numbers) ) (prin "Or a single dot '.' to stop: ") (T (= "." (setq Reply (catch '(NIL) (in NIL (read))))) (bye) ) (cond ((str?