From 1f62bab132525d5e3a38c66087fc5e37828717b9 Mon Sep 17 00:00:00 2001 From: cassanof Date: Mon, 28 Aug 2023 15:49:05 -0700 Subject: [PATCH] fix parse --- programming_runs/generators/parse.py | 38 +++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/programming_runs/generators/parse.py b/programming_runs/generators/parse.py index 562c1e4..a2f7472 100644 --- a/programming_runs/generators/parse.py +++ b/programming_runs/generators/parse.py @@ -21,22 +21,22 @@ def parse_code_block(string: str, lang: str) -> Optional[str]: def parse_first_func(code: str, lang: str) -> Optional[str]: assert lang == "python", "Only python is supported for now. TODO: Rust" code_lines = code.split("\n") - def_i = 0 + def_i = -1 last_i = 0 for i, line in enumerate(code_lines): if line.startswith("def "): - if def_i == 0: + if def_i == -1: def_i = i else: break - if line == "" and def_i != 0: + if line == "" and def_i != -1: last_i = i break if last_i == 0: last_i = len(code_lines) - 1 - if def_i == 0: + if def_i == -1: return None return "\n".join(code_lines[def_i:last_i+1]) @@ -70,3 +70,33 @@ def bleh(): return aaa """ print(parse_code_block(CODE, "python")) + CODE="""def total_match(lst1: List[str], lst2: List[str]) -> List[str]: + \"\"\" + Write a function that accepts two lists of strings and returns the list that has + total number of chars in the all strings of the list less than the other list. + + if the two lists have the same number of chars, return the first list. + + Examples + >>> total_match([], []) + [] + >>> total_match(['hi', 'admin'], ['hI', 'Hi']) + ['hI', 'Hi'] + >>> total_match(['hi', 'admin'], ['hi', 'hi', 'admin', 'project']) + ['hi', 'admin'] + >>> total_match(['hi', 'admin'], ['hI', 'hi', 'hi']) + ['hI', 'hi', 'hi'] + >>> total_match(['4'], ['1', '2', '3', '4', '5']) + ['4'] + \"\"\" + total_chars_lst1 = sum(len(word) for word in lst1) + total_chars_lst2 = sum(len(word) for word in lst2) + + if total_chars_lst1 < total_chars_lst2: + return lst1 + elif total_chars_lst1 > total_chars_lst2: + return lst2 + else: + return lst1 + """ + print(parse_code_block(CODE, "python"))