aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-11 09:49:30 +0000
committerGitHub <noreply@github.com>2021-12-11 09:49:30 +0000
commit3c5c2c10dac9dd2061012cb5b53363a6cb546582 (patch)
tree76dc96d889bc86f2681b0ae7675edecd5cb215c1
parenta64c96703f1714a1598fe7fb90450a2763122643 (diff)
parent47f1c1bf0b562df3816bb59511b51230fc1efc80 (diff)
downloadperlweeklychallenge-club-3c5c2c10dac9dd2061012cb5b53363a6cb546582.tar.gz
perlweeklychallenge-club-3c5c2c10dac9dd2061012cb5b53363a6cb546582.tar.bz2
perlweeklychallenge-club-3c5c2c10dac9dd2061012cb5b53363a6cb546582.zip
Merge pull request #5360 from pauloscustodio/devel
Devel
-rw-r--r--challenge-025/paulo-custodio/Makefile2
-rw-r--r--challenge-025/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-025/paulo-custodio/python/ch-1.py44
-rw-r--r--challenge-025/paulo-custodio/python/ch-2.py60
-rw-r--r--challenge-025/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-025/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-025/paulo-custodio/test.pl22
-rw-r--r--challenge-026/paulo-custodio/Makefile2
-rw-r--r--challenge-026/paulo-custodio/perl/ch-1.pl8
-rw-r--r--challenge-026/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-026/paulo-custodio/python/ch-1.py19
-rw-r--r--challenge-026/paulo-custodio/python/ch-2.py28
-rw-r--r--challenge-026/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-026/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-026/paulo-custodio/test.pl18
15 files changed, 192 insertions, 47 deletions
diff --git a/challenge-025/paulo-custodio/Makefile b/challenge-025/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-025/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-025/paulo-custodio/perl/ch-1.pl b/challenge-025/paulo-custodio/perl/ch-1.pl
index 22f59dca77..765a8c77f0 100644
--- a/challenge-025/paulo-custodio/perl/ch-1.pl
+++ b/challenge-025/paulo-custodio/perl/ch-1.pl
@@ -1,8 +1,8 @@
-#!/usr/bin/env perl
+#!/usr/bin/perl
# Challenge 025
#
-# Generate a longest sequence of the following “English Pokemon” names where
+# Generate a longest sequence of the following "English Pokemon" names where
# each name starts with the last letter of previous name.
use Modern::Perl;
diff --git a/challenge-025/paulo-custodio/python/ch-1.py b/challenge-025/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..48e3403468
--- /dev/null
+++ b/challenge-025/paulo-custodio/python/ch-1.py
@@ -0,0 +1,44 @@
+#!/usr/bin/python3
+
+# Challenge 025
+#
+# Generate a longest sequence of the following "English Pokemon" names where
+# each name starts with the last letter of previous name.
+
+names = """
+ audino bagon baltoy banette bidoof braviary bronzor carracosta
+ charmeleon cresselia croagunk darmanitan deino emboar emolga
+ exeggcute gabite girafarig gulpin haxorus heatmor heatran
+ ivysaur jellicent jumpluff kangaskhan kricketune landorus
+ ledyba loudred lumineon lunatone machamp magnezone mamoswine
+ nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena
+ porygon2 porygonz registeel relicanth remoraid rufflet sableye
+ scolipede scrafty seaking sealeo silcoon simisear snivy snorlax
+ spoink starly tirtouga trapinch treecko tyrogue vigoroth
+ vulpix wailord wartortle whismur wingull yamask
+""".split()
+
+def find_longest_seq(words):
+ longest_path = []
+
+ def find_longest(path):
+ nonlocal longest_path, words
+
+ # find words that can still be used
+ pending = []
+ for word in words:
+ if word not in path:
+ if len(path)==0 or path[-1][-1]==word[0]:
+ pending.append(word)
+ if len(pending)==0: # end of search
+ if len(path) > len(longest_path):
+ longest_path = path
+ else: # find each possible path
+ for word in pending:
+ find_longest([*path, word])
+
+ find_longest([])
+ return longest_path
+
+seq = find_longest_seq(names)
+print(*seq)
diff --git a/challenge-025/paulo-custodio/python/ch-2.py b/challenge-025/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..6a39ecf9fc
--- /dev/null
+++ b/challenge-025/paulo-custodio/python/ch-2.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+
+# Challenge 025
+#
+# Task #2
+# Create script to implement Chaocipher. Please checkout wiki page for more
+# information.
+
+import sys
+import re
+
+left = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
+right = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
+
+def permute_alphabets(pos):
+ global left, right
+
+ # permute left alphabet
+ left = left[pos:]+left[:pos]
+ left = left[0]+left[2:14]+left[1]+left[14:]
+
+ # permute right alphabet
+ right = right[pos:]+right[:pos]
+ right = right[1:]+right[0]
+ right = right[:2]+right[3:14]+right[2]+right[14:]
+
+def find_pos(letter, alphabet):
+ found = re.search(letter, alphabet)
+ return found.start(0)
+
+def encode(text):
+ global left, right
+
+ encoded = ""
+ for letter in text:
+ pos = find_pos(letter, right)
+ code = left[pos]
+ encoded += code
+
+ permute_alphabets(pos)
+ return encoded
+
+def decode(encoded):
+ global left, right
+
+ text = ""
+ for code in encoded:
+ pos = find_pos(code, left)
+ letter = right[pos]
+ text += letter
+
+ permute_alphabets(pos)
+ return text
+
+if sys.argv[1]=="encode":
+ print(encode(sys.argv[2]))
+elif sys.argv[1]=="decode":
+ print(decode(sys.argv[2]))
+else:
+ print("Usage: ch-2.py encode|decode text")
diff --git a/challenge-025/paulo-custodio/t/test-1.yaml b/challenge-025/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..26df1b4cd5
--- /dev/null
+++ b/challenge-025/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: machamp petilil landorus scrafty yamask kricketune emboar registeel loudred darmanitan nosepass simisear relicanth heatmor rufflet trapinch haxorus seaking girafarig gabite exeggcute emolga audino
diff --git a/challenge-025/paulo-custodio/t/test-2.yaml b/challenge-025/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..603685e435
--- /dev/null
+++ b/challenge-025/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: encode WELLDONEISBETTERTHANWELLSAID
+ input:
+ output: OAHQHCNYNXTSZJRRHJBYHQKSOUJY
+- setup:
+ cleanup:
+ args: decode OAHQHCNYNXTSZJRRHJBYHQKSOUJY
+ input:
+ output: WELLDONEISBETTERTHANWELLSAID
diff --git a/challenge-025/paulo-custodio/test.pl b/challenge-025/paulo-custodio/test.pl
deleted file mode 100644
index eb06b45b8e..0000000000
--- a/challenge-025/paulo-custodio/test.pl
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use Modern::Perl;
-
-is capture("perl perl/ch-1.pl"), <<END;
-machamp petilil landorus scrafty yamask kricketune emboar registeel loudred darmanitan nosepass simisear relicanth heatmor rufflet trapinch haxorus seaking girafarig gabite exeggcute emolga audino
-END
-
-my($text, $encoded) = qw( WELLDONEISBETTERTHANWELLSAID
- OAHQHCNYNXTSZJRRHJBYHQKSOUJY );
-is capture("perl perl/ch-2.pl encode $text"), "$encoded\n";
-is capture("perl perl/ch-2.pl decode $encoded"), "$text\n";
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}
diff --git a/challenge-026/paulo-custodio/Makefile b/challenge-026/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-026/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-026/paulo-custodio/perl/ch-1.pl b/challenge-026/paulo-custodio/perl/ch-1.pl
index b301adbc08..c22ef33cdf 100644
--- a/challenge-026/paulo-custodio/perl/ch-1.pl
+++ b/challenge-026/paulo-custodio/perl/ch-1.pl
@@ -3,10 +3,10 @@
# Challenge 026
#
# Task #1
-# Create a script that accepts two strings, let us call it, “stones” and
-# “jewels”. It should print the count of “alphabet” from the string “stones”
-# found in the string “jewels”. For example, if your stones is “chancellor” and
-# “jewels” is “chocolate”, then the script should print “8”. To keep it simple,
+# Create a script that accepts two strings, let us call it, "stones" and
+# "jewels". It should print the count of "alphabet" from the string "stones"
+# found in the string "jewels". For example, if your stones is "chancellor" and
+# "jewels" is "chocolate", then the script should print "8". To keep it simple,
# only A-Z,a-z characters are acceptable. Also make the comparison case
# sensitive.
diff --git a/challenge-026/paulo-custodio/perl/ch-2.pl b/challenge-026/paulo-custodio/perl/ch-2.pl
index a1a389a8fa..fc10fe6944 100644
--- a/challenge-026/paulo-custodio/perl/ch-2.pl
+++ b/challenge-026/paulo-custodio/perl/ch-2.pl
@@ -9,7 +9,7 @@
use Modern::Perl;
use Math::Trig;
-say mean(@ARGV);
+say sprintf("%.1f", mean(@ARGV));
sub mean {
my(@a) = @_;
diff --git a/challenge-026/paulo-custodio/python/ch-1.py b/challenge-026/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d26cb6d4a3
--- /dev/null
+++ b/challenge-026/paulo-custodio/python/ch-1.py
@@ -0,0 +1,19 @@
+#!/usr/bin/python3
+
+# Challenge 026
+#
+# Task #1
+# Create a script that accepts two strings, let us call it, "stones" and
+# "jewels". It should print the count of "alphabet" from the string "stones"
+# found in the string "jewels". For example, if your stones is "chancellor" and
+# "jewels" is "chocolate", then the script should print "8". To keep it simple,
+# only A-Z,a-z characters are acceptable. Also make the comparison case
+# sensitive.
+
+import sys
+
+def count(letters, word):
+ letters = set([x for x in letters])
+ return len(list(filter(lambda x: x in letters, [x for x in word])))
+
+print(count(sys.argv[1], sys.argv[2]))
diff --git a/challenge-026/paulo-custodio/python/ch-2.py b/challenge-026/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..6f1df080cc
--- /dev/null
+++ b/challenge-026/paulo-custodio/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/python3
+
+# Challenge 026
+#
+# Task #2
+# Create a script that prints mean angles of the given list of angles in degrees.
+# Please read wiki page that explains the formula in details with an example.
+
+import sys
+import math
+
+def mean(a):
+ # convert to radians
+ a = list(map(math.radians, a))
+
+ # compute sum of sin and cos
+ x = sum(map(math.cos, a))
+ y = sum(map(math.sin, a))
+
+ # compute mean
+ a = math.atan2(y, x)
+
+ # convert back to degrees
+ a = math.degrees(a)
+
+ return a
+
+print("{:.1f}".format(mean([int(x) for x in sys.argv[1:]])))
diff --git a/challenge-026/paulo-custodio/t/test-1.yaml b/challenge-026/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..f8a5a8bfb4
--- /dev/null
+++ b/challenge-026/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: chancellor chocolate
+ input:
+ output: 8
diff --git a/challenge-026/paulo-custodio/t/test-2.yaml b/challenge-026/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..54b060edc3
--- /dev/null
+++ b/challenge-026/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 0 90
+ input:
+ output: 45.0
+- setup:
+ cleanup:
+ args: 0 45 90
+ input:
+ output: 45.0
diff --git a/challenge-026/paulo-custodio/test.pl b/challenge-026/paulo-custodio/test.pl
deleted file mode 100644
index d62439d477..0000000000
--- a/challenge-026/paulo-custodio/test.pl
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use Modern::Perl;
-
-is capture("perl perl/ch-1.pl chancellor chocolate"), "8\n";
-
-is capture("perl perl/ch-2.pl 0 90"), "45\n";
-is capture("perl perl/ch-2.pl 0 45 90"), "45\n";
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}