diff --git a/int_parse.py b/int_parse.py deleted file mode 100644 index 8b6318e..0000000 --- a/int_parse.py +++ /dev/null @@ -1,81 +0,0 @@ -# This file contains all the functions to parse the input string - -from scraper import * - - -class Variable: - """Class for a variable (of type int, bool, array, double, string, etc.) - - Attributes - ---------- - datatype : str - String representation of the data type of the variable - Eg: "string", "bool", "int[n]" (for array) - name : str - Name of the variable - """ - - def __init__(self, datatype_cpp, datatype_py, name): - self.datatype_cpp = datatype_cpp - self.datatype_py = datatype_py - self.name = name - - -class Wrapper: - """Class for a loop including repetitive input - - Attributes - ---------- - iterations : int - Number of times the outer loop runs in the wrapper - *enclosed : tuple of Wrapper and/or Variable objects - Everything enclosed in the outer loop's body - """ - - def __init__(self, iterations, *enclosed): - self.iterations = iterations - self.enclosed = enclosed - - -# Useful where questions might use words instead of digits for small numbers -text_numbers = {"one": 1, - "two": 2, - "three": 3, - "four": 4, - "five": 5, - "six": 6, - "seven": 7, - "eight": 8, - "nine": 9, - "ten": 10} - - -# Function to check a line of input for the presence of an array -def check_integer(line): - - is_integer = False - words = line.split() - found_index = -1 - n = len(words) - for i in range(n): - word = words[i].lower() - if word == "integer" or word == "number": - is_integer = True - found_index = i - break - if not is_integer: - return - - # qty = words[found_index - 1].lower().strip(",$") - # try: - # qty = int(qty) - # except ValueError: - # try: - # qty = text_numbers[qty] - # except KeyError: - # pass - name = words[found_index + 1].lower().strip(",$") - # i = name.find('_') - # if i != -1: - # name = name[:i] - return Variable("int", "int", name) \ No newline at end of file diff --git a/problem_parser.py b/problem_parser.py index 01b2c53..9074c30 100644 --- a/problem_parser.py +++ b/problem_parser.py @@ -115,8 +115,77 @@ def check_matrix(line): return -all_fxns = (check_array, check_str, check_matrix#, check_integer - ,) +# Function to check a line of input for the presence of an array +def check_integer(line): + target_string = line + res = re.search("[^ ]+ (integer|number) ", target_string) + + if res is None: + res2 = re.search("[^ ]+ (integers|numbers) ", target_string) + + if res2 is None: + return + + var = res2.group().split() + num2 = var[0].strip('$') + target_string = target_string[res2.span()[1]:] + + try: + num2 = int(num2) + except ValueError: + try: + num2 = text_numbers[num2] + except KeyError: + print("call check_array") # call check_array + return + + l = [] + x = 0 + while x < num2: + flag = 0 + if x < num2 - 2: + search_name = re.search("[^ ]*, ", target_string) + elif x == num2 - 2: + search_name = re.search("[^ ]* and ", target_string) + flag = 1 + elif x == num2 - 1: + search_name = re.search("[^ ]*", target_string) + flag = 2 + + if search_name is None: + print("Error") + else: + var_detail = search_name.group().split() + if flag == 0: + name = var_detail[0].strip("$,") + elif flag == 1: + name = var_detail[0].strip("$") + else: + name = var_detail[0].strip("$.") + l.append(name) + + target_string = target_string[search_name.span()[1]:] + x = x + 1 + + obj = [] + + for y in l: + obj.append(Variable("int", "int", y)) + + return obj + + else: + index = res.group().split() + target_string = target_string[res.span()[1]:] + + search_string = re.search("[^ ]*", target_string) + var_detail = search_string.group().split() + name = var_detail[0].strip('$.') + + return Variable("int", "int", name) + + +all_fxns = (check_array, check_integer, check_str, check_matrix,) # Function to make all the necessary checks