diff options
| -rw-r--r--[-rwxr-xr-x] | challenge-114/paulo-custodio/perl/ch-1.pl | 22 | ||||
| -rw-r--r--[-rwxr-xr-x] | challenge-114/paulo-custodio/perl/ch-2.pl | 34 | ||||
| -rw-r--r--[-rwxr-xr-x] | challenge-115/paulo-custodio/perl/ch-1.pl | 44 | ||||
| -rw-r--r--[-rwxr-xr-x] | challenge-115/paulo-custodio/perl/ch-2.pl | 38 | ||||
| -rw-r--r--[-rwxr-xr-x] | challenge-116/paulo-custodio/perl/ch-1.pl | 62 | ||||
| -rw-r--r--[-rwxr-xr-x] | challenge-116/paulo-custodio/perl/ch-2.pl | 28 |
6 files changed, 114 insertions, 114 deletions
diff --git a/challenge-114/paulo-custodio/perl/ch-1.pl b/challenge-114/paulo-custodio/perl/ch-1.pl index 1acdabc3d2..c543df4a7e 100755..100644 --- a/challenge-114/paulo-custodio/perl/ch-1.pl +++ b/challenge-114/paulo-custodio/perl/ch-1.pl @@ -1,18 +1,18 @@ #!/usr/bin/env perl # Challenge 114 -# +# # TASK #1 - Next Palindrome Number # Submitted by: Mohammad S Anwar # You are given a positive integer $N. -# +# # Write a script to find out the next Palindrome Number higher than the given # integer $N. -# +# # Example # Input: $N = 1234 # Output: 1331 -# +# # Input: $N = 999 # Output: 1001 @@ -22,14 +22,14 @@ my $N = shift || 0; say next_palindrome($N); sub is_palindrome { - my($n) = @_; - return reverse($n)==$n; + my($n) = @_; + return reverse($n)==$n; } sub next_palindrome { - my($n) = @_; - while (1) { - $n++; - return $n if is_palindrome($n); - } + my($n) = @_; + while (1) { + $n++; + return $n if is_palindrome($n); + } } diff --git a/challenge-114/paulo-custodio/perl/ch-2.pl b/challenge-114/paulo-custodio/perl/ch-2.pl index fcae901dc9..c76fcaf286 100755..100644 --- a/challenge-114/paulo-custodio/perl/ch-2.pl +++ b/challenge-114/paulo-custodio/perl/ch-2.pl @@ -1,24 +1,24 @@ #!/usr/bin/env perl # Challenge 114 -# +# # TASK #2 - Higher Integer Set Bits # Submitted by: Mohammad S Anwar # You are given a positive integer $N. -# -# Write a script to find the next higher integer having the same number of +# +# Write a script to find the next higher integer having the same number of # 1 bits in binary representation as $N. -# +# # Example # Input: $N = 3 # Output: 5 -# +# # Binary representation of $N is 011. There are two 1 bits. So the next higher # integer is 5 having the same the number of 1 bits i.e. 101. -# +# # Input: $N = 12 # Output: 17 -# +# # Binary representation of $N is 1100. There are two 1 bits. So the next higher # integer is 17 having the same number of 1 bits i.e. 10001. @@ -28,17 +28,17 @@ my $N = shift || 0; say next_same_1bits($N); sub num_1bits { - my($n) = @_; - my $sum = 0; - map {$sum += $_} split //, sprintf("%b", $n); - return $sum; + my($n) = @_; + my $sum = 0; + map {$sum += $_} split //, sprintf("%b", $n); + return $sum; } sub next_same_1bits { - my($n) = @_; - my $num1s = num_1bits($n); - while (1) { - $n++; - return $n if num_1bits($n) == $num1s; - } + my($n) = @_; + my $num1s = num_1bits($n); + while (1) { + $n++; + return $n if num_1bits($n) == $num1s; + } } diff --git a/challenge-115/paulo-custodio/perl/ch-1.pl b/challenge-115/paulo-custodio/perl/ch-1.pl index 2fed84e369..6d28fbd2d6 100755..100644 --- a/challenge-115/paulo-custodio/perl/ch-1.pl +++ b/challenge-115/paulo-custodio/perl/ch-1.pl @@ -1,21 +1,21 @@ #!/usr/bin/env perl # Challenge 115 -# +# # TASK #1 - String Chain # Submitted by: Mohammad S Anwar # You are given an array of strings. -# +# # Write a script to find out if the given strings can be chained to form a # circle. Print 1 if found otherwise 0. -# -# A string $S can be put before another string $T in circle if the last +# +# A string $S can be put before another string $T in circle if the last # character of $S is same as first character of $T. -# +# # Examples: # Input: @S = ("abc", "dea", "cd") # Output: 1 as we can form circle e.g. "abc", "cd", "dea". -# +# # Input: @S = ("ade", "cbd", "fgh") # Output: 0 as we can't form circle. @@ -27,20 +27,20 @@ my @words = @ARGV; say is_circle(\@words) ? 1 : 0; sub is_circle { - my($pending, @words) = @_; - my $found_solution; - - if (@$pending == 0) { - $found_solution ||= substr($words[-1],-1,1) eq substr($words[0],0,1); - } - else { - for my $word (@$pending) { - if (@words == 0 || - substr($words[-1],-1,1) eq substr($word,0,1)) { - my @new_pending = grep {$_ ne $word} @$pending; - $found_solution ||= is_circle(\@new_pending, @words, $word); - } - } - } - return $found_solution; + my($pending, @words) = @_; + my $found_solution; + + if (@$pending == 0) { + $found_solution ||= substr($words[-1],-1,1) eq substr($words[0],0,1); + } + else { + for my $word (@$pending) { + if (@words == 0 || + substr($words[-1],-1,1) eq substr($word,0,1)) { + my @new_pending = grep {$_ ne $word} @$pending; + $found_solution ||= is_circle(\@new_pending, @words, $word); + } + } + } + return $found_solution; } diff --git a/challenge-115/paulo-custodio/perl/ch-2.pl b/challenge-115/paulo-custodio/perl/ch-2.pl index d08b1104f0..afb6efc0ab 100755..100644 --- a/challenge-115/paulo-custodio/perl/ch-2.pl +++ b/challenge-115/paulo-custodio/perl/ch-2.pl @@ -1,21 +1,21 @@ #!/usr/bin/env perl # Challenge 115 -# +# # TASK #2 - Largest Multiple # Submitted by: Mohammad S Anwar # You are given a list of positive integers (0-9), single digit. -# +# # Write a script to find the largest multiple of 2 that can be formed from the # list. -# +# # Examples # Input: @N = (1, 0, 2, 6) # Output: 6210 -# +# # Input: @N = (1, 4, 2, 8) # Output: 8412 -# +# # Input: @N = (4, 1, 7, 6) # Output: 7614 @@ -26,18 +26,18 @@ my @nums = @ARGV; say largest_mult2(@nums); sub largest_mult2 { - my(@nums) = @_; - - # select smallest even number for last element - my @even = sort {$a->[1] <=> $b->[1]} - grep {$_->[1] % 2 == 0} - map {[$_, $nums[$_]]} 0..$#nums; - return 0 if !@even; # no even numbers - my($index, $last) = @{$even[0]}; - splice(@nums, $index, 1); # remove it from @nums - - # sort the other elements in descending order - @nums = sort {$b <=> $a} @nums; - - return 0+join('',@nums,$last); + my(@nums) = @_; + + # select smallest even number for last element + my @even = sort {$a->[1] <=> $b->[1]} + grep {$_->[1] % 2 == 0} + map {[$_, $nums[$_]]} 0..$#nums; + return 0 if !@even; # no even numbers + my($index, $last) = @{$even[0]}; + splice(@nums, $index, 1); # remove it from @nums + + # sort the other elements in descending order + @nums = sort {$b <=> $a} @nums; + + return 0+join('',@nums,$last); } diff --git a/challenge-116/paulo-custodio/perl/ch-1.pl b/challenge-116/paulo-custodio/perl/ch-1.pl index 945f89a661..ba664c35a3 100755..100644 --- a/challenge-116/paulo-custodio/perl/ch-1.pl +++ b/challenge-116/paulo-custodio/perl/ch-1.pl @@ -1,23 +1,23 @@ #!/usr/bin/env perl # Challenge 116 -# +# # TASK #1 - Number Sequence # Submitted by: Mohammad S Anwar # You are given a number $N >= 10. -# +# # Write a script to split the given number such that the difference between two # consecutive numbers is always 1 and it shouldn't have leading 0. -# +# # Print the given number if it impossible to split the number. -# +# # Example # Input: $N = 1234 # Output: 1,2,3,4 -# +# # Input: $N = 91011 # Output: 9,10,11 -# +# # Input: $N = 10203 # Output: 10203 as it is impossible to split satisfying the conditions. @@ -28,29 +28,29 @@ my $N = shift; print_sequences($N); sub print_sequences { - my($rest, @prev) = @_; - my $found_solution = 0; - - if ($rest eq '') { - if (!$found_solution) { - say join(",", @prev); - $found_solution = 1; - } - } - else { - for (1 .. length($rest)) { - my $pref = substr($rest,0,$_); - my $suff = substr($rest,$_); - next if $suff =~ /^0/; - if (@prev) { - if ($prev[-1] + 1 == $pref) { - $found_solution ||= print_sequences($suff, @prev, $pref); - } - } - else { - $found_solution ||= print_sequences($suff, @prev, $pref); - } - } - } - return $found_solution; + my($rest, @prev) = @_; + my $found_solution = 0; + + if ($rest eq '') { + if (!$found_solution) { + say join(",", @prev); + $found_solution = 1; + } + } + else { + for (1 .. length($rest)) { + my $pref = substr($rest,0,$_); + my $suff = substr($rest,$_); + next if $suff =~ /^0/; + if (@prev) { + if ($prev[-1] + 1 == $pref) { + $found_solution ||= print_sequences($suff, @prev, $pref); + } + } + else { + $found_solution ||= print_sequences($suff, @prev, $pref); + } + } + } + return $found_solution; } diff --git a/challenge-116/paulo-custodio/perl/ch-2.pl b/challenge-116/paulo-custodio/perl/ch-2.pl index 09ea8e1232..a84fe54eec 100755..100644 --- a/challenge-116/paulo-custodio/perl/ch-2.pl +++ b/challenge-116/paulo-custodio/perl/ch-2.pl @@ -1,21 +1,21 @@ #!/usr/bin/env perl # Challenge 116 -# +# # TASK #2 - Sum of Squares # Submitted by: Mohammad Meraj Zia # You are given a number $N >= 10. -# +# # Write a script to find out if the given number $N is such that sum of squares # of all digits is a perfect square. Print 1 if it is otherwise 0. -# +# # Example # Input: $N = 34 # Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2 -# +# # Input: $N = 50 # Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2 -# +# # Input: $N = 52 # Output: 0 as 5^2 + 2^2 => 25 + 4 => 29 @@ -25,14 +25,14 @@ my $N = shift || 0; say sum_of_squares_is_perfect_square($N) ? 1 : 0; sub sum_of_squares_is_perfect_square { - my($num) = @_; - return 0 if $num < 10; - my $sum = 0; - for my $digit (split(//, $num)) { - $sum += $digit*$digit; - } - my $sqrt_int = int(sqrt($sum)); - return $sqrt_int*$sqrt_int==$sum; + my($num) = @_; + return 0 if $num < 10; + my $sum = 0; + for my $digit (split(//, $num)) { + $sum += $digit*$digit; + } + my $sqrt_int = int(sqrt($sum)); + return $sqrt_int*$sqrt_int==$sum; } -
\ No newline at end of file + |
