From e64734f9b42c5838a7d3ade2d6c2e8ba14625cf0 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 9 Mar 2021 19:39:28 +0000 Subject: Add Lua and Python solutions to challenge 102 --- challenge-102/paulo-custodio/lua/ch-2.lua | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 challenge-102/paulo-custodio/lua/ch-2.lua (limited to 'challenge-102/paulo-custodio/lua/ch-2.lua') diff --git a/challenge-102/paulo-custodio/lua/ch-2.lua b/challenge-102/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..30a664f315 --- /dev/null +++ b/challenge-102/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,46 @@ +#!/usr/bin/env lua + +--[[ +Challenge 102 + +TASK #2 > Hash-counting String +Submitted by: Stuart Little + +You are given a positive integer $N. + +Write a script to produce Hash-counting string of that length. + +The definition of a hash-counting string is as follows: +- the string consists only of digits 0-9 and hashes, '#' +- there are no two consecutive hashes: '##' does not appear in your string +- the last character is a hash +- the number immediately preceding each hash (if it exists) is the position +of that hash in the string, with the position being counted up from 1 + +It can be shown that for every positive integer N there is exactly one such +length-N string. +Examples: + +(a) "#" is the counting string of length 1 +(b) "2#" is the counting string of length 2 +(c) "#3#" is the string of length 3 +(d) "#3#5#7#10#" is the string of length 10 +(e) "2#4#6#8#11#14#" is the string of length 14 +--]] + +function hash_counting(n) + local out, i = '', n + while i > 0 do + p = i + out = '#'..out + i = i - 1 + while i > 0 and p ~= 0 do + out = tostring(p % 10)..out + i = i - 1 + p = math.floor(p / 10) + end + end + return out +end + +print(hash_counting(tonumber(arg[1]))) -- cgit From c51f9bd979dbd79f20cb0abd543dc9b4df9f4e84 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 9 Mar 2021 20:01:18 +0000 Subject: Remove tabs --- challenge-102/paulo-custodio/lua/ch-2.lua | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'challenge-102/paulo-custodio/lua/ch-2.lua') diff --git a/challenge-102/paulo-custodio/lua/ch-2.lua b/challenge-102/paulo-custodio/lua/ch-2.lua index 30a664f315..72d378f496 100644 --- a/challenge-102/paulo-custodio/lua/ch-2.lua +++ b/challenge-102/paulo-custodio/lua/ch-2.lua @@ -14,10 +14,10 @@ The definition of a hash-counting string is as follows: - the string consists only of digits 0-9 and hashes, '#' - there are no two consecutive hashes: '##' does not appear in your string - the last character is a hash -- the number immediately preceding each hash (if it exists) is the position +- the number immediately preceding each hash (if it exists) is the position of that hash in the string, with the position being counted up from 1 -It can be shown that for every positive integer N there is exactly one such +It can be shown that for every positive integer N there is exactly one such length-N string. Examples: @@ -29,18 +29,18 @@ Examples: --]] function hash_counting(n) - local out, i = '', n - while i > 0 do - p = i - out = '#'..out - i = i - 1 - while i > 0 and p ~= 0 do - out = tostring(p % 10)..out - i = i - 1 - p = math.floor(p / 10) - end - end - return out + local out, i = '', n + while i > 0 do + p = i + out = '#'..out + i = i - 1 + while i > 0 and p ~= 0 do + out = tostring(p % 10)..out + i = i - 1 + p = math.floor(p / 10) + end + end + return out end print(hash_counting(tonumber(arg[1]))) -- cgit