From 02a1bcc3201bac07f1ecf76f42c9dace016eb805 Mon Sep 17 00:00:00 2001 From: vamsimeenavilli Date: Thu, 1 Dec 2022 23:46:40 +0530 Subject: Vamsi's Perl and Python Solutions for week 193 --- challenge-193/vamsi-meenavilli/perl/ch-1.pl | 37 +++++++++++++++++++++ challenge-193/vamsi-meenavilli/perl/ch-2.pl | 48 +++++++++++++++++++++++++++ challenge-193/vamsi-meenavilli/python/ch-1.py | 20 +++++++++++ challenge-193/vamsi-meenavilli/python/ch-2.py | 39 ++++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 challenge-193/vamsi-meenavilli/perl/ch-1.pl create mode 100644 challenge-193/vamsi-meenavilli/perl/ch-2.pl create mode 100644 challenge-193/vamsi-meenavilli/python/ch-1.py create mode 100644 challenge-193/vamsi-meenavilli/python/ch-2.py diff --git a/challenge-193/vamsi-meenavilli/perl/ch-1.pl b/challenge-193/vamsi-meenavilli/perl/ch-1.pl new file mode 100644 index 0000000000..ccd179a814 --- /dev/null +++ b/challenge-193/vamsi-meenavilli/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl +use strict; +use warnings FATAL => 'all'; + +use Test2::V0; + +=head1 AUTHORS + +Vamsi Meenavilli + +=head1 DESCRIPTION + + https://theweeklychallenge.org/blog/perl-weekly-challenge-193 + + Task 1: Binary String + Submitted by: Mohammad S Anwar + You are given an integer, $n > 0. + + Write a script to find all possible binary numbers of size $n. + +=cut + +is(binaryString(2), ['00', '01', '10', '11'], 'Example 1'); +is(binaryString(3), ['000', '001', '010', '011', '100', '101', '110', '111'], 'Example 2'); + +sub binaryString { + my ($binary_number_size) = @_; + + my @binary_strings = (); + for (my $i = 0; $i < 2**$binary_number_size; $i++) { + push(@binary_strings, sprintf("%0${binary_number_size}b", $i)) + } + + return(\@binary_strings); +} + +done_testing(); \ No newline at end of file diff --git a/challenge-193/vamsi-meenavilli/perl/ch-2.pl b/challenge-193/vamsi-meenavilli/perl/ch-2.pl new file mode 100644 index 0000000000..e2f7cf7694 --- /dev/null +++ b/challenge-193/vamsi-meenavilli/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use strict; +use warnings FATAL => 'all'; + +use Test2::V0; + +=head1 AUTHORS + +Vamsi Meenavilli + +=head1 DESCRIPTION + + https://theweeklychallenge.org/blog/perl-weekly-challenge-193 + + Task 2: Odd String + Submitted by: Mohammad S Anwar + 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. + +=cut + +is(oddString("adc", "wzy", "abc"), "abc", 'Example 1'); +is(oddString("aaa", "bob", "ccc", "ddd"), "bob", 'Example 2'); + +sub oddString { + my (@strings_list) = @_; + + my %strings_list_diff_hash = (); + + foreach my $string (@strings_list) { + my $diff = join(',', map { + ord(substr($string, $_ + 1, 1)) - ord(substr($string, $_, 1)) + } (0..length($string) - 2)); + + push(@{$strings_list_diff_hash{$diff}}, $string) + } + + foreach my $diff (keys(%strings_list_diff_hash)) { + if (scalar(@{$strings_list_diff_hash{$diff}}) == 1) { + return($strings_list_diff_hash{$diff}[0]); + } + } +} + +done_testing() diff --git a/challenge-193/vamsi-meenavilli/python/ch-1.py b/challenge-193/vamsi-meenavilli/python/ch-1.py new file mode 100644 index 0000000000..00a3fd9ed6 --- /dev/null +++ b/challenge-193/vamsi-meenavilli/python/ch-1.py @@ -0,0 +1,20 @@ +""" + Week 192: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-193 + + Task 1: Binary String + Submitted by: Mohammad S Anwar + You are given an integer, $n > 0. + + Write a script to find all possible binary numbers of size $n. +""" + +def binaryString(binary_string_size): + return [bin(i)[2:].zfill(binary_string_size) for i in range(pow(2, binary_string_size))] + +def binaryStringTest(): + assert binaryString(2) == ['00', '01', '10', '11'], 'Example 1 Failed.' + assert binaryString(3) == ['000', '001', '010', '011', '100', '101', '110', '111'], 'Example 2 Failed.' + +binaryStringTest() \ No newline at end of file diff --git a/challenge-193/vamsi-meenavilli/python/ch-2.py b/challenge-193/vamsi-meenavilli/python/ch-2.py new file mode 100644 index 0000000000..8ca2fc9a57 --- /dev/null +++ b/challenge-193/vamsi-meenavilli/python/ch-2.py @@ -0,0 +1,39 @@ +""" + Week 192: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-193 + + Task 2: Odd String + Submitted by: Mohammad S Anwar + 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. +""" + +def oddString(strings_list): + strings_list_diff_map = {} + + for i in strings_list: + diff = ','.join([str(ord(i[j + 1]) - ord(i[j])) for j in range(len(i) - 1)]) + + if diff in strings_list_diff_map: + strings_list_diff_map[diff].append(i) + else: + strings_list_diff_map[diff] = [i] + + for i in strings_list_diff_map: + if len(strings_list_diff_map[i]) == 1: + return strings_list_diff_map[i][0] + +def oddStringTest(): + assert oddString(["adc", "wzy", "abc"]) == "abc", 'Example 1 Failed' + assert oddString(["aaa", "bob", "ccc", "ddd"]) == "bob", 'Example 2 Failed' + +oddStringTest() + + + + + -- cgit From 1e689df1abdba6669fe7e29f656c70a0da8e82c6 Mon Sep 17 00:00:00 2001 From: vamsimeenavilli Date: Thu, 1 Dec 2022 23:52:21 +0530 Subject: Improved solution for perl task 1 and corrected indentation in python task 1 --- challenge-193/vamsi-meenavilli/perl/ch-1.pl | 7 +------ challenge-193/vamsi-meenavilli/python/ch-1.py | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/challenge-193/vamsi-meenavilli/perl/ch-1.pl b/challenge-193/vamsi-meenavilli/perl/ch-1.pl index ccd179a814..03047a7c83 100644 --- a/challenge-193/vamsi-meenavilli/perl/ch-1.pl +++ b/challenge-193/vamsi-meenavilli/perl/ch-1.pl @@ -26,12 +26,7 @@ is(binaryString(3), ['000', '001', '010', '011', '100', '101', '110', '111'], 'E sub binaryString { my ($binary_number_size) = @_; - my @binary_strings = (); - for (my $i = 0; $i < 2**$binary_number_size; $i++) { - push(@binary_strings, sprintf("%0${binary_number_size}b", $i)) - } - - return(\@binary_strings); + return [map {sprintf("%0${binary_number_size}b", $_)} (0..(2**$binary_number_size) - 1)]; } done_testing(); \ No newline at end of file diff --git a/challenge-193/vamsi-meenavilli/python/ch-1.py b/challenge-193/vamsi-meenavilli/python/ch-1.py index 00a3fd9ed6..562b55f4fb 100644 --- a/challenge-193/vamsi-meenavilli/python/ch-1.py +++ b/challenge-193/vamsi-meenavilli/python/ch-1.py @@ -1,7 +1,7 @@ """ Week 192: - https://theweeklychallenge.org/blog/perl-weekly-challenge-193 + https://theweeklychallenge.org/blog/perl-weekly-challenge-193 Task 1: Binary String Submitted by: Mohammad S Anwar -- cgit