aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-02-18 21:06:08 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-02-18 21:06:08 +0000
commitac4d89f845c6454f12fe5bd9e701937ac0951d00 (patch)
treec0272ea37e086f995978b140c80ebd4c605deca1
parentecebe513c84edd4e5b259a8ef245232f771ae77d (diff)
downloadperlweeklychallenge-club-ac4d89f845c6454f12fe5bd9e701937ac0951d00.tar.gz
perlweeklychallenge-club-ac4d89f845c6454f12fe5bd9e701937ac0951d00.tar.bz2
perlweeklychallenge-club-ac4d89f845c6454f12fe5bd9e701937ac0951d00.zip
Add Basic and Lua solutions to challenge 100
-rw-r--r--challenge-100/paulo-custodio/basic/ch-1.bas56
-rw-r--r--challenge-100/paulo-custodio/basic/ch-2.bas91
-rw-r--r--challenge-100/paulo-custodio/lua/ch-1.lua57
-rw-r--r--challenge-100/paulo-custodio/lua/ch-2.lua79
4 files changed, 283 insertions, 0 deletions
diff --git a/challenge-100/paulo-custodio/basic/ch-1.bas b/challenge-100/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..7c2b970b32
--- /dev/null
+++ b/challenge-100/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,56 @@
+' 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
+' Example 2:
+' Input: 19:15
+' 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
+
+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
+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
new file mode 100644
index 0000000000..07a9496a04
--- /dev/null
+++ b/challenge-100/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,91 @@
+' 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
+
+const as integer MAX_ROWS = 20
+ dim shared triangle(MAX_ROWS, MAX_ROWS) as integer
+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
+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
+end function
+
+function min_sum() as integer
+ 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
+loop
+print trim(str(min_sum()))
diff --git a/challenge-100/paulo-custodio/lua/ch-1.lua b/challenge-100/paulo-custodio/lua/ch-1.lua
new file mode 100644
index 0000000000..69527e9fe2
--- /dev/null
+++ b/challenge-100/paulo-custodio/lua/ch-1.lua
@@ -0,0 +1,57 @@
+#!/usr/bin/env lua
+
+--[[
+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
+Example 2:
+Input: 19:15
+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
+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
new file mode 100644
index 0000000000..c8da806390
--- /dev/null
+++ b/challenge-100/paulo-custodio/lua/ch-2.lua
@@ -0,0 +1,79 @@
+#!/usr/bin/env lua
+
+--[[
+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
+--]]
+
+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
+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
+end
+
+for i=1, #arg do
+ add_row(i, arg[i])
+end
+io.write(min_sum(), "\n")