
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check whether we can form 24 by placing operators in python
Suppose we have a list of four numbers, each numbers are in range 1 to 9, in a fixed order. Now if we place the operators +, -, *, and / (/ denotes integer division) between the numbers, and group them with brackets, we have to check whether it is possible to get the value 24 or not.
So, if the input is like nums = [5, 3, 6, 8, 7], then the output will be True, as (5 * 3) - 6 + (8 + 7) = 24.
To solve this, we will follow these steps −
- Define a function recur() . This will take arr
- answer := a new list
- for i in range 0 to size of arr - 1, do
- pre := recur(arr[from index 0 to i])
- suf := recur(arr[from index i + 1 to end])
- for each k in pre, do
- for each j in suf, do
- insert (k + j) at the end of answer
- insert (k - j) at the end of answer
- insert (k * j) at the end of answer
- if j is not 0, then
- insert (quotient of k / j) at the end of answer
- for each j in suf, do
- if size of answer is 0 and size of arr is 1, then
- insert arr[0] at the end of answer
- return answer
- From the main method check whether 24 is in recur(nums) or not, if yes return True, otherwise false
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): def recur(arr): answer = [] for i in range(len(arr) - 1): pre, suf = recur(arr[: i + 1]), recur(arr[i + 1 :]) for k in pre: for j in suf: answer.append(k + j) answer.append(k - j) answer.append(k * j) if j != 0: answer.append(k // j) if len(answer) == 0 and len(arr) == 1: answer.append(arr[0]) return answer return 24 in recur(nums) ob = Solution() nums = [5, 3, 6, 8, 7] print(ob.solve(nums))
Input
[5, 3, 6, 8, 7]
Output
True
- Related Articles
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Python program to check whether we can pile up cubes or not
- Program to check we can form array from pieces or not in Python
- Program to check whether we can reach last position from index 0 in Python
- Program to check whether we can get N queens solution or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check whether we can eat favorite candy on our favorite day in Python
- Program to check whether we can split a string into descending consecutive values in Python
- Program to check we can cross river by stones or not in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check whether first player can win a game where players can form string char by char in C++
- Program to check whether we can pick up and drop every passenger in given list in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to check whether two trees can be formed by swapping nodes or not in Python

Advertisements