Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [QUIZ] Symbolify (#169)

Reply
Thread Tools

[QUIZ] Symbolify (#169)

 
 
Alex LeDonne
Guest
Posts: n/a
 
      07-11-2008
On Fri, Jul 11, 2008 at 1:19 PM, James Gray <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Matthew Moss
Guest
Posts: n/a
 
      07-11-2008
> > ## Symbolify (#169)
>
> > Your task this week is to count. Yup, that's it.

>
> > Oh, by the way... You may only use the characters `?`, `*`, `(`, `)` =

=A0
> > and `-`.

>
> Excellent quiz. =A0Full marks Matthew. =A0


Somehow, with all the comparisons of how small solutions are, methinks
there are other techniques that I hadn't considered. Mine own solution
is short, but not *that* short.

Though I haven't attempted to shrink it yet, so maybe I can make it
smaller... Let's see...

Okay, I can see how this can be seriously abused. But I guess I asked
for it when I said "cheating encouraged!" No worries; it will be
interesting to see how people do. I just wonder if anyone will write a
solution that doesn't cheat. Or perhaps I'm still underestimating your
solutions? Guess I'll see on Monday.

 
Reply With Quote
 
 
 
 
Alex LeDonne
Guest
Posts: n/a
 
      07-11-2008
On Fri, Jul 11, 2008 at 1:57 PM, Chris Shea <(E-Mail Removed)> wrote:
> On Jul 11, 10:41 am, Alex LeDonne <(E-Mail Removed)> wrote:
>> On Fri, Jul 11, 2008 at 12:12 PM, ara.t.howard <(E-Mail Removed)> 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


Hmm... that wasn't in the tests. That took 46 characters (so far) for
my golfed solution.

-A

 
Reply With Quote
 
ara.t.howard
Guest
Posts: n/a
 
      07-11-2008

On Jul 11, 2008, at 12:26 PM, Matthew Moss wrote:

> I just wonder if anyone will write a
> solution that doesn't cheat.


no.

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




 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      07-11-2008
On Jul 11, 2008, at 1:26 PM, Matthew Moss wrote:

> Okay, I can see how this can be seriously abused. But I guess I asked
> for it when I said "cheating encouraged!" No worries; it will be
> interesting to see how people do. I just wonder if anyone will write a
> solution that doesn't cheat. Or perhaps I'm still underestimating your
> solutions? Guess I'll see on Monday.


Mine builds strings using just the characters mentioned, so I doubt it
would be classified as cheating.

James Edward Gray II

 
Reply With Quote
 
Matthew Moss
Guest
Posts: n/a
 
      07-11-2008
This is not required extra work... just for a little more fun. I have
some suspicions about some techniques, what I call "ugly" cheats (as
opposed to the "pretty" cheats who ignored you in high school... umm,
ignore that last part).

Anyway, here's another test:

nums = (0...1000).sort_by { rand }
strs = nums.map { |n| symbolify(n) }

strs.zip(nums).sort_by { rand }.each do |str, num|
res = eval(str)
raise "Not a string!" unless str.is_a? String
raise "Invalid chars!" unless str.delete("?*()-").empty?
raise "Decode failed!" unless res == num
end

puts "Passed!"


This might affect some, maybe not others. I wrote two solutions, one
classified as "pretty" while the other is "ugly" (i.e. fails this
test). Again, you're not required to pass this... Looking at some ugly
solutions will certainly be okay.

Interestingly, my pretty solution confuses IRB. If I run on the
command line:
> ruby -r moss test.rb

Passed!

However, if I start up irb:
> require 'moss'

=> true
> symbolify(60)


Then I get a nice stack crawl (not gonna show yet, spoiler) that ends
in "Maybe IRB bug!!!". (I imagine you guys can guess what I'm
doing...)

 
Reply With Quote
 
Matthew Moss
Guest
Posts: n/a
 
      07-11-2008
Shortly after posting this, I realize that there could easily be ugly
cheats that pass this test as well...

You'd think I know Ruby a little better at this point in time... =)

 
Reply With Quote
 
Alex LeDonne
Guest
Posts: n/a
 
      07-11-2008
On Fri, Jul 11, 2008 at 2:02 PM, Alex LeDonne
<(E-Mail Removed)> wrote:
> On Fri, Jul 11, 2008 at 1:19 PM, James Gray <(E-Mail Removed)> 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
>


After clearing a "duh" moment, I've improved these encoded lengths...
symbolify(999).length == 127
symbolify(9999).length == 127
symbolify(999999).length == 337
symbolify(12345678901234567890).length == 1112
symbolify(("9"*2100).to_i).length == 350265

-A

 
Reply With Quote
 
Wouter Smeenk
Guest
Posts: n/a
 
      07-11-2008
2008/7/11 James Gray <(E-Mail Removed)>:
> 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 squeeze my solution in 19 characters and 23 with normal
whitespace usage.

Wouter Smeenk

 
Reply With Quote
 
Chris Shea
Guest
Posts: n/a
 
      07-11-2008
On Jul 11, 12:57*pm, Matthew Moss <(E-Mail Removed)> wrote:
> Anyway, here's another test:
>
> * nums = (0...1000).sort_by { rand }
> * strs = nums.map { |n| symbolify(n) }
>
> * strs.zip(nums).sort_by { rand }.each do |str, num|
> * * res = eval(str)
> * * raise "Not a string!" *unless str.is_a? String
> * * raise "Invalid chars!" unless str.delete("?*()-").empty?
> * * raise "Decode failed!" unless res == num
> * end
>
> * puts "Passed!"


I've done some translating of these tests into test/unit (and some
meta-programming to help me test multiple solutions easily. Here's
what the end of my solution file looks like:

###
require 'test/unit'

class TestSymbolify < Test::Unit::TestCase
@test_number = 1

def self.solution_test(module_name, range, test=:delayed_eval_tests)
class_eval <<-EOD
def test_#{@test_number}_#{module_name.to_s.gsub('::', '_')}
Object.send(:include, #{module_name})
#{test}(#{range})
end
EOD
@test_number += 1
end

def symbolify_assertions_for(string, integer)
assert_kind_of(String, string)
assert_match(/\A[?*()-]*\Z/, string)
assert_equal(integer, eval(string))
end

def cheat_friendly_tests(range)
range.each do |i|
s = symbolify(i)
symbolify_assertions_for(s,i)
end
end

def delayed_eval_tests(range)
nums = range.sort_by { rand }
strs = nums.map { |n| symbolify(n) }

strs.zip(nums).sort_by { rand }.each do |s, i|
symbolify_assertions_for(s,i)
end
end

# The actual tests
solution_test(Symbolify::Integers, -1000..1000)
solution_test(Symbolify::NaturalNumbers, 0..1000)
solution_test(Symbolify::Cheating,
-1000..1000, :cheat_friendly_tests)
end
###

It might be a little much... but I *was* having fun.

Chris
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SUMMARY] Symbolify (#169) Matthew Moss Ruby 1 07-17-2008 08:31 PM



Advertisments