From 4fbb301ccb5620c8bcee20462bd1bb8145b0d13c Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 26 Oct 2021 09:52:33 +0100 Subject: Whitespace --- challenge-121/paulo-custodio/perl/ch-2.pl | 2 - challenge-129/paulo-custodio/perl/ch-1.pl | 52 +++++++++--------- challenge-129/paulo-custodio/perl/ch-2.pl | 46 ++++++++-------- challenge-129/paulo-custodio/python/ch-1.py | 20 +++---- challenge-129/paulo-custodio/python/ch-2.py | 6 +-- challenge-130/paulo-custodio/perl/ch-1.pl | 6 +-- challenge-130/paulo-custodio/perl/ch-2.pl | 76 +++++++++++++------------- challenge-130/paulo-custodio/python/ch-1.py | 4 +- challenge-130/paulo-custodio/python/ch-2.py | 12 ++--- challenge-130/paulo-custodio/t/test-2.yaml | 4 +- challenge-131/paulo-custodio/perl/ch-1.pl | 28 +++++----- challenge-131/paulo-custodio/perl/ch-2.pl | 18 +++---- challenge-131/paulo-custodio/python/ch-1.py | 6 +-- challenge-131/paulo-custodio/python/ch-2.py | 8 +-- challenge-131/paulo-custodio/t/test-2.yaml | 4 +- challenge-132/paulo-custodio/perl/ch-1.pl | 40 +++++++------- challenge-132/paulo-custodio/perl/ch-2.pl | 52 +++++++++--------- challenge-132/paulo-custodio/python/ch-1.py | 20 +++---- challenge-132/paulo-custodio/python/ch-2.py | 16 +++--- challenge-132/paulo-custodio/t/test-2.yaml | 4 +- challenge-133/paulo-custodio/perl/ch-1.pl | 84 ++++++++++++++--------------- challenge-133/paulo-custodio/perl/ch-2.pl | 72 ++++++++++++------------- challenge-133/paulo-custodio/python/ch-1.py | 84 ++++++++++++++--------------- challenge-133/paulo-custodio/python/ch-2.py | 14 ++--- challenge-133/paulo-custodio/t/test-1.yaml | 1 - challenge-133/paulo-custodio/t/test-2.yaml | 2 +- challenge-134/paulo-custodio/perl/ch-1.pl | 8 +-- challenge-134/paulo-custodio/perl/ch-2.pl | 14 ++--- challenge-134/paulo-custodio/python/ch-1.py | 8 +-- challenge-134/paulo-custodio/python/ch-2.py | 4 +- challenge-134/paulo-custodio/t/test-1.yaml | 2 +- challenge-135/paulo-custodio/perl/ch-1.pl | 10 ++-- challenge-135/paulo-custodio/perl/ch-2.pl | 46 ++++++++-------- challenge-135/paulo-custodio/python/ch-1.py | 4 +- challenge-135/paulo-custodio/python/ch-2.py | 8 +-- challenge-136/paulo-custodio/perl/ch-1.pl | 32 +++++------ challenge-136/paulo-custodio/perl/ch-2.pl | 52 +++++++++--------- challenge-136/paulo-custodio/python/ch-1.py | 18 +++---- challenge-136/paulo-custodio/python/ch-2.py | 50 ++++++++--------- 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) */ -# +# # 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) */ -# +# # 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) */ 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. # Example 3: # Input: 1967/02/14 # Output: 1912/07/08, 2076/04/30 -# -# 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 1912/07/08. # Someone born today will be your current age on 2076/04/30. @@ -47,14 +47,14 @@ my $mirror2 = $today->clone->add($days); say $mirror1->ymd('/'), ", ", $mirror2->ymd('/'); sub parse_date { - my($str) = @_; - $str =~ m{^(\d{4})/(\d{2})/(\d{2})$} - or die "date format should be yyyy/mm/dd\n"; - my($yy,$mm,$dd) = ($1,$2,$3); - my $date = DateTime->new( - year => $yy, - month => $mm, - day => $dd, - ); - return $date; + my($str) = @_; + $str =~ m{^(\d{4})/(\d{2})/(\d{2})$} + or die "date format should be yyyy/mm/dd\n"; + my($yy,$mm,$dd) = ($1,$2,$3); + my $date = DateTime->new( + year => $yy, + month => $mm, + day => $dd, + ); + return $date; } diff --git a/challenge-132/paulo-custodio/perl/ch-2.pl b/challenge-132/paulo-custodio/perl/ch-2.pl index 2fb0f1ca4f..409721c46e 100644 --- a/challenge-132/paulo-custodio/perl/ch-2.pl +++ b/challenge-132/paulo-custodio/perl/ch-2.pl @@ -4,7 +4,7 @@ # Submitted by: Mohammad S Anwar # Write a script to implement Hash Join algorithm as suggested # by wikipedia. -# +# # 1. For each tuple r in the build input R # 1.1 Add r to the in-memory hash table # 1.2 If the size of the hash table equals the maximum @@ -17,7 +17,7 @@ # join tuples to the output relation # Example # Input: -# +# # @player_ages = ( # [20, "Alex" ], # [28, "Joe" ], @@ -26,7 +26,7 @@ # [25, "David" ], # [18, "Simon" ], # ); -# +# # @player_names = ( # ["Alex", "Stewart"], # ["Joe", "Root" ], @@ -35,12 +35,12 @@ # ["Alex", "Jones" ], # ["Simon","Duane" ], # ); -# +# # Output: -# -# Based on index = 1 of @players_age and +# +# Based on index = 1 of @players_age and # index = 0 of @players_name. -# +# # 20, "Alex", "Stewart" # 20, "Alex", "Jones" # 18, "Alex", "Stewart" @@ -74,27 +74,27 @@ my @player_names = ( my $player_names_key = 0; my @result = hash_join(\@player_ages, $player_ages_key, - \@player_names, $player_names_key); + \@player_names, $player_names_key); for (@result) { - say join(", ", map {dump($_)} @$_); + say join(", ", map {dump($_)} @$_); } sub hash_join { - my($table1, $key1, $table2, $key2) = @_; - my %build; - for my $row (@$table1) { - my $key = $row->[$key1]; - $build{$key} ||= []; - push @{$build{$key}}, $row; - } - my @result; - for my $row_probe (@$table2) { - my $key = $row_probe->[$key2]; - for my $row_build (@{$build{$key}}) { - my @row = (@$row_build, - @{$row_probe}[0..$key2-1, $key2+1..$#$row_probe]); - push @result, \@row; - } - } - return @result; + my($table1, $key1, $table2, $key2) = @_; + my %build; + for my $row (@$table1) { + my $key = $row->[$key1]; + $build{$key} ||= []; + push @{$build{$key}}, $row; + } + my @result; + for my $row_probe (@$table2) { + my $key = $row_probe->[$key2]; + for my $row_build (@{$build{$key}}) { + my @row = (@$row_build, + @{$row_probe}[0..$key2-1, $key2+1..$#$row_probe]); + push @result, \@row; + } + } + return @result; } diff --git a/challenge-132/paulo-custodio/python/ch-1.py b/challenge-132/paulo-custodio/python/ch-1.py index adc4693080..102be7cc9a 100644 --- a/challenge-132/paulo-custodio/python/ch-1.py +++ b/challenge-132/paulo-custodio/python/ch-1.py @@ -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. # Example 3: # Input: 1967/02/14 # Output: 1912/07/08, 2076/04/30 -# -# 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 1912/07/08. # Someone born today will be your current age on 2076/04/30. diff --git a/challenge-132/paulo-custodio/python/ch-2.py b/challenge-132/paulo-custodio/python/ch-2.py index 6d48ba2c8c..dac7fc89f9 100644 --- a/challenge-132/paulo-custodio/python/ch-2.py +++ b/challenge-132/paulo-custodio/python/ch-2.py @@ -4,7 +4,7 @@ # Submitted by: Mohammad S Anwar # Write a script to implement Hash Join algorithm as suggested # by wikipedia. -# +# # 1. For each tuple r in the build input R # 1.1 Add r to the in-memory hash table # 1.2 If the size of the hash table equals the maximum @@ -17,7 +17,7 @@ # join tuples to the output relation # Example # Input: -# +# # @player_ages = ( # [20, "Alex" ], # [28, "Joe" ], @@ -26,7 +26,7 @@ # [25, "David" ], # [18, "Simon" ], # ); -# +# # @player_names = ( # ["Alex", "Stewart"], # ["Joe", "Root" ], @@ -35,12 +35,12 @@ # ["Alex", "Jones" ], # ["Simon","Duane" ], # ); -# +# # Output: -# -# Based on index = 1 of @players_age and +# +# Based on index = 1 of @players_age and # index = 0 of @players_name. -# +# # 20, "Alex", "Stewart" # 20, "Alex", "Jones" # 18, "Alex", "Stewart" @@ -95,6 +95,6 @@ def hash_join(table1, key1, table2, key2): return result result = hash_join(player_ages, player_ages_key, - player_names, player_names_key) + player_names, player_names_key) for row in result: print(', '.join(row)) diff --git a/challenge-132/paulo-custodio/t/test-2.yaml b/challenge-132/paulo-custodio/t/test-2.yaml index f57a59a386..23a664809c 100644 --- a/challenge-132/paulo-custodio/t/test-2.yaml +++ b/challenge-132/paulo-custodio/t/test-2.yaml @@ -1,6 +1,6 @@ - setup: cleanup: - args: + args: input: output: | 20, "Alex", "Stewart" @@ -10,4 +10,4 @@ 28, "Joe", "Blog" 20, "Alex", "Jones" 18, "Alex", "Jones" - 18, "Simon", "Duane" \ No newline at end of file + 18, "Simon", "Duane" diff --git a/challenge-133/paulo-custodio/perl/ch-1.pl b/challenge-133/paulo-custodio/perl/ch-1.pl index 84b460c143..8d2a021e3c 100644 --- a/challenge-133/paulo-custodio/perl/ch-1.pl +++ b/challenge-133/paulo-custodio/perl/ch-1.pl @@ -1,42 +1,42 @@ -#!/usr/bin/env perl - -# TASK #1 > Integer Square Root -# Submitted by: Mohammad S Anwar -# You are given a positive integer $N. -# -# Write a script to calculate the integer square root of the given number. -# -# Please avoid using built-in function. Find out more about it here. -# -# Examples -# Input: $N = 10 -# Output: 3 -# -# Input: $N = 27 -# Output: 5 -# -# Input: $N = 85 -# Output: 9 -# -# Input: $N = 101 -# Output: 10 - -# solution: https://en.wikipedia.org/wiki/Integer_square_root - -use Modern::Perl; -my $n = shift || 0; -say isqrt($n); - -sub isqrt { - my($n) = @_; - my $x0 = $n >> 1; # initial estimate - return $n if $x0 == 0; - - # loop - my $x1 = ($x0 + $n/$x0) >> 1; - while ($x1 < $x0) { - $x0 = $x1; - $x1 = ($x0 + $n/$x0) >> 1; - } - return $x0; -} +#!/usr/bin/env perl + +# TASK #1 > Integer Square Root +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to calculate the integer square root of the given number. +# +# Please avoid using built-in function. Find out more about it here. +# +# Examples +# Input: $N = 10 +# Output: 3 +# +# Input: $N = 27 +# Output: 5 +# +# Input: $N = 85 +# Output: 9 +# +# Input: $N = 101 +# Output: 10 + +# solution: https://en.wikipedia.org/wiki/Integer_square_root + +use Modern::Perl; +my $n = shift || 0; +say isqrt($n); + +sub isqrt { + my($n) = @_; + my $x0 = $n >> 1; # initial estimate + return $n if $x0 == 0; + + # loop + my $x1 = ($x0 + $n/$x0) >> 1; + while ($x1 < $x0) { + $x0 = $x1; + $x1 = ($x0 + $n/$x0) >> 1; + } + return $x0; +} diff --git a/challenge-133/paulo-custodio/perl/ch-2.pl b/challenge-133/paulo-custodio/perl/ch-2.pl index dd2cd4f7f8..a439a93d8c 100644 --- a/challenge-133/paulo-custodio/perl/ch-2.pl +++ b/challenge-133/paulo-custodio/perl/ch-2.pl @@ -1,36 +1,36 @@ -#!/usr/bin/env perl - -# TASK #2 > Smith Numbers -# Submitted by: Mohammad S Anwar -# Write a script to generate first 10 Smith Numbers in base 10. -# -# According to Wikipedia: -# -# In number theory, a Smith number is a composite number for which, in a given -# number base, the sum of its digits is equal to the sum of the digits in its -# prime factorization in the given number base. -# - -use Modern::Perl; -use ntheory 'factor', 'is_prime'; -use List::Util 'sum'; - -my $n = 1; -my $count = 0; -while ($count < 10) { - $n++; - if (is_smith($n)) { - say $n; - $count++ - } -} - -sub is_smith { - my($n) = @_; - return if is_prime($n); - my @digits = split //, $n; - my $sum1 = sum(@digits); - my @fact_digits = split //, join '', factor($n); - my $sum2 = sum(@fact_digits); - return $sum1 == $sum2; -} +#!/usr/bin/env perl + +# TASK #2 > Smith Numbers +# Submitted by: Mohammad S Anwar +# Write a script to generate first 10 Smith Numbers in base 10. +# +# According to Wikipedia: +# +# In number theory, a Smith number is a composite number for which, in a given +# number base, the sum of its digits is equal to the sum of the digits in its +# prime factorization in the given number base. +# + +use Modern::Perl; +use ntheory 'factor', 'is_prime'; +use List::Util 'sum'; + +my $n = 1; +my $count = 0; +while ($count < 10) { + $n++; + if (is_smith($n)) { + say $n; + $count++ + } +} + +sub is_smith { + my($n) = @_; + return if is_prime($n); + my @digits = split //, $n; + my $sum1 = sum(@digits); + my @fact_digits = split //, join '', factor($n); + my $sum2 = sum(@fact_digits); + return $sum1 == $sum2; +} diff --git a/challenge-133/paulo-custodio/python/ch-1.py b/challenge-133/paulo-custodio/python/ch-1.py index 69d2f7e92b..1b905ee574 100644 --- a/challenge-133/paulo-custodio/python/ch-1.py +++ b/challenge-133/paulo-custodio/python/ch-1.py @@ -1,42 +1,42 @@ -#!/usr/bin/env python3 - -# TASK #1 > Integer Square Root -# Submitted by: Mohammad S Anwar -# You are given a positive integer $N. -# -# Write a script to calculate the integer square root of the given number. -# -# Please avoid using built-in function. Find out more about it here. -# -# Examples -# Input: $N = 10 -# Output: 3 -# -# Input: $N = 27 -# Output: 5 -# -# Input: $N = 85 -# Output: 9 -# -# Input: $N = 101 -# Output: 10 - -# solution: https://en.wikipedia.org/wiki/Integer_square_root - -import sys - -def isqrt(n): - x0 = n >> 1 # initial estimate - if x0 == 0: - return n - - # loop - x1 = int(x0 + n/x0) >> 1 - while x1 < x0: - x0 = x1; - x1 = int(x0 + n/x0) >> 1 - - return x0 - -n = int(sys.argv[1]) -print(isqrt(n)) +#!/usr/bin/env python3 + +# TASK #1 > Integer Square Root +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to calculate the integer square root of the given number. +# +# Please avoid using built-in function. Find out more about it here. +# +# Examples +# Input: $N = 10 +# Output: 3 +# +# Input: $N = 27 +# Output: 5 +# +# Input: $N = 85 +# Output: 9 +# +# Input: $N = 101 +# Output: 10 + +# solution: https://en.wikipedia.org/wiki/Integer_square_root + +import sys + +def isqrt(n): + x0 = n >> 1 # initial estimate + if x0 == 0: + return n + + # loop + x1 = int(x0 + n/x0) >> 1 + while x1 < x0: + x0 = x1; + x1 = int(x0 + n/x0) >> 1 + + return x0 + +n = int(sys.argv[1]) +print(isqrt(n)) diff --git a/challenge-133/paulo-custodio/python/ch-2.py b/challenge-133/paulo-custodio/python/ch-2.py index 1cab0e4e43..adfc274f4a 100644 --- a/challenge-133/paulo-custodio/python/ch-2.py +++ b/challenge-133/paulo-custodio/python/ch-2.py @@ -3,13 +3,13 @@ # TASK #2 > Smith Numbers # Submitted by: Mohammad S Anwar # Write a script to generate first 10 Smith Numbers in base 10. -# +# # According to Wikipedia: -# +# # In number theory, a Smith number is a composite number for which, in a given -# number base, the sum of its digits is equal to the sum of the digits in its +# number base, the sum of its digits is equal to the sum of the digits in its # prime factorization in the given number base. -# +# def is_prime(n): if n <= 1: @@ -35,10 +35,10 @@ def get_prime_factors(n): n //= i else: i += 1 - + if n>1: prime_factors.append(n) - + return prime_factors def is_smith(n): @@ -50,7 +50,7 @@ def is_smith(n): fact_digits = [int(x) for x in factors] sum2 = sum(fact_digits) return sum1==sum2 - + n=1 count=0 while count<10: diff --git a/challenge-133/paulo-custodio/t/test-1.yaml b/challenge-133/paulo-custodio/t/test-1.yaml index 88a52a8657..490678400b 100644 --- a/challenge-133/paulo-custodio/t/test-1.yaml +++ b/challenge-133/paulo-custodio/t/test-1.yaml @@ -18,4 +18,3 @@ args: 101 input: output: 10 - diff --git a/challenge-133/paulo-custodio/t/test-2.yaml b/challenge-133/paulo-custodio/t/test-2.yaml index 6827399570..e915b1fd0c 100644 --- a/challenge-133/paulo-custodio/t/test-2.yaml +++ b/challenge-133/paulo-custodio/t/test-2.yaml @@ -1,6 +1,6 @@ - setup: cleanup: - args: + args: input: output: | 4 diff --git a/challenge-134/paulo-custodio/perl/ch-1.pl b/challenge-134/paulo-custodio/perl/ch-1.pl index d7ccd780e7..637e723c76 100644 --- a/challenge-134/paulo-custodio/perl/ch-1.pl +++ b/challenge-134/paulo-custodio/perl/ch-1.pl @@ -3,10 +3,10 @@ # TASK #1 > Pandigital Numbers # Submitted by: Mohammad S Anwar # Write a script to generate first 5 Pandigital Numbers in base 10. -# +# # As per the wikipedia, it says: -# -# A pandigital number is an integer that in a given base has among its +# +# A pandigital number is an integer that in a given base has among its # significant digits each digit used in the base at least once. # solution from https://oeis.org/A050278 @@ -16,4 +16,4 @@ use Math::Combinatorics; my @A050278 = sort {$a<=>$b} map {0+join('', @$_)} grep {$_->[0]!=0} permute(0..9); splice(@A050278, 5, $#A050278); -say join("\n", @A050278); \ No newline at end of file +say join("\n", @A050278); diff --git a/challenge-134/paulo-custodio/perl/ch-2.pl b/challenge-134/paulo-custodio/perl/ch-2.pl index 8a01b8ee55..617193f257 100644 --- a/challenge-134/paulo-custodio/perl/ch-2.pl +++ b/challenge-134/paulo-custodio/perl/ch-2.pl @@ -3,8 +3,8 @@ # TASK #2 > Distinct Terms Count # Submitted by: Mohammad S Anwar # You are given 2 positive numbers, $m and $n. -# -# Write a script to generate multiplication table and display count of distinct +# +# Write a script to generate multiplication table and display count of distinct # terms. use Modern::Perl; @@ -12,9 +12,9 @@ use Modern::Perl; my($m, $n) = @ARGV or die "Usage: ch-2.pl m n\n"; my %terms; for my $a (1..$m) { - for my $b (1..$n) { - my $prod = $a*$b; - $terms{$prod} = 1; - } + for my $b (1..$n) { + my $prod = $a*$b; + $terms{$prod} = 1; + } } -say scalar(keys %terms); \ No newline at end of file +say scalar(keys %terms); diff --git a/challenge-134/paulo-custodio/python/ch-1.py b/challenge-134/paulo-custodio/python/ch-1.py index 814125549b..55590a928a 100644 --- a/challenge-134/paulo-custodio/python/ch-1.py +++ b/challenge-134/paulo-custodio/python/ch-1.py @@ -3,17 +3,17 @@ # TASK #1 > Pandigital Numbers # Submitted by: Mohammad S Anwar # Write a script to generate first 5 Pandigital Numbers in base 10. -# +# # As per the wikipedia, it says: -# -# A pandigital number is an integer that in a given base has among its +# +# A pandigital number is an integer that in a given base has among its # significant digits each digit used in the base at least once. # solution from https://oeis.org/A050278 from itertools import permutations -A050278 = [int(''.join(d)) for d in permutations('0123456789', 10) if d[0] != '0'] +A050278 = [int(''.join(d)) for d in permutations('0123456789', 10) if d[0] != '0'] A050278.sort() for i in range(0, 5): print(A050278[i]) diff --git a/challenge-134/paulo-custodio/python/ch-2.py b/challenge-134/paulo-custodio/python/ch-2.py index d378a11ca2..b37227a192 100644 --- a/challenge-134/paulo-custodio/python/ch-2.py +++ b/challenge-134/paulo-custodio/python/ch-2.py @@ -3,8 +3,8 @@ # TASK #2 > Distinct Terms Count # Submitted by: Mohammad S Anwar # You are given 2 positive numbers, $m and $n. -# -# Write a script to generate multiplication table and display count of distinct +# +# Write a script to generate multiplication table and display count of distinct # terms. import sys diff --git a/challenge-134/paulo-custodio/t/test-1.yaml b/challenge-134/paulo-custodio/t/test-1.yaml index 81b36fedcb..505eb2be78 100644 --- a/challenge-134/paulo-custodio/t/test-1.yaml +++ b/challenge-134/paulo-custodio/t/test-1.yaml @@ -1,6 +1,6 @@ - setup: cleanup: - args: + args: input: output: | 1023456789 diff --git a/challenge-135/paulo-custodio/perl/ch-1.pl b/challenge-135/paulo-custodio/perl/ch-1.pl index ebb2200c2c..9c8c982e26 100644 --- a/challenge-135/paulo-custodio/perl/ch-1.pl +++ b/challenge-135/paulo-custodio/perl/ch-1.pl @@ -3,9 +3,9 @@ # TASK #1 > Middle 3-digits # Submitted by: Mohammad S Anwar # You are given an integer. -# +# # Write a script find out the middle 3-digits of the given integer, if possible otherwise throw sensible error. -# +# # Example 1 # Input: $n = 1234567 # Output: 345 @@ -24,11 +24,11 @@ use Modern::Perl; my $n = abs(shift||0); my $len = length($n); if ($len%2==0) { - say "even number of digits"; + say "even number of digits"; } elsif ($len<3) { - say "too short"; + say "too short"; } else { - say substr($n, ($len-3)/2, 3); + say substr($n, ($len-3)/2, 3); } diff --git a/challenge-135/paulo-custodio/perl/ch-2.pl b/challenge-135/paulo-custodio/perl/ch-2.pl index aad313eac5..d5016cb86c 100644 --- a/challenge-135/paulo-custodio/perl/ch-2.pl +++ b/challenge-135/paulo-custodio/perl/ch-2.pl @@ -3,12 +3,12 @@ # TASK #2 > Validate SEDOL # Submitted by: Mohammad S Anwar # You are given 7-characters alphanumeric SEDOL. -# -# Write a script to validate the given SEDOL. Print 1 if it is a valid SEDOL +# +# Write a script to validate the given SEDOL. Print 1 if it is a valid SEDOL # otherwise 0. -# +# # For more information about SEDOL, please checkout the wikipedia page. -# +# # Example 1 # Input: $SEDOL = '2936921' # Output: 1 @@ -25,26 +25,26 @@ my $SEDOL = shift||""; say check_sedol($SEDOL); sub check_sedol { - my($str) = @_; - return 0 unless $str =~ /^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$/; - my $input = substr($str, 0, 6); - my $check_digit = compute_check_digit($input); - if ($input.$check_digit eq $str) { - return 1; - } - else { - return 0; - } + my($str) = @_; + return 0 unless $str =~ /^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$/; + my $input = substr($str, 0, 6); + my $check_digit = compute_check_digit($input); + if ($input.$check_digit eq $str) { + return 1; + } + else { + return 0; + } } sub compute_check_digit { - my($input) = @_; - my @weight = (1, 3, 1, 7, 3, 9); - my @input = map {$_ ge 'A' ? ord($_)-ord('A')+10 : ord($_)-ord('0')} - split //, $input; - my $sum = 0; - for my $i (0..$#weight) { - $sum += $input[$i] * $weight[$i]; - } - return (10-$sum%10); + my($input) = @_; + my @weight = (1, 3, 1, 7, 3, 9); + my @input = map {$_ ge 'A' ? ord($_)-ord('A')+10 : ord($_)-ord('0')} + split //, $input; + my $sum = 0; + for my $i (0..$#weight) { + $sum += $input[$i] * $weight[$i]; + } + return (10-$sum%10); } diff --git a/challenge-135/paulo-custodio/python/ch-1.py b/challenge-135/paulo-custodio/python/ch-1.py index 389816b534..b310faccc3 100644 --- a/challenge-135/paulo-custodio/python/ch-1.py +++ b/challenge-135/paulo-custodio/python/ch-1.py @@ -3,9 +3,9 @@ # TASK #1 > Middle 3-digits # Submitted by: Mohammad S Anwar # You are given an integer. -# +# # Write a script find out the middle 3-digits of the given integer, if possible otherwise throw sensible error. -# +# # Example 1 # Input: $n = 1234567 # Output: 345 diff --git a/challenge-135/paulo-custodio/python/ch-2.py b/challenge-135/paulo-custodio/python/ch-2.py index 9f1bb2594a..fe3641e471 100644 --- a/challenge-135/paulo-custodio/python/ch-2.py +++ b/challenge-135/paulo-custodio/python/ch-2.py @@ -3,12 +3,12 @@ # TASK #2 > Validate SEDOL # Submitted by: Mohammad S Anwar # You are given 7-characters alphanumeric SEDOL. -# -# Write a script to validate the given SEDOL. Print 1 if it is a valid SEDOL +# +# Write a script to validate the given SEDOL. Print 1 if it is a valid SEDOL # otherwise 0. -# +# # For more information about SEDOL, please checkout the wikipedia page. -# +# # Example 1 # Input: $SEDOL = '2936921' # Output: 1 diff --git a/challenge-136/paulo-custodio/perl/ch-1.pl b/challenge-136/paulo-custodio/perl/ch-1.pl index fae0d5f06a..5970f44d72 100644 --- a/challenge-136/paulo-custodio/perl/ch-1.pl +++ b/challenge-136/paulo-custodio/perl/ch-1.pl @@ -5,28 +5,28 @@ # TASK #1 > Two Friendly # Submitted by: Mohammad S Anwar # You are given 2 positive numbers, $m and $n. -# +# # Write a script to find out if the given two numbers are Two Friendly. -# -# Two positive numbers, m and n are two friendly when gcd(m, n) = 2 ^ p -# where p > 0. The greatest common divisor (gcd) of a set of numbers is -# the largest positive number that divides all the numbers in the set +# +# Two positive numbers, m and n are two friendly when gcd(m, n) = 2 ^ p +# where p > 0. The greatest common divisor (gcd) of a set of numbers is +# the largest positive number that divides all the numbers in the set # without remainder. -# +# # Example 1 # Input: $m = 8, $n = 24 # Output: 1 -# +# # Reason: gcd(8,24) = 8 => 2 ^ 3 # Example 2 # Input: $m = 26, $n = 39 # Output: 0 -# +# # Reason: gcd(26,39) = 13 # Example 3 # Input: $m = 4, $n = 10 # Output: 1 -# +# # Reason: gcd(4,10) = 2 => 2 ^ 1 use Modern::Perl; @@ -35,11 +35,11 @@ use ntheory 'gcd'; say is_power_2(gcd(@ARGV)); sub is_power_2 { - my($n) = @_; - my $p = 2; - while ($p <= $n) { - return 1 if $p==$n; - $p *= 2; - } - return 0; + my($n) = @_; + my $p = 2; + while ($p <= $n) { + return 1 if $p==$n; + $p *= 2; + } + return 0; } diff --git a/challenge-136/paulo-custodio/perl/ch-2.pl b/challenge-136/paulo-custodio/perl/ch-2.pl index fcbadd1cca..cddd75c94a 100644 --- a/challenge-136/paulo-custodio/perl/ch-2.pl +++ b/challenge-136/paulo-custodio/perl/ch-2.pl @@ -5,31 +5,31 @@ # TASK #2 > Fibonacci Sequence # Submitted by: Mohammad S Anwar # You are given a positive number $n. -# +# # Write a script to find how many different sequences you can create using # Fibonacci numbers where the sum of unique numbers in each sequence are the # same as the given number. -# +# # Fibonacci Numbers: 1,2,3,5,8,13,21,34,55,89, ... -# +# # Example 1 # Input: $n = 16 # Output: 4 -# +# # Reason: There are 4 possible sequences that can be created using Fibonacci # numbers # i.e. (3 + 13), (1 + 2 + 13), (3 + 5 + 8) and (1 + 2 + 5 + 8). # Example 2 # Input: $n = 9 # Output: 2 -# +# # Reason: There are 2 possible sequences that can be created using Fibonacci # numbers # i.e. (1 + 3 + 5) and (1 + 8). # Example 3 # Input: $n = 15 # Output: 2 -# +# # Reason: There are 2 possible sequences that can be created using Fibonacci # numbers # i.e. (2 + 5 + 8) and (2 + 13). @@ -46,27 +46,27 @@ say count_combin_sum($n, @fibs); sub fibonacci_upto { - my($n) = @_; - my @fibs; - my $i = 2; # skip first '1' - do { - push @fibs, term($i++); - } while ($fibs[-1] < $n); - pop @fibs if $fibs[-1] > $n; - return @fibs; + my($n) = @_; + my @fibs; + my $i = 2; # skip first '1' + do { + push @fibs, term($i++); + } while ($fibs[-1] < $n); + pop @fibs if $fibs[-1] > $n; + return @fibs; } sub count_combin_sum { - my($n, @terms) = @_; - my $count = 0; - for my $k (1..@terms) { - my @combin = combine($k, @terms); - for (@combin) { - my @combo = @$_; - if (sum(@combo) == $n) { - $count++; - } - } - } - return $count; + my($n, @terms) = @_; + my $count = 0; + for my $k (1..@terms) { + my @combin = combine($k, @terms); + for (@combin) { + my @combo = @$_; + if (sum(@combo) == $n) { + $count++; + } + } + } + return $count; } diff --git a/challenge-136/paulo-custodio/python/ch-1.py b/challenge-136/paulo-custodio/python/ch-1.py index 2a65d9aa1d..20549299d8 100644 --- a/challenge-136/paulo-custodio/python/ch-1.py +++ b/challenge-136/paulo-custodio/python/ch-1.py @@ -5,28 +5,28 @@ # TASK #1 > Two Friendly # Submitted by: Mohammad S Anwar # You are given 2 positive numbers, $m and $n. -# +# # Write a script to find out if the given two numbers are Two Friendly. -# -# Two positive numbers, m and n are two friendly when gcd(m, n) = 2 ^ p -# where p > 0. The greatest common divisor (gcd) of a set of numbers is -# the largest positive number that divides all the numbers in the set +# +# Two positive numbers, m and n are two friendly when gcd(m, n) = 2 ^ p +# where p > 0. The greatest common divisor (gcd) of a set of numbers is +# the largest positive number that divides all the numbers in the set # without remainder. -# +# # Example 1 # Input: $m = 8, $n = 24 # Output: 1 -# +# # Reason: gcd(8,24) = 8 => 2 ^ 3 # Example 2 # Input: $m = 26, $n = 39 # Output: 0 -# +# # Reason: gcd(26,39) = 13 # Example 3 # Input: $m = 4, $n = 10 # Output: 1 -# +# # Reason: gcd(4,10) = 2 => 2 ^ 1 import sys diff --git a/challenge-136/paulo-custodio/python/ch-2.py b/challenge-136/paulo-custodio/python/ch-2.py index 092dd8df9d..91506afbac 100644 --- a/challenge-136/paulo-custodio/python/ch-2.py +++ b/challenge-136/paulo-custodio/python/ch-2.py @@ -5,31 +5,31 @@ # TASK #2 > Fibonacci Sequence # Submitted by: Mohammad S Anwar # You are given a positive number $n. -# +# # Write a script to find how many different sequences you can create using # Fibonacci numbers where the sum of unique numbers in each sequence are the # same as the given number. -# +# # Fibonacci Numbers: 1,2,3,5,8,13,21,34,55,89, ... -# +# # Example 1 # Input: $n = 16 # Output: 4 -# +# # Reason: There are 4 possible sequences that can be created using Fibonacci # numbers # i.e. (3 + 13), (1 + 2 + 13), (3 + 5 + 8) and (1 + 2 + 5 + 8). # Example 2 # Input: $n = 9 # Output: 2 -# +# # Reason: There are 2 possible sequences that can be created using Fibonacci # numbers # i.e. (1 + 3 + 5) and (1 + 8). # Example 3 # Input: $n = 15 # Output: 2 -# +# # Reason: There are 2 possible sequences that can be created using Fibonacci # numbers # i.e. (2 + 5 + 8) and (2 + 13). @@ -40,17 +40,17 @@ from itertools import combinations def fibonacci(n): a = 0 b = 1 - + # Check is n is less # than 0 if n < 0: print("Incorrect input") - + # Check is n is equal # to 0 elif n == 0: return 0 - + # Check if n is equal to 1 elif n == 1: return b @@ -88,25 +88,25 @@ print(count_combin_sum(n, fibs)) # use Math::Fibonacci 'term'; # use Math::Combinatorics; # use List::Util 'sum'; -# +# # @ARGV or die "Usage: ch-2.pl n\n"; # my $n = shift; # my @fibs = fibs_upto($n); # say count_combin_sum($n, @fibs); -# -# +# +# # sub count_combin_sum { -# my($n, @fibs) = @_; -# my $count = 0; -# for my $k (1..@fibs) { -# my @combin = combine($k, @fibs); -# for (@combin) { -# my @combo = @$_; -# if (sum(@combo) == $n) { -# $count++; -# } -# } -# } -# return $count; +# my($n, @fibs) = @_; +# my $count = 0; +# for my $k (1..@fibs) { +# my @combin = combine($k, @fibs); +# for (@combin) { +# my @combo = @$_; +# if (sum(@combo) == $n) { +# $count++; +# } +# } +# } +# return $count; # } -# \ No newline at end of file +# -- cgit