From 61cb5f1f04d6541803bd51a3cefe6c3978fa46b8 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 27 Dec 2021 12:24:45 +0000 Subject: Add Perl and Python solutions to challenge 145 --- challenge-145/paulo-custodio/perl/ch-1.pl | 29 +++++++++++++++++ challenge-145/paulo-custodio/perl/ch-2.pl | 50 +++++++++++++++++++++++++++++ challenge-145/paulo-custodio/python/ch-1.py | 22 +++++++++++++ challenge-145/paulo-custodio/python/ch-2.py | 50 +++++++++++++++++++++++++++++ challenge-145/paulo-custodio/t/test-1.yaml | 5 +++ challenge-145/paulo-custodio/t/test-2.yaml | 30 +++++++++++++++++ 6 files changed, 186 insertions(+) create mode 100644 challenge-145/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-145/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-145/paulo-custodio/python/ch-1.py create mode 100644 challenge-145/paulo-custodio/python/ch-2.py create mode 100644 challenge-145/paulo-custodio/t/test-1.yaml create mode 100644 challenge-145/paulo-custodio/t/test-2.yaml diff --git a/challenge-145/paulo-custodio/perl/ch-1.pl b/challenge-145/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..bf986b31f1 --- /dev/null +++ b/challenge-145/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +# Challenge 145 +# +# TASK #1 > Dot Product +# Submitted by: Mohammad S Anwar +# You are given 2 arrays of same size, @a and @b. +# +# Write a script to implement Dot Product. +# +# Example: +# @a = (1, 2, 3); +# @b = (4, 5, 6); +# +# $dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32 + +use Modern::Perl; + +sub dot_product { + my(@ab) = @_; + my $n = int(@ab/2); + my $p = 0; + for my $i (0..$n-1) { + $p += $ab[$i]*$ab[$n+$i]; + } + return $p; +} + +say dot_product(@ARGV); diff --git a/challenge-145/paulo-custodio/perl/ch-2.pl b/challenge-145/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..2c1b966324 --- /dev/null +++ b/challenge-145/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +# Challenge 145 +# +# TASK #2 > Palindromic Tree +# Submitted by: Mohammad S Anwar +# You are given a string $s. +# +# Write a script to create a Palindromic Tree for the given string. +# +# I found this blog exaplaining Palindromic Tree in detail. +# +# Example 1: +# Input: $s = 'redivider' +# Output: r redivider e edivide d divid i ivi v +# Example 2: +# Input: $s = 'deific' +# Output: d e i ifi f c +# Example 3: +# Input: $s = 'rotors' +# Output: r rotor o oto t s +# Example 4: +# Input: $s = 'challenge' +# Output: c h a l ll e n g +# Example 5: +# Input: $s = 'champion' +# Output: c h a m p i o n +# Example 6: +# Input: $s = 'christmas' +# Output: c h r i s t m a + +use Modern::Perl; + +# https://rosettacode.org/wiki/Eertree#Perl +sub build_eertree { + my($str) = @_; + + my @pal; + for my $n (1 .. length($str)) { + for my $m (1 .. length($str)-($n-1)) { + my $strpal = substr($str, $n-1, $m); + push @pal, $strpal if ($strpal eq reverse $strpal); + } + } + + my %seen; + say join ' ', grep {not $seen{$_}++} @pal; +} + +build_eertree(shift||""); diff --git a/challenge-145/paulo-custodio/python/ch-1.py b/challenge-145/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..bde8b1b742 --- /dev/null +++ b/challenge-145/paulo-custodio/python/ch-1.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +# Challenge 145 +# +# TASK #1 > Dot Product +# Submitted by: Mohammad S Anwar +# You are given 2 arrays of same size, @a and @b. +# +# Write a script to implement Dot Product. +# +# Example: +# @a = (1, 2, 3); +# @b = (4, 5, 6); +# +# $dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32 + +import sys +import numpy + +ab = [int(x) for x in sys.argv[1:]] +n = int(len(ab)/2) +print(numpy.dot(ab[:n], ab[n:])) diff --git a/challenge-145/paulo-custodio/python/ch-2.py b/challenge-145/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..0756bb13ae --- /dev/null +++ b/challenge-145/paulo-custodio/python/ch-2.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 + +# Challenge 145 +# +# TASK #2 > Palindromic Tree +# Submitted by: Mohammad S Anwar +# You are given a string $s. +# +# Write a script to create a Palindromic Tree for the given string. +# +# I found this blog exaplaining Palindromic Tree in detail. +# +# Example 1: +# Input: $s = 'redivider' +# Output: r redivider e edivide d divid i ivi v +# Example 2: +# Input: $s = 'deific' +# Output: d e i ifi f c +# Example 3: +# Input: $s = 'rotors' +# Output: r rotor o oto t s +# Example 4: +# Input: $s = 'challenge' +# Output: c h a l ll e n g +# Example 5: +# Input: $s = 'champion' +# Output: c h a m p i o n +# Example 6: +# Input: $s = 'christmas' +# Output: c h r i s t m a + +import sys + +# https://rosettacode.org/wiki/Eertree#Perl +def build_eertree(text): + pal = [] + for n in range(1, len(text)+1): + for m in range(1, len(text)-(n-1)+1): + strpal = text[n-1:n-1+m] + if strpal==strpal[::-1]: + pal.append(strpal) + + seen = set() + for strpal in pal: + if strpal not in seen: + print(strpal, end=" ") + seen.add(strpal) + print("") + +build_eertree(sys.argv[1]) diff --git a/challenge-145/paulo-custodio/t/test-1.yaml b/challenge-145/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..6d13141d85 --- /dev/null +++ b/challenge-145/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 1 2 3 4 5 6 + input: + output: 32 diff --git a/challenge-145/paulo-custodio/t/test-2.yaml b/challenge-145/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..5f3d3ba49a --- /dev/null +++ b/challenge-145/paulo-custodio/t/test-2.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: redivider + input: + output: r redivider e edivide d divid i ivi v +- setup: + cleanup: + args: deific + input: + output: d e i ifi f c +- setup: + cleanup: + args: rotors + input: + output: r rotor o oto t s +- setup: + cleanup: + args: challenge + input: + output: c h a l ll e n g +- setup: + cleanup: + args: champion + input: + output: c h a m p i o n +- setup: + cleanup: + args: christmas + input: + output: c h r i s t m a -- cgit From 334029e5f043365b08eefda94c620376d58ae611 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 27 Dec 2021 15:43:46 +0000 Subject: Add Perl solution to challenge 035 --- challenge-035/paulo-custodio/Makefile | 2 ++ challenge-035/paulo-custodio/README | 1 + challenge-035/paulo-custodio/perl/ch-1.pl | 38 ++++++++++++++++++++++++++++++ challenge-035/paulo-custodio/perl/ch-2.pl | 30 +++++++++++++++++++++++ challenge-035/paulo-custodio/t/test-1.yaml | 5 ++++ challenge-035/paulo-custodio/t/test-2.yaml | 5 ++++ 6 files changed, 81 insertions(+) create mode 100644 challenge-035/paulo-custodio/Makefile create mode 100644 challenge-035/paulo-custodio/README create mode 100644 challenge-035/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-035/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-035/paulo-custodio/t/test-1.yaml create mode 100644 challenge-035/paulo-custodio/t/test-2.yaml diff --git a/challenge-035/paulo-custodio/Makefile b/challenge-035/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-035/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-035/paulo-custodio/README b/challenge-035/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-035/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-035/paulo-custodio/perl/ch-1.pl b/challenge-035/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..1346f11cbf --- /dev/null +++ b/challenge-035/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +# Challenge 035 +# +# TASK #1 +# Contributed by Paul Johnson +# Write a program to encode text into binary encoded morse code. +# Pay attention to any changes which might need to be made to the text to +# make it valid morse code. +# +# Morse code consists of dots, dashes and gaps. It can be encoded in binary +# in the following fashion: +# +# dot: 1 +# dash: 111 +# intra-character gap: 0 +# character gap: 000 +# word gap: 0000000 +# An intra-character gap is inserted between the dots and dashes in a character. + +use Modern::Perl; +use Text::Morse; + +sub encode_bin_morse { + my($text) = @_; + my $morse = Text::Morse->new; + my $encoded = $morse->Encode($text); # ' ' between chars, '\n' between words + for ($encoded) { + s/\./10/g; # . = 1 + 0 gap + s/\-/1110/g; # - = 111 + 0 gap + s/ /00/g; # inter-word = 000 - 0 gap + s/\n/0000/g; # word gap = 0000000 - 00 - 0 + s/0+$// # remove trailing zeros + } + return $encoded; +} + +say encode_bin_morse("@ARGV"); diff --git a/challenge-035/paulo-custodio/perl/ch-2.pl b/challenge-035/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..1450b46fed --- /dev/null +++ b/challenge-035/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +# Challenge 035 +# +# TASK #2 +# Contributed by Paul Johnson +# Write a program to decode binary morse code. +# Consider how it might be possible to recover from badly formed morse code. +# +# a) by splitting the morse code on gaps +# b) without looking further than one digit ahead + +use Modern::Perl; +use Text::Morse; + +sub decode_bin_morse { + my($encoded) = @_; + for ($encoded) { + s/0+$//; # remove trailing zeros + s/0000000/\n/g; # word gap + s/000/ /g; # character gap + s/1110?/-/g; # - + s/10?/./g; # . + } + my $morse = Text::Morse->new; + my $text = $morse->Decode($encoded); + return $text; +} + +say decode_bin_morse("@ARGV"); diff --git a/challenge-035/paulo-custodio/t/test-1.yaml b/challenge-035/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..a26ea529b6 --- /dev/null +++ b/challenge-035/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: MORSE CODE + input: + output: 11101110001110111011100010111010001010100010000000111010111010001110111011100011101010001 diff --git a/challenge-035/paulo-custodio/t/test-2.yaml b/challenge-035/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..9955ded934 --- /dev/null +++ b/challenge-035/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 11101110001110111011100010111010001010100010000000111010111010001110111011100011101010001 + input: + output: MORSE CODE -- cgit From 7191fb81bf1851e1a3804bf7975b6902432e889e Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 27 Dec 2021 16:41:55 +0000 Subject: Add Python solution to challenge 035 --- challenge-035/paulo-custodio/python/ch-1.py | 47 +++++++++++++++++++++++++++++ challenge-035/paulo-custodio/python/ch-2.py | 47 +++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 challenge-035/paulo-custodio/python/ch-1.py create mode 100644 challenge-035/paulo-custodio/python/ch-2.py diff --git a/challenge-035/paulo-custodio/python/ch-1.py b/challenge-035/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..f0ce1e091d --- /dev/null +++ b/challenge-035/paulo-custodio/python/ch-1.py @@ -0,0 +1,47 @@ +#!/usr/bin/python3 + +# Challenge 035 +# +# TASK #1 +# Contributed by Paul Johnson +# Write a program to encode text into binary encoded morse code. +# Pay attention to any changes which might need to be made to the text to +# make it valid morse code. +# +# Morse code consists of dots, dashes and gaps. It can be encoded in binary +# in the following fashion: +# +# dot: 1 +# dash: 111 +# intra-character gap: 0 +# character gap: 000 +# word gap: 0000000 +# An intra-character gap is inserted between the dots and dashes in a character. + +import sys +import re + +# morse code chart +MORSE = { 'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', + 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', + 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', + 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', + 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', + 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', + '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', + '0':'-----'} + +def encode_bin_morse(text): + encoded = "" + for c in text.upper(): + if c.isalnum(): + morse = MORSE[c] + morse = re.sub(r"\.", "10", morse) + morse = re.sub(r"\-", "1110", morse) + encoded += morse+"00" + else: + encoded += "0000" + encoded = re.sub(r"0+$", "", encoded) + return encoded + +print(encode_bin_morse(" ".join(sys.argv[1:]))) diff --git a/challenge-035/paulo-custodio/python/ch-2.py b/challenge-035/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..f38b35a5bb --- /dev/null +++ b/challenge-035/paulo-custodio/python/ch-2.py @@ -0,0 +1,47 @@ +#!/usr/bin/python3 + +# Challenge 035 +# +# Challenge 035 +# +# TASK #2 +# Contributed by Paul Johnson +# Write a program to decode binary morse code. +# Consider how it might be possible to recover from badly formed morse code. +# +# a) by splitting the morse code on gaps +# b) without looking further than one digit ahead + +import sys +import re + +# morse code chart +MORSE = { 'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', + 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', + 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', + 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', + 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', + 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', + '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', + '0':'-----'} + +MORSE_INV = {v:k for k, v in MORSE.items()} + +def decode_bin_morse(encoded): + encoded = re.sub(r"0+$", "", encoded) # remove trailing 0s + encoded = re.sub(r"0000000", "\n", encoded) # word gap + encoded = re.sub(r"000", " ", encoded) # character gap + encoded = re.sub(r"1110?", "-", encoded) # - + encoded = re.sub(r"10?", ".", encoded) # . + + text = "" + words = encoded.split("\n") + for word in words: + chars = word.split() + for c in chars: + text += MORSE_INV[c] + text += " " + text = re.sub(r"\s+$", "", text) + return text + +print(decode_bin_morse(" ".join(sys.argv[1:]))) -- cgit