diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-19 05:16:05 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-19 05:16:05 +0000 |
| commit | df31fa7ce15d62e53f5df6f639718921eb722827 (patch) | |
| tree | 96babbcd1317a42131db8f0c861f95bd0a8b64d0 /challenge-100 | |
| parent | 0d4700b6ebc0749902dd1c9f6a1a05e7cd858513 (diff) | |
| parent | ac4d89f845c6454f12fe5bd9e701937ac0951d00 (diff) | |
| download | perlweeklychallenge-club-df31fa7ce15d62e53f5df6f639718921eb722827.tar.gz perlweeklychallenge-club-df31fa7ce15d62e53f5df6f639718921eb722827.tar.bz2 perlweeklychallenge-club-df31fa7ce15d62e53f5df6f639718921eb722827.zip | |
Merge pull request #3565 from pauloscustodio/paulo-custodio
Paulo Custodio
Diffstat (limited to 'challenge-100')
| -rw-r--r-- | challenge-100/paulo-custodio/awk/ch-1.awk | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/awk/ch-2.awk | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/basic/ch-1.bas | 56 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/basic/ch-2.bas | 91 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/c/ch-1.c | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/c/ch-2.c | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/cpp/ch-1.cpp | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/cpp/ch-2.cpp | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/forth/ch-1.fs | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/forth/ch-2.fs | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/lua/ch-1.lua | 57 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/lua/ch-2.lua | 79 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/python/ch-1.py | 2 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/python/ch-2.py | 2 |
16 files changed, 307 insertions, 0 deletions
diff --git a/challenge-100/paulo-custodio/awk/ch-1.awk b/challenge-100/paulo-custodio/awk/ch-1.awk index 03d750b366..07cba14123 100644 --- a/challenge-100/paulo-custodio/awk/ch-1.awk +++ b/challenge-100/paulo-custodio/awk/ch-1.awk @@ -1,5 +1,7 @@ #!/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 d315475853..aefd2cf858 100644 --- a/challenge-100/paulo-custodio/awk/ch-2.awk +++ b/challenge-100/paulo-custodio/awk/ch-2.awk @@ -1,5 +1,7 @@ #!/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/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/c/ch-1.c b/challenge-100/paulo-custodio/c/ch-1.c index d0a33a1f36..9638b7d656 100644 --- a/challenge-100/paulo-custodio/c/ch-1.c +++ b/challenge-100/paulo-custodio/c/ch-1.c @@ -1,4 +1,6 @@ /* +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 1d68e1d2d6..6a772d3a01 100644 --- a/challenge-100/paulo-custodio/c/ch-2.c +++ b/challenge-100/paulo-custodio/c/ch-2.c @@ -1,4 +1,6 @@ /* +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 389d36f9c9..9f4bcbd5e5 100644 --- a/challenge-100/paulo-custodio/cpp/ch-1.cpp +++ b/challenge-100/paulo-custodio/cpp/ch-1.cpp @@ -1,4 +1,6 @@ /* +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 b672a5cffe..c94b0355a3 100644 --- a/challenge-100/paulo-custodio/cpp/ch-2.cpp +++ b/challenge-100/paulo-custodio/cpp/ch-2.cpp @@ -1,4 +1,6 @@ /* +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 192b0dc2a5..fe0e8f1e76 100644 --- a/challenge-100/paulo-custodio/forth/ch-1.fs +++ b/challenge-100/paulo-custodio/forth/ch-1.fs @@ -1,5 +1,7 @@ #! /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 35156151a7..54939601a9 100644 --- a/challenge-100/paulo-custodio/forth/ch-2.fs +++ b/challenge-100/paulo-custodio/forth/ch-2.fs @@ -1,5 +1,7 @@ #! /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/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") diff --git a/challenge-100/paulo-custodio/perl/ch-1.pl b/challenge-100/paulo-custodio/perl/ch-1.pl index 58ffb22647..cb8bd9c9aa 100644 --- a/challenge-100/paulo-custodio/perl/ch-1.pl +++ b/challenge-100/paulo-custodio/perl/ch-1.pl @@ -1,5 +1,7 @@ #!/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 a75afbffc8..2264dcae93 100644 --- a/challenge-100/paulo-custodio/perl/ch-2.pl +++ b/challenge-100/paulo-custodio/perl/ch-2.pl @@ -1,5 +1,7 @@ #!/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 863b39f2ba..12fad2210b 100644 --- a/challenge-100/paulo-custodio/python/ch-1.py +++ b/challenge-100/paulo-custodio/python/ch-1.py @@ -1,5 +1,7 @@ #!/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 d75c2540c9..44a2c9f077 100644 --- a/challenge-100/paulo-custodio/python/ch-2.py +++ b/challenge-100/paulo-custodio/python/ch-2.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +# Challenge 100 +# # TASK #2 > Triangle Sum # Submitted by: Mohammad S Anwar # You are given triangle array. |
