aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvamsimeenavilli <vamsi.meenavilli@gmail.com>2022-12-01 23:46:40 +0530
committervamsimeenavilli <vamsi.meenavilli@gmail.com>2022-12-01 23:46:40 +0530
commit02a1bcc3201bac07f1ecf76f42c9dace016eb805 (patch)
treeb60e1f30422fe8423649f0f17a311d0e8b7a4dcc
parenta21adbf5d755f19bbcd6361ca5454336e72acfca (diff)
downloadperlweeklychallenge-club-02a1bcc3201bac07f1ecf76f42c9dace016eb805.tar.gz
perlweeklychallenge-club-02a1bcc3201bac07f1ecf76f42c9dace016eb805.tar.bz2
perlweeklychallenge-club-02a1bcc3201bac07f1ecf76f42c9dace016eb805.zip
Vamsi's Perl and Python Solutions for week 193
-rw-r--r--challenge-193/vamsi-meenavilli/perl/ch-1.pl37
-rw-r--r--challenge-193/vamsi-meenavilli/perl/ch-2.pl48
-rw-r--r--challenge-193/vamsi-meenavilli/python/ch-1.py20
-rw-r--r--challenge-193/vamsi-meenavilli/python/ch-2.py39
4 files changed, 144 insertions, 0 deletions
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()
+
+
+
+
+