From 610575e0b649289691ad18682b503f73db2d013b Mon Sep 17 00:00:00 2001 From: Solathian Date: Sun, 4 Dec 2022 16:44:32 +0100 Subject: Adding files for challange 193 --- challenge-193/solathian/perl/ch-1.pl | 30 ++++++++++++++++ challenge-193/solathian/perl/ch-2.pl | 67 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 challenge-193/solathian/perl/ch-1.pl create mode 100644 challenge-193/solathian/perl/ch-2.pl 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 -- cgit