diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-09 22:52:09 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-11 21:49:58 +0000 |
| commit | 6d58732cdd23a50220c754bdec2ddf3a505003e0 (patch) | |
| tree | 65e9128c19f583ffb363cfe8d6341050bdc8feaf | |
| parent | 45b61fa12dcdd1075ef4f23583384c15643c602f (diff) | |
| download | perlweeklychallenge-club-6d58732cdd23a50220c754bdec2ddf3a505003e0.tar.gz perlweeklychallenge-club-6d58732cdd23a50220c754bdec2ddf3a505003e0.tar.bz2 perlweeklychallenge-club-6d58732cdd23a50220c754bdec2ddf3a505003e0.zip | |
Add Ada solution to challenge 099
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | challenge-098/paulo-custodio/ada/ch_1.adb (renamed from challenge-098/paulo-custodio/ada/ch-1.adb) | 0 | ||||
| -rw-r--r-- | challenge-098/paulo-custodio/ada/ch_2.adb (renamed from challenge-098/paulo-custodio/ada/ch-2.adb) | 0 | ||||
| -rw-r--r-- | challenge-099/paulo-custodio/ada/ch_1.adb | 88 | ||||
| -rw-r--r-- | challenge-099/paulo-custodio/ada/ch_2.adb | 79 | ||||
| -rw-r--r-- | challenge-099/paulo-custodio/t/test-1.yaml | 3 | ||||
| -rw-r--r-- | challenge-099/paulo-custodio/t/test-2.yaml | 1 |
7 files changed, 168 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore index ab8c2f5f55..a70b9e5e03 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.class *.exe *.o +*.obj *.ali # Rust languageoutput directory diff --git a/challenge-098/paulo-custodio/ada/ch-1.adb b/challenge-098/paulo-custodio/ada/ch_1.adb index 47c6decd43..47c6decd43 100644 --- a/challenge-098/paulo-custodio/ada/ch-1.adb +++ b/challenge-098/paulo-custodio/ada/ch_1.adb diff --git a/challenge-098/paulo-custodio/ada/ch-2.adb b/challenge-098/paulo-custodio/ada/ch_2.adb index 7cf52f9fb0..7cf52f9fb0 100644 --- a/challenge-098/paulo-custodio/ada/ch-2.adb +++ b/challenge-098/paulo-custodio/ada/ch_2.adb diff --git a/challenge-099/paulo-custodio/ada/ch_1.adb b/challenge-099/paulo-custodio/ada/ch_1.adb new file mode 100644 index 0000000000..925660d9d7 --- /dev/null +++ b/challenge-099/paulo-custodio/ada/ch_1.adb @@ -0,0 +1,88 @@ +-- TASK #1 › Pattern Match +-- Submitted by: Mohammad S Anwar +-- You are given a string $S and a pattern $P. +-- +-- Write a script to check if given pattern validate the entire string. +-- Print 1 if pass otherwise 0. +-- +-- The patterns can also have the following characters: +-- +-- ? - Match any single character. +-- * - Match any sequence of characters. +-- Example 1: +-- Input: $S = "abcde" $P = "a*e" +-- Output: 1 +-- Example 2: +-- Input: $S = "abcde" $P = "a*d" +-- Output: 0 +-- Example 3: +-- Input: $S = "abcde" $P = "?b*d" +-- Output: 0 +-- Example 4: +-- Input: $S = "abcde" $P = "a*c?e" +-- Output: 1 + +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Command_Line; +with Ada.Strings.Bounded; + +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; + + -- match string and pattern + function match(t, p : SB.Bounded_String) return Boolean is + text : SB.Bounded_String := t; + pattern : SB.Bounded_String := p; + begin + loop + -- string and pattern finished + if text="" and pattern="" then + return True; + -- string or pattern finished + elsif text="" or pattern="" then + return False; + -- match any character + elsif SB.Head(pattern, 1)="?" then + SB.Delete(text, 1, 1); + SB.Delete(pattern, 1, 1); + -- match any sub-sequence + elsif SB.Head(pattern, 1)="*" then + Delete(pattern, 1, 1); + for i in 1 .. SB.Length(text) loop + if match(SB.Bounded_Slice(text, i, SB.Length(text)), + pattern) then + return True; + end if; + end loop; + return False; + -- chars differ + elsif SB.Head(text, 1)/=SB.Head(pattern, 1) then + return false; + -- check next char + else + SB.Delete(text, 1, 1); + SB.Delete(pattern, 1, 1); + end if; + end loop; + end match; + + -- local variables + i : Integer := 1; + text, pattern : SB.Bounded_String; +begin + while i+1 <= CL.Argument_Count loop + text := SB.To_Bounded_String(CL.Argument(I)); + pattern := SB.To_Bounded_String(CL.Argument(I+1)); + if match(text, pattern) then + Put_Line("1"); + else + Put_Line("0"); + end if; + i := i + 2; + end loop; +end ch_1; diff --git a/challenge-099/paulo-custodio/ada/ch_2.adb b/challenge-099/paulo-custodio/ada/ch_2.adb new file mode 100644 index 0000000000..9fd9d54077 --- /dev/null +++ b/challenge-099/paulo-custodio/ada/ch_2.adb @@ -0,0 +1,79 @@ +-- TASK #2 › Unique Sub-sequence +-- Submitted by : Mohammad S Anwar +-- You are given two strings $S and $T. +-- +-- Write a script to find out count of different unique sub-sequences matching +-- $T without changing the position of characters. +-- +-- Example 1: +-- Input : $S = "littleit', $T = 'lit' +-- Output : 5 +-- +-- 1: [lit] tleit +-- 2: [li] t[t] leit +-- 3: [li] ttlei[t] +-- 4: litt[l] e[it] +-- 5: [l] ittle[it] +-- Example 2: +-- Input : $S = "london', $T = 'lon' +-- Output : 3 +-- +-- 1: [lon] don +-- 2: [lo] ndo[n] +-- 3: [l] ond[on] + +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; +with Ada.Command_Line; +with Ada.Strings.Bounded; + +procedure ch_2 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; + + -- count sub-sequences + function count_subsequences(txt, tst: SB.Bounded_String) return Integer is + matching, nonmatching : Integer := 0; + text : SB.Bounded_String := txt; + test : SB.Bounded_String := tst; + begin + loop + -- if test is empty, matched + if test="" then + return 1; + -- text is empty, did not match + elsif text="" then + return 0; + -- head is same, check two paths + elsif SB.Head(text, 1)=SB.Head(test, 1) then + matching := count_subsequences( + SB.Bounded_Slice(text, 2, SB.Length(text)), + SB.Bounded_Slice(test, 2, SB.Length(test))); + nonmatching := count_subsequences( + SB.Bounded_Slice(text, 2, SB.Length(text)), + test); + return matching + nonmatching; + -- different head, keep test + else + SB.Delete(text, 1, 1); + end if; + end loop; + end count_subsequences; + + -- local variables + i : Integer := 1; + count : Integer := 0; + text, test : SB.Bounded_String; +begin + while i+1 <= CL.Argument_Count loop + text := SB.To_Bounded_String(CL.Argument(I)); + test := SB.To_Bounded_String(CL.Argument(I+1)); + count := count_subsequences(text, test); + Put_Line(Trim(Integer'Image(count), Ada.Strings.Left)); + i := i + 2; + end loop; +end ch_2; diff --git a/challenge-099/paulo-custodio/t/test-1.yaml b/challenge-099/paulo-custodio/t/test-1.yaml index 308a277832..db355f7773 100644 --- a/challenge-099/paulo-custodio/t/test-1.yaml +++ b/challenge-099/paulo-custodio/t/test-1.yaml @@ -32,6 +32,3 @@ input: output: | 0 - - - diff --git a/challenge-099/paulo-custodio/t/test-2.yaml b/challenge-099/paulo-custodio/t/test-2.yaml index ff48c2404f..50a14f6f47 100644 --- a/challenge-099/paulo-custodio/t/test-2.yaml +++ b/challenge-099/paulo-custodio/t/test-2.yaml @@ -11,4 +11,3 @@ input: output: | 3 - |
