I am currently working on a function named generate_qns_from_list(). This function should take in a list. This function should convert the list into a list of dictionaries. Each dictionary will have two keys - "qns" and "ans". The value for the "qns" key will be a string of the integers taken from a list in the input list. And the integers are separated with the characters "+", "-", "x", "/" from the input list as shown below:
input_list = [["+",1,3,3], ["-",2,5,-1],
["x",3,2],["/",12,3,2],["x",0,23],["+",1,2,3,4]]
generate_qns_from_list(input_list)
# output
[{"qns": "1 + 3 + 3", "ans": 7},
{"qns": "2 - 5 - -1", "ans": -2},
{"qns": "3 x 2", "ans": 6},
{"qns": "12 / 3 / 2", "ans": 2},
{"qns": "0 x 23", "ans": 0},
{"qns": "1 + 2 + 3 + 4", "ans": 10}]
This is my code:
import math
def generate_qns_from_list(lst):
qns_list = []
for sub_list in lst:
operator, *numbers = sub_list
d = {}
qns = str(operator).join(map(str, sub_list))
d["qns"] = f"{qns}"
ans = math.prod(sub_list)
d["ans"] = ans
qns_list.append(d)
return qns_list
I keep getting this output:
[{'qns': '++1+3+3', 'ans': '+++++++++'},
{'qns': '--2-5--1', 'ans': ''},
{'qns': 'xx3x2', 'ans': 'xxxxxx'},
{'qns': '//12/3/2',
'ans': '////////////////////////////////////////////////////////////////////////'},
{'qns': 'xx0x23', 'ans': ''},
{'qns': '++1+2+3+4', 'ans': '++++++++++++++++++++++++'}]