aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-02-19 20:10:38 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-02-19 20:10:38 +0000
commit798c5692eb4c43474a1ba230bb93f9fdfc3fd311 (patch)
treea7c50c73bce0c30b883fb9310b048b77a2fe3830
parent86f1e20620f3acc15e30b400119b9c9b8c161f97 (diff)
downloadperlweeklychallenge-club-798c5692eb4c43474a1ba230bb93f9fdfc3fd311.tar.gz
perlweeklychallenge-club-798c5692eb4c43474a1ba230bb93f9fdfc3fd311.tar.bz2
perlweeklychallenge-club-798c5692eb4c43474a1ba230bb93f9fdfc3fd311.zip
Remove tabs
-rw-r--r--challenge-097/paulo-custodio/ada/ch_1.adb50
-rw-r--r--challenge-097/paulo-custodio/ada/ch_2.adb86
-rw-r--r--challenge-100/paulo-custodio/ada/ch_1.adb186
-rw-r--r--challenge-100/paulo-custodio/ada/ch_2.adb162
-rw-r--r--challenge-100/paulo-custodio/awk/ch-1.awk72
-rw-r--r--challenge-100/paulo-custodio/awk/ch-2.awk64
-rw-r--r--challenge-100/paulo-custodio/basic/ch-1.bas72
-rw-r--r--challenge-100/paulo-custodio/basic/ch-2.bas80
-rw-r--r--challenge-100/paulo-custodio/cpp/ch-1.cpp2
-rw-r--r--challenge-100/paulo-custodio/forth/ch-1.fs14
-rw-r--r--challenge-100/paulo-custodio/forth/ch-2.fs28
-rw-r--r--challenge-100/paulo-custodio/lua/ch-1.lua62
-rw-r--r--challenge-100/paulo-custodio/lua/ch-2.lua46
-rw-r--r--challenge-100/paulo-custodio/perl/ch-1.pl6
-rw-r--r--challenge-100/paulo-custodio/perl/ch-2.pl24
-rw-r--r--challenge-100/paulo-custodio/python/ch-1.py14
-rw-r--r--challenge-100/paulo-custodio/python/ch-2.py52
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
<