aboutsummaryrefslogtreecommitdiff
path: root/challenge-250/jeanluc2020/python
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2024-01-01 10:08:24 +0100
committerThomas Köhler <jean-luc@picard.franken.de>2024-01-01 10:08:24 +0100
commitab435ac20e78ef0026c349ad4de13345fd236ac9 (patch)
tree8e54b4203314c7f4d065881b5f8d81c2611b53cd /challenge-250/jeanluc2020/python
parent558be93cbb41f6a2459bdb7194e2d9348c87ea38 (diff)
downloadperlweeklychallenge-club-ab435ac20e78ef0026c349ad4de13345fd236ac9.tar.gz
perlweeklychallenge-club-ab435ac20e78ef0026c349ad4de13345fd236ac9.tar.bz2
perlweeklychallenge-club-ab435ac20e78ef0026c349ad4de13345fd236ac9.zip
Add solution 250.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-250/jeanluc2020/python')
-rwxr-xr-xchallenge-250/jeanluc2020/python/ch-1.py60
-rwxr-xr-xchallenge-250/jeanluc2020/python/ch-2.py64
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-250/jeanluc2020/python/ch-1.py b/challenge-250/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..000c1334d1
--- /dev/null
+++ b/challenge-250/jeanluc2020/python/ch-1.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/#TASK1
+#
+# Task 1: Smallest Index
+# ======================
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to find the smallest index i such that i mod 10 == $ints[i]
+# otherwise return -1.
+#
+## Example 1
+##
+## Input: @ints = (0, 1, 2)
+## Output: 0
+##
+## i=0: 0 mod 10 = 0 == $ints[0].
+## i=1: 1 mod 10 = 1 == $ints[1].
+## i=2: 2 mod 10 = 2 == $ints[2].
+## All indices have i mod 10 == $ints[i], so we return the smallest index 0.
+#
+## Example 2
+##
+## Input: @ints = (4, 3, 2, 1)
+## Output: 2
+##
+## i=0: 0 mod 10 = 0 != $ints[0].
+## i=1: 1 mod 10 = 1 != $ints[1].
+## i=2: 2 mod 10 = 2 == $ints[2].
+## i=3: 3 mod 10 = 3 != $ints[3].
+## 2 is the only index which has i mod 10 == $ints[i].
+#
+## Example 3
+##
+## Input: @ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
+## Output: -1
+## Explanation: No index satisfies i mod 10 == $ints[i].
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Walk the array from the start to the end, return once
+# i mod 10 == $ints[i]. In the end, return -1 if no entry
+# satisfied the condition.
+
+def smallest_index(ints: list):
+ print("Input: (", ", ".join([str(x) for x in ints]), ")")
+ for i in range(len(ints)):
+ if i % 10 == ints[i]:
+ print(f"Output: {i}")
+ return
+ print("Output: -1")
+
+smallest_index([0, 1, 2])
+smallest_index([4, 3, 2, 1])
+smallest_index([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
+
diff --git a/challenge-250/jeanluc2020/python/ch-2.py b/challenge-250/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..3ffa4cf1e3
--- /dev/null
+++ b/challenge-250/jeanluc2020/python/ch-2.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/#TASK2
+#
+# Task 2: Alphanumeric String Value
+# =================================
+#
+# You are given an array of alphanumeric strings.
+#
+# Write a script to return the maximum value of alphanumeric string in the
+# given array.
+#
+# The value of alphanumeric string can be defined as
+#
+# a) The numeric representation of the string in base 10 if it is made up of
+# digits only.
+# b) otherwise the length of the string
+#
+#
+## Example 1
+##
+## Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
+## Output: 6
+##
+## "perl" consists of letters only so the value is 4.
+## "2" is digits only so the value is 2.
+## "000" is digits only so the value is 0.
+## "python" consits of letters so the value is 6.
+## "r4ku" consists of letters and digits so the value is 4.
+#
+## Example 2
+##
+## Input: @alphanumstr = ("001", "1", "000", "0001")
+## Output: 1
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Try to match each string against a non-digit character. If that succeeds (or
+# the length is 0), use the length. If it doesn't, convert the string to an
+# integer.
+
+import re
+
+def alphanumeric_string_value(alphanumstr: list):
+ print("Input: (", ", ".join(alphanumstr) , ")")
+ max = 0
+ for string in alphanumstr:
+ if len(string) > 0:
+ if re.match('[^\d]', string):
+ if len(string) > max:
+ max = len(string)
+ else:
+ if int(string) > max:
+ max = int(string)
+ print(f"Output: {max}")
+
+
+
+alphanumeric_string_value(["perl", "2", "000", "python", "r4ku"])
+alphanumeric_string_value(["001", "1", "000", "0001"])
+