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 (limited to 'challenge-100') 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-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 -- 12 files changed, 24 deletions(-) (limited to 'challenge-100') 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-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 ++++---- 15 files changed, 442 insertions(+), 442 deletions(-) (limited to 'challenge-100') 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) + if sum1 < sum2 then + min_sum_1 = sum1 + else + min_sum_1 = sum2 + end if + end if end function function min_sum() as integer - min_sum = min_sum_1(0, 1, 1) + min_sum = min_sum_1(0, 1, 1) end function dim i as integer i = 1 do while command(i) <> "" - add_row(i, command(i)) - i = i + 1 + add_row(i, command(i)) + i = i + 1 loop print trim(str(min_sum())) diff --git a/challenge-100/paulo-custodio/cpp/ch-1.cpp b/challenge-100/paulo-custodio/cpp/ch-1.cpp index 389d36f9c9..0516a4bd31 100644 --- a/challenge-100/paulo-custodio/cpp/ch-1.cpp +++ b/challenge-100/paulo-custodio/cpp/ch-1.cpp @@ -72,7 +72,7 @@ std::istream& operator>>(std::istream& is, Time& time) { // get optional am|pm std::string ampm; - is >> ampm; + is >> ampm; // convert from 12 to 24 format if (!ampm.empty()) { diff --git a/challenge-100/paulo-custodio/forth/ch-1.fs b/challenge-100/paulo-custodio/forth/ch-1.fs index 192b0dc2a5..a4008e5be2 100644 --- a/challenge-100/paulo-custodio/forth/ch-1.fs +++ b/challenge-100/paulo-custodio/forth/ch-1.fs @@ -3,11 +3,11 @@ \ 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 @@ -24,7 +24,7 @@ AGAIN ; -\ parse a time string, return number of minutes since midnight +\ parse a time string, return number of minutes since midnight \ and true if it was in AM/PM format : parse-time { str len -- hours minutes f-am-pm } 0 0 { hours minutes } @@ -54,12 +54,12 @@ : print12 { hours minutes -- } FALSE { pm } \ assume AM - hours DUP 12 > IF + hours DUP 12 > IF DROP hours 12 - TO hours TRUE TO pm ELSE - DUP 12 = IF + DUP 12 = IF DROP TRUE TO pm ELSE DUP 0= IF - DROP 12 TO HOURS + DROP 12 TO HOURS ELSE DROP THEN THEN THEN hours minutes print24 diff --git a/challenge-100/paulo-custodio/forth/ch-2.fs b/challenge-100/paulo-custodio/forth/ch-2.fs index 35156151a7..7592af483c 100644 --- a/challenge-100/paulo-custodio/forth/ch-2.fs +++ b/challenge-100/paulo-custodio/forth/ch-2.fs @@ -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,16 +29,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 @@ -78,7 +78,7 @@ CREATE triangle max-height DUP * CELLS ALLOT \ parse and store triangle : parse-triangle ( -- ) - -1 TO last-row + -1 TO last-row BEGIN NEXT-ARG DUP 0> WHILE parse-line REPEAT 2DROP @@ -90,7 +90,7 @@ CREATE triangle max-height DUP * CELLS ALLOT row col triangle[] @ sum + TO sum row last-row = IF sum - ELSE + ELSE sum row 1+ col RECURSE \ compute left sum sum row 1+ col 1+ RECURSE \ comppute right sum MIN diff --git a/challenge-100/paulo-custodio/lua/ch-1.lua b/challenge-100/paulo-custodio/lua/ch-1.lua index 69527e9fe2..f83035128c 100644 --- a/challenge-100/paulo-custodio/lua/ch-1.lua +++ b/challenge-100/paulo-custodio/lua/ch-1.lua @@ -21,37 +21,37 @@ Output: 07:15 pm or 07:15pm --]] function convert_time(text) - text = string.lower(text) - local hour, minute = string.match(text, "(%d+):(%d+)") - hour = tonumber(hour) - minute = tonumber(minute) - local am = string.match(text, "am") - local pm = string.match(text, "pm") - if am or pm then - -- 12->24 - if pm then - if hour < 12 then - hour = hour + 12 - end - else - if hour == 12 then - hour = 0 - end - end - return string.format("%02d:%02d", hour, minute) - else - -- 24->12 - local ampm = "am" - if hour == 0 then - hour = 12 - elseif hour == 12 then - ampm = "pm" - elseif hour > 12 then - hour = hour - 12 - ampm = "pm" - end - return string.format("%02d:%02d%s", hour, minute, ampm) - end + text = string.lower(text) + local hour, minute = string.match(text, "(%d+):(%d+)") + hour = tonumber(hour) + minute = tonumber(minute) + local am = string.match(text, "am") + local pm = string.match(text, "pm") + if am or pm then + -- 12->24 + if pm then + if hour < 12 then + hour = hour + 12 + end + else + if hour == 12 then + hour = 0 + end + end + return string.format("%02d:%02d", hour, minute) + else + -- 24->12 + local ampm = "am" + if hour == 0 then + hour = 12 + elseif hour == 12 then + ampm = "pm" + elseif hour > 12 then + hour = hour - 12 + ampm = "pm" + end + return string.format("%02d:%02d%s", hour, minute, ampm) + end end io.write(convert_time(arg[1])) diff --git a/challenge-100/paulo-custodio/lua/ch-2.lua b/challenge-100/paulo-custodio/lua/ch-2.lua index c8da806390..9f917fe53a 100644 --- a/challenge-100/paulo-custodio/lua/ch-2.lua +++ b/challenge-100/paulo-custodio/lua/ch-2.lua @@ -7,7 +7,7 @@ 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: @@ -48,32 +48,32 @@ The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 triangle = {} -function add_row(row, text) - rows = row - table.insert(triangle, {}) - for i=1, row do - local s, e = string.find(text, "(%d+)") - local n = tonumber(string.sub(text, s, e)) - text = string.sub(text, e+1, -1) - table.insert(triangle[row], n) - end +function add_row(row, text) + rows = row + table.insert(triangle, {}) + for i=1, row do + local s, e = string.find(text, "(%d+)") + local n = tonumber(string.sub(text, s, e)) + text = string.sub(text, e+1, -1) + table.insert(triangle[row], n) + end end function min_sum(sum, row, col) - sum = sum or 0 - row = row or 1 - col = col or 1 - sum = sum + triangle[row][col] - if row == rows then - return sum - else - local sum1 = min_sum(sum, row+1, col) - local sum2 = min_sum(sum, row+1, col+1) - return math.min(sum1, sum2) - end + sum = sum or 0 + row = row or 1 + col = col or 1 + sum = sum + triangle[row][col] + if row == rows then + return sum + else + local sum1 = min_sum(sum, row+1, col) + local sum2 = min_sum(sum, row+1, col+1) + return math.min(sum1, sum2) + end end - + for i=1, #arg do - add_row(i, arg[i]) + add_row(i, arg[i]) end io.write(min_sum(), "\n") diff --git a/challenge-100/paulo-custodio/perl/ch-1.pl b/challenge-100/paulo-custodio/perl/ch-1.pl index 58ffb22647..c45d44b567 100644 --- a/challenge-100/paulo-custodio/perl/ch-1.pl +++ b/challenge-100/paulo-custodio/perl/ch-1.pl @@ -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 diff --git a/challenge-100/paulo-custodio/perl/ch-2.pl b/challenge-100/paulo-custodio/perl/ch-2.pl index a75afbffc8..7221ce2880 100644 --- a/challenge-100/paulo-custodio/perl/ch-2.pl +++ b/challenge-100/paulo-custodio/perl/ch-2.pl @@ -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,16 +29,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 diff --git a/challenge-100/paulo-custodio/python/ch-1.py b/challenge-100/paulo-custodio/python/ch-1.py index 863b39f2ba..818624d76b 100644 --- a/challenge-100/paulo-custodio/python/ch-1.py +++ b/challenge-100/paulo-custodio/python/ch-1.py @@ -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 @@ -21,8 +21,8 @@ import sys; import datetime; if re.search(r'am|pm', sys.argv[1], re.I): - t = datetime.datetime.strptime(sys.argv[1], "%I:%M%p") - print(t.strftime("%H:%M")) + t = datetime.datetime.strptime(sys.argv[1], "%I:%M%p") + print(t.strftime("%H:%M")) else: - t = datetime.datetime.strptime(sys.argv[1], "%H:%M") - print(t.strftime("%I:%M%p").lower()) + t = datetime.datetime.strptime(sys.argv[1], "%H:%M") + print(t.strftime("%I:%M%p").lower()) diff --git a/challenge-100/paulo-custodio/python/ch-2.py b/challenge-100/paulo-custodio/python/ch-2.py index d75c2540c9..9491b675e0 100644 --- a/challenge-100/paulo-custodio/python/ch-2.py +++ b/challenge-100/paulo-custodio/python/ch-2.py @@ -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,16 +29,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 @@ -49,26 +49,26 @@ import sys; triangle = [] def add_row(row, items): - triangle.append(items) + triangle.append(items) def parse(args): - for i in range(0, len(args)): - items = [int(x) for x in args[i].split(",")] - add_row(i, items) + for i in range(0, len(args)): + items = [int(x) for x in args[i].split(",")] + add_row(i, items) def min_sum(): - def min_sum_1(sum, row, col): - sum += triangle[row][col] - if row+1 == len(triangle): - return sum - else: - sum1 = min_sum_1(sum, row+1, col) - sum2 = min_sum_1(sum, row+1, col+1) - return min(sum1, sum2) - return min_sum_1(0, 0, 0) + def min_sum_1(sum, row, col): + sum += triangle[row][col] + if row+1 == len(triangle): + return sum + else: + sum1 = min_sum_1(sum, row+1, col) + sum2 = min_sum_1(sum, row+1, col+1) + return min(sum1, sum2) + return min_sum_1(0, 0, 0) parse(sys.argv[1:]) print(min_sum()) - + -- cgit