aboutsummaryrefslogtreecommitdiff
path: root/challenge-239/jeanluc2020/python
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2023-10-16 23:06:53 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2023-10-16 23:06:53 +0200
commit1fd35b642bbc84be7293410b951273b54ae74cef (patch)
treebd8516eb52724aa204331394cd550f45556d7bc3 /challenge-239/jeanluc2020/python
parentafac53ab966abe7e14039640d054f82eb323097c (diff)
downloadperlweeklychallenge-club-1fd35b642bbc84be7293410b951273b54ae74cef.tar.gz
perlweeklychallenge-club-1fd35b642bbc84be7293410b951273b54ae74cef.tar.bz2
perlweeklychallenge-club-1fd35b642bbc84be7293410b951273b54ae74cef.zip
Add solution 239
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-239/jeanluc2020/python')
-rwxr-xr-xchallenge-239/jeanluc2020/python/ch-1.py61
-rwxr-xr-xchallenge-239/jeanluc2020/python/ch-2.py70
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-239/jeanluc2020/python/ch-1.py b/challenge-239/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..0c520dd59d
--- /dev/null
+++ b/challenge-239/jeanluc2020/python/ch-1.py
@@ -0,0 +1,61 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-239/#TASK1
+#
+# Task 1: Same String
+# ===================
+#
+# You are given two arrays of strings.
+#
+# Write a script to find out if the word created by concatenating the array
+# elements is the same.
+#
+## Example 1
+##
+## Input: @arr1 = ("ab", "c")
+## @arr2 = ("a", "bc")
+## Output: true
+##
+## Using @arr1, word1 => "ab" . "c" => "abc"
+## Using @arr2, word2 => "a" . "bc" => "abc"
+#
+## Example 2
+##
+## Input: @arr1 = ("ab", "c")
+## @arr2 = ("ac", "b")
+## Output: false
+##
+## Using @arr1, word1 => "ab" . "c" => "abc"
+## Using @arr2, word2 => "ac" . "b" => "acb"
+#
+## Example 3
+##
+## Input: @arr1 = ("ab", "cd", "e")
+## @arr2 = ("abcde")
+## Output: true
+##
+## Using @arr1, word1 => "ab" . "cd" . "e" => "abcde"
+## Using @arr2, word2 => "abcde"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# This one is simple, just compare the strings after putting them
+# together from the arrays' content.
+#
+
+
+def same_string(arr1: list, arr2: list):
+ print("Input: (\"", "\", \"".join(arr1), "\"), (\"", "\", \"".join(arr2), "\")", sep='')
+ if "".join(arr1) == "".join(arr2):
+ print("Output: true")
+ else:
+ print("Output: false")
+
+
+same_string( [ "ab", "c" ], ["a", "bc"])
+same_string( [ "ab", "c" ], ["ac", "b"])
+same_string( [ "ab", "cd", "e" ], [ "abcde" ])
+
diff --git a/challenge-239/jeanluc2020/python/ch-2.py b/challenge-239/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..651842ae7d
--- /dev/null
+++ b/challenge-239/jeanluc2020/python/ch-2.py
@@ -0,0 +1,70 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-239/#TASK2
+#
+# Task 2: Consistent Strings
+# ==========================
+#
+# You are given an array of strings and allowed string having distinct
+# characters.
+#
+## A string is consistent if all characters in the string appear in the string
+## allowed.
+#
+# Write a script to return the number of consistent strings in the given array.
+#
+## Example 1
+##
+## Input: @str = ("ad", "bd", "aaab", "baa", "badab")
+## $allowed = "ab"
+## Output: 2
+##
+## Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
+#
+## Example 2
+##
+## Input: @str = ("a", "b", "c", "ab", "ac", "bc", "abc")
+## $allowed = "abc"
+## Output: 7
+#
+## Example 3
+##
+## Input: @str = ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d")
+## $allowed = "cad"
+## Output: 4
+##
+## Strings "cc", "acd", "ac", and "d" are consistent.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Create a dict that uses the characters of allowed as
+# the keys. Then for each string in the array, check all the
+# characters. If one of those isn't in the hash table, the
+# string is not consistent, so we don't count the string.
+# Otherwise, count the string as consistent.
+#
+
+def consistent_strings(arr: list, allowed: str):
+ print("Input: @str = (\"", "\", \"".join(arr), "\"), $allowed = \"", allowed, "\"", sep='')
+ allowed_chars = {}
+ for char in [*allowed]:
+ allowed_chars[char] = 1
+ count = 0
+ for string in arr:
+ consistent = 1
+ for char in [*string]:
+ if char not in allowed_chars:
+ consistent = 0
+ break
+ if consistent:
+ count += 1
+ print(f"Output: {count}")
+
+
+consistent_strings( ["ad", "bd", "aaab", "baa", "badab"], "ab")
+consistent_strings( ["a", "b", "c", "ab", "ac", "bc", "abc"], "abc")
+consistent_strings( ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"], "cad")
+