Symbolify (#169)

On Jul 11, 12:57 pm, Matthew M. [email protected] wrote:

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

Chris S. wrote:

I’ve done some translating of these tests into test/unit (and some
meta-programming to help me test multiple solutions easily.

I’m sorry, I’m a little new to unit testing: how do I set up my program
to use this?

My solution doesn’t have any OO code, it’s just a method definition and
Matthew’s test code.

-Dana

On Jul 11, 2008, at 1:25 PM, Alex LeDonne wrote:

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

mine are all zero length :wink:

a @ http://codeforpeople.com/

On Jul 11, 2008, at 12:57 PM, Matthew M. wrote:

end

puts “Passed!”

52 chars for this attm

a @ http://codeforpeople.com/

On Fri, Jul 11, 2008 at 3:27 PM, Wouter S. [email protected]
wrote:

I found a 19-character, 23-with-whitespace solution as well, thanks to
your inspiring post, Wouter.

My golfed solution which also handles negative numbers is 39
characters, 51 with whitespace.

These both pass both of the provided tests - they really build
strings, as James indicated his does. No cheating here…

-A

James G. wrote:

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 G. II

One line; 38 chars (uses no whitespace); no cheating; 1200 OK;
output doesn’t use the ‘*’ character.

Glen Pankow

From: “Alex LeDonne” [email protected]

My golfed solution which also handles negative numbers is 39
characters, 51 with whitespace.

These both pass both of the provided tests - they really build
strings, as James indicated his does. No cheating here…

Wow… Nice…

My ultra cheezy cheat version is 18 characters… I’m amazed
people are coming up with such short non-cheat versions :slight_smile:

Regards,

Bill

On Jul 11, 3:21 pm, “ara.t.howard” [email protected] wrote:

On Jul 11, 2008, at 1:25 PM, Alex LeDonne wrote:

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

mine are all zero length :wink:

Yeah, my ugly solution has zero length strings as well. :wink:

On Fri, Jul 11, 2008 at 9:57 PM, Bill K. [email protected] wrote:

My ultra cheezy cheat version is 18 characters… I’m amazed
people are coming up with such short non-cheat versions :slight_smile:

Regards,

Bill

I, on the other hand, am looking forward to the various “cheating”
solutions. I re-learned some operator precedence rules solving this
the old fashioned way, but I expect I’ll learn more from solutions
that produce, for example, zero-length strings, but still pass the
tests.

But then I’ve come to expect such wizardry from some of the submitters
to this quiz…

-A

On 11 Jul 2008, at 20:25, Alex LeDonne wrote:

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

I’ve been trying the short representations that are just plain old
strings thing too. I’ve got it down to

symbolify(9999).length == 48
symbolify(999999).length == 87
symbolify(12345678901234567890).length == 221
symbolify
(1234567899999999999999999999999999999999999901234567890).length ==796
symbolify((“9”*2100).to_i).length == 33219

not quite the prettiest code you’ve ever seen though :slight_smile:

Fred

not quite the prettiest code you’ve ever seen though :slight_smile:

Or even
symbolify(9999).length == 37
symbolify(999999).length == 48
symbolify(12345678901234567890).length == 275
symbolify
(1234567899999999999999999999999999999999999901234567890).length ==640
symbolify((“9”*2100).to_i).length == 26762

Enough from me!
Fred

Or even
symbolify(9999).length == 37
symbolify(999999).length == 48
symbolify(12345678901234567890).length == 275
symbolify
(1234567899999999999999999999999999999999999901234567890).length ==640
symbolify((“9”*2100).to_i).length == 26762

Well, well.

I tried the following:

p (0..10000).to_a.inject(0) {|a, i|
    s = symbolify(i)
    x = eval(s)
    raise "Decode failed! #{i} != #{x} : #{s}" unless i == x
    a + s.size
}

For my current solution, this prints: 556136

From: “Alex LeDonne” [email protected]

But then I’ve come to expect such wizardry from some of the submitters
to this quiz…

Although I’d mulled over a slightly more clever cheat, I went
with a totally unclever, groan-inducing approach. It’s a
Kobayashi Maru solution…

(c.f. http://echosphere.net/star_trek_insp/insp_kobayashi.jpg )

On the other hand, my non-cheat solution is currently 70
characters, with whitespace squeezed out… :confused:

And it has to run with ulimit -s unlimited or it will die with
(eval):1: stack level too deep (SystemStackError) before it even
gets to 1000… lol

Oh well . . . :slight_smile:

Regards,

Bill

2008/7/12 Frederick C. [email protected]:

For my current solution, this prints: 556136

419100 for me. I’m pretty sure I’ve got the odd extra set of parentheses I
don’t really need so it should be possible to shave it down a little.

Fred

My results for the short encoding:

symbolify(9999).length == 43
symbolify(999999).length == 52
symbolify(12345678901234567890).length == 224
symbolify(1234567899999999999999999999999999999999999901234567890).length
== 636
symbolify((‘9’*2100).to_i).length == 25724
(0…10000).to_a.inject(0) {|a, i| a + symbolify(i).length} == 400312

Only after the second test it begins to catchup. This also has allot
of extra parentheses so expect better result! :slight_smile:

I wrote some simple testcode that might be useful:

[
“symbolify(9999).length”,
“symbolify(999999).length”,
“symbolify(12345678901234567890).length”,
“symbolify(1234567899999999999999999999999999999999999901234567890).length”,
“symbolify((‘9’*2100).to_i).length”,
“(0…10000).to_a.inject(0) {|a, i| a + symbolify(i).length}”
].each do |test|
print test + " == "
$stdout.flush
puts (eval test).to_s
end

Wouter

On 12 Jul 2008, at 09:39, Wouter S. wrote:

Only after the second test it begins to catchup. This also has allot
of extra parentheses so expect better result! :slight_smile:

Yup, I shaved some off mine:
symbolify(9999).length == 37
symbolify(999999).length == 48
symbolify(12345678901234567890).length == 266
symbolify
(1234567899999999999999999999999999999999999901234567890).length == 645
symbolify((‘9’*2100).to_i).length == 25295
(0…10000).to_a.inject(0) {|a, i| a + symbolify(i).length} == 396700

feeling like diminishing returns at this point

On Jul 11, 12:17 pm, Matthew M. [email protected] wrote:

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

Symbolify (#169)

eval(symbolify(1200))
p symbolify(999).length
p symbolify(9999).length
p symbolify(999999).length
p symbolify(12345678901234567890).length
p symbolify((“9"2100).to_i).length
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!”

outputs:
3
4
6
20
2100
Passed!

Now I’m going for the no cheating.

Lucas

On Jul 11, 2008, at 12:25 , Alex LeDonne wrote:

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

I’m not a good golfer by any means… Hell, I preach against it. My
original solution is a LOT longer than any of the numbers (size of
code) given here, it doesn’t cheat… I suspect it is faster than any
of the golfed solutions tho. But seeing Ara and James and folks
mention the size of their solutions just blew me away. I knew I was
doing something terribly wrong. Or not. I dunno…

That said, I just wrote a version that cheats heavily and came up with:

p symbolify(999).length # => 5
p symbolify(9999).length # => 6
p symbolify(999999).length # => 9
p symbolify(12345678901234567890).length # => 28
p symbolify((“9”*2100).to_i).length # => 3005

and the total size for 0…10k is 56101.

I still think my other solution is better, despite being much much
bigger and having much bigger output. It’ll be a ton faster for wide
ranges of values, and it doesn’t cheat.

23 characters symbolify(i) body, no cheating.

On Fri, Jul 11, 2008 at 11:17 AM, Matthew M. [email protected]
wrote:

1000.times do |i|
don’t expect everyone to have the same output. Well, not before the


Matthew M. [email protected]

My golfed solutions (with whitespace for readability):

def symbolify_1(i)
# natural numbers only
“??-??” + “-?(–?)” * i
end

def symbolify_2(i)
# all integers
“??-??” + (i<0 ? “-?)–?(” : “-?(–?)”) * i.abs
end

-A

On Jul 13, 2008, at 10:17 AM, Alex LeDonne wrote:

My golfed solutions (with whitespace for readability):

def symbolify_1(i)

natural numbers only

“??-??” + “-?(–?)” * i
end

Mine was similar to this:

def symbolify(n)
“??-??” + “–(?)-?()” * n
end

James Edward G. II