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 | |
| 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
34 files changed, 343 insertions, 0 deletions
diff --git a/challenge-099/paulo-custodio/ada/ch_1.adb b/challenge-099/paulo-custodio/ada/ch_1.adb index 925660d9d7..fd79db2f4c 100644 --- a/challenge-099/paulo-custodio/ada/ch_1.adb +++ b/challenge-099/paulo-custodio/ada/ch_1.adb @@ -1,3 +1,5 @@ +-- Challenge 099 +-- -- TASK #1 › Pattern Match -- Submitted by: Mohammad S Anwar -- You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/ada/ch_2.adb b/challenge-099/paulo-custodio/ada/ch_2.adb index 9fd9d54077..e05007fb0e 100644 --- a/challenge-099/paulo-custodio/ada/ch_2.adb +++ b/challenge-099/paulo-custodio/ada/ch_2.adb @@ -1,3 +1,5 @@ +-- Challenge 099 +-- -- TASK #2 › Unique Sub-sequence -- Submitted by : Mohammad S Anwar -- You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/awk/ch-1.awk b/challenge-099/paulo-custodio/awk/ch-1.awk index e090e3fe64..57a0b663f3 100644 --- a/challenge-099/paulo-custodio/awk/ch-1.awk +++ b/challenge-099/paulo-custodio/awk/ch-1.awk @@ -1,5 +1,7 @@ #!/usr/bin/gawk +# Challenge 099 +# # TASK #1 > Pattern Match # Submitted by: Mohammad S Anwar # You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/awk/ch-2.awk b/challenge-099/paulo-custodio/awk/ch-2.awk index 8fd50fa366..abf6668f74 100644 --- a/challenge-099/paulo-custodio/awk/ch-2.awk +++ b/challenge-099/paulo-custodio/awk/ch-2.awk @@ -1,5 +1,7 @@ #!/usr/bin/gawk +# Challenge 099 +# # TASK #2 > Unique Subsequence # Submitted by: Mohammad S Anwar # You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/basic/ch-1.bas b/challenge-099/paulo-custodio/basic/ch-1.bas index 255f3147ce..1cc64aee12 100644 --- a/challenge-099/paulo-custodio/basic/ch-1.bas +++ b/challenge-099/paulo-custodio/basic/ch-1.bas @@ -1,3 +1,5 @@ +' Challenge 099 +' ' TASK #1 › Pattern Match ' Submitted by: Mohammad S Anwar ' You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/basic/ch-2.bas b/challenge-099/paulo-custodio/basic/ch-2.bas index b2733f8867..ac658b84b3 100644 --- a/challenge-099/paulo-custodio/basic/ch-2.bas +++ b/challenge-099/paulo-custodio/basic/ch-2.bas @@ -1,3 +1,5 @@ +' Challenge 099 +' ' TASK #2 › Unique Sub-sequence ' Submitted by : Mohammad S Anwar ' You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/c/ch-1.c b/challenge-099/paulo-custodio/c/ch-1.c index a22c7d91e2..82c1b4edb7 100644 --- a/challenge-099/paulo-custodio/c/ch-1.c +++ b/challenge-099/paulo-custodio/c/ch-1.c @@ -1,4 +1,6 @@ /* +Challenge 099 + TASK #1 › Pattern Match Submitted by: Mohammad S Anwar You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/c/ch-2.c b/challenge-099/paulo-custodio/c/ch-2.c index 5582d110a4..3b75674d56 100644 --- a/challenge-099/paulo-custodio/c/ch-2.c +++ b/challenge-099/paulo-custodio/c/ch-2.c @@ -1,4 +1,6 @@ /* +Challenge 099 + TASK #2 › Unique Sub-sequence Submitted by : Mohammad S Anwar You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/cpp/ch-1.cpp b/challenge-099/paulo-custodio/cpp/ch-1.cpp index 5fe6d95de2..67687d4582 100644 --- a/challenge-099/paulo-custodio/cpp/ch-1.cpp +++ b/challenge-099/paulo-custodio/cpp/ch-1.cpp @@ -1,4 +1,6 @@ /* +Challenge 099 + TASK #1 › Pattern Match Submitted by: Mohammad S Anwar You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/cpp/ch-2.cpp b/challenge-099/paulo-custodio/cpp/ch-2.cpp index cf0cfbd04a..e553a5eaa2 100644 --- a/challenge-099/paulo-custodio/cpp/ch-2.cpp +++ b/challenge-099/paulo-custodio/cpp/ch-2.cpp @@ -1,4 +1,6 @@ /* +Challenge 099 + TASK #2 › Unique Sub-sequence Submitted by : Mohammad S Anwar You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/forth/ch-1.fs b/challenge-099/paulo-custodio/forth/ch-1.fs index adcb5f14d7..3dd302fad2 100644 --- a/challenge-099/paulo-custodio/forth/ch-1.fs +++ b/challenge-099/paulo-custodio/forth/ch-1.fs @@ -1,5 +1,7 @@ #! /usr/bin/env gforth +\ Challenge 099 +\ \ TASK #1 › Pattern Match \ Submitted by: Mohammad S Anwar \ You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/forth/ch-2.fs b/challenge-099/paulo-custodio/forth/ch-2.fs index 7d1e47eead..26ff1a758d 100644 --- a/challenge-099/paulo-custodio/forth/ch-2.fs +++ b/challenge-099/paulo-custodio/forth/ch-2.fs @@ -1,5 +1,7 @@ #! /usr/bin/env gforth +\ Challenge 099 +\ \ TASK #2 › Unique Sub-sequence \ Submitted by : Mohammad S Anwar \ You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/lua/ch-1.lua b/challenge-099/paulo-custodio/lua/ch-1.lua index 225beeb34d..989dd87715 100644 --- a/challenge-099/paulo-custodio/lua/ch-1.lua +++ b/challenge-099/paulo-custodio/lua/ch-1.lua @@ -1,6 +1,8 @@ #!/usr/bin/env lua --[[ +Challenge 099 + TASK #1 › Pattern Match Submitted by: Mohammad S Anwar You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/lua/ch-2.lua b/challenge-099/paulo-custodio/lua/ch-2.lua index d74eb53626..9c52b32a2e 100644 --- a/challenge-099/paulo-custodio/lua/ch-2.lua +++ b/challenge-099/paulo-custodio/lua/ch-2.lua @@ -1,6 +1,8 @@ #!/usr/bin/env lua --[[ +Challenge 099 + TASK #2 › Unique Sub-sequence Submitted by : Mohammad S Anwar You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/perl/ch-1.pl b/challenge-099/paulo-custodio/perl/ch-1.pl index effafe2108..71b3a7ea93 100644 --- a/challenge-099/paulo-custodio/perl/ch-1.pl +++ b/challenge-099/paulo-custodio/perl/ch-1.pl @@ -1,5 +1,7 @@ #!/usr/bin/perl +# Challenge 099 +# # TASK #1 › Pattern Match # Submitted by: Mohammad S Anwar # You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/perl/ch-2.pl b/challenge-099/paulo-custodio/perl/ch-2.pl index d0a8c71867..d13a90524f 100644 --- a/challenge-099/paulo-custodio/perl/ch-2.pl +++ b/challenge-099/paulo-custodio/perl/ch-2.pl @@ -1,5 +1,7 @@ #!/usr/bin/perl +# Challenge 099 +# # TASK #2 › Unique Subsequence # Submitted by: Mohammad S Anwar # You are given two strings $S and $T. diff --git a/challenge-099/paulo-custodio/python/ch-1.py b/challenge-099/paulo-custodio/python/ch-1.py index 60295131f4..930c9dc328 100644 --- a/challenge-099/paulo-custodio/python/ch-1.py +++ b/challenge-099/paulo-custodio/python/ch-1.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +# Challenge 099 +# # TASK #1 > Pattern Match # Submitted by: Mohammad S Anwar # You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/python/ch-2.py b/challenge-099/paulo-custodio/python/ch-2.py index e065b17674..e82e57f635 100644 --- a/challenge-099/paulo-custodio/python/ch-2.py +++ b/challenge-099/paulo-custodio/python/ch-2.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +# Challenge 099 +# # TASK #2 > Unique Sub-sequence # Submitted by : Mohammad S Anwar # You are given two strings $S and $T. 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. |
