Skip to content

Commit

Permalink
Merge pull request #20 from r3kste/main
Browse files Browse the repository at this point in the history
PR#20
  • Loading branch information
Proxihox authored Sep 7, 2024
2 parents 70f942a + 6ab08a9 commit c902e91
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 201 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.gitignore
.idea
__pycache__
*.out
Binary file removed src/a.out
Binary file not shown.
41 changes: 21 additions & 20 deletions src/fetcher.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
from scraper import *
import os

from scraper import Problem

path = os.path.dirname(os.path.realpath(__file__))
path = f"{path}/problems"

path = "src/test_cases/"
def write_file(file_name,code_str):
with open(file_name,"w") as f:
f.write(code_str)
f.close()

def write_file(file_name, code_str):
with open(file_name, "w") as f:
f.write(code_str)

prob = input("Enter problem name, Eg: 1980G: ")
p = Problem(prob[:4],prob[4])
content = "".join(p.input) + "@"
for test in p.tests:
content += test.input + "@"
write_file(path+prob+".txt",content)

def fetch(prob):
if isinstance(prob, str):
contestId, index = prob.split("_")
else:
contestId, index = prob

try:
p = Problem(prob[:4],prob[4])
input_string = "".join(p.input)
tests = [test.input for test in p.tests]
p = Problem(contestId, index)
p = {"input": rf"{p.input}", "tests": rf"{p.tests}"}
write_file(f"{path}/{contestId}_{index}.txt", str(p))
except:
with open(path + prob + ".txt",'r') as f:
s = f.read().split("@")
input_string = s[0]
tests = s[1:]
return input_string,tests
with open(f"{path}/{contestId}_{index}.txt", "r") as f:
p = eval(f.read())

return p["input"], p["tests"]
80 changes: 42 additions & 38 deletions src/problem_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# This file contains all the functions to parse the input string

from scraper import *
import re

from scraper import Problem


class Variable:
Expand Down Expand Up @@ -41,16 +43,18 @@ def __init__(self, iterations, *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}
text_numbers = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10,
}


def ignore(line):
Expand All @@ -75,15 +79,15 @@ def check_array(line):
return

var_details = match.group().split()
qty = var_details[0].strip('$')
qty = var_details[0].strip("$")
try:
qty = int(qty)
except ValueError:
try:
qty = text_numbers[qty]
except KeyError:
pass
name = var_details[2].strip(',$')
name = var_details[2].strip(",$")

match = re.search(".*_", name)
if match is not None:
Expand All @@ -93,19 +97,19 @@ def check_array(line):

def check_str(line):
target_string = line
is_str=re.search(".*string.*", line)
is_strs=re.search(".*strings.*", line)
if is_str and not is_strs :
is_str = re.search(".*string.*", line)
is_strs = re.search(".*strings.*", line)
if is_str and not is_strs:
return Variable("string", "str", "s")
elif is_strs :
elif is_strs:
res2 = re.search("[^ ]+ strings ", target_string)

if res2 is None:
return

var = res2.group().split()
num2 = var[0].strip('$')
target_string = target_string[res2.span()[1]:]
num2 = var[0].strip("$")
target_string = target_string[res2.span()[1] :]

try:
num2 = int(num2)
Expand Down Expand Up @@ -140,7 +144,7 @@ def check_str(line):
name = var_detail[0].strip("$.")
l.append(name)

target_string = target_string[search_name.span()[1]:]
target_string = target_string[search_name.span()[1] :]
x = x + 1

obj = []
Expand All @@ -149,19 +153,21 @@ def check_str(line):
obj.append(Variable("string", "str", y))

return obj
else :
else:
return


def check_matrix(line):
is_matrix = re.search(".*lines.*(numbers|integers)",line)
is_matrix = re.search(".*lines.*(numbers|integers)", line)
if is_matrix:
result = line.split("lines")
num1 = result[0].split()[-1][1:-1]
return_val = check_array(result[1])
num2 = return_val.datatype_cpp[4:-1]
arr = return_val.name
return Variable("int[{}][{}]".format(num1,num2), "int[{}][{}]".format(num1,num2), arr)
return Variable(
"int[{}][{}]".format(num1, num2), "int[{}][{}]".format(num1, num2), arr
)
else:
return

Expand All @@ -172,7 +178,7 @@ def check_integer(line):
res = re.search("[^ ]+ (integer|number) ", target_string)

if res is not None:
target_string = target_string[res.span()[1]:]
target_string = target_string[res.span()[1] :]
if target_string[0:2] == "of" and res.group().split()[1] == "number":
res = None
target_string = line
Expand All @@ -181,7 +187,7 @@ def check_integer(line):
index = res.group().split()
search_string = re.search("[^ ]*", target_string)
var_detail = search_string.group().split()
name = var_detail[0].strip('$.')
name = var_detail[0].strip("$.")

return Variable("int", "int", name)

Expand All @@ -192,8 +198,8 @@ def check_integer(line):
return

var = res2.group().split()
num2 = var[0].strip('$')
target_string = target_string[res2.span()[1]:]
num2 = var[0].strip("$")
target_string = target_string[res2.span()[1] :]

try:
num2 = int(num2)
Expand Down Expand Up @@ -228,7 +234,7 @@ def check_integer(line):
name = var_detail[0].strip("$.")
l.append(name)

target_string = target_string[search_name.span()[1]:]
target_string = target_string[search_name.span()[1] :]
x = x + 1

obj = []
Expand All @@ -240,19 +246,18 @@ def check_integer(line):


# IMPORTANT : Priority order is decided here
all_fxns = (check_matrix,
check_array,
check_str,
check_integer,
)
all_fxns = (
check_matrix,
check_array,
check_str,
check_integer,
)


# Function to make all the necessary checks
def check_all(para):
para = re.sub(r"\." + " |\n|\n\n", "\n", para)
lines = para.split('\n')
def check_all(inp):
all_data = []
for line in lines:
for line in inp:
if ignore(line):
continue
for check in all_fxns:
Expand All @@ -262,6 +267,5 @@ def check_all(para):
all_data.extend(op)
except TypeError:
all_data.append(op)
print(check)
break
return all_data
1 change: 1 addition & 0 deletions src/problems/1922_A.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'input': "['Input', 'The first line contains an integer $t$ ($1$ $\\\\le$ $t$ $\\\\le$ $1000$)\\xa0— the number of test cases.', 'The first line of each test case contains an integer $n$ ($1$ $\\\\le$ $n$ $\\\\le$ $20$)\\xa0— the length of the given strings.', 'The next three lines contain the strings $a,$ $b$ and $c$. Each string consists of exactly $n$ lowercase Latin letters.']", 'tests': '[T: 4\n\nInput\n4\n1\na\nb\nc\n2\naa\nbb\naa\n10\nmathforces\nluckforces\nadhoccoder\n3\nacc\nabd\nabc\n\nOutput\nYES\nNO\nYES\nNO]'}
1 change: 1 addition & 0 deletions src/problems/1980_E.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'input': "['Input', 'The first line contains an integer $t$ ($1$ $\\\\le$ $t$ $\\\\le$ $10^4$) — the number of test cases. The descriptions of the test cases follow.', 'The first line of each test case description contains $2$ integers $n$ and $m$ ($1$ $\\\\le$ $n,$ $m$ $\\\\le$ $n$ $\\\\cdot$ $m$ $\\\\le$ $2$ $\\\\cdot$ $10^5$) — the sizes of the matrix.', 'The next $n$ lines contain $m$ integers $a_{ij}$ each ($1$ $\\\\le$ $a_{ij}$ $\\\\le$ $n$ $\\\\cdot$ $m$). It is guaranteed that matrix $a$ is a permutation.', 'The next $n$ lines contain $m$ integers $b_{ij}$ each ($1$ $\\\\le$ $b_{ij}$ $\\\\le$ $n$ $\\\\cdot$ $m$). It is guaranteed that matrix $b$ is a permutation.', 'It is guaranteed that the sum of the values $n$ $\\\\cdot$ $m$ for all test cases does not exceed $2$ $\\\\cdot$ $10^5$.']", 'tests': '[T: 7\n\nInput\n7\n1 1\n1\n1\n2 2\n1 2\n3 4\n4 3\n2 1\n2 2\n1 2\n3 4\n4 3\n1 2\n3 4\n1 5 9 6\n12 10 4 8\n7 11 3 2\n1 5 9 6\n12 10 4 8\n7 11 3 2\n3 3\n1 5 9\n6 4 2\n3 8 7\n9 5 1\n2 4 6\n7 8 3\n2 3\n1 2 6\n5 4 3\n6 1 2\n3 4 5\n1 5\n5 1 2 3 4\n4 2 5 1 3\n\nOutput\nYES\nYES\nNO\nYES\nYES\nNO\nYES]'}
1 change: 1 addition & 0 deletions src/problems/2008_A.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'input': '[\'Input\', \'The first line contains a single integer $t$ ($1\\\\le$ $t\\\\le$ $100$) \\xa0— the number of test cases.\', "The only line of each test case contains two integers $a$ and $b$ ($0\\\\le$ $a,b<10$) \\xa0— the number of \'1\'s and the number of \'2\'s in the array."]', 'tests': '[T: 5\n\nInput\n5\n0 1\n0 3\n2 0\n2 3\n3 1\n\nOutput\nNO\nNO\nYES\nYES\nNO]'}
1 change: 1 addition & 0 deletions src/problems/2008_F.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'input': "['Input', 'The first line contains a single integer $t$ ($1\\\\le$ $t\\\\le$ $10^4$) \\xa0— the number of test cases.', 'The first line of each test case contains a single integer $n$ ($2\\\\le$ $n\\\\le$ $2\\\\cdot$ $10^5$) \\xa0— the number of elements in the array.', 'The second line of each test case contains $n$ integers $a_1,$ $a_2,$ $\\\\dots,$ $a_n$ ($0\\\\le$ $a_i\\\\le$ $10^9$) \\xa0— the elements of the array.', 'It is guaranteed that the sum of $n$ across all test cases does not exceed $2\\\\cdot$ $10^5$.']", 'tests': '[T: 3\n\nInput\n3\n3\n3 2 3\n4\n2 2 2 4\n5\n1 2 3 4 5\n\nOutput\n7\n6\n500000012]'}
1 change: 1 addition & 0 deletions src/problems/2008_G.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'input': "['Input', 'The first line contains a single integer $t$ ($1\\\\le$ $t\\\\le$ $10^4$) \\xa0— the number of test cases.', 'The first line of each test case contains two integers $n$ and $k$ ($1\\\\le$ $n\\\\le$ $2\\\\cdot$ $10^5,1\\\\le$ $k\\\\le$ $10^9$) \\xa0— the number of elements in the array and the value $k$ for $mex_k$.', 'The second line of each test case contains $n$ integers $a_1,$ $a_2,$ $\\\\dots,a_n$ ($1\\\\le$ $a_i\\\\le$ $10^9$) \\xa0— the elements of the array.', 'It is guaranteed that the sum of $n$ across all test cases does not exceed $2\\\\cdot$ $10^5$.']", 'tests': '[T: 6\n\nInput\n6\n1 3\n3\n2 10\n1 1\n3 1\n1 2 3\n3 2\n1 2 4\n4 5\n2 2 2 16\n4 5\n2 2 2 3\n\nOutput\n2\n11\n3\n4\n8\n8]'}
4 changes: 3 additions & 1 deletion src/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def __init__(self, contestId, index):
try:
self.input = [
self.mark_latex_symbols(line)
for line in self.soup.find("div", "input-specification").stripped_strings
for line in self.soup.find(
"div", "input-specification"
).stripped_strings
]
except:
raise Exception("Fetch failed")
Expand Down
53 changes: 0 additions & 53 deletions src/test_cases/1980G.txt

This file was deleted.

Loading

0 comments on commit c902e91

Please sign in to comment.