diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-03-04 09:39:17 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-03-04 14:19:40 +0100 |
| commit | 2b5457f572cf53ed6ff5b679de754feafa5b8eb6 (patch) | |
| tree | dee7a4007ed79c9bbbc87a1000d910eae4b80396 /challenge-259/luca-ferrari/python/ch-2.py | |
| parent | ad381c1123e47114d3d98f1749f9f354eebafcdd (diff) | |
| download | perlweeklychallenge-club-2b5457f572cf53ed6ff5b679de754feafa5b8eb6.tar.gz perlweeklychallenge-club-2b5457f572cf53ed6ff5b679de754feafa5b8eb6.tar.bz2 perlweeklychallenge-club-2b5457f572cf53ed6ff5b679de754feafa5b8eb6.zip | |
PWC 259
Task 1 Raku done
Task 2 Raku done
Task 1 PL/Perl done
Task 2 PL/Perl done
Task 1 PL/PgSQL done
Task 2 PL/PgSQL done
Task 1 Python done
Task 2 Python done
Task 1 PL/Java done
Task 2 PL/Java done
Diffstat (limited to 'challenge-259/luca-ferrari/python/ch-2.py')
| -rw-r--r-- | challenge-259/luca-ferrari/python/ch-2.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/challenge-259/luca-ferrari/python/ch-2.py b/challenge-259/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..d8da389ecc --- /dev/null +++ b/challenge-259/luca-ferrari/python/ch-2.py @@ -0,0 +1,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: ] ) ) |
