20
1
From Wikipedia Set-theoretic definition of natural numbers
The set N of natural numbers is defined as the smallest set containing 0 and closed under the successor function S defined by S(n) = n ∪ {n}.
The first few numbers defined this way are 0 = {}, 1 = {0} = {{}}, 2 = {0,1} = {{},{{}}}, 3 = {0,1,2} = {{},{{}},{{},{{}}}}.
Using this definition of natural numbers count the length of a string.
Input a string of characters from a-zA-Z of any length
Output the length of the string in set notation without separators
Examples
Input Empty string
Output {}
Input a
Output {{}}
Input aaaa
Output {{}{{}}{{}{{}}}{{}{{}}{{}{{}}}}}
For readability output for 'aaaa' with separators is
{
{}
{{}}
{{} {{}} }
{{} {{}} {{} {{} } } }
}
Conditions
- No digits 0 to 9 to appear in the code;
- No use of character code conversion to generate numbers;
- No use of +-*/ for arithmetic calculations including increment and decrement;
- No mathematical operations other than Boolean Logic;
- Input string does not count in determining byte length;
Winner Shortest code length in bytes.
As this is my first question I hope I have made it clear and rigorous enough. Friendly advice accepted.
Hm, can you implement a recursive definition without calling
f(n-1)
? – Martin Ender – 2014-04-10T10:27:38.0601I have a solution (non-optimal in terms of byte length) that does not use recursion. If A is an array then A.pop();f(A) is recursive. – jing3142 – 2014-04-10T10:34:14.683
That's a good point. – Martin Ender – 2014-04-10T10:37:59.777
1@jing3142 Kudos for implementing a solution yourself to make sure it's possible. +1 for great question. – Kyle Strand – 2014-04-10T18:49:22.350
What does the restriction to a-zA-Z mean? Should we detect whitespace/rubish? or should we just expect this and report the length of the string, regardles on the content? – V-X – 2014-04-11T08:44:10.210
@V-X the input string only consists of these characters, content is immaterial. – jing3142 – 2014-04-11T12:39:12.680