aboutsummaryrefslogtreecommitdiff
path: root/challenge-258/luca-ferrari/python
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2024-02-26 09:28:02 +0100
committerLuca Ferrari <fluca1978@gmail.com>2024-02-26 13:14:56 +0100
commit093553020c7cf39461a2be0e1c8851c983ba863b (patch)
tree8ed8911f016afa849954c27ed0a8fa22101e25c7 /challenge-258/luca-ferrari/python
parent4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff)
downloadperlweeklychallenge-club-093553020c7cf39461a2be0e1c8851c983ba863b.tar.gz
perlweeklychallenge-club-093553020c7cf39461a2be0e1c8851c983ba863b.tar.bz2
perlweeklychallenge-club-093553020c7cf39461a2be0e1c8851c983ba863b.zip
PWC 258
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 Java done Task 2 Java done
Diffstat (limited to 'challenge-258/luca-ferrari/python')
-rw-r--r--challenge-258/luca-ferrari/python/ch-1.py25
-rw-r--r--challenge-258/luca-ferrari/python/ch-2.py36
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-258/luca-ferrari/python/ch-1.py b/challenge-258/luca-ferrari/python/ch-1.py
new file mode 100644
index 0000000000..4ba789cf54
--- /dev/null
+++ b/challenge-258/luca-ferrari/python/ch-1.py
@@ -0,0 +1,25 @@
+#!python
+
+#
+# Perl Weekly Challenge 258
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-258>
+#
+
+import sys
+
+# task implementation
+# the return value will be printed
+def task_1( args ):
+ sum = 0
+ for n in args:
+ if len( n ) % 2 == 0:
+ sum += 1
+
+ return sum
+
+
+# invoke the main without the command itself
+if __name__ == '__main__':
+ print( task_1( sys.argv[ 1: ] ) )
diff --git a/challenge-258/luca-ferrari/python/ch-2.py b/challenge-258/luca-ferrari/python/ch-2.py
new file mode 100644
index 0000000000..a5f6a97177
--- /dev/null
+++ b/challenge-258/luca-ferrari/python/ch-2.py
@@ -0,0 +1,36 @@
+#!python
+
+#
+# Perl Weekly Challenge 258
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-258>
+#
+
+import sys
+
+
+# task implementation
+# the return value will be printed
+def task_2( args ):
+ k = int( args[ 0 ] )
+ nums = list( map( int, args[ 1: ] ) )
+
+ def is_index_ok( v, k ):
+ b = '{0:08b}'.format( v )
+ count = sum( map( int, list( '{0:08b}'.format( v ) ) ) )
+ return count == k
+
+ indexes = list( filter( lambda i: is_index_ok( i, k ),
+ range(0, len( nums ) ) ) )
+
+ summy = 0
+ for i in indexes:
+ summy += nums[ i ]
+
+ return summy
+
+
+# invoke the main without the command itself
+if __name__ == '__main__':
+ print( task_2( sys.argv[ 1: ] ) )