diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-29 10:55:09 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-29 10:55:09 +0100 |
| commit | 1c83475aade8956c9ef0de26484981e6e93d73fa (patch) | |
| tree | 906eeb28ea7e388d68fac858a3e1e31fa3bef20a | |
| parent | d6ac72f68709c2bc8717461e37fc3a898aef9261 (diff) | |
| download | perlweeklychallenge-club-1c83475aade8956c9ef0de26484981e6e93d73fa.tar.gz perlweeklychallenge-club-1c83475aade8956c9ef0de26484981e6e93d73fa.tar.bz2 perlweeklychallenge-club-1c83475aade8956c9ef0de26484981e6e93d73fa.zip | |
Add Perl and Pyhton solutions to challenge 122
| -rw-r--r-- | challenge-001/paulo-custodio/brainfuck/ch-2.pl | 24 | ||||
| -rw-r--r-- | challenge-122/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-122/paulo-custodio/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-122/paulo-custodio/perl/ch-2.pl | 55 | ||||
| -rw-r--r-- | challenge-122/paulo-custodio/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-122/paulo-custodio/python/ch-2.py | 52 | ||||
| -rw-r--r-- | challenge-122/paulo-custodio/t/test-1.yaml | 23 | ||||
| -rw-r--r-- | challenge-122/paulo-custodio/t/test-2.yaml | 30 |
8 files changed, 225 insertions, 12 deletions
diff --git a/challenge-001/paulo-custodio/brainfuck/ch-2.pl b/challenge-001/paulo-custodio/brainfuck/ch-2.pl index 7ef784110a..65b62fd55a 100644 --- a/challenge-001/paulo-custodio/brainfuck/ch-2.pl +++ b/challenge-001/paulo-custodio/brainfuck/ch-2.pl @@ -14,18 +14,18 @@ use Modern::Perl; my $text = ""; for my $n (1..20) { - if ($n%15==0) { - $text .= "fizzbuzz\n"; - } - elsif ($n%3==0) { - $text .= "fizz\n"; - } - elsif ($n%5==0) { - $text .= "buzz\n"; - } - else { - $text .= "$n\n"; - } + if ($n%15==0) { + $text .= "fizzbuzz\n"; + } + elsif ($n%3==0) { + $text .= "fizz\n"; + } + elsif ($n%5==0) { + $text .= "buzz\n"; + } + else { + $text .= "$n\n"; + } } for (split //, $text) { diff --git a/challenge-122/paulo-custodio/Makefile b/challenge-122/paulo-custodio/Makefile new file mode 100644 index 0000000000..6316089eb8 --- /dev/null +++ b/challenge-122/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-122/paulo-custodio/perl/ch-1.pl b/challenge-122/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..ea7ea0371a --- /dev/null +++ b/challenge-122/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +# TASK #1 > Average of Stream +# Submitted by: Mohammad S Anwar +# You are given a stream of numbers, @N. +# +# Write a script to print the average of the stream at every point. +# +# Example +# Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) +# Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... +# +# Average of first number is 10. +# Average of first 2 numbers (10+20)/2 = 15 +# Average of first 3 numbers (10+20+30)/3 = 20 +# Average of first 4 numbers (10+20+30+40)/4 = 25 and so on. + +use Modern::Perl; + +my $sum = 0; +my $count = 0; +while (<>) { + $sum += $_; + $count++; + say sprintf("%.2f", $sum/$count); +} diff --git a/challenge-122/paulo-custodio/perl/ch-2.pl b/challenge-122/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..de1bfb1ee4 --- /dev/null +++ b/challenge-122/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl + +# TASK #2 > Basketball Points +# Submitted by: Mohammad S Anwar +# You are given a score $S. +# +# You can win basketball points e.g. 1 point, 2 points and 3 points. +# +# Write a script to find out the different ways you can score $S. +# +# Example +# Input: $S = 4 +# Output: 1 1 1 1 +# 1 1 2 +# 1 2 1 +# 1 3 +# 2 1 1 +# 2 2 +# 3 1 +# +# Input: $S = 5 +# Output: 1 1 1 1 1 +# 1 1 1 2 +# 1 1 2 1 +# 1 1 3 +# 1 2 1 1 +# 1 2 2 +# 1 3 1 +# 2 1 1 1 +# 2 1 2 +# 2 2 1 +# 2 3 +# 3 1 1 +# 3 2 + +use Modern::Perl; +use List::Util 'sum'; + +my $N = shift||0; +show_scores($N); + +sub show_scores { + my($N, @points) = @_; + my $s = @points ? sum(@points) : 0; + if ($s > $N) { + } + elsif ($s == $N) { + say "@points"; + } + else { + show_scores($N, @points, 1); + show_scores($N, @points, 2); + show_scores($N, @points, 3); + } +} diff --git a/challenge-122/paulo-custodio/python/ch-1.py b/challenge-122/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..ab54b706fa --- /dev/null +++ b/challenge-122/paulo-custodio/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +# TASK #1 > Average of Stream +# Submitted by: Mohammad S Anwar +# You are given a stream of numbers, @N. +# +# Write a script to print the average of the stream at every point. +# +# Example +# Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) +# Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... +# +# Average of first number is 10. +# Average of first 2 numbers (10+20)/2 = 15 +# Average of first 3 numbers (10+20+30)/3 = 20 +# Average of first 4 numbers (10+20+30+40)/4 = 25 and so on. + +import sys + +sum = 0 +count = 0 +for line in sys.stdin: + sum += int(line) + count += 1 + print("{:.2f}".format(sum/count)) diff --git a/challenge-122/paulo-custodio/python/ch-2.py b/challenge-122/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..0556080767 --- /dev/null +++ b/challenge-122/paulo-custodio/python/ch-2.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# TASK #2 > Basketball Points +# Submitted by: Mohammad S Anwar +# You are given a score $S. +# +# You can win basketball points e.g. 1 point, 2 points and 3 points. +# +# Write a script to find out the different ways you can score $S. +# +# Example +# Input: $S = 4 +# Output: 1 1 1 1 +# 1 1 2 +# 1 2 1 +# 1 3 +# 2 1 1 +# 2 2 +# 3 1 +# +# Input: $S = 5 +# Output: 1 1 1 1 1 +# 1 1 1 2 +# 1 1 2 1 +# 1 1 3 +# 1 2 1 1 +# 1 2 2 +# 1 3 1 +# 2 1 1 1 +# 2 1 2 +# 2 2 1 +# 2 3 +# 3 1 1 +# 3 2 + +import sys + +def show_scores(N): + def scores(N, points): + s = sum(points) + if s > N: + pass + elif s == N: + print(" ".join([str(x) for x in points])) + else: + scores(N, [*points, 1]) + scores(N, [*points, 2]) + scores(N, [*points, 3]) + scores(N, []) + +N = int(sys.argv[1]) +show_scores(N) diff --git a/challenge-122/paulo-custodio/t/test-1.yaml b/challenge-122/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..e62a1cd366 --- /dev/null +++ b/challenge-122/paulo-custodio/t/test-1.yaml @@ -0,0 +1,23 @@ +- setup: + cleanup: + args: + input: | + 10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90 + output: | + 10.00 + 15.00 + 20.00 + 25.00 + 30.00 + 35.00 + 40.00 + 45.00 + 50.00 diff --git a/challenge-122/paulo-custodio/t/test-2.yaml b/challenge-122/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..df6bd41cc9 --- /dev/null +++ b/challenge-122/paulo-custodio/t/test-2.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: 4 + input: + output: | + 1 1 1 1 + 1 1 2 + 1 2 1 + 1 3 + 2 1 1 + 2 2 + 3 1 +- setup: + cleanup: + args: 5 + input: + output: | + 1 1 1 1 1 + 1 1 1 2 + 1 1 2 1 + 1 1 3 + 1 2 1 1 + 1 2 2 + 1 3 1 + 2 1 1 1 + 2 1 2 + 2 2 1 + 2 3 + 3 1 1 + 3 2 |
