diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-03-09 19:35:12 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-03-09 19:35:12 +0000 |
| commit | 6ca196a3892498e062c5125369399a436d03d0f7 (patch) | |
| tree | 4cef0b93296aa5981d1db8bd7ef752f8ec5cd72a /challenge-091 | |
| parent | 2bdf51703359ef35c7ec3829ee25be3e89c45490 (diff) | |
| download | perlweeklychallenge-club-6ca196a3892498e062c5125369399a436d03d0f7.tar.gz perlweeklychallenge-club-6ca196a3892498e062c5125369399a436d03d0f7.tar.bz2 perlweeklychallenge-club-6ca196a3892498e062c5125369399a436d03d0f7.zip | |
Add Lua and Python solutions to challenge 091
Diffstat (limited to 'challenge-091')
| -rw-r--r-- | challenge-091/paulo-custodio/forth/ch-1.fs | 12 | ||||
| -rw-r--r-- | challenge-091/paulo-custodio/forth/ch-2.fs | 25 | ||||
| -rw-r--r-- | challenge-091/paulo-custodio/lua/ch-1.lua | 27 | ||||
| -rw-r--r-- | challenge-091/paulo-custodio/lua/ch-2.lua | 29 | ||||
| -rw-r--r-- | challenge-091/paulo-custodio/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-091/paulo-custodio/python/ch-2.py | 20 | ||||
| -rw-r--r-- | challenge-091/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-091/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-091/paulo-custodio/test.pl | 27 |
9 files changed, 155 insertions, 40 deletions
diff --git a/challenge-091/paulo-custodio/forth/ch-1.fs b/challenge-091/paulo-custodio/forth/ch-1.fs index 2be004860a..e50b54ae75 100644 --- a/challenge-091/paulo-custodio/forth/ch-1.fs +++ b/challenge-091/paulo-custodio/forth/ch-1.fs @@ -1,12 +1,12 @@ -\ THE WEEKLY CHALLENGE - 091 +#! /usr/bin/env gforth + +\ Challenge 091 +\ \ TASK #1: Count Number \ \ You are given a positive number $N. Write a script to count number and \ display as you read it. -\ Start the script with N in the stack, e.g. -\ gforth -e 12345 ch-1.fs - \ This solution uses the data stack to hold the digit counts, while \ computing them right-to-left, and displays them left-to-right at \ the end. A -1 is used as a stack marker, as there cannot be a -1 @@ -40,4 +40,6 @@ CR ; -read_number BYE +NEXT-ARG S>NUMBER? 0= THROW DROP ( N ) +read_number +BYE diff --git a/challenge-091/paulo-custodio/forth/ch-2.fs b/challenge-091/paulo-custodio/forth/ch-2.fs index ad179d6636..69e6d49327 100644 --- a/challenge-091/paulo-custodio/forth/ch-2.fs +++ b/challenge-091/paulo-custodio/forth/ch-2.fs @@ -1,4 +1,7 @@ -\ THE WEEKLY CHALLENGE - 091 +#! /usr/bin/env gforth + +\ Challenge 091 +\ \ TASK #2: Jump Game \ \ You are given an array of positive numbers @N, where value at each index @@ -6,18 +9,20 @@ \ if you can jump to the last index. Print 1 if you are able to reach the last \ index otherwise 0. -\ Start the script with the table and its size on the stack, e.g. -\ gforth -e '1 2 1 2 4' ch-2.fs -\ setup: tbl tbl-size -VALUE tbl-size \ save table size -: ,values - tbl-size 0 DO - tbl-size I - 1- ROLL , - LOOP +0 VALUE tbl-size \ save table size + +\ collect_args, append them to heap +: collect_args ( -- ) + 0 TO tbl-size + BEGIN NEXT-ARG DUP 0> WHILE + S>NUMBER? 0= THROW DROP , + tbl-size 1+ TO tbl-size + REPEAT ; -CREATE tbl ,values +CREATE tbl +collect_args \ run the table : run ( -- f ) diff --git a/challenge-091/paulo-custodio/lua/ch-1.lua b/challenge-091/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..96270413e7 --- /dev/null +++ b/challenge-091/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,27 @@ +#!/usr/bin/env lua + +--[[ +THE WEEKLY CHALLENGE - 091 + +TASK #1: Count Number + +You are given a positive number $N. Write a script to count number and +display as you read it. + +Solution with regular expressions: +Just match the first digit and a sequence of equal matches, capture the +results and show them. +--]] + +function count_number(n) + local inp, out = tostring(n), '' + while (inp ~= '') do + local digit = string.match(inp, "^%d") + local digits = string.match(inp, "^"..digit.."+") + out = out..tostring(#digits)..digit + inp = string.sub(inp, #digits+1) + end + return out +end + +print(count_number(tonumber(arg[1]))) diff --git a/challenge-091/paulo-custodio/lua/ch-2.lua b/challenge-091/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..cf1a47424e --- /dev/null +++ b/challenge-091/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,29 @@ +#!/usr/bin/env lua + +--[[ +THE WEEKLY CHALLENGE - 091 + +TASK #2: Jump Game + +You are given an array of positive numbers @N, where value at each index +determines how far you are allowed to jump further. Write a script to decide +if you can jump to the last index. Print 1 if you are able to reach the last +index otherwise 0. +--]] + +N = {} + +for i=1,#arg do + table.insert(N, tonumber(arg[i])) +end + +p = 1 +while (p < #N and N[p] ~= 0) do + p = p + N[p] +end + +if (p == #N) then + io.write(1, "\n") +else + io.write(0, "\n") +end diff --git a/challenge-091/paulo-custodio/python/ch-1.py b/challenge-091/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..3eeb00c151 --- /dev/null +++ b/challenge-091/paulo-custodio/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +# THE WEEKLY CHALLENGE - 091 +# TASK #1: Count Number +# +# You are given a positive number $N. Write a script to count number and +# display as you read it. + +# Solution with regular expressions: +# Just match the first digit and a sequence of equal matches, capture the +# results and show them. + +import re +import sys + +def count_number(n): + n = str(n) + out = '' + while n != '': + m = re.match(r'^((\d)\2*)', n) + out += str(m.end())+m.group(2) + n = n[m.end():] + return out + +print(count_number(int(sys.argv[1]))) diff --git a/challenge-091/paulo-custodio/python/ch-2.py b/challenge-091/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..84db6f66d8 --- /dev/null +++ b/challenge-091/paulo-custodio/python/ch-2.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +# THE WEEKLY CHALLENGE - 091 +# TASK #2: Jump Game +# +# You are given an array of positive numbers @N, where value at each index +# determines how far you are allowed to jump further. Write a script to decide +# if you can jump to the last index. Print 1 if you are able to reach the last +# index otherwise 0. + +import sys + +n = [int(x) for x in sys.argv[1:]] +pos = 0 +while pos < len(n)-1 and n[pos] != 0: + pos += n[pos] +if pos==len(n)-1: + print(1) +else: + print(0) diff --git a/challenge-091/paulo-custodio/t/test-1.yaml b/challenge-091/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..bb525a1030 --- /dev/null +++ b/challenge-091/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1122234 + input: + output: 21321314 +- setup: + cleanup: + args: 2333445 + input: + output: 12332415 +- setup: + cleanup: + args: 12345 + input: + output: 1112131415 diff --git a/challenge-091/paulo-custodio/t/test-2.yaml b/challenge-091/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..d6c7f55d2d --- /dev/null +++ b/challenge-091/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 1 2 + input: + output: 1 +- setup: + cleanup: + args: 1 3 1 2 + input: + output: 0 +- setup: + cleanup: + args: 2 1 1 0 2 + input: + output: 0 diff --git a/challenge-091/paulo-custodio/test.pl b/challenge-091/paulo-custodio/test.pl index 04d89a6ab3..01ed2b83cd 100644 --- a/challenge-091/paulo-custodio/test.pl +++ b/challenge-091/paulo-custodio/test.pl @@ -2,29 +2,6 @@ use strict; use warnings; -use Test::More; +use 5.030; -# forth -is `gforth -e 1122234 forth/ch-1.fs`, "21321314\n"; -is `gforth -e 2333445 forth/ch-1.fs`, "12332415\n"; -is `gforth -e 12345 forth/ch-1.fs`, "1112131415\n"; - -is `gforth -e '1 2 1 2 4' forth/ch-2.fs`, "1 \n"; -is `gforth -e '1 3 1 2 4' forth/ch-2.fs`, "0 \n"; -is `gforth -e '2 1 1 0 2 5' forth/ch-2.fs`, "0 \n"; - -# perl -is `perl perl/ch-1.pl 1122234`, "21321314\n"; -is `perl perl/ch-1.pl 2333445`, "12332415\n"; -is `perl perl/ch-1.pl 12345`, "1112131415\n"; - -is `perl perl/ch-1a.pl 1122234`, "21321314\n"; -is `perl perl/ch-1a.pl 2333445`, "12332415\n"; -is `perl perl/ch-1a.pl 12345`, "1112131415\n"; - -is `perl perl/ch-2.pl 1 2 1 2 `, "1\n"; -is `perl perl/ch-2.pl 1 3 1 2 `, "0\n"; -is `perl perl/ch-2.pl 2 1 1 0 2`, "0\n"; - - -done_testing; +require '../../challenge-001/paulo-custodio/test.pl'; |
