diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-10-31 23:06:52 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-10-31 23:06:52 +0000 |
| commit | e373cd895c0eb2d99846334ed21278b0d457a2a0 (patch) | |
| tree | b5e248cdfdf58e73254f1501ee88070be170196d | |
| parent | 5a6f51a436f3f94d93437eb67d58724c6e83c81b (diff) | |
| parent | 5ff7a880eb5ae7a8ae15ddbd8d1f398cebc2c21e (diff) | |
| download | perlweeklychallenge-club-e373cd895c0eb2d99846334ed21278b0d457a2a0.tar.gz perlweeklychallenge-club-e373cd895c0eb2d99846334ed21278b0d457a2a0.tar.bz2 perlweeklychallenge-club-e373cd895c0eb2d99846334ed21278b0d457a2a0.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-001/paulo-custodio/brainfuck/ch-2.pl | 24 | ||||
| -rw-r--r-- | challenge-118/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-118/paulo-custodio/d/ch_2.d | 2 | ||||
| -rw-r--r-- | challenge-118/paulo-custodio/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-118/paulo-custodio/test.pl | 4 | ||||
| -rw-r--r-- | challenge-121/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-121/paulo-custodio/python/ch-1.py | 30 | ||||
| -rw-r--r-- | challenge-121/paulo-custodio/python/ch-2.py | 57 | ||||
| -rw-r--r-- | challenge-121/paulo-custodio/test.pl | 4 | ||||
| -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 |
16 files changed, 344 insertions, 21 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-118/paulo-custodio/Makefile b/challenge-118/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-118/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-118/paulo-custodio/d/ch_2.d b/challenge-118/paulo-custodio/d/ch_2.d index 122b253812..eed57ac24d 100644 --- a/challenge-118/paulo-custodio/d/ch_2.d +++ b/challenge-118/paulo-custodio/d/ch_2.d @@ -227,7 +227,7 @@ Move[] next_moves(Game* game) { for (int i = 0; i < moves.length; i++) { Move[] temp_moves; try_num_moves(&temp_moves, game, moves[i].row, moves[i].col); - moves[i].num_moves = temp_moves.length; + moves[i].num_moves = cast(int)temp_moves.length; if (min_moves > moves[i].num_moves) min_moves = moves[i].num_moves; } diff --git a/challenge-118/paulo-custodio/python/ch-1.py b/challenge-118/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..b7652634d3 --- /dev/null +++ b/challenge-118/paulo-custodio/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +# Challenge 118 +# +# TASK #1 - Binary Palindrome +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to find out if the binary representation of the given integer +# is Palindrome. Print 1 if it is otherwise 0. +# +# Example +# Input: $N = 5 +# Output: 1 as binary representation of 5 is 101 which is Palindrome. +# +# Input: $N = 4 +# Output: 0 as binary representation of 4 is 100 which is NOT Palindrome. + +import sys + +N = int(sys.argv[1]) +bits = "{:b}".format(N) +rbits = bits[::-1] +if bits==rbits: + print(1) +else: + print(0) diff --git a/challenge-118/paulo-custodio/test.pl b/challenge-118/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-118/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-121/paulo-custodio/Makefile b/challenge-121/paulo-custodio/Makefile new file mode 100644 index 0000000000..6316089eb8 --- /dev/null +++ b/challenge-121/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-121/paulo-custodio/python/ch-1.py b/challenge-121/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..c4f5276926 --- /dev/null +++ b/challenge-121/paulo-custodio/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +# Challenge 121 +# +# TASK #1 > Invert Bit +# Submitted by: Mohammad S Anwar +# You are given integers 0 <= $m <= 255 and 1 <= $n <= 8. +# +# Write a script to invert $n bit from the end of the binary representation of +# $m and print the decimal representation of the new binary number. +# +# Example +# Input: $m = 12, $n = 3 +# Output: 8 +# +# Binary representation of $m = 00001100 +# Invert 3rd bit from the end = 00001000 +# Decimal equivalent of 00001000 = 8 +# +# Input $m = 18, $n = 4 +# Output: 26 +# +# Binary representation of $m = 00010010 +# Invert 4th bit from the end = 00011010 +# Decimal equivalent of 00011010 = 26 + +import sys + +m, n = int(sys.argv[1]), int(sys.argv[2]) +print(m ^ (1 << (n-1))) diff --git a/challenge-121/paulo-custodio/python/ch-2.py b/challenge-121/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..7a3b6f4443 --- /dev/null +++ b/challenge-121/paulo-custodio/python/ch-2.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +# Challenge 121 +# +# TASK #2 > The Travelling Salesman +# Submitted by: Jorg Sommrey +# You are given a NxN matrix containing the distances between N cities. +# +# Write a script to find a round trip of minimum length visiting all N cities +# exactly once and returning to the start. +# +# Example +# Matrix: [0, 5, 2, 7] +# [5, 0, 5, 3] +# [3, 1, 0, 6] +# [4, 5, 4, 0] +# +# Output: +# length = 10 +# tour = (0 2 1 3 0) + +import sys + +def read_dist(): + dist = [] + for line in sys.stdin: + row = [int(x) for x in line.split()] + dist.append(row) + return dist + +def shortest_tour(dist): + short_length = 100000 + short_path = [] + + def tour(length, path): + nonlocal short_length, short_path + + cities = list(set(range(0, len(dist))) - set(path)) + cities.sort() + + if len(cities)==0: + # no more cities to visit + length += dist[path[-1]][path[0]] + path.append(path[0]) + if length < short_length: + short_length, short_path = length, path + else: + # try each city + for city in cities: + tour(length + dist[path[-1]][city], [*path, city]) + + tour(0, [0]) + return (short_length, short_path) + +length, path = shortest_tour(read_dist()) +print("length =", length) +print("tour = ("+ " ".join([str(x) for x in path]) + ")") diff --git a/challenge-121/paulo-custodio/test.pl b/challenge-121/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-121/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; 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 |
