Answer the question
In order to leave comments, you need to log in
How to solve the problem of iterating over unknown variables with a known answer in Python?
Good day everyone!
I solve a problem in two parts, using any additional libraries is prohibited:
The first part is SOLVED:
Make an algorithm that divides a list of numbers into 4 parts. In each such piece:
- it takes the number with index 0 as an instruction (1 - addition, 2 - multiplication, 99 - stopping the formula),
- numbers with index 1 and 2 as variables that need to be added or multiplied,
- number 4 as a position, on which you want to put the result of the calculation,
- returns the number that is at index 0 in the list after the formula is run.
inputlist = [
1, 15, 30, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 6, 1, 19, 2, 19, 9, 23, 1, 23, 5, 27, 2, 6, 27, 31, 1,
31, 5, 35, 1, 35, 5, 39, 2, 39, 6, 43, 2, 43, 10, 47, 1, 47, 6, 51, 1, 51, 6, 55, 2, 55, 6, 59, 1, 10, 59,
63, 1, 5, 63, 67, 2, 10, 67, 71, 1, 6, 71, 75, 1, 5, 75, 79, 1, 10, 79, 83, 2, 83, 10, 87, 1, 87, 9, 91, 1,
91, 10, 95, 2, 6, 95, 99, 1, 5, 99, 103, 1, 103, 13, 107, 1, 107, 10, 111, 2, 9, 111, 115, 1, 115, 6, 119,
2, 13, 119, 123, 1, 123, 6, 127, 1, 5, 127, 131, 2, 6, 131, 135, 2, 6, 135, 139, 1, 139, 5, 143, 1, 143,
10, 147, 1, 147, 2, 151, 1, 151, 13, 0, 99, 2, 0, 14, 0
]
def program_solver(inputlist: list):
"""
Solves mathematical issue.
:param inputlist:list, a list of integers
:return: int, the first number of the modified list
"""
chunk_of_numbers = []
source_code = inputlist[0] # нужен для того, чтобы вернуть список в исходный вид (понадобится в ч.2)
for a in inputlist:
chunk_of_numbers.append(int(a))
if len(chunk_of_numbers) == 4:
if chunk_of_numbers[0] == 1:
sum_of_the_chunk = inputlist[chunk_of_numbers[1]] + inputlist[chunk_of_numbers[2]]
inputlist[chunk_of_numbers[3]] = sum_of_the_chunk
chunk_of_numbers.clear()
elif chunk_of_numbers[0] == 2:
mult_of_the_chunk = inputlist[chunk_of_numbers[1]] * inputlist[chunk_of_numbers[2]]
inputlist[chunk_of_numbers[3]] = mult_of_the_chunk
chunk_of_numbers.clear()
elif chunk_of_numbers[0] == 99:
result = inputlist[0]
inputlist[0] = source_code
return result
desire_result = 7156875
number_range = range(1, 100)
n = 0
program_check = 0
for x in number_range:
inputlist[1] = x
for y in number_range:
inputlist[2] = y
print(inputlist)
program_check = program_solver(inputlist)
print(program_check)
if program_solver(inputlist) == desire_result:
print(f"x = {x}, y = {y}")
else:
n += 1
print(inputlist)
print(n)
print(inputlist)
[1, 1, 1, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 6, 1, 19, 2, 19, 9, 23, 1, 23, 5, 27, 2, 6, 27, 31, 1, 31, 5, 35, 1, 35, 5, 39, 2, 39, 6, 43, 2, 43, 10, 47, 1, 47, 6, 51, 1, 51, 6, 55, 2, 55, 6, 59, 1, 10, 59, 63, 1, 5, 63, 67, 2, 10, 67, 71, 1, 6, 71, 75, 1, 5, 75, 79, 1, 10, 79, 83, 2, 83, 10, 87, 1, 87, 9, 91, 1, 91, 10, 95, 2, 6, 95, 99, 1, 5, 99, 103, 1, 103, 13, 107, 1, 107, 10, 111, 2, 9, 111, 115, 1, 115, 6, 119, 2, 13, 119, 123, 1, 123, 6, 127, 1, 5, 127, 131, 2, 6, 131, 135, 2, 6, 135, 139, 1, 139, 5, 143, 1, 143, 10, 147, 1, 147, 2, 151, 1, 151, 13, 0, 99, 2, 0, 14, 0]
521343
[1, 1, 2, 2, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 6, 1, 2, 2, 19, 9, 6, 1, 23, 5, 7, 2, 6, 27, 14, 1, 31, 5, 15, 1, 35, 5, 16, 2, 39, 6, 32, 2, 43, 10, 128, 1, 47, 6, 130, 1, 51, 6, 132, 2, 55, 6, 264, 1, 10, 59, 268, 1, 5, 63, 269, 2, 10, 67, 1076, 1, 6, 71, 1078, 1, 5, 75, 1079, 1, 10, 79, 1083, 2, 83, 10, 4332, 1, 87, 9, 4335, 1, 91, 10, 4339, 2, 6, 95, 8678, 1, 5, 99, 8679, 1, 103, 13, 8684, 1, 107, 10, 8688, 2, 9, 111, 26064, 1, 115, 6, 26066, 2, 13, 119, 130330, 1, 123, 6, 130332, 1, 5, 127, 130333, 2, 6, 131, 260666, 2, 6, 135, 521332, 1, 139, 5, 521333, 1, 143, 10, 521337, 1, 147, 2, 521338, 1, 151, 13, 0, 99, 2, 0, 14, 0]
Traceback (most recent call last):
File "<input>", line 48, in <module>
File "<input>", line 20, in program_solver
IndexError: list assignment index out of range
Answer the question
In order to leave comments, you need to log in
Solution I got:
def program_solver(inputlist: list):
"""
Solves mathematical issue.
:param inputlist:list, a list of integers
:return: int, the first number of the modified list
"""
chunk_of_numbers = []
temp_list = inputlist[:]
for a in temp_list:
chunk_of_numbers.append(int(a))
if len(chunk_of_numbers) == 4:
if chunk_of_numbers[0] == 1:
sum_of_the_chunk = temp_list[chunk_of_numbers[1]] + temp_list[chunk_of_numbers[2]]
temp_list[chunk_of_numbers[3]] = sum_of_the_chunk
chunk_of_numbers.clear()
elif chunk_of_numbers[0] == 2:
mult_of_the_chunk = temp_list[chunk_of_numbers[1]] * temp_list[chunk_of_numbers[2]]
temp_list[chunk_of_numbers[3]] = mult_of_the_chunk
chunk_of_numbers.clear()
elif chunk_of_numbers[0] == 99:
result = temp_list[0]
return result
inputlist = [
1, 15, 30, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 6, 1, 19, 2, 19, 9, 23, 1, 23, 5, 27, 2, 6, 27, 31, 1,
31, 5, 35, 1, 35, 5, 39, 2, 39, 6, 43, 2, 43, 10, 47, 1, 47, 6, 51, 1, 51, 6, 55, 2, 55, 6, 59, 1, 10, 59,
63, 1, 5, 63, 67, 2, 10, 67, 71, 1, 6, 71, 75, 1, 5, 75, 79, 1, 10, 79, 83, 2, 83, 10, 87, 1, 87, 9, 91, 1,
91, 10, 95, 2, 6, 95, 99, 1, 5, 99, 103, 1, 103, 13, 107, 1, 107, 10, 111, 2, 9, 111, 115, 1, 115, 6, 119,
2, 13, 119, 123, 1, 123, 6, 127, 1, 5, 127, 131, 2, 6, 131, 135, 2, 6, 135, 139, 1, 139, 5, 143, 1, 143,
10, 147, 1, 147, 2, 151, 1, 151, 13, 0, 99, 2, 0, 14, 0
]
desire_result = 7156875
number_range = range(1, 100)
n = 0
program_check = 0
for x in number_range:
inputlist[1] = x
for y in number_range:
inputlist[2] = y
program_check = program_solver(inputlist)
print(program_check)
if program_check == desire_result:
print(f"x = {x}, y = {y}")
break
if program_check == desire_result:
break
Create a copy of the input list before feeding it to program_solver(), because this function modifies the input list and it is not surprising to get an "index out of range" error on the second and subsequent runs of program_solver().
> inputlist[0] = source_code
in the first task it is not clear why you are restoring the old value of the first element of the list
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question