diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-26 09:52:33 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-26 09:52:33 +0100 |
| commit | 4fbb301ccb5620c8bcee20462bd1bb8145b0d13c (patch) | |
| tree | ca2cd6d56d2437c433c0913480dd769a0566911b | |
| parent | de4d4e92a64a4cd61ae6db8da41662c88553772a (diff) | |
| download | perlweeklychallenge-club-4fbb301ccb5620c8bcee20462bd1bb8145b0d13c.tar.gz perlweeklychallenge-club-4fbb301ccb5620c8bcee20462bd1bb8145b0d13c.tar.bz2 perlweeklychallenge-club-4fbb301ccb5620c8bcee20462bd1bb8145b0d13c.zip | |
Whitespace
39 files changed, 467 insertions, 470 deletions
diff --git a/challenge-121/paulo-custodio/perl/ch-2.pl b/challenge-121/paulo-custodio/perl/ch-2.pl index 4a45cb60ee..a2ae24043f 100644 --- a/challenge-121/paulo-custodio/perl/ch-2.pl +++ b/challenge-121/paulo-custodio/perl/ch-2.pl @@ -64,5 +64,3 @@ sub tour { } } } - - diff --git a/challenge-129/paulo-custodio/perl/ch-1.pl b/challenge-129/paulo-custodio/perl/ch-1.pl index c5195f9e12..d29cce7989 100644 --- a/challenge-129/paulo-custodio/perl/ch-1.pl +++ b/challenge-129/paulo-custodio/perl/ch-1.pl @@ -3,9 +3,9 @@ # TASK #1 > Root Distance # Submitted by: Mohammad S Anwar # You are given a tree and a node of the given tree. -# +# # Write a script to find out the distance of the given node from the root. -# +# # Example 1: # Tree: # 1 @@ -15,16 +15,16 @@ # 4 # / \ # 5 6 -# +# # Node: 6 # Output: 3 as the distance of given node 6 from the root (1). -# +# # Node: 5 # Output: 3 -# +# # Node: 2 # Output: 1 -# +# # Node: 4 # Output: 2 # Example 2: @@ -38,13 +38,13 @@ # 6 7 # / \ # 8 9 -# +# # Node: 7 # Output: 3 as the distance of given node 6 from the root (1). -# +# # Node: 8 # Output: 4 -# +# # Node: 6 # Output: 3 @@ -76,14 +76,14 @@ sub parse_subtree { if ($row+2 <= $#{$lines}) { # parse left subtree if ($col-2 >= 0 && - $col-2 < length($lines->[$row+1]) && - substr($lines->[$row+1], $col-1, 1) eq '/') { + $col-2 < length($lines->[$row+1]) && + substr($lines->[$row+1], $col-1, 1) eq '/') { my $child = parse_subtree($lines, $row+2, $col-2); $node->left($child); } # parse right subtree - if ($col+2 < length($lines->[$row+2]) && - substr($lines->[$row+1], $col+1, 1) eq '\\') { + if ($col+2 < length($lines->[$row+2]) && + substr($lines->[$row+1], $col+1, 1) eq '\\') { my $child = parse_subtree($lines, $row+2, $col+2); $node->right($child); } @@ -92,20 +92,20 @@ sub parse_subtree { } sub root_dist { - my($node, $value, $dist) = @_; - $dist ||= 0; - return $dist if $value == $node->value; - if ($node->left) { - my $found = root_dist($node->left, $value, $dist+1); - return $found if $found > 0; - } - if ($node->right) { - my $found = root_dist($node->right, $value, $dist+1); - return $found if $found > 0; - } - return -1; + my($node, $value, $dist) = @_; + $dist ||= 0; + return $dist if $value == $node->value; + if ($node->left) { + my $found = root_dist($node->left, $value, $dist+1); + return $found if $found > 0; + } + if ($node->right) { + my $found = root_dist($node->right, $value, $dist+1); + return $found if $found > 0; + } + return -1; } - + my $tree = parse_tree(); my $value = shift||0; say(root_dist($tree, $value)); diff --git a/challenge-129/paulo-custodio/perl/ch-2.pl b/challenge-129/paulo-custodio/perl/ch-2.pl index 949beebbab..6e870c038e 100644 --- a/challenge-129/paulo-custodio/perl/ch-2.pl +++ b/challenge-129/paulo-custodio/perl/ch-2.pl @@ -3,11 +3,11 @@ # TASK #2 > Add Linked Lists # Submitted by: Mohammad S Anwar # You are given two linked list having single digit positive numbers. -# +# # Write a script to add the two linked list and create a new linked representing # the sum of the two linked list numbers. The two linked lists may or may not # have the same number of elements. -# +# # HINT: Just a suggestion, feel free to come up with your own unique way to deal # with the task. I am expecting a class representing linked list. It should have # methods to create a linked list given list of single digit positive numbers @@ -19,7 +19,7 @@ # Input: L1 = 1 -> 2 -> 3 # L2 = 3 -> 2 -> 1 # Output: 4 -> 4 -> 4 -# +# # Example 2: # Input: L1 = 1 -> 2 -> 3 -> 4 -> 5 # L2 = 6 -> 5 -> 5 @@ -37,27 +37,27 @@ my $sum = add_streams($l1, $l2); say show($sum); sub reverse_stream { - my($in) = @_; - my $out; - while ($in) { - my $n = drop($in); - $out = node($n, $out); - } - return $out; + my($in) = @_; + my $out; + while ($in) { + my $n = drop($in); + $out = node($n, $out); + } + return $out; } sub add_streams { - my($l1, $l2) = @_; - $l1 = reverse_stream($l1); - $l2 = reverse_stream($l2); - my($sum); - my $carry = 0; - while ($l1 || $l2 || $carry) { - my $n1 = drop($l1) // 0; - my $n2 = drop($l2) // 0; - my $s = $n1+$n2+$carry; - $sum = node($s % 10, $sum); - $carry = int($s / 10); - } - return $sum; + my($l1, $l2) = @_; + $l1 = reverse_stream($l1); + $l2 = reverse_stream($l2); + my($sum); + my $carry = 0; + while ($l1 || $l2 || $carry) { + my $n1 = drop($l1) // 0; + my $n2 = drop($l2) // 0; + my $s = $n1+$n2+$carry; + $sum = node($s % 10, $sum); + $carry = int($s / 10); + } + return $sum; } diff --git a/challenge-129/paulo-custodio/python/ch-1.py b/challenge-129/paulo-custodio/python/ch-1.py index 78306de292..dfa8cbd377 100644 --- a/challenge-129/paulo-custodio/python/ch-1.py +++ b/challenge-129/paulo-custodio/python/ch-1.py @@ -3,9 +3,9 @@ # TASK #1 > Root Distance # Submitted by: Mohammad S Anwar # You are given a tree and a node of the given tree. -# +# # Write a script to find out the distance of the given node from the root. -# +# # Example 1: # Tree: # 1 @@ -15,16 +15,16 @@ # 4 # / \ # 5 6 -# +# # Node: 6 # Output: 3 as the distance of given node 6 from the root (1). -# +# # Node: 5 # Output: 3 -# +# # Node: 2 # Output: 1 -# +# # Node: 4 # Output: 2 # Example 2: @@ -38,13 +38,13 @@ # 6 7 # / \ # 8 9 -# +# # Node: 7 # Output: 3 as the distance of given node 6 from the root (1). -# +# # Node: 8 # Output: 4 -# +# # Node: 6 # Output: 3 @@ -102,7 +102,7 @@ def root_dist(tree, value): if found > 0: return found return -1 - + return subtree_dist(tree, value, 0) value = int(sys.argv.pop()) diff --git a/challenge-129/paulo-custodio/python/ch-2.py b/challenge-129/paulo-custodio/python/ch-2.py index 8cf54a885f..054730edc5 100644 --- a/challenge-129/paulo-custodio/python/ch-2.py +++ b/challenge-129/paulo-custodio/python/ch-2.py @@ -3,11 +3,11 @@ # TASK #2 > Add Linked Lists # Submitted by: Mohammad S Anwar # You are given two linked list having single digit positive numbers. -# +# # Write a script to add the two linked list and create a new linked representing # the sum of the two linked list numbers. The two linked lists may or may not # have the same number of elements. -# +# # HINT: Just a suggestion, feel free to come up with your own unique way to deal # with the task. I am expecting a class representing linked list. It should have # methods to create a linked list given list of single digit positive numbers @@ -19,7 +19,7 @@ # Input: L1 = 1 -> 2 -> 3 # L2 = 3 -> 2 -> 1 # Output: 4 -> 4 -> 4 -# +# # Example 2: # Input: L1 = 1 -> 2 -> 3 -> 4 -> 5 # L2 = 6 -> 5 -> 5 diff --git a/challenge-130/paulo-custodio/perl/ch-1.pl b/challenge-130/paulo-custodio/perl/ch-1.pl index c3cb2c7384..8122ef8589 100644 --- a/challenge-130/paulo-custodio/perl/ch-1.pl +++ b/challenge-130/paulo-custodio/perl/ch-1.pl @@ -4,9 +4,9 @@ # Submitted by: Mohammad S Anwar # You are given an array of positive integers, such that all the # numbers appear even number of times except one number. -# +# # Write a script to find that integer. -# +# # Example 1 # Input: @N = (2, 5, 4, 4, 5, 5, 2) # Output: 5 as it appears 3 times in the array where as all other @@ -18,6 +18,6 @@ use Modern::Perl; my @N = @ARGV; -my %count; +my %count; $count{$_}++ for @N; say $_ for (grep {$count{$_}%2==1} keys %count); diff --git a/challenge-130/paulo-custodio/perl/ch-2.pl b/challenge-130/paulo-custodio/perl/ch-2.pl index 5c2f5180eb..90b55a7efa 100644 --- a/challenge-130/paulo-custodio/perl/ch-2.pl +++ b/challenge-130/paulo-custodio/perl/ch-2.pl @@ -3,12 +3,12 @@ # TASK #2 > Binary Search Tree # Submitted by: Mohammad S Anwar # You are given a tree. -# +# # Write a script to find out if the given tree is Binary Search Tree # (BST). -# +# # According to wikipedia, the definition of BST: -# +# # A binary search tree is a rooted binary tree, whose internal nodes # each store a key (and optionally, an associated value), and each has # two distinguished sub-trees, commonly denoted left and right. The @@ -17,7 +17,7 @@ # sub-tree, and less than or equal to any key stored in the right # sub-tree. The leaves (final nodes) of the tree contain no key and # have no structure to distinguish them from one another. -# +# # Example 1 # Input: # 8 @@ -25,7 +25,7 @@ # 5 9 # / \ # 4 6 -# +# # Output: 1 as the given tree is a BST. # Example 2 # Input: @@ -34,7 +34,7 @@ # 4 7 # / \ # 3 6 -# +# # Output: 0 as the given tree is a not BST. use Modern::Perl; @@ -66,14 +66,14 @@ sub parse_subtree { if ($row+2 <= $#{$lines}) { # parse left subtree if ($col-2 >= 0 && - $col-2 < length($lines->[$row+1]) && - substr($lines->[$row+1], $col-1, 1) eq '/') { + $col-2 < length($lines->[$row+1]) && + substr($lines->[$row+1], $col-1, 1) eq '/') { my $child = parse_subtree($lines, $row+2, $col-2); $node->left($child); } # parse right subtree - if ($col+2 < length($lines->[$row+2]) && - substr($lines->[$row+1], $col+1, 1) eq '\\') { + if ($col+2 < length($lines->[$row+2]) && + substr($lines->[$row+1], $col+1, 1) eq '\\') { my $child = parse_subtree($lines, $row+2, $col+2); $node->right($child); } @@ -82,40 +82,40 @@ sub parse_subtree { } sub subtree_min { - my($node) = @_; - my $min = $node->value; - if ($node->left) { - $min = min($min, subtree_min($node->left)); - } - if ($node->right) { - $min = min($min, subtree_min($node->right)); - } - return $min; + my($node) = @_; + my $min = $node->value; + if ($node->left) { + $min = min($min, subtree_min($node->left)); + } + if ($node->right) { + $min = min($min, subtree_min($node->right)); + } + return $min; } sub subtree_max { - my($node) = @_; - my $max = $node->value; - if ($node->left) { - $max = max($max, subtree_max($node->left)); - } - if ($node->right) { - $max = max($max, subtree_max($node->right)); - } - return $max; + my($node) = @_; + my $max = $node->value; + if ($node->left) { + $max = max($max, subtree_max($node->left)); + } + if ($node->right) { + $max = max($max, subtree_max($node->right)); + } + return $max; } sub subtree_is_bst { - my($node) = @_; - if ($node->left) { - return 0 if !subtree_is_bst($node->left); - return 0 if subtree_max($node->left) > $node->value; - } - if ($node->right) { - return 0 if !subtree_is_bst($node->right); - return 0 if subtree_min($node->right) < $node->value; - } - return 1; + my($node) = @_; + if ($node->left) { + return 0 if !subtree_is_bst($node->left); + return 0 if subtree_max($node->left) > $node->value; + } + if ($node->right) { + return 0 if !subtree_is_bst($node->right); + return 0 if subtree_min($node->right) < $node->value; + } + return 1; } my $tree = parse_tree(); diff --git a/challenge-130/paulo-custodio/python/ch-1.py b/challenge-130/paulo-custodio/python/ch-1.py index 00794b20da..9a1e347c28 100644 --- a/challenge-130/paulo-custodio/python/ch-1.py +++ b/challenge-130/paulo-custodio/python/ch-1.py @@ -4,9 +4,9 @@ # Submitted by: Mohammad S Anwar # You are given an array of positive integers, such that all the # numbers appear even number of times except one number. -# +# # Write a script to find that integer. -# +# # Example 1 # Input: @N = (2, 5, 4, 4, 5, 5, 2) # Output: 5 as it appears 3 times in the array where as all other diff --git a/challenge-130/paulo-custodio/python/ch-2.py b/challenge-130/paulo-custodio/python/ch-2.py index e2e4f427f7..6eb0944ce3 100644 --- a/challenge-130/paulo-custodio/python/ch-2.py +++ b/challenge-130/paulo-custodio/python/ch-2.py @@ -3,12 +3,12 @@ # TASK #2 > Binary Search Tree # Submitted by: Mohammad S Anwar # You are given a tree. -# +# # Write a script to find out if the given tree is Binary Search Tree # (BST). -# +# # According to wikipedia, the definition of BST: -# +# # A binary search tree is a rooted binary tree, whose internal nodes # each store a key (and optionally, an associated value), and each has # two distinguished sub-trees, commonly denoted left and right. The @@ -17,7 +17,7 @@ # sub-tree, and less than or equal to any key stored in the right # sub-tree. The leaves (final nodes) of the tree contain no key and # have no structure to distinguish them from one another. -# +# # Example 1 # Input: # 8 @@ -25,7 +25,7 @@ # 5 9 # / \ # 4 6 -# +# # Output: 1 as the given tree is a BST. # Example 2 # Input: @@ -34,7 +34,7 @@ # 4 7 # / \ # 3 6 -# +# # Output: 0 as the given tree is a not BST. import fileinput diff --git a/challenge-130/paulo-custodio/t/test-2.yaml b/challenge-130/paulo-custodio/t/test-2.yaml index 514072061c..c34fbec23b 100644 --- a/challenge-130/paulo-custodio/t/test-2.yaml +++ b/challenge-130/paulo-custodio/t/test-2.yaml @@ -1,6 +1,6 @@ - setup: cleanup: - args: + args: input: | | 8 | / \ @@ -10,7 +10,7 @@ output: 1 - setup: cleanup: - args: + args: input: | | 5 | / \ diff --git a/challenge-131/paulo-custodio/perl/ch-1.pl b/challenge-131/paulo-custodio/perl/ch-1.pl index 544735f33e..cb460b0440 100644 --- a/challenge-131/paulo-custodio/perl/ch-1.pl +++ b/challenge-131/paulo-custodio/perl/ch-1.pl @@ -3,10 +3,10 @@ # TASK #1 > Consecutive Arrays # Submitted by: Mark Anderson # You are given a sorted list of unique positive integers. -# +# # Write a script to return list of arrays where the arrays are consecutive # integers. -# +# # Example 1: # Input: (1, 2, 3, 6, 7, 8, 9) # Output: ([1, 2, 3], [6, 7, 8, 9]) @@ -28,16 +28,16 @@ my @output = cons_arrays(@input); say "[".join(", ", map {"[".join(", ", @$_)."]"} @output)."]"; sub cons_arrays { - my(@input) = @_; - my @output = [shift @input]; - while (@input) { - my $n = shift @input; - if ($n == $output[-1][-1] + 1) { - push @{$output[-1]}, $n; - } - else { - push @output, [$n]; - } - } - return @output; + my(@input) = @_; + my @output = [shift @input]; + while (@input) { + my $n = shift @input; + if ($n == $output[-1][-1] + 1) { + push @{$output[-1]}, $n; + } + else { + push @output, [$n]; + } + } + return @output; } diff --git a/challenge-131/paulo-custodio/perl/ch-2.pl b/challenge-131/paulo-custodio/perl/ch-2.pl index d37470a793..1252dd60eb 100644 --- a/challenge-131/paulo-custodio/perl/ch-2.pl +++ b/challenge-131/paulo-custodio/perl/ch-2.pl @@ -3,16 +3,16 @@ # TASK #2 > Find Pairs # Submitted by: Yary # You are given a string of delimiter pairs and a string to search. -# +# # Write a script to return two strings, the first with any characters # matching the "opening character" set, the second with any matching # the "closing character" set. -# +# # Example 1: # Input: # Delimiter pairs: ""[]() # Search String: "I like (parens) and the Apple ][+" they said. -# +# # Output: # "([" # ")]" @@ -20,7 +20,7 @@ # Input: # Delimiter pairs: **//<> # Search String: /* This is a comment (in some languages) */ <could be a tag> -# +# # Output: # /**/< # /**/> @@ -33,9 +33,9 @@ my $string = <>; my $open_delims = "["; my $close_delims = "["; while (length($delims) >= 2) { - $open_delims .= "\\".substr($delims,0,1); - $close_delims .= "\\".substr($delims,1,1); - $delims = substr($delims,2); + $open_delims .= "\\".substr($delims,0,1); + $close_delims .= "\\".substr($delims,1,1); + $delims = substr($delims,2); } $open_delims .= "]"; $close_delims .= "]"; @@ -43,8 +43,8 @@ $close_delims .= "]"; my $open_string; my $close_string; for my $c (split //, $string) { - $open_string .= $c if $c =~ /$open_delims/; - $close_string .= $c if $c =~ /$close_delims/; + $open_string .= $c if $c =~ /$open_delims/; + $close_string .= $c if $c =~ /$close_delims/; } say $open_string; diff --git a/challenge-131/paulo-custodio/python/ch-1.py b/challenge-131/paulo-custodio/python/ch-1.py index 3f047245c9..5ec166762f 100644 --- a/challenge-131/paulo-custodio/python/ch-1.py +++ b/challenge-131/paulo-custodio/python/ch-1.py @@ -3,10 +3,10 @@ # TASK #1 > Consecutive Arrays # Submitted by: Mark Anderson # You are given a sorted list of unique positive integers. -# +# # Write a script to return list of arrays where the arrays are consecutive # integers. -# +# # Example 1: # Input: (1, 2, 3, 6, 7, 8, 9) # Output: ([1, 2, 3], [6, 7, 8, 9]) @@ -34,4 +34,4 @@ def cons_arrays(input): input = [int(x) for x in sys.argv[1:]] output = cons_arrays(input) -print(output)
\ No newline at end of file +print(output) diff --git a/challenge-131/paulo-custodio/python/ch-2.py b/challenge-131/paulo-custodio/python/ch-2.py index 52fe765741..921bddd6bc 100644 --- a/challenge-131/paulo-custodio/python/ch-2.py +++ b/challenge-131/paulo-custodio/python/ch-2.py @@ -3,16 +3,16 @@ # TASK #2 > Find Pairs # Submitted by: Yary # You are given a string of delimiter pairs and a string to search. -# +# # Write a script to return two strings, the first with any characters # matching the "opening character" set, the second with any matching # the "closing character" set. -# +# # Example 1: # Input: # Delimiter pairs: ""[]() # Search String: "I like (parens) and the Apple ][+" they said. -# +# # Output: # "([" # ")]" @@ -20,7 +20,7 @@ # Input: # Delimiter pairs: **//<> # Search String: /* This is a comment (in some languages) */ <could be a tag> -# +# # Output: # /**/< # /**/> diff --git a/challenge-131/paulo-custodio/t/test-2.yaml b/challenge-131/paulo-custodio/t/test-2.yaml index 9fc70d6e9a..2229e7787c 100644 --- a/challenge-131/paulo-custodio/t/test-2.yaml +++ b/challenge-131/paulo-custodio/t/test-2.yaml @@ -1,6 +1,6 @@ - setup: cleanup: - args: + args: input: | ""[]() "I like (parens) and the Apple ][+" they said. @@ -9,7 +9,7 @@ ")]" - setup: cleanup: - args: + args: input: | **//<> /* This is a comment (in some languages) */ <could be a tag> diff --git a/challenge-132/paulo-custodio/perl/ch-1.pl b/challenge-132/paulo-custodio/perl/ch-1.pl index 5b5990ef2c..87e1bef032 100644 --- a/challenge-132/paulo-custodio/perl/ch-1.pl +++ b/challenge-132/paulo-custodio/perl/ch-1.pl @@ -3,32 +3,32 @@ # TASK #1 > Mirror Dates # Submitted by: Mark Anderson # You are given a date (yyyy/mm/dd). -# -# Assuming, the given date is your date of birth. Write a script to find the +# +# Assuming, the given date is your date of birth. Write a script to find the # mirror dates of the given date. -# +# # Dave Cross has built cool site that does something similar. -# +# # Assuming today is 2021/09/22. # Example 1: # Input: 2021/09/18 # Output: 2021/09/14, 2021/09/26 -# -# On the date you were born, someone who was your current age, would have +# +# On the date you were born, someone who was your current age, would have # been born on 2021/09/14. # Someone born today will be your current age on 2021/09/26. # Example 2: # Input: 1975/10/10 # Output: 1929/10/27, 2067/09/05 -# -# On the date you were born, someone who was your current age, would have +# +# On the date you were born, someone who was your current age, would have # been born on 1929/10/27. # Someone born today will be your current age on 2067/09/05. |
