-2

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': '++++++++++++++++++++++++'}]
BioGeek
  • 21,897
  • 23
  • 83
  • 145
Deathage
  • 13
  • 2
  • Can you clarify what you mean with "how I can add user input functionality to obtain the numbers for the input_list"? – BioGeek Apr 29 '21 at 07:30
  • I trying to get the output as shown in the page. The problem is that I have try to split the list to get the character "+" and the integer from the input_list to form a formula. – Deathage Apr 29 '21 at 07:40

3 Answers3

1

The problem is that I have try to split the list to get the character "+" and the integer from the input_list to form a formula.

There are several ways to do this. You can use slicing:

input_list = [["+",1,3,3], ["-",2,5,-1],
["x",3,2],["/",12,3,2],["x",0,23],["+",1,2,3,4]]

for sub_list in input_list:
  operator = sub_list[0]
  numbers = sub_list[1:]
  print(operator)
  print(numbers)

Or you can use extended iterable unpacking:

input_list = [["+",1,3,3], ["-",2,5,-1],
["x",3,2],["/",12,3,2],["x",0,23],["+",1,2,3,4]]

for sub_list in input_list:
  operator, *numbers =  sub_list
  print(operator)
  print(numbers)
BioGeek
  • 21,897
  • 23
  • 83
  • 145
0

You can try this.

input_list = [["+",1,3,3], ["-",2,5,-1],
["x",3,2],["/",12,3,2],["x",0,23],["+",1,2,3,4]]
def generate_qns_from_list(l=input_list):
    for i in l:
        operation = i.pop(0)
        if operation == "x":
            operation_to_do = "*"
        else:
            operation_to_do = operation
        query = f" {operation_to_do} ".join(map(str, i))
        print({"qns": query ,"ans": eval(query)})
{'qns': '1 + 3 + 3', 'ans': 7}
{'qns': '2 - 5 - -1', 'ans': -2}
{'qns': '3 * 2', 'ans': 6}
{'qns': '12 / 3 / 2', 'ans': 2.0}
{'qns': '0 * 23', 'ans': 0}
{'qns': '1 + 2 + 3 + 4', 'ans': 10}

This will not work for all the queries but its a start

Dharman
  • 30,962
  • 25
  • 85
  • 135
WiLL_K
  • 575
  • 1
  • 3
  • 22
  • 1
    Be aware that [using `eval` is dangerous!](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) Normally, I would recommend using [`ast.literal_eval`](https://docs.python.org/3/library/ast.html#ast.literal_eval) instead, but that [has been made more strict and no longer supports addition and subtraction](https://github.com/python/cpython/pull/4035). – BioGeek Apr 29 '21 at 08:27
  • Its ok it you know what you are doing and have enough checks – WiLL_K Apr 29 '21 at 08:30
  • If only you are using the code, it is probably OK to use `eval`, but if `input_list` comes from somewhere else (for example, users input it on a website), then there are always ways to circumvent the checks you have written (see the linked article from Ned Batchelder). – BioGeek Apr 29 '21 at 08:37
0

This should work:

def generate_qns_from_list(inp_list):
    placeholder = []
    for i in range(len(inp_list)):
        numbers = list(map(str, inp_list[i][1:])) # a list of the numbers (as strings)
        placeholder.append({"qns": (" " + inp_list[i][0] + " ").join(numbers)}) # make the questions
        placeholder[i]["ans"] = eval(inp_list[i][0].join(numbers).replace("x", "*")) # make the answers
    return placeholder
Jacob Stuligross
  • 1,434
  • 5
  • 18