aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-02 06:52:09 +0000
committerGitHub <noreply@github.com>2022-12-02 06:52:09 +0000
commit3e0e855f5dcc210c12a0e390ebaafc98fe974ec4 (patch)
tree983adf7186e20831818e9c4640c7e56fe0dc3c9c
parenta21adbf5d755f19bbcd6361ca5454336e72acfca (diff)
parent1e689df1abdba6669fe7e29f656c70a0da8e82c6 (diff)
downloadperlweeklychallenge-club-3e0e855f5dcc210c12a0e390ebaafc98fe974ec4.tar.gz
perlweeklychallenge-club-3e0e855f5dcc210c12a0e390ebaafc98fe974ec4.tar.bz2
perlweeklychallenge-club-3e0e855f5dcc210c12a0e390ebaafc98fe974ec4.zip
Merge pull request #7187 from vamsi-meenavilli/vamsi-challenge-193
Vamsi challenge 193
-rw-r--r--challenge-193/vamsi-meenavilli/perl/ch-1.pl32
-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, 139 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..03047a7c83
--- /dev/null
+++ b/challenge-193/vamsi-meenavilli/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/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) = @_;
+
+ 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/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..562b55f4fb
--- /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()
+
+
+
+
+