aboutsummaryrefslogtreecommitdiff
path: root/challenge-134
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-134')
-rw-r--r--challenge-134/paulo-custodio/perl/ch-1.pl19
-rw-r--r--challenge-134/paulo-custodio/perl/ch-2.pl20
-rw-r--r--challenge-134/paulo-custodio/python/ch-1.py19
-rw-r--r--challenge-134/paulo-custodio/python/ch-2.py18
-rw-r--r--challenge-134/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-134/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-134/paulo-custodio/test.pl200
7 files changed, 296 insertions, 0 deletions
diff --git a/challenge-134/paulo-custodio/perl/ch-1.pl b/challenge-134/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..d7ccd780e7
--- /dev/null
+++ b/challenge-134/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+# 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
+# significant digits each digit used in the base at least once.
+
+# solution from https://oeis.org/A050278
+
+use Modern::Perl;
+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
diff --git a/challenge-134/paulo-custodio/perl/ch-2.pl b/challenge-134/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..8a01b8ee55
--- /dev/null
+++ b/challenge-134/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl
+
+# 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
+# terms.
+
+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;
+ }
+}
+say scalar(keys %terms); \ No newline at end of file
diff --git a/challenge-134/paulo-custodio/python/ch-1.py b/challenge-134/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..814125549b
--- /dev/null
+++ b/challenge-134/paulo-custodio/python/ch-1.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+
+# 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
+# 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.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
new file mode 100644
index 0000000000..d378a11ca2
--- /dev/null
+++ b/challenge-134/paulo-custodio/python/ch-2.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python3
+
+# 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
+# terms.
+
+import sys
+
+m, n = int(sys.argv[1]), int(sys.argv[2])
+terms = set()
+for a in range(1, m+1):
+ for b in range(1, n+1):
+ prod = a * b
+ terms.add(prod)
+print(len(terms))
diff --git a/challenge-134/paulo-custodio/t/test-1.yaml b/challenge-134/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..81b36fedcb
--- /dev/null
+++ b/challenge-134/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: |
+ 1023456789
+ 1023456798
+ 1023456879
+ 1023456897
+ 1023456978
diff --git a/challenge-134/paulo-custodio/t/test-2.yaml b/challenge-134/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..44de742e13
--- /dev/null
+++ b/challenge-134/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 3 3
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 3 5
+ input:
+ output: 11
diff --git a/challenge-134/paulo-custodio/test.pl b/challenge-134/paulo-custodio/test.pl
new file mode 100644
index 0000000000..4e2418c4ae
--- /dev/null
+++ b/challenge-134/paulo-custodio/test.pl
@@ -0,0 +1,200 @@
+#!/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;