blob: d8da389ecc4a404ea8d34670768002a937d0f629 (
plain)
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
|
#!python
#
# Perl Weekly Challenge 259
# Task 2
#
# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-259>
#
import sys
# task implementation
# the return value will be printed
def task_2( args ):
line = args[ 0 ]
parsed = {}
id = ''
key = ''
value = ''
is_value = False
is_key = False
for c in line:
if c == '{' :
continue
elif c == '%':
continue
elif c == '}':
return parsed
elif c == ' ':
if not 'id' in parsed and len( id ) > 0 :
parsed[ 'id' ] = id
parsed[ 'fields' ] = []
is_key = True
continue
elif 'fields' in parsed:
parsed[ 'fields' ].append( { key : value } )
is_key = True
key = ''
value = ''
is_value = False
elif c != '=' :
if not 'id' in parsed:
id += c
is_value = False
is_key = False
continue
else:
if is_key:
key += c
else:
value += c
elif c == '=':
if is_key:
is_value = True
is_key = False
return parsed
# invoke the main without the command itself
if __name__ == '__main__':
print( task_2( sys.argv[ 1: ] ) )
|