aboutsummaryrefslogtreecommitdiff
path: root/challenge-259/luca-ferrari/python/ch-2.py
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2024-03-04 09:39:17 +0100
committerLuca Ferrari <fluca1978@gmail.com>2024-03-04 14:19:40 +0100
commit2b5457f572cf53ed6ff5b679de754feafa5b8eb6 (patch)
treedee7a4007ed79c9bbbc87a1000d910eae4b80396 /challenge-259/luca-ferrari/python/ch-2.py
parentad381c1123e47114d3d98f1749f9f354eebafcdd (diff)
downloadperlweeklychallenge-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.py69
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: ] ) )