diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-16 23:27:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-16 23:27:56 +0100 |
| commit | 9c3e2ca8f3eb9a2dd8ae0073b36816f21367a3b6 (patch) | |
| tree | 3b4dcf535d6961d852db7f909035c0c69b0f37a4 | |
| parent | b7c77dc5482e4e7a239b9d70fbea0fd3f714a3e2 (diff) | |
| parent | 1fd35b642bbc84be7293410b951273b54ae74cef (diff) | |
| download | perlweeklychallenge-club-9c3e2ca8f3eb9a2dd8ae0073b36816f21367a3b6.tar.gz perlweeklychallenge-club-9c3e2ca8f3eb9a2dd8ae0073b36816f21367a3b6.tar.bz2 perlweeklychallenge-club-9c3e2ca8f3eb9a2dd8ae0073b36816f21367a3b6.zip | |
Merge pull request #8887 from jeanluc2020/jeanluc-239
Add solution 239
| -rw-r--r-- | challenge-239/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-239/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-239/jeanluc2020/perl/ch-1.pl | 63 | ||||
| -rwxr-xr-x | challenge-239/jeanluc2020/perl/ch-2.pl | 74 | ||||
| -rwxr-xr-x | challenge-239/jeanluc2020/python/ch-1.py | 61 | ||||
| -rwxr-xr-x | challenge-239/jeanluc2020/python/ch-2.py | 70 |
6 files changed, 270 insertions, 0 deletions
diff --git a/challenge-239/jeanluc2020/blog-1.txt b/challenge-239/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..8dafa8df91 --- /dev/null +++ b/challenge-239/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-239-1.html diff --git a/challenge-239/jeanluc2020/blog-2.txt b/challenge-239/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..81d70bbdcf --- /dev/null +++ b/challenge-239/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-239-2.html diff --git a/challenge-239/jeanluc2020/perl/ch-1.pl b/challenge-239/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..b6b1e9cf03 --- /dev/null +++ b/challenge-239/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/perl +# 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. +# +use strict; +use warnings; + +same_string( [ "ab", "c" ], ["a", "bc"]); +same_string( [ "ab", "c" ], ["ac", "b"]); +same_string( [ "ab", "cd", "e" ], [ "abcde" ]); + +sub same_string { + my ($arr1, $arr2) = @_; + print "Input: (\"" . join("\", \"", @$arr1) . "\"), (\"" . join("\", \"", @$arr2) . "\")\n"; + if( join("", @$arr1) eq join("", @$arr2) ) { + print "Output: true\n"; + } else { + print "Output: false\n"; + } +} diff --git a/challenge-239/jeanluc2020/perl/ch-2.pl b/challenge-239/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..b39b0e5cd0 --- /dev/null +++ b/challenge-239/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl +# 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 hash table 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. +# +use strict; +use warnings; + +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"); + +sub consistent_strings { + my ($str, $allowed) = @_; + print "Input: \@str = (\"" . join("\", \"", @$str) . "\"), \$allowed = \"$allowed\"\n"; + my %allowed_chars = map { $_ => 1, } split //, $allowed; + my $count = 0; + foreach my $string (@$str) { + my @chars = split //, $string; + my $consistent = 1; + foreach my $char (@chars) { + unless($allowed_chars{$char}) { + $consistent = 0; + last; + } + } + $count++ if $consistent; + } + print "Output: $count\n"; +} + 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") + |
