diff options
| -rw-r--r-- | challenge-027/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-027/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-027/paulo-custodio/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-027/paulo-custodio/perl/ch-2.pl | 38 | ||||
| -rw-r--r-- | challenge-027/paulo-custodio/python/ch-1.py | 23 | ||||
| -rw-r--r-- | challenge-027/paulo-custodio/python/ch-2.py | 26 | ||||
| -rw-r--r-- | challenge-027/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-027/paulo-custodio/t/test-2.yaml | 5 | ||||
| -rw-r--r-- | challenge-143/paulo-custodio/perl/ch-1.pl | 98 | ||||
| -rw-r--r-- | challenge-143/paulo-custodio/perl/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-143/paulo-custodio/python/ch-1.py | 6 | ||||
| -rw-r--r-- | challenge-143/paulo-custodio/python/ch-2.py | 30 |
12 files changed, 211 insertions, 88 deletions
diff --git a/challenge-027/paulo-custodio/Makefile b/challenge-027/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-027/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-027/paulo-custodio/README b/challenge-027/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-027/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-027/paulo-custodio/perl/ch-1.pl b/challenge-027/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..e4b4feca3e --- /dev/null +++ b/challenge-027/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +# Challenge 027 +# +# Task #1 +# Write a script to find the intersection of two straight lines. The +# co-ordinates of the two lines should be provided as command line parameter. +# For example: +# +# The two ends of Line 1 are represented as co-ordinates (a,b) and (c,d). +# +# The two ends of Line 2 are represented as co-ordinates (p,q) and (r,s). +# +# The script should print the co-ordinates of point of intersection of the +# above two lines. + +use Modern::Perl; + +my($x1,$y1,$x2,$y2,$x3,$y3,$x4,$y4) = @ARGV; +my $D = ($x1-$x2)*($y3-$y4)-($y1-$y2)*($x3-$x4); +my $x = (($x1*$y2-$y1*$x2)*($x3-$x4)-($x1-$x2)*($x3*$y4-$y3*$x4))/$D; +my $y = (($x1*$y2-$y1*$x2)*($y3-$y4)-($y1-$y2)*($x3*$y4-$y3*$x4))/$D; +say sprintf("%.1f %.1f", $x, $y); diff --git a/challenge-027/paulo-custodio/perl/ch-2.pl b/challenge-027/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..8091076b58 --- /dev/null +++ b/challenge-027/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +# Challenge 027 +# +# Task #2 +# Write a script that allows you to capture/display historical data. It could +# be an object or a scalar. For example +# +# my $x = 10; $x = 20; $x -= 5; +# +# After the above operations, it should list $x historical value in order. + +use Modern::Perl; + +{ + package LoggingScalar; + sub TIESCALAR { + my($class, $value) = @_; + return bless [$value], $class; + } + sub FETCH { + my($self) = @_; + return $self->[-1]; + } + sub STORE { + my($self, $value) = @_; + push @$self, $value; + } + sub show_hist { + my($self) = @_; + say join(" ", @$self); + } +} + +tie my $x, 'LoggingScalar', 10; +$x = 20; +$x -= 5; +tied($x)->show_hist(); diff --git a/challenge-027/paulo-custodio/python/ch-1.py b/challenge-027/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..8f3e6c9731 --- /dev/null +++ b/challenge-027/paulo-custodio/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 + +# Challenge 027 +# +# Task #1 +# Write a script to find the intersection of two straight lines. The +# co-ordinates of the two lines should be provided as command line parameter. +# For example: +# +# The two ends of Line 1 are represented as co-ordinates (a,b) and (c,d). +# +# The two ends of Line 2 are represented as co-ordinates (p,q) and (r,s). +# +# The script should print the co-ordinates of point of intersection of the +# above two lines. + +import sys + +x1,y1,x2,y2,x3,y3,x4,y4 = [int(x) for x in sys.argv[1:9]] +D = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) +x = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/D +y = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/D +print("{:.1f} {:.1f}".format(x, y)) diff --git a/challenge-027/paulo-custodio/python/ch-2.py b/challenge-027/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..99fd57554b --- /dev/null +++ b/challenge-027/paulo-custodio/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +# Challenge 027 +# +# Task #2 +# Write a script that allows you to capture/display historical data. It could +# be an object or a scalar. For example +# +# my $x = 10; $x = 20; $x -= 5; +# +# After the above operations, it should list $x historical value in order. + +class LoggingScalar: + def __init__(self, value): + self.values = [value] + def set(self, value): + self.values.append(value) + def get(self): + return self.values[-1] + def show_hist(self): + print(" ".join([str(x) for x in self.values])) + +x = LoggingScalar(10) +x.set(20) +x.set(x.get()-5) +x.show_hist() diff --git a/challenge-027/paulo-custodio/t/test-1.yaml b/challenge-027/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..31ebb25d9b --- /dev/null +++ b/challenge-027/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 15 10 49 25 29 5 32 32 + input: + output: 30.3 16.8 diff --git a/challenge-027/paulo-custodio/t/test-2.yaml b/challenge-027/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..78447f751a --- /dev/null +++ b/challenge-027/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 10 20 15 diff --git a/challenge-143/paulo-custodio/perl/ch-1.pl b/challenge-143/paulo-custodio/perl/ch-1.pl index 90b6fe6506..73a806191b 100644 --- a/challenge-143/paulo-custodio/perl/ch-1.pl +++ b/challenge-143/paulo-custodio/perl/ch-1.pl @@ -3,10 +3,10 @@ # TASK #1 > Calculator # Submitted by: Mohammad S Anwar # You are given a string, $s, containing mathematical expression. -# +# # Write a script to print the result of the mathematical expression. To keep # it simple, please only accept + - * (). -# +# # Example 1: # Input: $s = "10 + 20 - 5" # Output: 25 @@ -20,61 +20,61 @@ use Modern::Perl; # ($input, $value) = expr($input) sub expr { - my($input) = @_; - ($input, my $value) = factor($input); - while (1) { - if ($input =~ s/^\s*\*//) { - ($input, my $b) = factor($input); - $value *= $b; - } - elsif ($input =~ s/^\s*\///) { - ($input, my $b) = factor($input); - $value /= $b; - } - elsif ($input =~ /^\s*(?:\)|$)/) { - return ($input, $value); - } - else { - die "expected / or * at: $input\n"; - } - } + my($input) = @_; + ($input, my $value) = factor($input); + while (1) { + if ($input =~ s/^\s*\*//) { + ($input, my $b) = factor($input); + $value *= $b; + } + elsif ($input =~ s/^\s*\///) { + ($input, my $b) = factor($input); + $value /= $b; + } + elsif ($input =~ /^\s*(?:\)|$)/) { + return ($input, $value); + } + else { + die "expected / or * at: $input\n"; + } + } } # ($input, $value) = factor($input) sub factor { - my($input) = @_; - ($input, my $value) = term($input); - while (1) { - if ($input =~ s/^\s*\+//) { - ($input, my $b) = term($input); - $value += $b; - } - elsif ($input =~ s/^\s*\-//) { - ($input, my $b) = term($input); - $value -= $b; - } - else { - return ($input, $value); - } - } + my($input) = @_; + ($input, my $value) = term($input); + while (1) { + if ($input =~ s/^\s*\+//) { + ($input, my $b) = term($input); + $value += $b; + } + elsif ($input =~ s/^\s*\-//) { + ($input, my $b) = term($input); + $value -= $b; + } + else { + return ($input, $value); + } + } } # ($input, $value) = term($input) sub term { - my($input) = @_; - while (1) { - if ($input =~ s/^\s*([-+]?\d+)//) { - return ($input, $1); - } - elsif ($input =~ s/^\s*\(//) { - ($input, my $value) = expr($input); - $input =~ s/^\s*\)// or die "expected ) at: $input\n"; - return ($input, $value); - } - else { - die "expected ( or number at: $input\n"; - } - } + my($input) = @_; + while (1) { + if ($input =~ s/^\s*([-+]?\d+)//) { + return ($input, $1); + } + elsif ($input =~ s/^\s*\(//) { + ($input, my $value) = expr($input); + $input =~ s/^\s*\)// or die "expected ) at: $input\n"; + return ($input, $value); + } + else { + die "expected ( or number at: $input\n"; + } + } } my $input = "@ARGV"; diff --git a/challenge-143/paulo-custodio/perl/ch-2.pl b/challenge-143/paulo-custodio/perl/ch-2.pl index a805a8180b..9433cd520d 100644 --- a/challenge-143/paulo-custodio/perl/ch-2.pl +++ b/challenge-143/paulo-custodio/perl/ch-2.pl @@ -3,46 +3,46 @@ # TASK #2 > Stealthy Number # Submitted by: Mohammad S Anwar # You are given a positive number, $n. -# +# # Write a script to find out if the given number is Stealthy Number. -# +# # A positive integer N is stealthy, if there exist positive integers a, b, c, d # such that a * b = c * d = N and a + b = c + d + 1. -# +# # Example 1 # Input: $n = 36 # Output: 1 -# +# # Since 36 = 4 (a) * 9 (b) = 6 (c) * 6 (d) and 4 (a) + 9 (b) = 6 (c) + 6 (d) + 1. # Example 2 # Input: $n = 12 # Output: 1 -# +# # Since 2 * 6 = 3 * 4 and 2 + 6 = 3 + 4 + 1 # Example 3 # Input: $n = 6 # Output: 0 -# +# # Since 2 * 3 = 1 * 6 but 2 + 3 != 1 + 6 + 1 use Modern::Perl; sub is_stealthy { - my($n) = @_; - for my $a (1..$n) { - if ($n % $a == 0) { - my $b = $n / $a; # a*b=n - for my $c (1..$n) { - if ($n % $c == 0) { - my $d = $n / $c; # c*d=n - if ($a+$b==$c+$d+1) { - return 1; - } - } - } - } - } - return 0; + my($n) = @_; + for my $a (1..$n) { + if ($n % $a == 0) { + my $b = $n / $a; # a*b=n + for my $c (1..$n) { + if ($n % $c == 0) { + my $d = $n / $c; # c*d=n + if ($a+$b==$c+$d+1) { + return 1; + } + } + } + } + } + return 0; } my $n = shift//1; diff --git a/challenge-143/paulo-custodio/python/ch-1.py b/challenge-143/paulo-custodio/python/ch-1.py index a81c52fd0e..7705a4eafe 100644 --- a/challenge-143/paulo-custodio/python/ch-1.py +++ b/challenge-143/paulo-custodio/python/ch-1.py @@ -3,10 +3,10 @@ # TASK #1 > Calculator # Submitted by: Mohammad S Anwar # You are given a string, $s, containing mathematical expression. -# +# # Write a script to print the result of the mathematical expression. To keep # it simple, please only accept + - * (). -# +# # Example 1: # Input: $s = "10 + 20 - 5" # Output: 25 @@ -46,7 +46,7 @@ def factor(input): value -= b else: return input, value - + def term(input): input = input.strip()+" " match = re.match(r"[-+]?\d+", input) diff --git a/challenge-143/paulo-custodio/python/ch-2.py b/challenge-143/paulo-custodio/python/ch-2.py index 3d1976d3f0..850ebfac33 100644 --- a/challenge-143/paulo-custodio/python/ch-2.py +++ b/challenge-143/paulo-custodio/python/ch-2.py @@ -3,40 +3,40 @@ # TASK #2 > Stealthy Number # Submitted by: Mohammad S Anwar # You are given a positive number, $n. -# +# # Write a script to find out if the given number is Stealthy Number. -# +# # A positive integer N is stealthy, if there exist positive integers a, b, c, d # such that a * b = c * d = N and a + b = c + d + 1. -# +# # Example 1 # Input: $n = 36 # Output: 1 -# +# # Since 36 = 4 (a) * 9 (b) = 6 (c) * 6 (d) and 4 (a) + 9 (b) = 6 (c) + 6 (d) + 1. # Example 2 # Input: $n = 12 # Output: 1 -# +# # Since 2 * 6 = 3 * 4 and 2 + 6 = 3 + 4 + 1 # Example 3 # Input: $n = 6 # Output: 0 -# +# # Since 2 * 3 = 1 * 6 but 2 + 3 != 1 + 6 + 1 import sys def is_stealthy(n): - for a in range(1, n+1): - if n%a==0: - b = n/a # a*b=n - for c in range(1, n+1): - if n%c==0: - d = n/c # c*d=n - if a+b==c+d+1: - return 1 - return 0 + for a in range(1, n+1): + if n%a==0: + b = n/a # a*b=n + for c in range(1, n+1): + if n%c==0: + d = n/c # c*d=n + if a+b==c+d+1: + return 1 + return 0 n = int(sys.argv[1]) print(is_stealthy(n)) |
