Velocity Reviews > Ruby > [SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 )

# [SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 )

email55555 email55555
Guest
Posts: n/a

 01-09-2005
Wow ... Jannis Harder's solution is really short ....

This is base on Jannis Harder's solution (398 bytes) ...
We could make Jannis Harder's solution even shorter, for example:
(1) y.map!{|v|v<<32} could rewrite to y.map!{|v|v<<f} ==> save 1 byte
(2) many ?: code like ?x: y could rewrite to ?x:y ( no need space
before y) ==> save n bytes
(3) and ... (see my code about my idea to make it even shorter )

Here is my even shorter solution ( base on Jannis Harder's solution )
it is 349 bytes ( 49 bytes shorter than Jannis Harder's solution )

s=\$*.index("-s");s=(s ?\$*.slice!(s,2)[1].to_i: 2)
i=\$*.join.split("").map{|x|x.to_i}
d,="7krtt1r30v/s".unpack("m")[0].unpack("B*")
f=" "
a,b=f*s,f+"-"*s+f
c,e,o="|",a+f+f,49
y=([0]*5).map{""}
i.each{|z|u=d[z*7,7]
3.times{|x|y[x*2]<<(u[x*3]<o ?e:b)<<f}
[1,3].each{|x|y[x]<<(u[x==1?1:4]<o ?f:c)+a+(u[x==1?2:5]<0 ?f:c)<<f}}
y[1,1]*=s
y[2+s,1]*=s
puts y

Jannis Harder
Guest
Posts: n/a

 01-09-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The digits 2 5 and 6 doesn't work
Input: 0123456789
Output:
~ -- -- -- -- -- -- -- --
| | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | |
~ -- -- -- -- -- -- --
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
~ -- -- -- -- -- -- --
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB4ZY35YRWfc27RzQRAsedAJ43X22BPxJg62iBJln0v9 ezQ9si7wCeMaXe
9K2BAuj4VqY9kg5nK8Wn4J4=
=LvK9
-----END PGP SIGNATURE-----

email55555 email55555
Guest
Posts: n/a

 01-09-2005
Thanks Jannis Harder found the bug.
It is still 349 bytes program:
( the bug is on "u[x==1?2:5]<0", it should be "u[x==1?2:5]<o", typo

s=\$*.index("-s");s=(s ?\$*.slice!(s,2)[1].to_i: 2)
i=\$*.join.split("").map{|x|x.to_i}
d,="7krtt1r30v/s".unpack("m")[0].unpack("B*")
f=" "
a,b=f*s,f+"-"*s+f
c,e,o="|",a+f+f,49
y=([0]*5).map{""}
i.each{|z|u=d[z*7,7]
3.times{|x|y[x*2]<<(u[x*3]<o ?e:b)<<f}
[1,3].each{|x|y[x]<<(u[x==1?1:4]<o ?f:c)+a+(u[x==1?2:5]<o ?f:c)<<f}}
y[1,1]*=s
y[2+s,1]*=s
puts y

On Sun, 9 Jan 2005 15:13:25 -0500, email55555 email55555
<(E-Mail Removed)> wrote:
> Wow ... Jannis Harder's solution is really short ....
>
> This is base on Jannis Harder's solution (398 bytes) ...
> We could make Jannis Harder's solution even shorter, for example:
> (1) y.map!{|v|v<<32} could rewrite to y.map!{|v|v<<f} ==> save 1 byte
> (2) many ?: code like ?x: y could rewrite to ?x:y ( no need space
> before y) ==> save n bytes
> (3) and ... (see my code about my idea to make it even shorter )
>
> Here is my even shorter solution ( base on Jannis Harder's solution )
> it is 349 bytes ( 49 bytes shorter than Jannis Harder's solution )
>
> s=\$*.index("-s");s=(s ?\$*.slice!(s,2)[1].to_i: 2)
> i=\$*.join.split("").map{|x|x.to_i}
> d,="7krtt1r30v/s".unpack("m")[0].unpack("B*")
> f=" "
> a,b=f*s,f+"-"*s+f
> c,e,o="|",a+f+f,49
> y=([0]*5).map{""}
> i.each{|z|u=d[z*7,7]
> 3.times{|x|y[x*2]<<(u[x*3]<o ?e:b)<<f}
> [1,3].each{|x|y[x]<<(u[x==1?1:4]<o ?f:c)+a+(u[x==1?2:5]<0 ?f:c)<<f}}
> y[1,1]*=s
> y[2+s,1]*=s
> puts y
>

Florian Gross
Guest
Posts: n/a

 01-09-2005
email55555 email55555 wrote:

> Thanks Jannis Harder found the bug.
> It is still 349 bytes program:
> ( the bug is on "u[x==1?2:5]<0", it should be "u[x==1?2:5]<o", typo
> error "o" instead of "0")
>
> s=\$*.index("-s");s=(s ?\$*.slice!(s,2)[1].to_i: 2)

Why the spaces? Why the parentheses?
s=\$*.index "-s";s=s?\$*.slice!(s,2)[1].to_i:2

> i=\$*.join.split("").map{|x|x.to_i}
> d,="7krtt1r30v/s".unpack("m")[0].unpack("B*")
> f=" "
> a,b=f*s,f+"-"*s+f
> c,e,o="|",a+f+f,49
> y=([0]*5).map{""}

y=(1..5).map{""}

> i.each{|z|u=d[z*7,7]

i.map

> 3.times{|x|y[x*2]<<(u[x*3]<o ?e:b)<<f}

space after o

> [1,3].each{|x|y[x]<<(u[x==1?1:4]<o ?f:c)+a+(u[x==1?2:5]<o ?f:c)<<f}}

[1,3].map, space after o

Jannis Harder
Guest
Posts: n/a

 01-09-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

And shorter and shorter and:
Based on my solution and the ideas from email55555
1. Removed split("").map..... I'm using each_byte now ( z*7 to z*7-336)
2. replaced x==1?1:4 with h=x*4/3
2. replaced x==1?2:5 with h+1
3. replaced <<f with +f
4. repalced
~ y[1,1]*=s
~ y[2+s,1]*=s
with
~ y[3,1]*=s
~ y[1,1]*=s

323 byte:
### (this is 100bytes shorter than my first solution)
s=\$*.index("-s");s=(s ?\$*.slice!(s,2)[1].to_i: 2)
i=\$*.join
d,="7krtt1r30v/s".unpack("m")[0].unpack("B*")
f=" "
a,b=f*s,f+"-"*s+f
c,e,o="|",a+f+f,49
y=([0]*5).map{""}
i.each_byte{|z|u=d[z*7-336,7]
3.times{|x|y[x*2]<<(u[x*3]<o ?e:b)+f}
[1,3].each{|x|y[x]<<(u[h=x*4/3]<o ?f:c)+a+(u[h+1]<o ?f:c)+f}}
y[3,1]*=s
y[1,1]*=s
puts y
###
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB4Z4a5YRWfc27RzQRAki9AJ4jtcBX1JueoJM+tXn6V2 tMahXhOACgx3Km
5tUh3zTWjIpRjbZDgQ6WUX8=
=HbPU
-----END PGP SIGNATURE-----

Jannis Harder
Guest
Posts: n/a

 01-09-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Florian Gross schrieb:

| email55555 email55555 wrote:
|
|> Thanks Jannis Harder found the bug. It is still 349 bytes
|> program: ( the bug is on "u[x==1?2:5]<0", it should be
|> "u[x==1?2:5]<o", typo error "o" instead of "0")
|>
|> s=\$*.index("-s");s=(s ?\$*.slice!(s,2)[1].to_i: 2)
|
|
| Why the spaces? Why the parentheses? s=\$*.index
| "-s";s=s?\$*.slice!(s,2)[1].to_i:2

spaces:
~ syntax error without " "
paranthese:
~ no reasen

|> i=\$*.join.split("").map{|x|x.to_i}
|> d,="7krtt1r30v/s".unpack("m")[0].unpack("B*") f=" "
|> a,b=f*s,f+"-"*s+f c,e,o="|",a+f+f,49 y=([0]*5).map{""}
|
|
| y=(1..5).map{""}
|
|> i.each{|z|u=d[z*7,7]
|
|
| i.map

each_byte is shorter

|> 3.times{|x|y[x*2]<<(u[x*3]<o ?e:b)<<f}
|
|
| space after o

again, syntax error without " "

|
|> [1,3].each{|x|y[x]<<(u[x==1?1:4]<o ?f:c)+a+(u[x==1?2:5]<o
|> ?f:c)<<f}}
|
|
| [1,3].map, space after o

good idea, syntax error without

315 bytes:
###
s=\$*.index"-s";s=s ?\$*.slice!(s,2)[1].to_i: 2
i=\$*.join
d,="7krtt1r30v/s".unpack("m")[0].unpack"B*"
f=" "
a,b=f*s,f+"-"*s+f
c,e,o="|",a+f+f,49
y=(0..4).map{""}
i.each_byte{|z|u=d[z*7-336,7]
3.times{|x|y[x*2]<<(u[x*3]<o ?e:b)+f}
[1,3].map{|x|y[x]<<(u[h=x*4/3]<o ?f:c)+a+(u[h+1]<o ?f:c)+f}}
y[3,1]*=s
y[1,1]*=s
puts y
###

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB4aSP5YRWfc27RzQRAp6hAKCXC/2an6/KJVIqUqLckEiTd5MdmgCfccJ6
c/X941EiNfnfZXooYplDinY=
=hm7K
-----END PGP SIGNATURE-----

email55555 email55555
Guest
Posts: n/a

 01-09-2005
303 bytes.

s=\$*.index"-s";s=s ?\$*.slice!(s,2)[1].to_i: 2
d,="7krtt1r30v/s".unpack("m")[0].unpack"B*"
f=" "
a,c,o=f*s,"|",49
y=(0..4).map{""}
\$*.join.each_byte{|z|u=d[z*7-336,7]
3.times{|x|y[x*2]<<(u[x*3]<o ?a+f+f:f+"-"*s+f)+f}
[1,3].map{|x|y[x]<<(u[h=x*4/3]<o ?f:c)+a+(u[h+1]<o ?f:c)+f}}
y[3,1]*=s
y[1,1]*=s
puts y

Jannis Harder
Guest
Posts: n/a

 01-09-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

### 300 byte
s=\$*.index"-s";s=s ?\$*.slice!(s,2)[1].to_i: 2
d,="\21\265\22H\245\10-\0\23".unpack"B*"
f=" "
a,c,o=f*s,"|",48
y=(0..4).map{""}
\$*.join.each_byte{|z|u=d[z*7-336,7]
3.times{|x|y[x*2]<<(u[x*3]>o ?a+f+f:f+"-"*s+f)+f}
[1,3].map{|x|y[x]<<(u[h=x*4/3]>o ?f:c)+a+(u[h+1]>o ?f:c)+f}}
y[3,1]*=s
y[1,1]*=s
puts y
###

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB4a9R5YRWfc27RzQRAu2zAJ9gyN2ynBAXjJfAydB6Dc OnM8qvLACdGrRp
Ia1yd6iIt6nuzgGm6hfUsEY=
=FH5t
-----END PGP SIGNATURE-----

Jannis Harder
Guest
Posts: n/a

 01-09-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

### 296 byte
s=\$*.index"-s";s=s ?\$*.slice!(s,2)[1].to_i: 2
d,="\21\265\22H\245\10-\0\23".unpack"B*"
f=" "
o=48
y=(0..4).map{""}
\$*.join.each_byte{|z|u=d[z*7-336,7]
3.times{|x|y[x*2]<<(u[x*3]>o ?f*s+f+f:f+"-"*s+f)+f}
[1,3].map{|x|y[x]<<(u[h=x*4/3]>o ?f:"|")+f*s+(u[h+1]>o ?f:"|")+f}}
y[3,1]*=s
y[1,1]*=s
puts y
###
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB4bF45YRWfc27RzQRAj1JAJ9cyNpT9jPZs6lfeSpjQX Vi0DdsGgCghgxV
OjbUzEZgtrXxshx+gMTyUAE=
=5vsG
-----END PGP SIGNATURE-----

Jannis Harder
Guest
Posts: n/a

 01-09-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

### 295 bytes
s=(s=\$*.index"-s")?\$*.slice!(s,2)[1].to_i: 2
d,="\21\265\22H\245\10-\0\23".unpack"B*"
f=" "
o=48
y=(0..4).map{""}
\$*.join.each_byte{|z|u=d[z*7-336,7]
3.times{|x|y[x*2]<<(u[x*3]>o ?f*s+f+f:f+"-"*s+f)+f}
[1,3].map{|x|y[x]<<(u[h=x*4/3]>o ?f:"|")+f*s+(u[h+1]>o ?f:"|")+f}}
y[3,1]*=s
y[1,1]*=s
puts y
###
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB4bRt5YRWfc27RzQRAkowAJ0d43DuRrlg/+9wUK3qlQqUKzBCaACgngNn
tM4bVurLPrHZY6nmuJ8k91A=
=fQm/
-----END PGP SIGNATURE-----