From 47f1c1bf0b562df3816bb59511b51230fc1efc80 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 10 Dec 2021 18:51:04 +0000 Subject: Add Python solution to challenge 26 --- challenge-026/paulo-custodio/Makefile | 2 ++ challenge-026/paulo-custodio/perl/ch-1.pl | 8 ++++---- challenge-026/paulo-custodio/perl/ch-2.pl | 2 +- challenge-026/paulo-custodio/python/ch-1.py | 19 +++++++++++++++++++ challenge-026/paulo-custodio/python/ch-2.py | 28 ++++++++++++++++++++++++++++ challenge-026/paulo-custodio/t/test-1.yaml | 5 +++++ challenge-026/paulo-custodio/t/test-2.yaml | 10 ++++++++++ challenge-026/paulo-custodio/test.pl | 18 ------------------ 8 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 challenge-026/paulo-custodio/Makefile create mode 100644 challenge-026/paulo-custodio/python/ch-1.py create mode 100644 challenge-026/paulo-custodio/python/ch-2.py create mode 100644 challenge-026/paulo-custodio/t/test-1.yaml create mode 100644 challenge-026/paulo-custodio/t/test-2.yaml delete mode 100644 challenge-026/paulo-custodio/test.pl 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; -} -- cgit