-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating code to only write a simple parser and a simple output gener…
…ator + better algorithms
- Loading branch information
Showing
9 changed files
with
147 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# HashCode template | ||
|
||
## To modify | ||
|
||
- modify the problem parser (in `problem.py`) to your needs (from the INPUT and OUTPUT specifications) | ||
- create `algo_name.py`, with algo_name the name of your algorithm | ||
- run it with `main.py algo_name` to run on all files, or `main.py algo_name a b` to run on a and b problems | ||
|
||
## Adding an algorithm | ||
|
||
Create a file `algo_name.py` alongside `main.py`, base content looks like this: | ||
|
||
```python | ||
#!/usr/bin/env python3 | ||
# coding: utf-8 | ||
|
||
from hashcode import reporter | ||
|
||
|
||
@reporter(__file__) | ||
def run(filename: str, data: dict): | ||
out = {} | ||
|
||
# your code is here | ||
|
||
return out | ||
``` | ||
|
||
## Adding the INPUT and OUTPUT parsers | ||
|
||
Your parsers should be written in `problem.py`. | ||
|
||
### INPUT | ||
|
||
The `parse_input` method will be given the header as a dict, defined by the `FIELDS` which is a mapping `field name => type` (you can choose whatever field name you want, but **the type has to be exactly what you want**). | ||
|
||
The `parse_input` method is also given the rest of the lines of the file (without the first line) and should return what it parsed from those lines as a dict. **Do not include the header** it will be include by the caller. | ||
|
||
### OUTPUT | ||
|
||
The `generate_output` method will be given the original data parsed before for the problem, the dictionnary returned by the parser. It will also be given the solution your algorithm generated **as a dict**. | ||
|
||
It should return a list of string, each one being an element of the problem. | ||
|
||
**Then the parser will write at the top of the file the number of components you generated**. The first line of an output file for the google hashcode is **always** the number of solution, thus it should be carefully designed in your generate_output to return the right number of elements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
from . reporter import reporter | ||
from .hc_short import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
# coding: utf-8 | ||
|
||
from . import evalfile, pexist, gob, fbasenoext, fbasename, rm_eol | ||
from . import rlines, prtnice, reporter, dcopy | ||
from typing import List, Tuple | ||
|
||
|
||
@reporter("hc_parser.reader") | ||
def reader(fields: List[Tuple[str, type]], filename: str, callback) -> dict: | ||
content = None or rlines(filename) | ||
if content is None: | ||
raise RuntimeError( | ||
f"couldn't process {filename}: may be empty or can not read") | ||
|
||
first_line = content[0].split(' ') | ||
if len(first_line) > len(fields): | ||
raise RuntimeError("You forgot to update the FIELDS definition in problem.py, parsed too many fields") | ||
if len(first_line) < len(fields): | ||
raise RuntimeError(f"You want too many fields ({len(fields)}) but the file has {len(first_line)} fields in its header") | ||
|
||
data = { | ||
fields[i][0]: fields[i][1](rm_eol(e)) for i, | ||
e in enumerate(first_line) | ||
} | ||
|
||
cdata = callback(dcopy(data), content[1:]) | ||
data.update(cdata) | ||
|
||
return data | ||
|
||
|
||
@reporter("hc_parser.saver") | ||
def saver(fields: List[Tuple[str, type]], filename: str, callback) -> None: | ||
with open(f"parsed/{filename}", "w") as f: | ||
f.write(prtnice(reader(fields, f"tests/{filename}", callback))) | ||
|
||
|
||
def save_all(fields: List[Tuple[str, type]], callback) -> None: | ||
print("Saving all...") | ||
for f in gob(f"tests/*.txt"): | ||
print(f"Exporting {f}...\r") | ||
saver(fields, fbasename(f), callback) | ||
print("\nDone!") | ||
|
||
|
||
@reporter("hc_parser.generate_output") | ||
def generate_output(data: dict, filename: str, algo_name: str, callback) -> None: | ||
# if we need data from the problem source | ||
og_file = load(filename) | ||
out = callback(og_file, data) | ||
|
||
content = str(len(data)) + '\n' | ||
content += ''.join(map(str, (line for line in out))) | ||
|
||
racine = fbasenoext(filename) | ||
with open(f"output/{racine}-{algo_name}.txt", "w") as f: | ||
f.write(content) | ||
|
||
|
||
def load(filename: str) -> dict: | ||
return evalfile(f"parsed/{filename}") |
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env python3 | ||
# coding: utf-8 | ||
|
||
from typing import List | ||
|
||
|
||
# field name + its type, in the order they appear in the header | ||
# of the input file | ||
FIELDS = [ | ||
("Deadline", int), | ||
("BonusPerCar", int), | ||
("etc", int), | ||
] | ||
|
||
|
||
def parse_input(header: dict, content: List[str]) -> dict: | ||
# TODO | ||
|
||
return {} | ||
|
||
|
||
def generate_output(og_data: dict, solution: dict) -> List[str]: | ||
# TODO | ||
|
||
return ["line0"] |