import Nanoquery.Lang import Nanoquery.Util // a function to get the digits from an arithmetic expression def extract_digits(input) input = str(input) digits = {} loc = 0 digit = "" while (loc < len(input)) if input[loc] in "0123456789" digit += input[loc] else if len(digit) > 0 digits.append(int(digit)) digit = "" end end loc += 1 end // check if there's a final digit if len(digit) > 0 digits.append(int(digit)) end return digits end // a function to duplicate a digit list def dup(list) nlist = {} for n in list nlist.append(new(Integer, n)) end return nlist end // generate four random digits and output them random = new(Random) randDigits = {} for i in range(1, 4) randDigits.append(random.getInt(8) + 1) end println "Your digits are: " + randDigits + "\n" // get expressions from the user until a valid one is found won = false while !won print "> " expr = input() tempDigits = dup(randDigits) // check for invalid digits in the expression invalid = false digits = extract_digits(expr) for (i = 0) (!invalid and (i < len(digits))) (i += 1) if not digits[i] in tempDigits invalid = true println "Invalid digit " + digits[i] else tempDigits.remove(tempDigits) end end // if there were no invalid digits, check if the expression // evaluates to 24 if !invalid a = null try exec("a = " + expr) catch println "Invalid expression " + expr end println a if a = 24 println "Success!" won = true end end end