[QUIZ] Symbolify (#169)

A

ara.t.howard

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 ;-)


a @ http://codeforpeople.com/
 
A

ara.t.howard

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!"

52 chars for this attm

a @ http://codeforpeople.com/
 
A

Alex LeDonne

I managed to squeeze my solution in 19 characters and 23 with normal
whitespace usage.

Wouter Smeenk

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
 
G

Glen F. Pankow

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

eval(symbolify(1200))

James Edward Gray II

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

Glen Pankow
 
B

Bill Kelly

From: "Alex LeDonne said:
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...

Wow... Nice...

My ultra cheezy cheat version is 18 characters... I'm amazed
people are coming up with such short non-cheat versions :)


Regards,

Bill
 
A

Alex LeDonne

Wow... Nice...

My ultra cheezy cheat version is 18 characters... I'm amazed
people are coming up with such short non-cheat versions :)


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
 
F

Frederick Cheung

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 :)

Fred
 
B

Bill Kelly

From: "Alex LeDonne said:
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...

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... <grin>

(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... :/

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


Regards,

Bill
 
F

Frederick Cheung

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 :)

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
 
T

ThoML

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
 
F

Frederick Cheung

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

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
 
W

Wouter Smeenk

2008/7/12 Frederick Cheung said:
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! :)

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
 
F

Frederick Cheung

Only after the second test it begins to catchup. This also has allot
of extra parentheses so expect better result! :)
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

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
 
K

krusty.ar

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## 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
 
R

Ryan Davis

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.
 
A

Alex LeDonne

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

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

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
 
J

James Gray

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 Gray II
 
C

Chris Shea

Mine was similar to this:

def symbolify(n)
"??-??" + "--(?)-?()" * n
end

James Edward Gray II

I had similar solutions (but a smidge longer). Here are some
cheaters:

# Fix up muliplication so that it just returns the passed
# number. Every integer is encoded as '?**?*'. Fails the
# deylayed eval test.
def symbolify_m(i)
Fixnum.class_eval <<-EOD
def *(other)
#{i}
end
EOD
'?**?*'
end


# Fix up eval so that it just returns the passed argument.
# Every integer is encoded as the empty string. Fails the
# delayed eval test. Aliased so that you can still use irb.
def symbolify_e(i)
Kernel.class_eval <<-EOD
alias_method :cheated_eval, :eval
def eval(*args)
if args.size == 1 and args.first == ''
#{i}
else
cheated_eval(*args)
end
end
EOD
''
end

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top