diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-19 20:07:25 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-19 20:07:25 +0000 |
| commit | 329ed9f49bcfc398005fd80dc4988cf6014e6c4a (patch) | |
| tree | 1df218ff81666b868b63e001092e22f9cc59a1a7 | |
| parent | 7c1d91b40bea78621623e27b3a14fca8d0087a35 (diff) | |
| download | perlweeklychallenge-club-329ed9f49bcfc398005fd80dc4988cf6014e6c4a.tar.gz perlweeklychallenge-club-329ed9f49bcfc398005fd80dc4988cf6014e6c4a.tar.bz2 perlweeklychallenge-club-329ed9f49bcfc398005fd80dc4988cf6014e6c4a.zip | |
Add Ada solution to challenge 100
| -rw-r--r-- | challenge-100/paulo-custodio/ada/ch_1.adb | 122 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/ada/ch_2.adb | 129 |
2 files changed, 251 insertions, 0 deletions
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; |
