diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-20 01:18:05 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-20 01:18:05 +0000 |
| commit | 2102fa481829f30d4e6ec37d04fa77f87e7e9a50 (patch) | |
| tree | 158db5e61a8ba4e27e57b0feb52c6673a65af476 | |
| parent | 468bc290d602b49bc9c51d8905d7e389e931806e (diff) | |
| parent | 798c5692eb4c43474a1ba230bb93f9fdfc3fd311 (diff) | |
| download | perlweeklychallenge-club-2102fa481829f30d4e6ec37d04fa77f87e7e9a50.tar.gz perlweeklychallenge-club-2102fa481829f30d4e6ec37d04fa77f87e7e9a50.tar.bz2 perlweeklychallenge-club-2102fa481829f30d4e6ec37d04fa77f87e7e9a50.zip | |
Merge pull request #3576 from pauloscustodio/paulo-custodio
Paulo Custodio
46 files changed, 693 insertions, 587 deletions
diff --git a/challenge-096/paulo-custodio/t/test-1.yaml b/challenge-096/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..de8e55d531 --- /dev/null +++ b/challenge-096/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: The Weekly Challenge + input: + output: Challenge Weekly The +- setup: + cleanup: + args: ' Perl and Raku are part of the same family ' + input: + output: family same the of part are Raku and Perl diff --git a/challenge-096/paulo-custodio/t/test-2.yaml b/challenge-096/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..52de7d442e --- /dev/null +++ b/challenge-096/paulo-custodio/t/test-2.yaml @@ -0,0 +1,17 @@ +- setup: + cleanup: + args: kitten sitting + input: + output: | + |3 + |Operation 1: replace 'k' with 's' + |Operation 2: replace 'e' with 'i' + |Operation 3: insert 'g' at end +- setup: + cleanup: + args: sunday monday + input: + output: | + |2 + |Operation 1: replace 's' with 'm' + |Operation 2: replace 'u' with 'o' diff --git a/challenge-096/paulo-custodio/test.pl b/challenge-096/paulo-custodio/test.pl index 29b27ddfda..01ed2b83cd 100644 --- a/challenge-096/paulo-custodio/test.pl +++ b/challenge-096/paulo-custodio/test.pl @@ -3,69 +3,5 @@ use strict; use warnings; use 5.030; -use Test::More; -# hack so that output redirection works in msys -my $LUA = $^O eq "msys" ? "lua.exe" : "lua"; - -run("gcc c/ch-1.c -o c/ch-1"); -run("g++ cpp/ch-1.cpp -o cpp/ch-1"); -run("fbc basic/ch-1.bas -o basic/ch-1"); - -for (["The Weekly Challenge" => "Challenge Weekly The"], - ["' Perl and Raku are part of the same family '" => - "family same the of part are Raku and Perl"]) { - my($in, $out) = @$_; - - is capture( "perl perl/ch-1.pl $in"), "$out\n"; - is capture( "$LUA lua/ch-1.lua $in"), "$out\n"; - is capture("python python/ch-1.py $in"), "$out\n"; - is capture( "gforth forth/ch-1.fs $in"), "$out\n"; - is capture( "c/ch-1 $in"), "$out\n"; - is capture( "cpp/ch-1 $in"), "$out\n"; - is capture( "basic/ch-1 $in"), "$out\n"; -} - - -is capture("perl perl/ch-2a.pl kitten sitting"), "3\n"; -is capture("perl perl/ch-2a.pl sunday monday"), "2\n"; - -run("gcc c/ch-2.c -o c/ch-2"); -run("g++ cpp/ch-2.cpp -o cpp/ch-2"); -run("fbc basic/ch-2.bas -o basic/ch-2"); - -for (["kitten sitting" => <<END], -3 -Operation 1: replace 'k' with 's' -Operation 2: replace 'e' with 'i' -Operation 3: insert 'g' at end -END - ["sunday monday" => <<END]) { -2 -Operation 1: replace 's' with 'm' -Operation 2: replace 'u' with 'o' -END - my($in, $out) = @$_; - - is capture( "perl perl/ch-2.pl $in"), $out; - is capture( "$LUA lua/ch-2.lua $in"), $out; - is capture( "gforth forth/ch-2.fs $in"), $out; - is capture("python python/ch-2.py $in"), $out; - is capture( "c/ch-2 $in"), $out; - is capture( "cpp/ch-2 $in"), $out; - is capture( "basic/ch-2 $in"), $out; -} - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \r\t]*\n/\n/g; - return $out; -} - -sub run { - my($cmd) = @_; - ok 0==system($cmd), $cmd; -} +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-097/paulo-custodio/ada/ch_1.adb b/challenge-097/paulo-custodio/ada/ch_1.adb new file mode 100644 index 0000000000..af07de412d --- /dev/null +++ b/challenge-097/paulo-custodio/ada/ch_1.adb @@ -0,0 +1,49 @@ +-- Challenge 097 +-- +-- TASK #1 › Caesar Cipher +-- Submitted by: Mohammad S Anwar +-- You are given string $S containing alphabets A..Z only and a number $N. +-- +-- Write a script to encrypt the given string $S using Caesar Cipher with left +-- shift of size $N. +-- +-- Example +-- Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +-- Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" +-- +-- Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +-- Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW +-- +-- Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +-- Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD + +with Ada.Characters.Handling; use Ada.Characters.Handling; +with Ada.Command_Line; +with Ada.Text_IO; use Ada.Text_IO; + +procedure ch_1 is + -- command line arguments + package CL renames Ada.Command_Line; + + -- caeser cipher + function caeser(key : Integer; text : String) return String is + cipher : String := To_Upper(text); + cc : Integer; + begin + for i in cipher'First .. cipher'Last loop + cc := (Character'Pos(cipher(i)) - Character'Pos('A') + 26 - key) Mod 26; + cipher(i) := Character'Val(Character'Pos('A') + cc); + end loop; + return cipher; + end caeser; + + -- cipher key + key : Integer := 0; +begin + key := Integer'Value(CL.Argument(1)); + for i in 2 .. CL.Argument_Count loop + Put(caeser(key, CL.Argument(i))); + Put(" "); + end loop; + Put_Line(""); +end ch_1; diff --git a/challenge-097/paulo-custodio/ada/ch_2.adb b/challenge-097/paulo-custodio/ada/ch_2.adb new file mode 100644 index 0000000000..e6a0b163e4 --- /dev/null +++ b/challenge-097/paulo-custodio/ada/ch_2.adb @@ -0,0 +1,73 @@ +-- Challenge 097 +-- +-- TASK #2 › Binary Substings +-- Submitted by: Mohammad S Anwar +-- You are given a binary string $B and an integer $S. +-- +-- Write a script to split the binary string $B of size $S and then find the +-- minimum number of flips required to make it all the same. +-- +-- Example 1: +-- Input: $B = “101100101”, $S = 3 +-- Output: 1 +-- +-- Binary Substrings: +-- "101": 0 flip +-- "100": 1 flip to make it "101" +-- "101": 0 flip +-- Example 2: +-- Input $B = “10110111”, $S = 4 +-- Output: 2 +-- +-- Binary Substrings: +-- "1011": 0 flip +-- "0111": 2 flips to make it "1011" + +with Ada.Command_Line; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; +with Ada.Text_IO; use Ada.Text_IO; + +procedure ch_2 is + -- command line arguments + package CL renames Ada.Command_Line; + + -- integer formatting + package Integer_IO is new Ada.Text_IO.Integer_IO (Integer); + + -- compute number of flips between two strings + function str_flips(a, b : String) return Integer is + flips : Integer := 0; + begin + for i in a'First .. a'Last loop + if a(i)/=b(i) then + flips := flips + 1; + end if; + end loop; + return flips; + end str_flips; + + -- compute number of flips in a sequence + function bit_flips(bits : String; n : Integer) return Integer is + flips : Integer := 0; + a, b: String(1 .. n); + p : Integer; + begin + p := bits'First; + a := bits(p .. p+n-1); + for i in 1 .. (bits'Length / n) - 1 loop + p := bits'First + i*n; + b := bits(p .. p+n-1); + flips := flips + str_flips(a, b); + end loop; + return flips; + end bit_flips; + + -- variables + flips : Integer := 0; + bits : String := CL.Argument(1); + n : Integer := Integer'Value(CL.Argument(2)); +begin + flips := bit_flips(bits, n); + Integer_IO.Put(flips, 0); + Put_Line(""); +end ch_2; diff --git a/challenge-097/paulo-custodio/t/test-1.yaml b/challenge-097/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..d2045bfe96 --- /dev/null +++ b/challenge-097/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 3 THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG + input: + output: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD +- setup: + cleanup: + args: -3 QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD + input: + output: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG diff --git a/challenge-097/paulo-custodio/t/test-2.yaml b/challenge-097/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..442e50cbf4 --- /dev/null +++ b/challenge-097/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 101100101 3 + input: + output: 1 +- setup: + cleanup: + args: 10110111 4 + input: + output: 2 diff --git a/challenge-097/paulo-custodio/test.pl b/challenge-097/paulo-custodio/test.pl index d09b489e04..01ed2b83cd 100644 --- a/challenge-097/paulo-custodio/test.pl +++ b/challenge-097/paulo-custodio/test.pl @@ -3,62 +3,5 @@ use strict; use warnings; use 5.030; -use Test::More; -# hack so that output redirection works in msys -my $LUA = $^O eq "msys" ? "lua.exe" : "lua"; - -run("gcc c/ch-1.c -o c/ch-1"); -run("g++ cpp/ch-1.cpp -o cpp/ch-1"); -run("fbc basic/ch-1.bas -o basic/ch-1"); - -for (["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" - => "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD"]) { - my($in, $out) = @$_; - - is capture( "perl perl/ch-1.pl 3 $in "), "$out\n"; - is capture( "perl perl/ch-1.pl -3 $out"), "$in\n"; - is capture( "$LUA lua/ch-1.lua 3 $in "), "$out\n"; - is capture( "$LUA lua/ch-1.lua -3 $out"), "$in\n"; - is capture( "gforth forth/ch-1.fs 3 $in "), "$out\n"; - is capture( "gforth forth/ch-1.fs -3 $out"), "$in\n"; - is capture( "python python/ch-1.py 3 $in "), "$out\n"; - is capture( "python python/ch-1.py -3 $out"), "$in\n"; - is capture( "c/ch-1 3 $in "), "$out\n"; - is capture( "c/ch-1 -3 $out"), "$in\n"; - is capture( "cpp/ch-1 3 $in "), "$out\n"; - is capture( "cpp/ch-1 -3 $out"), "$in\n"; - is capture( "basic/ch-1 3 $in "), "$out\n"; - is capture( "basic/ch-1 -3 $out"), "$in\n"; -} - -run("gcc c/ch-2.c -o c/ch-2"); -run("g++ cpp/ch-2.cpp -o cpp/ch-2"); -run("fbc basic/ch-2.bas -o basic/ch-2"); - -for (["101100101 3" => "1"], - ["10110111 4" => "2"]) { - my($in, $out) = @$_; - - is capture( "perl perl/ch-2.pl $in"), "$out\n"; - is capture( "$LUA lua/ch-2.lua $in"), "$out\n"; - is capture( "gforth forth/ch-2.fs $in"), "$out\n"; - is capture("python python/ch-2.py $in"), "$out\n"; - is capture( "c/ch-2 $in"), "$out\n"; - is capture( "cpp/ch-2 $in"), "$out\n"; - is capture( "basic/ch-2 $in"), "$out\n"; -} - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \r\t]*\n/\n/g; - return $out; -} - -sub run { - my($cmd) = @_; - ok 0==system($cmd), $cmd; -} +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-098/paulo-custodio/ada/ch_2.adb b/challenge-098/paulo-custodio/ada/ch_2.adb index 7cf52f9fb0..a395fc6b76 100644 --- a/challenge-098/paulo-custodio/ada/ch_2.adb +++ b/challenge-098/paulo-custodio/ada/ch_2.adb @@ -85,13 +85,11 @@ procedure ch_2 is -- variables Nums : Vector; - I, N, Pos : Integer; + N, Pos : Integer; begin N := Integer'Value(CL.Argument(1)); - I := 2; - while I <= CL.Argument_Count loop + for I in 2 .. CL.Argument_Count loop Nums.Append(Integer'Value(CL.Argument(I))); - I := I + 1; end loop; Pos := search_insert(Nums, N); Put_Line(Trim(Integer'Image(Pos), Ada.Strings.Left)); diff --git a/challenge-098/paulo-custodio/test.pl b/challenge-098/paulo-custodio/test.pl index 6cc94723ba..01ed2b83cd 100755 --- a/challenge-098/paulo-custodio/test.pl +++ b/challenge-098/paulo-custodio/test.pl @@ -1,138 +1,7 @@ #!/usr/bin/perl -# run tests described in t/test-N.yaml - use strict; use warnings; use 5.030; -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', - c => 'c', - cpp => 'cpp', - forth => 'fs', - lua => 'lua', - perl => 'pl', - python => 'py', -); - -# filter tests if languages given on command line -our %TESTS; -if (!@ARGV) { - %TESTS = %LANG; -} -else { - $TESTS{$_}=1 for @ARGV; -} - -for my $lang (grep {-d} sort keys %LANG) { - next unless $TESTS{$lang}; - for my $prog (path($lang)->children(qr/\.$LANG{$lang}$/)) { - $prog->basename =~ /^ch[-_](.*)\.$LANG{$lang}$/ or die $prog; - my $task = $1; - - # compile if needed - my $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 $spec->{setup}; - $@ and die $@; - - # build test command line - my $cmd = "$exec ".($spec->{args} // ""); - chomp($cmd); - if($spec->{input}) { - path("in.txt")->spew($spec->{input}); - $cmd .= " < in.txt"; - } - if ($spec->{output}) { - path("out_exp.txt")->spew($spec->{output}); - $cmd .= " > out.txt"; - } - - # run test - run($cmd); - - # compare output - if ($spec->{output}) { - run("diff -w out_exp.txt out.txt"); - } - - # run cleaup code - if (Test::More->builder->is_passing) { - ok eval($spec->{cleanup}), $spec->{cleanup} if $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 (/^c$/) { - run("gcc $prog -o $prog_wo_ext") if (!-f $exe || -M $exe > -M $prog); - return $exe; - } - if (/cpp/) { - run("g++ $prog -o $prog_wo_ext") if (!-f $exe || -M $exe > -M $prog); - return $exe; - } - if (/forth/) { - return "gforth $prog"; - } - if (/lua/) { - return "$LUA $prog"; - } - if (/perl/) { - return "perl $prog"; - } - if (/python/) { - return "python $prog"; - } - die "unsupported language $lang"; - } -} -sub run { - my($cmd) = @_; - ok 0==system($cmd), $cmd; -} +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-099/paulo-custodio/ada/ch_1.adb b/challenge-099/paulo-custodio/ada/ch_1.adb index fd79db2f4c..925660d9d7 100644 --- a/challenge-099/paulo-custodio/ada/ch_1.adb +++ b/challenge-099/paulo-custodio/ada/ch_1.adb @@ -1,5 +1,3 @@ --- Challenge 099 --- -- TASK #1 › Pattern Match -- Submitted by: Mohammad S Anwar -- You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/ada/ch_2.adb b/challenge-099/paulo-custodio/ada/ch_2.adb index e05007fb0e..9fd9d54077 100644 --- a/challenge-099/paulo-custodio/ada/ch_2.adb +++ b/challenge-099/paulo-custodio/ada/ch_2.adb @@ -1,5 +1,3 @@ --- Challenge 099 --- -- TASK #2 › Unique Sub-sequence -- Submitted by : Mohammad S Anwar -- You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/awk/ch-1.awk b/challenge-099/paulo-custodio/awk/ch-1.awk index 57a0b663f3..e090e3fe64 100644 --- a/challenge-099/paulo-custodio/awk/ch-1.awk +++ b/challenge-099/paulo-custodio/awk/ch-1.awk @@ -1,7 +1,5 @@ #!/usr/bin/gawk -# Challenge 099 -# # TASK #1 > Pattern Match # Submitted by: Mohammad S Anwar # You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/awk/ch-2.awk b/challenge-099/paulo-custodio/awk/ch-2.awk index abf6668f74..8fd50fa366 100644 --- a/challenge-099/paulo-custodio/awk/ch-2.awk +++ b/challenge-099/paulo-custodio/awk/ch-2.awk @@ -1,7 +1,5 @@ #!/usr/bin/gawk -# Challenge 099 -# # TASK #2 > Unique Subsequence # Submitted by: Mohammad S Anwar # You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/basic/ch-1.bas b/challenge-099/paulo-custodio/basic/ch-1.bas index 1cc64aee12..255f3147ce 100644 --- a/challenge-099/paulo-custodio/basic/ch-1.bas +++ b/challenge-099/paulo-custodio/basic/ch-1.bas @@ -1,5 +1,3 @@ -' Challenge 099 -' ' TASK #1 › Pattern Match ' Submitted by: Mohammad S Anwar ' You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/basic/ch-2.bas b/challenge-099/paulo-custodio/basic/ch-2.bas index ac658b84b3..b2733f8867 100644 --- a/challenge-099/paulo-custodio/basic/ch-2.bas +++ b/challenge-099/paulo-custodio/basic/ch-2.bas @@ -1,5 +1,3 @@ -' Challenge 099 -' ' TASK #2 › Unique Sub-sequence ' Submitted by : Mohammad S Anwar ' You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/c/ch-1.c b/challenge-099/paulo-custodio/c/ch-1.c index 82c1b4edb7..a22c7d91e2 100644 --- a/challenge-099/paulo-custodio/c/ch-1.c +++ b/challenge-099/paulo-custodio/c/ch-1.c @@ -1,6 +1,4 @@ /* -Challenge 099 - TASK #1 › Pattern Match Submitted by: Mohammad S Anwar You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/c/ch-2.c b/challenge-099/paulo-custodio/c/ch-2.c index 3b75674d56..5582d110a4 100644 --- a/challenge-099/paulo-custodio/c/ch-2.c +++ b/challenge-099/paulo-custodio/c/ch-2.c @@ -1,6 +1,4 @@ /* -Challenge 099 - TASK #2 › Unique Sub-sequence Submitted by : Mohammad S Anwar You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/cpp/ch-1.cpp b/challenge-099/paulo-custodio/cpp/ch-1.cpp index 67687d4582..5fe6d95de2 100644 --- a/challenge-099/paulo-custodio/cpp/ch-1.cpp +++ b/challenge-099/paulo-custodio/cpp/ch-1.cpp @@ -1,6 +1,4 @@ /* -Challenge 099 - TASK #1 › Pattern Match Submitted by: Mohammad S Anwar You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/cpp/ch-2.cpp b/challenge-099/paulo-custodio/cpp/ch-2.cpp index e553a5eaa2..cf0cfbd04a 100644 --- a/challenge-099/paulo-custodio/cpp/ch-2.cpp +++ b/challenge-099/paulo-custodio/cpp/ch-2.cpp @@ -1,6 +1,4 @@ /* -Challenge 099 - TASK #2 › Unique Sub-sequence Submitted by : Mohammad S Anwar You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/forth/ch-1.fs b/challenge-099/paulo-custodio/forth/ch-1.fs index 3dd302fad2..adcb5f14d7 100644 --- a/challenge-099/paulo-custodio/forth/ch-1.fs +++ b/ |
