aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-134/paulo-custodio/test.pl198
-rw-r--r--challenge-135/paulo-custodio/perl/ch-1.pl34
-rw-r--r--challenge-135/paulo-custodio/perl/ch-2.pl50
-rw-r--r--challenge-135/paulo-custodio/python/ch-1.py35
-rw-r--r--challenge-135/paulo-custodio/python/ch-2.py43
-rw-r--r--challenge-135/paulo-custodio/t/test-1.yaml25
-rw-r--r--challenge-135/paulo-custodio/t/test-2.yaml15
-rw-r--r--challenge-135/paulo-custodio/test.pl4
8 files changed, 207 insertions, 197 deletions
diff --git a/challenge-134/paulo-custodio/test.pl b/challenge-134/paulo-custodio/test.pl
index 4e2418c4ae..ba6c37260b 100644
--- a/challenge-134/paulo-custodio/test.pl
+++ b/challenge-134/paulo-custodio/test.pl
@@ -1,200 +1,4 @@
#!/usr/bin/env perl
-
-# run tests described in t/test-N.yaml
-
use Modern::Perl;
use Test::More;
-use Path::Tiny;
-use YAML::Tiny;
-
-our $EXE = $^O =~ /MSWin32|msys/ ? ".exe" : "";
-
-# hack so that output redirection works in msys
-our $LUA = $^O eq "msys" ? "lua.exe" : "lua";
-
-our %LANG = (
- ada => 'adb',
- awk => 'awk',
- basic => 'bas',
- bc => 'bc',
- brainfuck=>'bfpp',
- c => 'c',
- cpp => 'cpp',
- d => 'd',
- forth => 'fs',
- fortran => 'f90',
- lua => 'lua',
- pascal => 'pas',
- perl => 'pl',
- python => 'py',
-);
-
-# filter tests if languages given on command line
-our %TESTS;
-if (!@ARGV) {
- %TESTS = %LANG;
-}
-else {
- $TESTS{$_}=1 for @ARGV;
-}
-
-# run independent tests
-my @indep_tests = <t/*.t>;
-if (@indep_tests) {
- ok 0==system("perl -S prove @indep_tests"), "prove @indep_tests";
-}
-
-# to be used in eval{} in the tests
-use vars qw( $prog $exec );
-
-for my $lang (grep {-d} sort keys %LANG) {
- next if -f "t/$lang.t"; # independent test
- next unless $TESTS{$lang};
-
- for $prog (path($lang)->children(qr/\.$LANG{$lang}$/)) {
- $prog->basename =~ /^ch[-_](.*)\.$LANG{$lang}$/ or die $prog;
- my $task = $1;
-
- # compile if needed
- $exec = build($lang, $prog);
-
- for my $test (path("t")->children(qr/test-$task\.yaml$/)) {
- # execute each test from test-N.yaml
- my $yaml = YAML::Tiny->read($test);
- for my $doc (@$yaml) {
- for my $spec (@$doc) {
- # run setup code
- ok eval($spec->{setup}), $spec->{setup}
- if defined($spec->{setup});
- $@ and die $@;
-
- # build test command line
- my $cmd;
- if ($lang =~ /^(bc|brainfuck)$/) { # needs args in stdin
- $cmd = "echo ".($spec->{args}//"")."|$exec";
- }
- else {
- $cmd = "$exec ".value_or_eval($spec->{args});
- }
- chomp($cmd);
-
- # input
- if(defined($spec->{input})) {
- path("in.txt")->spew(value_or_eval($spec->{input}));
- $cmd .= " < in.txt";
- }
- if (defined($spec->{output})) {
- $spec->{output} =~ s/^\|//mg; # delete initial bar
- path("out_exp.txt")->spew(value_or_eval($spec->{output}));
- $cmd .= " > out.txt";
- }
-
- # run test
- run($cmd);
-
- # compare output
- if (defined($spec->{output})) {
- # remove \\ \n added by bc
- if ($lang eq 'bc') {
- my $out = path("out.txt")->slurp;
- $out =~ s/\\\n//g;
- path("out.txt")->spew($out);
- }
-
- run("diff -w out_exp.txt out.txt");
- }
-
- # run cleaup code
- if (Test::More->builder->is_passing) {
- ok eval($spec->{cleanup}), $spec->{cleanup}
- if defined($spec->{cleanup});
- $@ and die $@;
- unlink("in.txt", "out.txt", "out_exp.txt");
- }
- else {
- die "tests failed\n"; # to give chance to examine output
- }
- }
- }
- }
- }
-}
-
-done_testing;
-
-# compile if needed, return executable line
-sub build {
- my($lang, $prog) = @_;
- my $exe = ($prog =~ s/\.\w+/$EXE/r);
- my $prog_wo_ext = ($prog =~ s/\.\w+//r);
- my $prog_base = path($prog)->basename;
- for ($lang) {
- if (/^ada$/) {
- run("cd ada; gnatmake $prog_base"); # gnatmake builds only if needed
- return $exe;
- }
- if (/^awk$/) {
- return "gawk -f $prog --";
- }
- if (/^basic$/) {
- run("fbc $prog -o $prog_wo_ext") if (!-f $exe || -M $exe > -M $prog);
- return $exe;
- }
- if (/^bc$/) {
- return "bc -lq $prog";
- }
- if (/^brainfuck$/) {
- run("perl bfpp.pl <$prog_wo_ext.bfpp >$prog_wo_ext.bf");
- return "brainfuck $prog_wo_ext.bf";
- }
- if (/^c$/) {
- run("gcc $prog -o $prog_wo_ext -lmpfr -lgmp") if (!-f $exe || -M $exe > -M $prog);
- return $exe;
- }
- if (/^cpp$/) {
- run("g++ $prog -o $prog_wo_ext -lmpfr -lgmpxx -lgmp") if (!-f $exe || -M $exe > -M $prog);
- return $exe;
- }
- if (/^d$/) {
- run("cd d; dmd $prog_base") if (!-f $exe || -M $exe > -M $prog);
- return $exe;
- }
- if (/^forth$/) {
- return "gforth $prog";
- }
- if (/^fortran$/) {
- run("gfortran $prog -o $prog_wo_ext") if (!-f $exe || -M $exe > -M $prog);
- return $exe;
- }
- if (/^lua$/) {
- return "$LUA $prog";
- }
- if (/^pascal$/) {
- run("fpc -o$exe $prog") if (!-f $exe || -M $exe > -M $prog);
- return $exe;
- }
- if (/^perl$/) {
- return "perl $prog";
- }
- if (/^python$/) {
- return "python3 $prog";
- }
- die "unsupported language $lang";
- }
-}
-
-sub run {
- my($cmd) = @_;
- ok 0==system($cmd), $cmd;
-}
-
-sub value_or_eval {
- my($str) = @_;
- $str //= "";
- $str =~ s/^\|//gm;
- my $value = ($str =~ /^eval\b/) ? eval($str) : $str;
- $@ and die "eval '$str' failed: $@";
- return $value;
-}
-
-1;
+require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-135/paulo-custodio/perl/ch-1.pl b/challenge-135/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..ebb2200c2c
--- /dev/null
+++ b/challenge-135/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+# 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
+# Example 2
+# Input: $n = -123
+# Output: 123
+# Example 3
+# Input: $n = 1
+# Output: too short
+# Example 4
+# Input: $n = 10
+# Output: even number of digits
+
+use Modern::Perl;
+
+my $n = abs(shift||0);
+my $len = length($n);
+if ($len%2==0) {
+ say "even number of digits";
+}
+elsif ($len<3) {
+ say "too short";
+}
+else {
+ 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
new file mode 100644
index 0000000000..aad313eac5
--- /dev/null
+++ b/challenge-135/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+# 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
+# otherwise 0.
+#
+# For more information about SEDOL, please checkout the wikipedia page.
+#
+# Example 1
+# Input: $SEDOL = '2936921'
+# Output: 1
+# Example 2
+# Input: $SEDOL = '1234567'
+# Output: 0
+# Example 3
+# Input: $SEDOL = 'B0YBKL9'
+# Output: 1
+
+use Modern::Perl;
+
+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;
+ }
+}
+
+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);
+}
diff --git a/challenge-135/paulo-custodio/python/ch-1.py b/challenge-135/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..389816b534
--- /dev/null
+++ b/challenge-135/paulo-custodio/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+# 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
+# Example 2
+# Input: $n = -123
+# Output: 123
+# Example 3
+# Input: $n = 1
+# Output: too short
+# Example 4
+# Input: $n = 10
+# Output: even number of digits
+
+import sys
+
+def mid3digits(n):
+ l = len(str(n))
+ if l%2==0:
+ return "even number of digits"
+ elif l<3:
+ return "too short"
+ else:
+ s = int((l-3)/2)
+ return str(n)[s:s+3];
+
+n = abs(int(str(sys.argv[1])))
+print(mid3digits(n))
diff --git a/challenge-135/paulo-custodio/python/ch-2.py b/challenge-135/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..9f1bb2594a
--- /dev/null
+++ b/challenge-135/paulo-custodio/python/ch-2.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+# 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
+# otherwise 0.
+#
+# For more information about SEDOL, please checkout the wikipedia page.
+#
+# Example 1
+# Input: $SEDOL = '2936921'
+# Output: 1
+# Example 2
+# Input: $SEDOL = '1234567'
+# Output: 0
+# Example 3
+# Input: $SEDOL = 'B0YBKL9'
+# Output: 1
+
+import sys
+import re
+
+def check_sedol(sedol):
+ def compute_check_digit(input_str):
+ weight = [1, 3, 1, 7, 3, 9]
+ input = [int(c, 36) for c in input_str]
+ sum = 0
+ for i in range(0, 6):
+ sum += input[i] * weight[i]
+ return str(10-sum%10)
+
+ if not re.match(r"^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$", sedol):
+ return 0
+ input = sedol[0:6]
+ check_digit = compute_check_digit(input)
+ if input+check_digit==sedol:
+ return 1
+ else:
+ return 0
+
+print(check_sedol(sys.argv[1]))
diff --git a/challenge-135/paulo-custodio/t/test-1.yaml b/challenge-135/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..eaf5d3a3a6
--- /dev/null
+++ b/challenge-135/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: 1234567
+ input:
+ output: 345
+- setup:
+ cleanup:
+ args: -123
+ input:
+ output: 123
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: too short
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: even number of digits
+- setup:
+ cleanup:
+ args: 1234
+ input:
+ output: even number of digits
diff --git a/challenge-135/paulo-custodio/t/test-2.yaml b/challenge-135/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..644605a018
--- /dev/null
+++ b/challenge-135/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 2936921
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 1234567
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: B0YBKL9
+ input:
+ output: 1
diff --git a/challenge-135/paulo-custodio/test.pl b/challenge-135/paulo-custodio/test.pl
new file mode 100644
index 0000000000..ba6c37260b
--- /dev/null
+++ b/challenge-135/paulo-custodio/test.pl
@@ -0,0 +1,4 @@
+#!/usr/bin/env perl
+use Modern::Perl;
+use Test::More;
+require '../../challenge-001/paulo-custodio/test.pl';