#!/usr/bin/env python # coding: utf-8 import random as random import statistics import json as json def make_string(list_num, operator_list): """Reads in two lists spits out a string. operator_list is one shorter than list_num and consists of ones and zeroes. list_num is a list of integers between 1 and 9. OPerator list assigns the operator. Out: Proper String """ string = num_2_word(list_num[0]) for i in range(1, len(list_num)): help_int = list_num[i] if operator_list[i - 1] == 0: string = string + ' + ' + num_2_word(help_int) elif operator_list[i - 1] == 1: string = string + ' - ' + num_2_word(help_int) return string def num_2_word(num): ''' Converting random integer to number for english language''' if num == 1: return 'one' elif num == 2: return 'two' elif num == 3: return 'three' elif num == 4: return 'four' elif num == 5: return 'five' elif num == 6: return 'six' elif num == 7: return 'seven' elif num == 8: return 'eight' elif num == 9: return 'nine' def sum_numbers(list_numbers, list_operators): """Uses operators to return the sum.""" sum = list_numbers[0] for i in range(1, len(list_numbers)): if list_operators[i - 1] == 0: sum = sum + list_numbers[i] if list_operators[i - 1] == 1: sum = sum - list_numbers[i] return sum def generate_numbers(num_tasks=100, length=6): """Complete function. Makes num_tasks number of integer lists of length=length and a operator list of length=length-1. Then, the sum is computed and it is converted into a string. Both is stored in three lists and stored as json files. """ true_task_list = [] results_task_list = [] numbered_task_list = [] for j in range(0, num_tasks): list_num = [random.randint(1, 9) for count in range(length)] operator_list = [random.randint(0, 1) for count in range(length - 1)] while (sum_numbers(list_num, operator_list) < 0) or (sum_numbers(list_num, operator_list) > 10): list_num = [random.randint(1, 9) for count in range(length)] # if function above is adjusted to output here, you can convert them directl.y # list_of_options.append(convert_draw_string(list_num)) list_of_options.append(list_num) # Add sum as tuple true_task_list.append([sum_numbers(list_num, operator_list), make_string(list_num, operator_list)]) # Convert list to string # Result string + Clean String results_task_list.append(sum_numbers(list_num, operator_list)) numbered_task_list.append([j, make_string(list_num, operator_list)]) with open('Task_and_Time/static/AdditionSubtraktion/true_task_list.json', 'w') as outfile: json.dump(true_task_list, outfile) generate_numbers(num_tasks=10000)