[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Symbolify (#169

Matthew Moss

7/11/2008 3:17:00 PM

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

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/rub....

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`).



--
Matthew Moss <matthew.moss@gmail.com>

74 Answers

ara.t.howard

7/11/2008 4:13:00 PM

0


On Jul 11, 2008, at 9:17 AM, Matthew Moss wrote:

> 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

i've got one line - tests pass.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Alex LeDonne

7/11/2008 4:42:00 PM

0

On Fri, Jul 11, 2008 at 12:12 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> On Jul 11, 2008, at 9:17 AM, Matthew Moss wrote:
>
>> 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
>
> i've got one line - tests pass.
>

Me too. My method body is one line, 25 characters. Did you manage to
stretch yours to 42? :)

-A

ara.t.howard

7/11/2008 4:52:00 PM

0


On Jul 11, 2008, at 10:41 AM, Alex LeDonne wrote:

> Me too. My method body is one line, 25 characters. Did you manage to
> stretch yours to 42? :)



haven't gone golfing yet - it's 74 now. you've guess my target
accurately though ;-)


a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




James Gray

7/11/2008 5:15:00 PM

0

On Jul 11, 2008, at 10:17 AM, Matthew Moss wrote:

> ## Symbolify (#169)
>
> Your task this week is to count. Yup, that's it.
>
> Oh, by the way... You may only use the characters `?`, `*`, `(`, `)`
> and `-`.

Excellent quiz. Full marks Matthew. ;)

James Edward Gray II

ara.t.howard

7/11/2008 5:17:00 PM

0


On Jul 11, 2008, at 11:07 AM, Dana Merrick wrote:

> Haha, I just did it and my solution just happened to be 42 characters.


damn - i've usurped!


a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




James Gray

7/11/2008 5:20:00 PM

0

On Jul 11, 2008, at 12:16 PM, ara.t.howard wrote:

>
> On Jul 11, 2008, at 11:07 AM, Dana Merrick wrote:
>
>> Haha, I just did it and my solution just happened to be 42
>> characters.
>
>
> damn - i've usurped!

Mine was 28 characters, with normal whitespace usage.

James Edward Gray II

James Gray

7/11/2008 5:23:00 PM

0

On Jul 11, 2008, at 12:23 PM, James Gray wrote:

> On Jul 11, 2008, at 12:16 PM, ara.t.howard wrote:
>
>>
>> On Jul 11, 2008, at 11:07 AM, Dana Merrick wrote:
>>
>>> Haha, I just did it and my solution just happened to be 42
>>> characters.
>>
>>
>> damn - i've usurped!
>
> Mine was 28 characters, with normal whitespace usage.

Of course, using my solution, the following code causes Ruby to
Segfault:

eval(symbolify(1200))

James Edward Gray II

ara.t.howard

7/11/2008 5:36:00 PM

0


On Jul 11, 2008, at 11:22 AM, James Gray wrote:

> Of course, using my solution, the following code causes Ruby to
> Segfault:
>
> eval(symbolify(1200))

a feature - no on needs numbers that big, 2 ** 10 should be enough for
all computing

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Chris Shea

7/11/2008 5:53:00 PM

0

On Jul 11, 10:41 am, Alex LeDonne <aledonne.listm...@gmail.com> wrote:
> On Fri, Jul 11, 2008 at 12:12 PM, ara.t.howard <ara.t.how...@gmail.com> wrote:
>
> > On Jul 11, 2008, at 9:17 AM, Matthew Moss wrote:
>
> >> 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
>
> > i've got one line - tests pass.
>
> Me too. My method body is one line, 25 characters. Did you manage to
> stretch yours to 42? :)
>
> -A

And it works for negative integers too?

Chris

Alex LeDonne

7/11/2008 6:03:00 PM

0

On Fri, Jul 11, 2008 at 1:19 PM, James Gray <james@grayproductions.net> wrote:
> On Jul 11, 2008, at 12:16 PM, ara.t.howard wrote:
>
>>
>> On Jul 11, 2008, at 11:07 AM, Dana Merrick wrote:
>>
>>> Haha, I just did it and my solution just happened to be 42 characters.
>>
>>
>> damn - i've usurped!
>
> Mine was 28 characters, with normal whitespace usage.
>
> James Edward Gray II
>

I managed to golf one char out of my first solution; it's 28 with
"normal whitespace usage", 24 without. The output is ugly and long,
though.


I also have a longer solution that produces relatively compact
representations relatively quickly such that

symbolify(999).length == 145
symbolify(9999).length == 145
symbolify(999999).length == 389
symbolify(12345678901234567890).length == 1290
symbolify(("9"*2100).to_i).length == 402035

-A