diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-04 18:27:17 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-04 18:27:17 +0000 |
| commit | ad8a7a3c381e43e2ff4440fd97ec7e394ab4c9ff (patch) | |
| tree | 3319fcddc063a28903b7a505f81817dafb7ef603 | |
| parent | 206ace99654d6337778b1a1551ed875760741a3d (diff) | |
| parent | 610575e0b649289691ad18682b503f73db2d013b (diff) | |
| download | perlweeklychallenge-club-ad8a7a3c381e43e2ff4440fd97ec7e394ab4c9ff.tar.gz perlweeklychallenge-club-ad8a7a3c381e43e2ff4440fd97ec7e394ab4c9ff.tar.bz2 perlweeklychallenge-club-ad8a7a3c381e43e2ff4440fd97ec7e394ab4c9ff.zip | |
Merge pull request #7201 from Solathian/branch-for-challenge-193
Adding files for challange 193
| -rw-r--r-- | challenge-193/solathian/perl/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-193/solathian/perl/ch-2.pl | 67 |
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-193/solathian/perl/ch-1.pl b/challenge-193/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..f458de8a24 --- /dev/null +++ b/challenge-193/solathian/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!usr/bin/perl +use v5.32; +use warnings; + +use feature 'signatures'; +no warnings 'experimental'; + +use Algorithm::Combinatorics qw(variations_with_repetition); + +# Challange 193 - 1 - Binary String +# You are given an integer, $n > 0. +# Write a script to find all possible binary numbers of size $n. + +sub binString($n) +{ + my @resultArr; + + foreach my $arrRef (variations_with_repetition( ['0', '1'], $n)) + { + push(@resultArr, join('', @$arrRef)); # re-create the string from the array and push + } + + say join(', ', @resultArr); # create result string from the collected strings + +} + +# binString(1); # 0, 1 +# binString(2); # Output: 00, 11, 01, 10 +# binString(3); # Output: 000, 001, 010, 100, 111, 110, 101, 011 +# binString(16); # ... diff --git a/challenge-193/solathian/perl/ch-2.pl b/challenge-193/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..9a66337344 --- /dev/null +++ b/challenge-193/solathian/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!usr/bin/perl +use v5.32; +use warnings; + +use feature 'signatures'; +no warnings 'experimental'; + +use Clone qw(clone); + +# Challange 193 - 2 - Odd String + + +# You are given a list of strings of same length, @s. +# Write a script to find the odd string in the given list. Use positional value of alphabet starting with 0, i.e. a = 0, b = 1, ... z = 25. +# Find the difference array for each string as shown in the example. Then pick the odd one out. + +sub oddString($listRef) +{ + my @differenceArrays; + + foreach my $string (@$listRef) + { + my @tempArray; + my @array = split('', $string); + + map($_ = (ord($_) - ord("a")), @array); + + for(my $i = 0; $i < $#array; $i++) + { + push(@tempArray, ($array[$i + 1] - $array[$i])); + } + + push(@differenceArrays, join(',', @tempArray)); + } + + + + for(my $i = 0; $i < @differenceArrays; $i++) + { + # save and remove the element from the array + my $current = clone($differenceArrays[$i]); + $differenceArrays[$i] = undef; + + + # check if the deepcopied array is still present in the difference array, if not then it is odd + if(not ($current ~~ @differenceArrays)) + { + say "The difference array for \"". $listRef->[$i] . "\" is odd"; + last; + } + + # restore + $differenceArrays[$i] = $current; + } + + say "Did not find Odd element."; +} + +# my @s = ("aaa", "bob", "ccc", "ddd"); +# oddString(\@s); +# The difference array for "bob" is the odd one. + + + +# my @s2 = ("adc", "wzy", "abc"); +# oddString(\@s2); +# The difference array for "abc" is the odd one.
\ No newline at end of file |
