How to resolve the algorithm 24 game step by step in the Lasso programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 24 game step by step in the Lasso programming language

Table of Contents

Problem Statement

The 24 Game tests one's mental arithmetic.

Write a program that randomly chooses and displays four digits, each from 1 ──► 9 (inclusive) with repetitions allowed. The program should prompt for the player to enter an arithmetic expression using just those, and all of those four digits, used exactly once each. The program should check then evaluate the expression. The goal is for the player to enter an expression that (numerically) evaluates to 24.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 24 game step by step in the Lasso programming language

Source code in the lasso programming language

[
if(sys_listunboundmethods !>> 'randoms') => {
	define randoms()::array => {
		local(out = array)
		loop(4) => { #out->insert(math_random(9,1)) }
		return #out
	}
}
if(sys_listunboundmethods !>> 'checkvalid') => {
	define checkvalid(str::string, nums::array)::boolean => {
		local(chk = array('*','/','+','-','(',')',' '), chknums = array, lastintpos = -1, poscounter = 0)
		loop(9) => { #chk->insert(loop_count) }
		with s in #str->values do => {
			#poscounter++
			#chk !>> #s && #chk !>> integer(#s) ? return false
			integer(#s) > 0 && #lastintpos + 1 >= #poscounter ? return false
			integer(#s) > 0 ? #chknums->insert(integer(#s))
			integer(#s) > 0 ? #lastintpos = #poscounter
		}
		#chknums->size != 4 ? return false
		#nums->sort
		#chknums->sort
		loop(4) => { #nums->get(loop_count) != #chknums(loop_count) ? return false }
		return true
	}
}
if(sys_listunboundmethods !>> 'executeexpr') => {
	define executeexpr(expr::string)::integer => {
		local(keep = string)
		with i in #expr->values do => {
			if(array('*','/','+','-','(',')') >> #i) => {
				#keep->append(#i)
			else
				integer(#i) > 0 ? #keep->append(decimal(#i))
			}
		}
		return integer(sourcefile('['+#keep+']','24game',true,true)->invoke)
	}
}

local(numbers = array, exprsafe = true, exprcorrect = false, exprresult = 0)
if(web_request->param('nums')->asString->size) => {
	with n in web_request->param('nums')->asString->split(',') do => { #numbers->insert(integer(#n->trim&)) }
}
#numbers->size != 4 ? #numbers = randoms()
if(web_request->param('nums')->asString->size) => {
	#exprsafe = checkvalid(web_request->param('expr')->asString,#numbers)
	if(#exprsafe) => {
		#exprresult = executeexpr(web_request->param('expr')->asString)
		#exprresult == 24 ? #exprcorrect = true
	}
}

]

24 Game

Rules:
Enter an expression that evaluates to 24

  • Only multiplication, division, addition, and subtraction operators/functions are allowed.
  • Brackets are allowed.
  • Forming multiple digit numbers from the supplied digits is disallowed. (So an answer of 12+12 when given 1, 2, 2, and 1 is wrong).
  • The order of the digits when given does not have to be preserved.

Numbers

[#numbers->join(', ')] (Reload)

[!#exprsafe ? '

Please provide a valid expression

']
[if(#exprsafe)]

Result: [#exprresult] [#exprcorrect ? 'is CORRECT!' | 'is incorrect']

[/if]

You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the Wren programming language
You may also check:How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the Python programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Scheme programming language