My solution:
def symbolify(i)
("?)-?(--"*i)[0..-3]
end
However, at about 3300 I get a SystemStackError and if I start at like 5000 I
segfault the ruby interpreter (1.8.6) ;-)
martin
On Friday 11 July 2008 16:17:15 Matthew Moss wrote:
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz 2:
>
> 1. Please do not post any solutions or spoiler discussion for this
> quiz until 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
> permanent, new website is in the works for Ruby Quiz 2. Until then,
> please visit the temporary website at
>
> <http://splatbang.com/rubyquiz/>.
>
> 3. Enjoy!
> Suggestion: A [QUIZ] in the subject of emails about the problem
> helps everyone on Ruby Talk follow the discussion. Please reply to
> the original quiz message, if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Symbolify (#169)
>
> Your task this week is to count. Yup, that's it.
>
> Oh, by the way... You may only use the characters `?`, `*`, `(`, `)` and
> `-`.
>
> Specifically, define a function `symbolify` that accepts an integer
> and returns a string composed of only the five characters shown above.
> The string, when evaluated, will equal the original number passed in.
>
> That is, the following test code should raise no exceptions:
>
> 1000.times do |i|
> s = symbolify(i)
> raise "Not a string!" unless s.is_a? String
> raise "Invalid chars!" unless s.delete("?*()-").empty?
>
> x = eval(s)
> raise "Decode failed!" unless i == x
> end
>
>
> There are at least a few different approaches that come to mind, so
> don't expect everyone to have the same output. Well, not before the
> output is passed through `eval`, that is.
>
> I am not requiring that you produce the shortest string (though you
> are welcome to attempt such a thing). The only thing I _do_ ask is
> that you not produce megabytes of data to represent a simple integer.
>
> P.S. Cheating is encouraged (except that you may not write code
> outside of `symbolify`).