From ad0e124c218efceb044dc3e9d7c7d03faa2be553 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 19 Feb 2021 20:02:07 +0000 Subject: Use new test script driven by yaml files --- challenge-096/paulo-custodio/t/test-1.yaml | 10 +++++ challenge-096/paulo-custodio/t/test-2.yaml | 17 ++++++++ challenge-096/paulo-custodio/test.pl | 66 +----------------------------- 3 files changed, 28 insertions(+), 65 deletions(-) create mode 100644 challenge-096/paulo-custodio/t/test-1.yaml create mode 100644 challenge-096/paulo-custodio/t/test-2.yaml 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" => < < Date: Fri, 19 Feb 2021 20:03:17 +0000 Subject: Add Ada solution to challenge 097 --- challenge-097/paulo-custodio/ada/ch_1.adb | 49 ++++++++++++++++++++ challenge-097/paulo-custodio/ada/ch_2.adb | 73 ++++++++++++++++++++++++++++++ challenge-097/paulo-custodio/t/test-1.yaml | 10 ++++ challenge-097/paulo-custodio/t/test-2.yaml | 10 ++++ challenge-097/paulo-custodio/test.pl | 59 +----------------------- 5 files changed, 143 insertions(+), 58 deletions(-) create mode 100644 challenge-097/paulo-custodio/ada/ch_1.adb create mode 100644 challenge-097/paulo-custodio/ada/ch_2.adb create mode 100644 challenge-097/paulo-custodio/t/test-1.yaml create mode 100644 challenge-097/paulo-custodio/t/test-2.yaml 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..ec7f2bd5f7 --- /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..d5e106e2c2 --- /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'; -- cgit From 7c1d91b40bea78621623e27b3a14fca8d0087a35 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 19 Feb 2021 20:05:04 +0000 Subject: Use a for instead of a while loop in Ada's solution of challenge 098 --- challenge-098/paulo-custodio/ada/ch_2.adb | 6 +- challenge-098/paulo-custodio/test.pl | 133 +----------------------------- 2 files changed, 3 insertions(+), 136 deletions(-) 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'; -- cgit From 329ed9f49bcfc398005fd80dc4988cf6014e6c4a Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 19 Feb 2021 20:07:25 +0000 Subject: Add Ada solution to challenge 100 --- challenge-100/paulo-custodio/ada/ch_1.adb | 122 ++++++++++++++++++++++++++++ challenge-100/paulo-custodio/ada/ch_2.adb | 129 ++++++++++++++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 challenge-100/paulo-custodio/ada/ch_1.adb create mode 100644 challenge-100/paulo-custodio/ada/ch_2.adb diff --git a/challenge-100/paulo-custodio/ada/ch_1.adb b/challenge-100/paulo-custodio/ada/ch_1.adb new file mode 100644 index 0000000000..5813929b91 --- /dev/null +++ b/challenge-100/paulo-custodio/ada/ch_1.adb @@ -0,0 +1,122 @@ +-- TASK #1 > Fun Time +-- Submitted by: Mohammad S Anwar +-- You are given a time (12 hour / 24 hour). +-- +-- Write a script to convert the given time from 12 hour format to 24 hour format +-- and vice versa. +-- +-- Ideally we expect a one-liner. +-- +-- Example 1: +-- Input: 05:15 pm or 05:15pm +-- Output: 17:15 +-- Example 2: +-- Input: 19:15 +-- Output: 07:15 pm or 07:15pm + +with Ada.Characters.Handling; use Ada.Characters.Handling; +with Ada.Command_Line; +with Ada.Strings.Bounded; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; +with Ada.Text_IO; use Ada.Text_IO; use Ada; + +procedure ch_1 is + -- command line arguments + package CL renames Ada.Command_Line; + + -- strings + package SB is new Ada.Strings.Bounded.Generic_Bounded_Length(Max => 100); + use SB; + + -- integer formatting + package Integer_IO is new Ada.Text_IO.Integer_IO (Integer); + + -- parse time + procedure parse_time(text : String; + hour, minute : out Integer; + am, pm: out Boolean) is + p_colon, p_am, p_pm, p_end_minute : Integer := 0; + begin + -- find bounds + p_colon := Index(text, ":"); + + p_am := Index(To_Lower(text), "am"); + if p_am > p_colon then + am := True; + else + am := False; + end if; + + p_pm := Index(To_Lower(text), "pm"); + if p_pm > p_colon then + pm := True; + else + pm := False; + end if; + + p_end_minute := text'Last; + if p_am > p_colon then + p_end_minute := Integer'Min(p_end_minute, p_am-1); + elsif p_pm > p_colon then + p_end_minute := Integer'Min(p_end_minute, p_pm-1); + end if; + + -- parse time + hour := Integer'Value(text(text'First .. p_colon-1)); + minute := Integer'Value(text(p_colon+1 .. p_end_minute)); + + -- convert 12->24 + if am then + if hour = 12 then + hour := 0; + end if; + elsif pm then + if hour /= 12 then + hour := hour + 12; + end if; + end if; + end parse_time; + + -- output hh:mm + procedure print_hhmm(hour, minute: Integer) is + begin + if hour < 10 then + Integer_IO.Put(0, 0); + end if; + Integer_IO.Put(hour, 0); + Text_IO.Put(":"); + if minute < 10 then + Integer_IO.Put(0, 0); + end if; + Integer_IO.Put(minute, 0); + end print_hhmm; + + -- convert time + procedure convert_time(text: String) is + hour, minute : Integer := 0; + am, pm : Boolean := False; + ampm : String := "am"; + begin + parse_time(text, hour, minute, am, pm); + + if not (am or pm) then + -- convert 24->12 + if hour = 0 then + hour := 12; + elsif hour = 12 then + ampm := "pm"; + elsif hour > 12 then + hour := hour - 12; + ampm := "pm"; + end if; + + print_hhmm(hour, minute); + Text_IO.Put(ampm); + else + print_hhmm(hour, minute); + end if; + end convert_time; + +begin + convert_time(CL.Argument(1)); +end ch_1; diff --git a/challenge-100/paulo-custodio/ada/ch_2.adb b/challenge-100/paulo-custodio/ada/ch_2.adb new file mode 100644 index 0000000000..333de0b777 --- /dev/null +++ b/challenge-100/paulo-custodio/ada/ch_2.adb @@ -0,0 +1,129 @@ +-- TASK #2 > Triangle Sum +-- Submitted by: Mohammad S Anwar +-- You are given triangle array. +-- +-- Write a script to find the minimum path sum from top to bottom. +-- +-- When you are on index i on the current row then you may move to either +-- index i or index i + 1 on the next row. +-- +-- Example 1: +-- Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ] +-- Output: 8 +-- +-- Explanation: The given triangle +-- +-- 1 +-- 2 4 +-- 6 4 9 +-- 5 1 7 2 +-- +-- The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 +-- +-- [1] +-- [2] 4 +-- 6 [4] 9 +-- 5 [1] 7 2 +-- Example 2: +-- Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ] +-- Output: 7 +-- +-- Explanation: The given triangle +-- +-- 3 +-- 3 1 +-- 5 2 3 +-- 4 3 1 3 +-- +-- The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 +-- +-- [3] +-- 3 [1] +-- 5 [2] 3 +-- 4 3 [1] 3 + +with Ada.Command_Line; +with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; +with Ada.Text_IO; use Ada.Text_IO; +use Ada; + +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); + + -- triangle data + Max_Rows : constant Integer := 20; + triangle : array(1 .. Max_Rows, 1 .. Max_Rows) of Integer; + rows : Integer := 0; + + -- parse number + function parse_number(text : in out String) return Integer is + i : Integer; + value : Integer := 0; + begin + -- skip non-digits + i := text'First; + while text(i) < '0' or text(i) > '9' loop + i := i + 1; + end loop; + + -- find end of digits and parse value + while i <= text'Last loop + if text(i) >= '0' and text(i) <= '9' then + if i = text'Last then + value := Integer'Value(text); + Delete(text, text'First, text'Last); + exit; + else + i := i + 1; + end if; + else + value := Integer'Value(text(text'First .. i-1)); + Delete(text, Text'First, i); + exit; + end if; + end loop; + + return value; + end parse_number; + + -- add row + procedure add_row(row : Integer; text1 : String) is + n : Integer; + text : String := text1; + begin + rows := row; + for col in 1 .. row loop + n := parse_number(text); + triangle(row, col) := n; + end loop; + end add_row; + + -- compute minimum sum + function min_sum return Integer is + function min_sum_1(sum, row, col : Integer) return Integer is + sum0, sum1, sum2 : Integer; + begin + sum0 := sum + triangle(row, col); + if row = rows then + return sum0; + else + sum1 := min_sum_1(sum0, row+1, col); + sum2 := min_sum_1(sum0, row+1, col+1); + return Integer'Min(sum1, sum2); + end if; + end min_sum_1; + begin + return min_sum_1(0, 1, 1); + end min_sum; + +begin + for i in 1 .. CL.Argument_Count loop + add_row(i, CL.Argument(I)); + end loop; + Integer_IO.Put(min_sum, 0); +end ch_2; -- cgit From 86f1e20620f3acc15e30b400119b9c9b8c161f97 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 19 Feb 2021 20:09:30 +0000 Subject: Add missing comment line --- challenge-099/paulo-custodio/ada/ch_1.adb | 2 -- challenge-099/paulo-custodio/ada/ch_2.adb | 2 -- challenge-099/paulo-custodio/awk/ch-1.awk | 2 -- challenge-099/paulo-custodio/awk/ch-2.awk | 2 -- challenge-099/paulo-custodio/basic/ch-1.bas | 2 -- challenge-099/paulo-custodio/basic/ch-2.bas | 2 -- challenge-099/paulo-custodio/c/ch-1.c | 2 -- challenge-099/paulo-custodio/c/ch-2.c | 2 -- challenge-099/paulo-custodio/cpp/ch-1.cpp | 2 -- challenge-099/paulo-custodio/cpp/ch-2.cpp | 2 -- challenge-099/paulo-custodio/forth/ch-1.fs | 2 -- challenge-099/paulo-custodio/forth/ch-2.fs | 2 -- challenge-099/paulo-custodio/lua/ch-1.lua | 2 -- challenge-099/paulo-custodio/lua/ch-2.lua | 2 -- challenge-099/paulo-custodio/perl/ch-1.pl | 2 -- challenge-099/paulo-custodio/perl/ch-2.pl | 2 -- challenge-099/paulo-custodio/python/ch-1.py | 2 -- challenge-099/paulo-custodio/python/ch-2.py | 2 -- challenge-100/paulo-custodio/awk/ch-1.awk | 2 -- challenge-100/paulo-custodio/awk/ch-2.awk | 2 -- challenge-100/paulo-custodio/c/ch-1.c | 2 -- challenge-100/paulo-custodio/c/ch-2.c | 2 -- challenge-100/paulo-custodio/cpp/ch-1.cpp | 2 -- challenge-100/paulo-custodio/cpp/ch-2.cpp | 2 -- challenge-100/paulo-custodio/forth/ch-1.fs | 2 -- challenge-100/paulo-custodio/forth/ch-2.fs | 2 -- challenge-100/paulo-custodio/perl/ch-1.pl | 2 -- challenge-100/paulo-custodio/perl/ch-2.pl | 2 -- challenge-100/paulo-custodio/python/ch-1.py | 2 -- challenge-100/paulo-custodio/python/ch-2.py | 2 -- 30 files changed, 60 deletions(-) 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/challenge-099/paulo-custodio/forth/ch-1.fs @@ -1,7 +1,5 @@ #! /usr/bin/env gforth -\ 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/forth/ch-2.fs b/challenge-099/paulo-custodio/forth/ch-2.fs index 26ff1a758d..7d1e47eead 100644 --- a/challenge-099/paulo-custodio/forth/ch-2.fs +++ b/challenge-099/paulo-custodio/forth/ch-2.fs @@ -1,7 +1,5 @@ #! /usr/bin/env gforth -\ 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/lua/ch-1.lua b/challenge-099/paulo-custodio/lua/ch-1.lua index 989dd87715..225beeb34d 100644 --- a/challenge-099/paulo-custodio/lua/ch-1.lua +++ b/challenge-099/paulo-custodio/lua/ch-1.lua @@ -1,8 +1,6 @@ #!/usr/bin/env lua --[[ -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/lua/ch-2.lua b/challenge-099/paulo-custodio/lua/ch-2.lua index 9c52b32a2e..d74eb53626 100644 --- a/challenge-099/paulo-custodio/lua/ch-2.lua +++ b/challenge-099/paulo-custodio/lua/ch-2.lua @@ -1,8 +1,6 @@ #!/usr/bin/env lua --[[ -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/perl/ch-1.pl b/challenge-099/paulo-custodio/perl/ch-1.pl index 71b3a7ea93..effafe2108 100644 --- a/challenge-099/paulo-custodio/perl/ch-1.pl +++ b/challenge-099/paulo-custodio/perl/ch-1.pl @@ -1,7 +1,5 @@ #!/usr/bin/perl -# 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/perl/ch-2.pl b/challenge-099/paulo-custodio/perl/ch-2.pl index d13a90524f..d0a8c71867 100644 --- a/challenge-099/paulo-custodio/perl/ch-2.pl +++ b/challenge-099/paulo-custodio/perl/ch-2.pl @@ -1,7 +1,5 @@ #!/usr/bin/perl -# 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/python/ch-1.py b/challenge-099/paulo-custodio/python/ch-1.py index 930c9dc328..60295131f4 100644 --- a/challenge-099/paulo-custodio/python/ch-1.py +++ b/challenge-099/paulo-custodio/python/ch-1.py @@ -1,7 +1,5 @@ #!/usr/bin/env python -# 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/python/ch-2.py b/challenge-099/paulo-custodio/python/ch-2.py index e82e57f635..e065b17674 100644 --- a/challenge-099/paulo-custodio/python/ch-2.py +++ b/challenge-099/paulo-custodio/python/ch-2.py @@ -1,7 +1,5 @@ #!/usr/bin/env python -# Challenge 099 -# # TASK #2 > Unique Sub-sequence # Submitted by : Mohammad S Anwar # You are given two strings $S and $T. diff --git a/challenge-100/paulo-custodio/awk/ch-1.awk b/challenge-100/paulo-custodio/awk/ch-1.awk index 07cba14123..03d750b366 100644 --- a/challenge-100/paulo-custodio/awk/ch-1.awk +++ b/challenge-100/paulo-custodio/awk/ch-1.awk @@ -1,7 +1,5 @@ #!/usr/bin/gawk -# Challenge 100 -# # TASK #1 > Fun Time # Submitted by: Mohammad S Anwar # You are given a time (12 hour / 24 hour). diff --git a/challenge-100/paulo-custodio/awk/ch-2.awk b/challenge-100/paulo-custodio/awk/ch-2.awk index aefd2cf858..d315475853 100644 --- a/challenge-100/paulo-custodio/awk/ch-2.awk +++ b/challenge-100/paulo-custodio/awk/ch-2.awk @@ -1,7 +1,5 @@ #!/usr/bin/gawk -# Challenge 100 -# # TASK #2 > Triangle Sum # Submitted by: Mohammad S Anwar # You are given triangle array. diff --git a/challenge-100/paulo-custodio/c/ch-1.c b/challenge-100/paulo-custodio/c/ch-1.c index 9638b7d656..d0a33a1f36 100644 --- a/challenge-100/paulo-custodio/c/ch-1.c +++ b/challenge-100/paulo-custodio/c/ch-1.c @@ -1,6 +1,4 @@ /* -Challenge 100 - TASK #1 > Fun Time Submitted by: Mohammad S Anwar You are given a time (12 hour / 24 hour). diff --git a/challenge-100/paulo-custodio/c/ch-2.c b/challenge-100/paulo-custodio/c/ch-2.c index 6a772d3a01..1d68e1d2d6 100644 --- a/challenge-100/paulo-custodio/c/ch-2.c +++ b/challenge-100/paulo-custodio/c/ch-2.c @@ -1,6 +1,4 @@ /* -Challenge 100 - TASK #2 > Triangle Sum Submitted by: Mohammad S Anwar You are given triangle array. diff --git a/challenge-100/paulo-custodio/cpp/ch-1.cpp b/challenge-100/paulo-custodio/cpp/ch-1.cpp index 9f4bcbd5e5..389d36f9c9 100644 --- a/challenge-100/paulo-custodio/cpp/ch-1.cpp +++ b/challenge-100/paulo-custodio/cpp/ch-1.cpp @@ -1,6 +1,4 @@ /* -Challenge 100 - TASK #1 > Fun Time Submitted by: Mohammad S Anwar You are given a time (12 hour / 24 hour). diff --git a/challenge-100/paulo-custodio/cpp/ch-2.cpp b/challenge-100/paulo-custodio/cpp/ch-2.cpp index c94b0355a3..b672a5cffe 100644 --- a/challenge-100/paulo-custodio/cpp/ch-2.cpp +++ b/challenge-100/paulo-custodio/cpp/ch-2.cpp @@ -1,6 +1,4 @@ /* -Challenge 100 - TASK #2 > Triangle Sum Submitted by: Mohammad S Anwar You are given triangle array. diff --git a/challenge-100/paulo-custodio/forth/ch-1.fs b/challenge-100/paulo-custodio/forth/ch-1.fs index fe0e8f1e76..192b0dc2a5 100644 --- a/challenge-100/paulo-custodio/forth/ch-1.fs +++ b/challenge-100/paulo-custodio/forth/ch-1.fs @@ -1,7 +1,5 @@ #! /usr/bin/env gforth -\ Challenge 100 -\ \ TASK #1 > Fun Time \ Submitted by: Mohammad S Anwar \ You are given a time (12 hour / 24 hour). diff --git a/challenge-100/paulo-custodio/forth/ch-2.fs b/challenge-100/paulo-custodio/forth/ch-2.fs index 54939601a9..35156151a7 100644 --- a/challenge-100/paulo-custodio/forth/ch-2.fs +++ b/challenge-100/paulo-custodio/forth/ch-2.fs @@ -1,7 +1,5 @@ #! /usr/bin/env gforth -\ Challenge 100 -\ \ TASK #2 > Triangle Sum \ Submitted by: Mohammad S Anwar \ You are given triangle array. diff --git a/challenge-100/paulo-custodio/perl/ch-1.pl b/challenge-100/paulo-custodio/perl/ch-1.pl index cb8bd9c9aa..58ffb22647 100644 --- a/challenge-100/paulo-custodio/perl/ch-1.pl +++ b/challenge-100/paulo-custodio/perl/ch-1.pl @@ -1,7 +1,5 @@ #!/usr/bin/perl -# Challenge 100 -# # TASK #1 > Fun Time # Submitted by: Mohammad S Anwar # You are given a time (12 hour / 24 hour). diff --git a/challenge-100/paulo-custodio/perl/ch-2.pl b/challenge-100/paulo-custodio/perl/ch-2.pl index 2264dcae93..a75afbffc8 100644 --- a/challenge-100/paulo-custodio/perl/ch-2.pl +++ b/challenge-100/paulo-custodio/perl/ch-2.pl @@ -1,7 +1,5 @@ #!/usr/bin/perl -# Challenge 100 -# # TASK #2 > Triangle Sum # Submitted by: Mohammad S Anwar # You are given triangle array. diff --git a/challenge-100/paulo-custodio/python/ch-1.py b/challenge-100/paulo-custodio/python/ch-1.py index 12fad2210b..863b39f2ba 100644 --- a/challenge-100/paulo-custodio/python/ch-1.py +++ b/challenge-100/paulo-custodio/python/ch-1.py @@ -1,7 +1,5 @@ #!/usr/bin/env python -# Challenge 100 -# # TASK #1 > Fun Time # Submitted by: Mohammad S Anwar # You are given a time (12 hour / 24 hour). diff --git a/challenge-100/paulo-custodio/python/ch-2.py b/challenge-100/paulo-custodio/python/ch-2.py index 44a2c9f077..d75c2540c9 100644 --- a/challenge-100/paulo-custodio/python/ch-2.py +++ b/challenge-100/paulo-custodio/python/ch-2.py @@ -1,7 +1,5 @@ #!/usr/bin/env python -# Challenge 100 -# # TASK #2 > Triangle Sum # Submitted by: Mohammad S Anwar # You are given triangle array. -- cgit From 798c5692eb4c43474a1ba230bb93f9fdfc3fd311 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 19 Feb 2021 20:10:38 +0000 Subject: Remove tabs --- challenge-097/paulo-custodio/ada/ch_1.adb | 50 ++++---- challenge-097/paulo-custodio/ada/ch_2.adb | 86 ++++++------- challenge-100/paulo-custodio/ada/ch_1.adb | 186 ++++++++++++++-------------- challenge-100/paulo-custodio/ada/ch_2.adb | 162 ++++++++++++------------ challenge-100/paulo-custodio/awk/ch-1.awk | 72 +++++------ challenge-100/paulo-custodio/awk/ch-2.awk | 64 +++++----- challenge-100/paulo-custodio/basic/ch-1.bas | 72 +++++------ challenge-100/paulo-custodio/basic/ch-2.bas | 80 ++++++------ challenge-100/paulo-custodio/cpp/ch-1.cpp | 2 +- challenge-100/paulo-custodio/forth/ch-1.fs | 14 +-- challenge-100/paulo-custodio/forth/ch-2.fs | 28 ++--- challenge-100/paulo-custodio/lua/ch-1.lua | 62 +++++----- challenge-100/paulo-custodio/lua/ch-2.lua | 46 +++---- challenge-100/paulo-custodio/perl/ch-1.pl | 6 +- challenge-100/paulo-custodio/perl/ch-2.pl | 24 ++-- challenge-100/paulo-custodio/python/ch-1.py | 14 +-- challenge-100/paulo-custodio/python/ch-2.py | 52 ++++---- 17 files changed, 510 insertions(+), 510 deletions(-) diff --git a/challenge-097/paulo-custodio/ada/ch_1.adb b/challenge-097/paulo-custodio/ada/ch_1.adb index ec7f2bd5f7..af07de412d 100644 --- a/challenge-097/paulo-custodio/ada/ch_1.adb +++ b/challenge-097/paulo-custodio/ada/ch_1.adb @@ -1,19 +1,19 @@ -- 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 @@ -25,25 +25,25 @@ 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; + -- 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(""); + 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 index d5e106e2c2..e6a0b163e4 100644 --- a/challenge-097/paulo-custodio/ada/ch_2.adb +++ b/challenge-097/paulo-custodio/ada/ch_2.adb @@ -1,16 +1,16 @@ -- 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" @@ -18,7 +18,7 @@ -- Example 2: -- Input $B = “10110111”, $S = 4 -- Output: 2 --- +-- -- Binary Substrings: -- "1011": 0 flip -- "0111": 2 flips to make it "1011" @@ -31,43 +31,43 @@ 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)); + -- 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(""); + flips := bit_flips(bits, n); + Integer_IO.Put(flips, 0); + Put_Line(""); end ch_2; diff --git a/challenge-100/paulo-custodio/ada/ch_1.adb b/challenge-100/paulo-custodio/ada/ch_1.adb index 5813929b91..e5f362c639 100644 --- a/challenge-100/paulo-custodio/ada/ch_1.adb +++ b/challenge-100/paulo-custodio/ada/ch_1.adb @@ -1,12 +1,12 @@ -- TASK #1 > Fun Time -- Submitted by: Mohammad S Anwar -- You are given a time (12 hour / 24 hour). --- +-- -- Write a script to convert the given time from 12 hour format to 24 hour format -- and vice versa. --- +-- -- Ideally we expect a one-liner. --- +-- -- Example 1: -- Input: 05:15 pm or 05:15pm -- Output: 17:15 @@ -17,7 +17,7 @@ with Ada.Characters.Handling; use Ada.Characters.Handling; with Ada.Command_Line; with Ada.Strings.Bounded; -with Ada.Strings.Fixed; use Ada.Strings.Fixed; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; with Ada.Text_IO; use Ada.Text_IO; use Ada; procedure ch_1 is @@ -28,95 +28,95 @@ procedure ch_1 is package SB is new Ada.Strings.Bounded.Generic_Bounded_Length(Max => 100); use SB; - -- integer formatting - package Integer_IO is new Ada.Text_IO.Integer_IO (Integer); - - -- parse time - procedure parse_time(text : String; - hour, minute : out Integer; - am, pm: out Boolean) is - p_colon, p_am, p_pm, p_end_minute : Integer := 0; - begin - -- find bounds - p_colon := Index(text, ":"); - - p_am := Index(To_Lower(text), "am"); - if p_am > p_colon then - am := True; - else - am := False; - end if; - - p_pm := Index(To_Lower(text), "pm"); - if p_pm > p_colon then - pm := True; - else - pm := False; - end if; - - p_end_minute := text'Last; - if p_am > p_colon then - p_end_minute := Integer'Min(p_end_minute, p_am-1); - elsif p_pm > p_colon then - p_end_minute := Integer'Min(p_end_minute, p_pm-1); - end if; - - -- parse time - hour := Integer'Value(text(text'First .. p_colon-1)); - minute := Integer'Value(text(p_colon+1 .. p_end_minute)); - - -- convert 12->24 - if am then - if hour = 12 then - hour := 0; - end if; - elsif pm then - if hour /= 12 then - hour := hour + 12; - end if; - end if; - end parse_time; - - -- output hh:mm - procedure print_hhmm(hour, minute: Integer) is - begin - if hour < 10 then - Integer_IO.Put(0, 0); - end if; - Integer_IO.Put(hour, 0); - Text_IO.Put(":"); - if minute < 10 then - Integer_IO.Put(0, 0); - end if; - Integer_IO.Put(minute, 0); - end print_hhmm; - - -- convert time - procedure convert_time(text: String) is - hour, minute : Integer := 0; - am, pm : Boolean := False; - ampm : String := "am"; - begin - parse_time(text, hour, minute, am, pm); + -- integer formatting + package Integer_IO is new Ada.Text_IO.Integer_IO (Integer); + + -- parse time + procedure parse_time(text : String; + hour, minute : out Integer; + am, pm: out Boolean) is + p_colon, p_am, p_pm, p_end_minute : Integer := 0; + begin + -- find bounds + p_colon := Index(text, ":"); + + p_am := Index(To_Lower(text), "am"); + if p_am > p_colon then + am := True; + else + am := False; + end if; + + p_pm := Index(To_Lower(text), "pm"); + if p_pm > p_colon then + pm := True; + else + pm := False; + end if; + + p_end_minute := text'Last; + if p_am > p_colon then + p_end_minute := Integer'Min(p_end_minute, p_am-1); + elsif p_pm > p_colon then + p_end_minute := Integer'Min(p_end_minute, p_pm-1); + end if; + + -- parse time + hour := Integer'Value(text(text'First .. p_colon-1)); + minute := Integer'Value(text(p_colon+1 .. p_end_minute)); + + -- convert 12->24 + if am then + if hour = 12 then + hour := 0; + end if; + elsif pm then + if hour /= 12 then + hour := hour + 12; + end if; + end if; + end parse_time; + + -- output hh:mm + procedure print_hhmm(hour, minute: Integer) is + begin + if hour < 10 then + Integer_IO.Put(0, 0); + end if; + Integer_IO.Put(hour, 0); + Text_IO.Put(":"); + if minute < 10 then + Integer_IO.Put(0, 0); + end if; + Integer_IO.Put(minute, 0); + end print_hhmm; + + -- convert time + procedure convert_time(text: String) is + hour, minute : Integer := 0; + am, pm : Boolean := False; + ampm : String := "am"; + begin + parse_time(text, hour, minute, am, pm); + + if not (am or pm) then + -- convert 24->12 + if hour = 0 then + hour := 12; + elsif hour = 12 then + ampm := "pm"; + elsif hour > 12 then + hour := hour - 12; + ampm := "pm"; + end if; + + print_hhmm(hour, minute); + Text_IO.Put(ampm); + else + print_hhmm(hour, minute); + end if; + end convert_time; - if not (am or pm) then - -- convert 24->12 - if hour = 0 then - hour := 12; - elsif hour = 12 then - ampm := "pm"; - elsif hour > 12 then - hour := hour - 12; - ampm := "pm"; - end if; - - print_hhmm(hour, minute); - Text_IO.Put(ampm); - else - print_hhmm(hour, minute); - end if; - end convert_time; - begin - convert_time(CL.Argument(1)); + convert_time(CL.Argument(1)); end ch_1; diff --git a/challenge-100/paulo-custodio/ada/ch_2.adb b/challenge-100/paulo-custodio/ada/ch_2.adb index 333de0b777..36d95ef473 100644 --- a/challenge-100/paulo-custodio/ada/ch_2.adb +++ b/challenge-100/paulo-custodio/ada/ch_2.adb @@ -1,25 +1,25 @@ -- TASK #2 > Triangle Sum -- Submitted by: Mohammad S Anwar -- You are given triangle array. --- +-- -- Write a script to find the minimum path sum from top to bottom. --- --- When you are on index i on the current row then you may move to either +-- +-- When you are on index i on the current row then you may move to either -- index i or index i + 1 on the next row. --- +-- -- Example 1: -- Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ] -- Output: 8 --- +-- -- Explanation: The given triangle --- +-- -- 1 -- 2 4 -- 6 4 9 -- 5 1 7 2 --- +-- -- The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 --- +-- -- [1] -- [2] 4 -- 6 [4] 9 @@ -27,16 +27,16 @@ -- Example 2: -- Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ] -- Output: 7 --- +-- -- Explanation: The given triangle --- +-- -- 3 -- 3 1 -- 5 2 3 -- 4 3 1 3 --- +-- -- The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 --- +-- -- [3] -- 3 [1] -- 5 [2] 3 @@ -44,86 +44,86 @@ with Ada.Command_Line; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; -with Ada.Strings.Fixed; use Ada.Strings.Fixed; -with Ada.Text_IO; use Ada.Text_IO; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; +with Ada.Text_IO; use Ada.Text_IO; use Ada; 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); - - -- triangle data - Max_Rows : constant Integer := 20; - triangle : array(1 .. Max_Rows, 1 .. Max_Rows) of Integer; - rows : Integer := 0; - - -- parse number - function parse_number(text : in out String) return Integer is - i : Integer; - value : Integer := 0; - begin - -- skip non-digits - i := text'First; - while text(i) < '0' or text(i) > '9' loop - i := i + 1; - end loop; - - -- find end of digits and parse value - while i <= text'Last loop - if text(i) >= '0' and text(i) <= '9' then - if i = text'Last then - value := Integer'Value(text); - Delete(text, text'First, text'Last); - exit; - else - i := i + 1; - end if; - else - value := Integer'Value(text(text'First .. i-1)); - Delete(text, Text'First, i); - exit; - end if; - end loop; + -- integer formatting + package Integer_IO is new Ada.Text_IO.Integer_IO (Integer); - return value; - end parse_number; + -- triangle data + Max_Rows : constant Integer := 20; + triangle : array(1 .. Max_Rows, 1 .. Max_Rows) of Integer; + rows : Integer := 0; - -- add row - procedure add_row(row : Integer; text1 : String) is - n : Integer; - text : String := text1; - begin - rows := row; - for col in 1 .. row loop - n := parse_number(text); - triangle(row, col) := n; - end loop; - end add_row; + -- parse number + function parse_number(text : in out String) return Integer is + i : Integer; + value : Integer := 0; + begin + -- skip non-digits + i := text'First; + while text(i) < '0' or text(i) > '9' loop + i := i + 1; + end loop; - -- compute minimum sum - function min_sum return Integer is - function min_sum_1(sum, row, col : Integer) return Integer is - sum0, sum1, sum2 : Integer; - begin - sum0 := sum + triangle(row, col); - if row = rows then - return sum0; - else - sum1 := min_sum_1(sum0, row+1, col); - sum2 := min_sum_1(sum0, row+1, col+1); - return Integer'Min(sum1, sum2); - end if; - end min_sum_1; - begin - return min_sum_1(0, 1, 1); - end min_sum; + -- find end of digits and parse value + while i <= text'Last loop + if text(i) >= '0' and text(i) <= '9' then + if i = text'Last then + value := Integer'Value(text); + Delete(text, text'First, text'Last); + exit; + else + i := i + 1; + end if; + else + value := Integer'Value(text(text'First .. i-1)); + Delete(text, Text'First, i); + exit; + end if; + end loop; + + return value; + end parse_number; + + -- add row + procedure add_row(row : Integer; text1 : String) is + n : Integer; + text : String := text1; + begin + rows := row; + for col in 1 .. row loop + n := parse_number(text); + triangle(row, col) := n; + end loop; + end add_row; + + -- compute minimum sum + function min_sum return Integer is + function min_sum_1(sum, row, col : Integer) return Integer is + sum0, sum1, sum2 : Integer; + begin + sum0 := sum + triangle(row, col); + if row = rows then + return sum0; + else + sum1 := min_sum_1(sum0, row+1, col); + sum2 := min_sum_1(sum0, row+1, col+1); + return Integer'Min(sum1, sum2); + end if; + end min_sum_1; + begin + return min_sum_1(0, 1, 1); + end min_sum; begin for i in 1 .. CL.Argument_Count loop - add_row(i, CL.Argument(I)); + add_row(i, CL.Argument(I)); end loop; - Integer_IO.Put(min_sum, 0); + Integer_IO.Put(min_sum, 0); end ch_2; diff --git a/challenge-100/paulo-custodio/awk/ch-1.awk b/challenge-100/paulo-custodio/awk/ch-1.awk index 03d750b366..83c0caffe6 100644 --- a/challenge-100/paulo-custodio/awk/ch-1.awk +++ b/challenge-100/paulo-custodio/awk/ch-1.awk @@ -3,12 +3,12 @@ # TASK #1 > Fun Time # Submitted by: Mohammad S Anwar # You are given a time (12 hour / 24 hour). -# +# # Write a script to convert the given time from 12 hour format to 24 hour format # and vice versa. -# +# # Ideally we expect a one-liner. -# +# # Example 1: # Input: 05:15 pm or 05:15pm # Output: 17:15 @@ -17,39 +17,39 @@ # Output: 07:15 pm or 07:15pm function convert_time(text, hour, arr, pm) { - text = tolower(text) - - # get hour - match(text, /^([0-9]+)/, arr) - hour = arr[1] - if (text ~ /am|pm/) { - # 12->24 - if (text ~ /pm/) { - if (hour < 12) hour += 12; - } - else { /am/ - if (hour == 12) hour = 0; - } - gsub(/^[0-9]+/, "", text); - gsub(/ *(am|pm)/, "", text); - printf("%02d", hour); print text; - } - else { - # 24->12 - pm = 0; - if (hour == 0) hour = 12; - else if (hour == 12) pm = 1; - else if (hour > 12) { hour -= 12; pm = 1; } - gsub(/^[0-9]+/, "", text); - printf("%02d%s", hour, text); - if (pm) - print "pm"; - else - print "am"; - } + text = tolower(text) + + # get hour + match(text, /^([0-9]+)/, arr) + hour = arr[1] + if (text ~ /am|pm/) { + # 12->24 + if (text ~ /pm/) { + if (hour < 12) hour += 12; + } + else { /am/ + if (hour == 12) hour = 0; + } + gsub(/^[0-9]+/, "", text); + gsub(/ *(am|pm)/, "", text); + printf("%02d", hour); print text; + } + else { + # 24->12 + pm = 0; + if (hour == 0) hour = 12; + else if (hour == 12) pm = 1; + else if (hour > 12) { hour -= 12; pm = 1; } + gsub(/^[0-9]+/, "", text); + printf("%02d%s", hour, text); + if (pm) + print "pm"; + else + print "am"; + } } BEGIN { - convert_time(ARGV[1]); - exit 0; -} \ No newline at end of file + convert_time(ARGV[1]); + exit 0; +} diff --git a/challenge-100/paulo-custodio/awk/ch-2.awk b/challenge-100/paulo-custodio/awk/ch-2.awk index d315475853..77fac41676 100644 --- a/challenge-100/paulo-custodio/awk/ch-2.awk +++ b/challenge-100/paulo-custodio/awk/ch-2.awk @@ -3,25 +3,25 @@ # TASK #2 > Triangle Sum # Submitted by: Mohammad S Anwar # You are given triangle array. -# +# # Write a script to find the minimum path sum from top to bottom. -# -# When you are on index i on the current row then you may move to either +# +# When you are on index i on the current row then you may move to either # index i or index i + 1 on the next row. -# +# # Example 1: # Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ] # Output: 8 -# +# # Explanation: The given triangle -# +# # 1 # 2 4 # 6 4 9 # 5 1 7 2 -# +# # The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 -# +# # [1] # [2] 4 # 6 [4] 9 @@ -29,40 +29,40 @@ # Example 2: # Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ] # Output: 7 -# +# # Explanation: The given triangle -# +# # 3 # 3 1 # 5 2 3 # 4 3 1 3 -# +# # The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 -# +# # [3] # 3 [1] # 5 [2] 3 # 4 3 [1] 3 function add_row(row, text, i, arr) { - rows = row; - for (i = 1; i <= row; i++) { - gsub(/^[^0-9]*/, "", text); - match(text, /^([0-9]+)/, arr); - triangle[row][i] = arr[1]; - gsub(/^[0-9]*/, "", text); - } + rows = row; + for (i = 1; i <= row; i++) { + gsub(/^[^0-9]*/, "", text); + match(text, /^([0-9]+)/, arr); + triangle[row][i] = arr[1]; + gsub(/^[0-9]*/, "", text); + } } function min_sum_1(sum, row, col, sum1, sum2) { - sum += triangle[row][col]; - if (row == rows) - return sum; - else { - sum1 = min_sum_1(sum, row+1, col); - sum2 = min_sum_1(sum, row+1, col+1); - return (sum1 < sum2) ? sum1 : sum2; - } + sum += triangle[row][col]; + if (row == rows) + return sum; + else { + sum1 = min_sum_1(sum, row+1, col); + sum2 = min_sum_1(sum, row+1, col+1); + return (sum1 < sum2) ? sum1 : sum2; + } } function min_sum() { @@ -70,9 +70,9 @@ function min_sum() { } BEGIN { - rows = 0; - for (i = 1; i < ARGC; i++) - add_row(i, ARGV[i]); - print min_sum(); - exit 0; + rows = 0; + for (i = 1; i < ARGC; i++) + add_row(i, ARGV[i]); + print min_sum(); + exit 0; } diff --git a/challenge-100/paulo-custodio/basic/ch-1.bas b/challenge-100/paulo-custodio/basic/ch-1.bas index 7c2b970b32..35cc526535 100644 --- a/challenge-100/paulo-custodio/basic/ch-1.bas +++ b/challenge-100/paulo-custodio/basic/ch-1.bas @@ -1,14 +1,14 @@ ' Challenge 100 -' +' ' TASK #1 > Fun Time ' Submitted by: Mohammad S Anwar ' You are given a time (12 hour / 24 hour). -' +' ' Write a script to convert the given time from 12 hour format to 24 hour format ' and vice versa. -' +' ' Ideally we expect a one-liner. -' +' ' Example 1: ' Input: 05:15 pm or 05:15pm ' Output: 17:15 @@ -17,40 +17,40 @@ ' Output: 07:15 pm or 07:15pm function format_nn(n as integer) as string - format_nn = right("00" & trim(str(n)), 2) -end function + format_nn = right("00" & trim(str(n)), 2) +end function function convert_time(text as string) as string - dim hour as integer, minute as integer, am as boolean, pm as boolean - dim ampm as string - - text = lcase(text) - hour = val(text) - minute = val(mid(text, instr(text, ":")+1)) - if instr(text, "am") > 0 then am = true - if instr(text, "pm") > 0 then pm = true - if am or pm then - ' 12->24 - if pm then - if hour < 12 then hour = hour + 12 - else - if hour = 12 then hour = 0 - end if - convert_time = format_nn(hour) & ":" & format_nn(minute) - else - ' 24->12 - - ampm = "am" - if hour = 0 then - hour = 12 - elseif hour = 12 then - ampm = "pm" - elseif hour > 12 then - hour = hour - 12 - ampm = "pm" - end if - convert_time = format_nn(hour) & ":" & format_nn(minute) & ampm - end if + dim hour as integer, minute as integer, am as boolean, pm as boolean + dim ampm as string + + text = lcase(text) + hour = val(text) + minute = val(mid(text, instr(text, ":")+1)) + if instr(text, "am") > 0 then am = true + if instr(text, "pm") > 0 then pm = true + if am or pm then + ' 12->24 + if pm then + if hour < 12 then hour = hour + 12 + else + if hour = 12 then hour = 0 + end if + convert_time = format_nn(hour) & ":" & format_nn(minute) + else + ' 24->12 + + ampm = "am" + if hour = 0 then + hour = 12 + elseif hour = 12 then + ampm = "pm" + elseif hour > 12 then + hour = hour - 12 + ampm = "pm" + end if + convert_time = format_nn(hour) & ":" & format_nn(minute) & ampm + end if end function print convert_time(command(1)) diff --git a/challenge-100/paulo-custodio/basic/ch-2.bas b/challenge-100/paulo-custodio/basic/ch-2.bas index 07a9496a04..6ba0800802 100644 --- a/challenge-100/paulo-custodio/basic/ch-2.bas +++ b/challenge-100/paulo-custodio/basic/ch-2.bas @@ -1,25 +1,25 @@ ' TASK #2 > Triangle Sum ' Submitted by: Mohammad S Anwar ' You are given triangle array. -' +' ' Write a script to find the minimum path sum from top to bottom. -' -' When you are on index i on the current row then you may move to either +' +' When you are on index i on the current row then you may move to either ' index i or index i + 1 on the next row. -' +' ' Example 1: ' Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ] ' Output: 8 -' +' ' Explanation: The given triangle -' +' ' 1 ' 2 4 ' 6 4 9 ' 5 1 7 2 -' +' ' The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 -' +' ' [1] ' [2] 4 ' 6 [4] 9 @@ -27,16 +27,16 @@ ' Example 2: ' Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ] ' Output: 7 -' +' ' Explanation: The given triangle -' +' ' 3 ' 3 1 ' 5 2 3 ' 4 3 1 3 -' +' ' The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 -' +' ' [3] ' 3 [1] ' 5 [2] 3 @@ -47,45 +47,45 @@ const as integer MAX_ROWS = 20 dim shared rows as integer sub add_row(row as integer, text as string) - dim as integer i, n - rows = row - for i = 1 to row - do while left(text,1)<"0" or left(text,1)>"9" - text = mid(text,2) - loop - n = val(text) - do while left(text,1)>="0" and left(text,1)<="9" - text = mid(text,2) - loop - triangle(row, i) = n - next i + dim as integer i, n + rows = row + for i = 1 to row + do while left(text,1)<"0" or left(text,1)>"9" + text = mid(text,2) + loop + n = val(text) + do while left(text,1)>="0" and left(text,1)<="9" + text = mid(text,2) + loop + triangle(row, i) = n + next i end sub function min_sum_1(sum as integer, row as integer, col as integer) as integer - dim as integer sum1, sum2 - sum = sum + triangle(row, col) - if row = rows then - min_sum_1 = sum - else - sum1 = min_sum_1(sum, row+1, col) - sum2 = min_sum_1(sum, row+1, col+1) - if sum1 < sum2 then - min_sum_1 = sum1 - else - min_sum_1 = sum2 - end if - end if + dim as integer sum1, sum2 + sum = sum + triangle(row, col) + if row = rows then + min_sum_1 = sum + else + sum1 = min_sum_1(sum, row+1, col) + sum2 = min_sum_1(sum, row+1, col+1) +