File size: 2,404 Bytes
e70dfd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import re


def output_from_text(text):
    #extract the number in the last line of text
    try:
        lines = text.strip().split("\n")
        lines = list(filter(None, lines))
        pattern = r'^[^a-zA-Z0-9\s]+$'
        lines = [s for s in lines if not re.fullmatch(pattern, s)]
        diff = -1
        diff = int(re.findall(r"\b\d+\b", lines[-1])[0])

        if diff != -1:
            return {
                "OUTPUT": diff,
                "ERROR": None
            }
        else:
            return{
                "OUTPUT": None,
                "ERROR": 'Does not give correct format'
            }
    except Exception as e:
        return {
            "OUTPUT": None,
            "ERROR": str(e)
        }

"""import json
import os

def output_from_text(output_text):
    try:
        
        output_text = output_text.split('\n')
        # print(output_text)
        out_words = output_text[-1].split(' ')
        if len(out_words) != 1:
            return {
                "OUTPUT": None,
                "ERROR": "Output file should have only 1 word"
            }
        else:
            count = out_words[0].split(':')
            if (count[0] == 'COUNT'):
                return {
                    "OUTPUT": {
                        "vanished": int(count[1])
                    },
                    "ERROR": None
                }
            else:
                return {
                    "OUTPUT": None,
                    "ERROR": "Output format is not correct"
                }
    except Exception as e:
        return {
            "OUTPUT": None,
            "ERROR": f"Unexpected error while parsing {str(e)}\n"
        }

def gold_to_output(i):
    gold_output = None
    try:
        #get directory of this file
        dir_of_file = os.path.dirname(os.path.realpath(__file__))
        #open the data file
        with open(dir_of_file + "\data.json") as f:
            data = json.load(f)
        row = data[i]
        circles = row['circles']
        triangles = row['triangles']
        squares = row['squares']
        vanished = row['vanished']
        output_prompt = f"CIRCLES:{circles} TRIANGLES:{triangles} SQUARES:{squares} VANISHED:{vanished}"

    except Exception as e:
        return {
            "OUTPUT": None,
            "ERROR": str(e)
        }
    
    return {
        "OUTPUT": output_prompt,
        "ERROR": None
    }"""