From 3d9f2beb39164bd4c0dc35eb28258b06bd37feca Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 11 Jan 2021 12:47:43 +0100 Subject: Perl solution for week 95/part 1 --- challenge-095/abigail/README.md | 57 +++++++--------------------------- challenge-095/abigail/perl/ch-1.pl | 43 +++++++++++++++++++++++++ challenge-095/abigail/t/ctest.ini | 6 ++-- challenge-095/abigail/t/input-1-1 | 3 ++ challenge-095/abigail/t/input-1-2 | 4 +++ challenge-095/abigail/t/input-1-3 | 3 ++ challenge-095/abigail/t/input-1-4 | 3 ++ challenge-095/abigail/t/output-1-1.exp | 3 ++ challenge-095/abigail/t/output-1-2.exp | 4 +++ challenge-095/abigail/t/output-1-3.exp | 3 ++ challenge-095/abigail/t/output-1-4.exp | 3 ++ 11 files changed, 84 insertions(+), 48 deletions(-) create mode 100644 challenge-095/abigail/perl/ch-1.pl create mode 100644 challenge-095/abigail/t/input-1-1 create mode 100644 challenge-095/abigail/t/input-1-2 create mode 100644 challenge-095/abigail/t/input-1-3 create mode 100644 challenge-095/abigail/t/input-1-4 create mode 100644 challenge-095/abigail/t/output-1-1.exp create mode 100644 challenge-095/abigail/t/output-1-2.exp create mode 100644 challenge-095/abigail/t/output-1-3.exp create mode 100644 challenge-095/abigail/t/output-1-4.exp diff --git a/challenge-095/abigail/README.md b/challenge-095/abigail/README.md index 1ab213691e..6ceb4f2573 100644 --- a/challenge-095/abigail/README.md +++ b/challenge-095/abigail/README.md @@ -1,58 +1,23 @@ # Solution by Abigail -## Task 1: Group Anagrams +## [Task 1](https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/#TASK1): -You are given an array of strings `@S`. +You are given a number `$N`. -Write a script to group Anagrams together in any random order. +Write a script to figure out if the given number is Palindrome. +Print `1` if true otherwise `0`. ### Examples - -#### Example 1 -~~~~ -Input: ("opt", "bat", "saw", "tab", "pot", "top", "was") -Output: [ ("bat", "tab"), - ("saw", "was"), - ("top", "pot", "opt") ] -~~~~ - -#### Example 2 -~~~~ -Input: ("x") -Output: [ ("x") ] ~~~~ +Input: 1221 +Output: 1 +Input: -101 +Output: 0, since -101 and 101- are not the same. -### Solutions -* [Node](node/ch-1.js) -* [Perl](perl/ch-1.pl) - - - -## Task 2: Binary Tree to Linked List - -You are given a binary tree. - -Write a script to represent the given binary tree as an object and -flatten it to a linked list object. Finally print the linked list -object. - -### Examples - -#### Example 1 -~~~~ -Input: - 1 - / \ - 2 3 - / \ -4 5 - / \ - 6 7 - -Output: - 1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3 +Input: 90 +Output: 0 ~~~~ ### Solutions -* [Perl](perl/ch-2.pl) +* [Perl](perl/ch-1.pl) diff --git a/challenge-095/abigail/perl/ch-1.pl b/challenge-095/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..526df11026 --- /dev/null +++ b/challenge-095/abigail/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# Run as "perl ch-1.pl < input-file" +# with one or more possible palidromes, each on their own line. +# + +# +# A recursive definition of a palindrome: +# - Empty, but followed by a digit (1) +# - A decimal dot, but followed by a digit (1) +# - A single digit +# - A digit, followed by a palindrome, followed by the same digit +# +# (1) The "followed by a digit" is to prevent an empty string, or a +# a lone dot to be considered a palindrome. +# +# We also wrap the pattern into a script run assertion; this means we +# accept palindromes from different scripts -- but we don't allow mixing +# scripts. +# +# That is, we accept +# "\N{TAMIL DIGIT ONE}\N{TAMIL DIGIT TWO}\N{TAMIL DIGIT ONE}" +# +# But not +# "\N{TAMIL DIGIT ONE}\N{THAI DIGIT TWO}\N{TAMIL DIGIT ONE}" +# +# As the latter mixes digits from two different scripts. +# + +binmode *STDIN, ':utf8'; +say /^(*sr: (? \.?(?=\d) | \d | (\d) (?&PAL) \g{-1}))$/x ? 1 : 0 while <>; + +__END__ diff --git a/challenge-095/abigail/t/ctest.ini b/challenge-095/abigail/t/ctest.ini index a103f94dae..a517632fbf 100644 --- a/challenge-095/abigail/t/ctest.ini +++ b/challenge-095/abigail/t/ctest.ini @@ -1,4 +1,6 @@ [names] -1-1 = Example 1 -1-2 = Example 2 +1-1 = Given examples +1-2 = Test decimal dot +1-3 = Very short inputs +1-4 = Unicode and script runs 2-1 = Example 1 diff --git a/challenge-095/abigail/t/input-1-1 b/challenge-095/abigail/t/input-1-1 new file mode 100644 index 0000000000..3cfc543642 --- /dev/null +++ b/challenge-095/abigail/t/input-1-1 @@ -0,0 +1,3 @@ +1221 +-101 +90 diff --git a/challenge-095/abigail/t/input-1-2 b/challenge-095/abigail/t/input-1-2 new file mode 100644 index 0000000000..49329b4437 --- /dev/null +++ b/challenge-095/abigail/t/input-1-2 @@ -0,0 +1,4 @@ +15.15 +15.51 +1.55.1 +. diff --git a/challenge-095/abigail/t/input-1-3 b/challenge-095/abigail/t/input-1-3 new file mode 100644 index 0000000000..be522c208f --- /dev/null +++ b/challenge-095/abigail/t/input-1-3 @@ -0,0 +1,3 @@ +3 + +44 diff --git a/challenge-095/abigail/t/input-1-4 b/challenge-095/abigail/t/input-1-4 new file mode 100644 index 0000000000..8fd1671e8e --- /dev/null +++ b/challenge-095/abigail/t/input-1-4 @@ -0,0 +1,3 @@ +๑๒๓๒๑ +๑๒.๒๑ +๑௧๓௧๑ diff --git a/challenge-095/abigail/t/output-1-1.exp b/challenge-095/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..e22493782f --- /dev/null +++ b/challenge-095/abigail/t/output-1-1.exp @@ -0,0 +1,3 @@ +1 +0 +0 diff --git a/challenge-095/abigail/t/output-1-2.exp b/challenge-095/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..5a4d7b6ab7 --- /dev/null +++ b/challenge-095/abigail/t/output-1-2.exp @@ -0,0 +1,4 @@ +0 +1 +0 +0 diff --git a/challenge-095/abigail/t/output-1-3.exp b/challenge-095/abigail/t/output-1-3.exp new file mode 100644 index 0000000000..16db301bb5 --- /dev/null +++ b/challenge-095/abigail/t/output-1-3.exp @@ -0,0 +1,3 @@ +1 +0 +1 diff --git a/challenge-095/abigail/t/output-1-4.exp b/challenge-095/abigail/t/output-1-4.exp new file mode 100644 index 0000000000..2f1465d159 --- /dev/null +++ b/challenge-095/abigail/t/output-1-4.exp @@ -0,0 +1,3 @@ +1 +1 +0 -- cgit