diff options
| author | irifkin <ianrifkin@ianrifkin.com> | 2023-10-17 11:25:52 -0400 |
|---|---|---|
| committer | irifkin <ianrifkin@ianrifkin.com> | 2023-10-17 11:25:52 -0400 |
| commit | 046e8e4b84c9ebaaec9030a7066e638224e1fe01 (patch) | |
| tree | 1dc4e28c9026e71d1d76d65570f19178cc1ed86d /challenge-239 | |
| parent | afac53ab966abe7e14039640d054f82eb323097c (diff) | |
| download | perlweeklychallenge-club-046e8e4b84c9ebaaec9030a7066e638224e1fe01.tar.gz perlweeklychallenge-club-046e8e4b84c9ebaaec9030a7066e638224e1fe01.tar.bz2 perlweeklychallenge-club-046e8e4b84c9ebaaec9030a7066e638224e1fe01.zip | |
Adding solutions for challenge 239 in Perl and Python
Diffstat (limited to 'challenge-239')
| -rw-r--r-- | challenge-239/ianrifkin/perl/task1.pl | 28 | ||||
| -rw-r--r-- | challenge-239/ianrifkin/perl/task2.pl | 49 | ||||
| -rw-r--r-- | challenge-239/ianrifkin/python/task1.py | 22 | ||||
| -rw-r--r-- | challenge-239/ianrifkin/python/task2.py | 34 |
4 files changed, 133 insertions, 0 deletions
diff --git a/challenge-239/ianrifkin/perl/task1.pl b/challenge-239/ianrifkin/perl/task1.pl new file mode 100644 index 0000000000..b8bd54a539 --- /dev/null +++ b/challenge-239/ianrifkin/perl/task1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use warnings; +use strict; + +# Task 1: Same String +# Write a script to find out if the word created by concatenating the array elements is the same. + +# Example 1 +my @arr1 = ("ab", "c"); +my @arr2 = ("a", "bc"); +check_equality(join('', @arr1), join('', @arr2)); + +# Example 2 +@arr1 = ("ab", "c"); +@arr2 = ("ac", "b"); +check_equality(join('', @arr1), join('', @arr2)); + +# Example 3 +@arr1 = ("ab", "cd", "e"); +@arr2 = ("abcde"); +check_equality(join('', @arr1), join('', @arr2)); + +sub check_equality { + my ($arr1, $arr2) = @_; + # Print the word 'True' or 'False' if the strings are the same or not + $arr1 eq $arr2 ? print "True\n" : print "False\n"; +} + diff --git a/challenge-239/ianrifkin/perl/task2.pl b/challenge-239/ianrifkin/perl/task2.pl new file mode 100644 index 0000000000..61ffced603 --- /dev/null +++ b/challenge-239/ianrifkin/perl/task2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +use warnings; +use strict; + +# 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. + + +# Example 1 +my @str = ("ad", "bd", "aaab", "baa", "badab"); +my $allowed = "ab"; +check_allowed($allowed, \@str); + +# Example 2 +@str = ("a", "b", "c", "ab", "ac", "bc", "abc"); +$allowed = "abc"; +check_allowed($allowed, \@str); + +# Example 3 +@str = ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d"); +$allowed = "cad"; +check_allowed($allowed, \@str); + +sub check_allowed { + my ($allowed, $str_ref) = @_; + my $success_count = 0; + + # Loop through each item in array of strings + foreach my $str (@$str_ref) { + # Loop through each character in string to check in allowed list + my @chars = (split('', $str)); + my $length = scalar @chars; + + for (my $i = 0; $i < $length; $i++) { + my $char = $chars[$i]; + # if character is allowed keep checking or increment success if it's the last in the string + if (index($allowed, $char) != -1) { + $success_count++ if $i == $length - 1; + } + else { + last; + } + + } + } + print "$success_count\n"; +} + diff --git a/challenge-239/ianrifkin/python/task1.py b/challenge-239/ianrifkin/python/task1.py new file mode 100644 index 0000000000..690ecec8a3 --- /dev/null +++ b/challenge-239/ianrifkin/python/task1.py @@ -0,0 +1,22 @@ +#!/usr/local/bin/python3 + +# Task 1: Same String +# Write a script to find out if the word created by concatenating the array elements is the same. + +def check_equality(arr1, arr2): + print(arr1 == arr2) + +# Example 1 +arr1 = ["ab", "c"] +arr2 = ["a", "bc"] +check_equality(''.join(arr1), ''.join(arr2)) + +# Example 2 +arr1 = ["ab", "c"] +arr2 = ["ac", "b"] +check_equality(''.join(arr1), ''.join(arr2)) + +# Example 3 +arr1 = ["ab", "cd", "e"] +arr2 = ["abcde"] +check_equality(''.join(arr1), ''.join(arr2)) diff --git a/challenge-239/ianrifkin/python/task2.py b/challenge-239/ianrifkin/python/task2.py new file mode 100644 index 0000000000..19fce0c495 --- /dev/null +++ b/challenge-239/ianrifkin/python/task2.py @@ -0,0 +1,34 @@ +#!/usr/local/bin/python3 + +# 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. + +def check_allowed(allowed, str_list): + success_count = 0 + # check each set of letters in the str list + for letters in str_list: + # if a letter doesn't match exit loop otherwise continue. + for i, char in enumerate(letters): + if char in allowed: + if i == (len(letters)-1): # if last then increment success + success_count += 1 + continue + else: + break # exit letters loop if one letter doesn't match + print(success_count) + +# Example 1 +str_list = ["ad", "bd", "aaab", "baa", "badab"] +allowed = "ab" +check_allowed(allowed, str_list) + +# Example 2 +str_list = ["a", "b", "c", "ab", "ac", "bc", "abc"] +allowed = "abc" +check_allowed(allowed, str_list) + +# Example 3 +str_list = ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"] +allowed = "cad" +check_allowed(allowed, str_list) |
