Validating number in perl

02 Jul

) 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.

" then puts(1,"\n") integer r_solve24 = routine_id("solve24") -- see below if r_solve24=-1 then -- (someone copied just this code out?

=0 then printf(1," %s on the %s character%s\n",) elsif idx#! (de check Expression (Lst Exe) (make (when (diff Lst (fish num?

The program should check then evaluate the expression. When you wish to check your answer enter "=" and it will perform the checks to ensure that you haven't performed any illegal operations, that you have used all four numbers and that your final value is 24. (example: if you are given the digits 2, 2, 6, 9 this program accepts the following solution: 6 * 4 * 2 / 2) The 24 Game Given four digits and using just the , -, *, and / operators; and the possible use of brackets, (), enter an expression that equates to 24. ''' 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. cashe (remq x cashe))) ;; the parser (define parse (match-lambda ;; parsing arythmetics [`(,x ... " user-expr '= ans)])]))) ;; reading and preparing the user's answer;; "1 2 * (3 4)" --╔═════════════════════════════════════════════════════════════════════════════╗ ║ Argument for the 24 REXX program is either of three forms: (blank) ║ ║ ssss ║ ║ ssss-ffff ║ ║ ║ ║ where SSSS and FFFF must be exactly four numerals (digits) comprised ║ ║ solely of the numerals (digits) 1 ──► 9 (inclusive, with no zeroes). ║ ║ ║ ║ If no argument is specified, the program finds a four digit number (with ║ ║ no zeroes) which has at least one solution, and then displays the number ║ ║ to the user, requesting that they enter a solution in the form of: ║ ║ ║ ║ w operator x operator y operator z ║ ║ ║ ║ where w x y and z are single digit numbers (no zeroes).

The goal is for the player to enter an expression that (numerically) evaluates to 24. Unfortunately, due to the lack of floating-point arithmetic in befunge, divide will result in the answer truncated to an integer. These are your four digits: 9, 2, 8, 7 24 = 9*8/2-7 Wrong! y These are your four digits: 5, 5, 5, 6 24 = 5*5 5-6 Correct! n success:1 failure:1 total:2''' 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. ║ ║ and operator can be any one of: - * / ║ ║ Parentheses ( ), brackets [ ], and/or braces may be used in the ║ ║ normal manner for grouping expressions. ║ ╚═════════════════════════════════════════════════════════════════════════════╝ A large part of this program deals with validating the user input (and issuing appropriate and meaningful error messages), and also that the digits presented to the user, do in fact, have a possible solution.

\ Generate four random digits and display to the user\ then get an expression from the user using , -, / and * and the digits\ the result must equal 24\ Only the words in namespace 'game' are available to the player:ns: game : n: ;: - n:- ;: * n:* ;: / n:/ ; ns: G var random-digitsvar user-input : one-digit \ a -- a rand n:abs 9 n:mod n:1 a:push ; : gen-digits \ - a [] clone nip \ the clone nip is not needed in versions past 1.0.2... Example: 6566 ( 24-game = m-w m-z 4numbers answer expr numbers , seed get-random convert Binary Minus To Unary , convert Division To Multiplication is Expresssion reciprocal . ───────── Using the digits 8961, enter an expression that equals 24 (or QUIT): 8 * [9-6] / 1 ◄■■■■■■■■■■■■■■ this is the user input. │ │ │ └─────────────────────┘ The above was created for Classic Rexx and can be used, unchanged, for Regina and other implementations of Classic Rexx.

" ENDIF UNTIL TRUE INPUT '"Play again", answer$ IF LEFT$(answer$,1) = "y" OR LEFT$(answer$,1) = "Y" THEN CLS : RUN QUIT The code functions by placing the 4 randomly generated numbers into the points labelled 1,2,3,4. The "is Numeric" function was taken from the "Determine_if_a_string_is_numeric" task. x) x] ;; unknown token [x (error "Not a number: " x)])) ;; parse the expresion (define result (parse expr)) ;; return the result if cashe is empty (if (empty? "))) ;; starting the program(define (start) (displayln "Combine given four numbers using operations - * / to get 24.") (displayln "Input 'q' to quit or your answer like '1 - 3 * (2 3)'") (new-game)) ;; starting a new game(define (new-game) ;; create a new number set (define numbers (build-list 4 (λ (_) ( 1 (random 9))))) (apply printf "Your numbers: ~a ~a ~a ~a\n" numbers) (new-input numbers)) ;; processing a new user input(define (new-input numbers) ;; if an exception is raized while processing, show the exeption message ;; and prompt for another input, but do not stop the program. (λ (exn) (displayln (exn-message exn)) (new-input numbers))]) ;; get the answer (define user-expr (read-the-answer)) ;; interprete it (case user-expr [(q) (display "Good buy!