[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

email55555 email55555

1/9/2005 8:13:00 PM

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


16 Answers

Jannis Harder

1/9/2005 8:38:00 PM

0

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

iD8DBQFB4ZY35YRWfc27RzQRAsedAJ43X22BPxJg62iBJln0v9ezQ9si7wCeMaXe
9K2BAuj4VqY9kg5nK8Wn4J4=
=LvK9
-----END PGP SIGNATURE-----



email55555 email55555

1/9/2005 8:51:00 PM

0

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)
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
<email55555@gmail.com> 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

1/9/2005 9:07:00 PM

0

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

1/9/2005 9:12:00 PM

0

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

iD8DBQFB4Z4a5YRWfc27RzQRAki9AJ4jtcBX1JueoJM+tXn6V2tMahXhOACgx3Km
5tUh3zTWjIpRjbZDgQ6WUX8=
=HbPU
-----END PGP SIGNATURE-----



Jannis Harder

1/9/2005 9:40:00 PM

0

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

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



email55555 email55555

1/9/2005 10:19:00 PM

0

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

1/9/2005 10:25:00 PM

0

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

iD8DBQFB4a9R5YRWfc27RzQRAu2zAJ9gyN2ynBAXjJfAydB6DcOnM8qvLACdGrRp
Ia1yd6iIt6nuzgGm6hfUsEY=
=FH5t
-----END PGP SIGNATURE-----



Jannis Harder

1/9/2005 10:35:00 PM

0

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

iD8DBQFB4bF45YRWfc27RzQRAj1JAJ9cyNpT9jPZs6lfeSpjQXVi0DdsGgCghgxV
OjbUzEZgtrXxshx+gMTyUAE=
=5vsG
-----END PGP SIGNATURE-----



Jannis Harder

1/9/2005 10:47:00 PM

0

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

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



email55555 email55555

1/10/2005 5:04:00 AM

0

Wow ... Jannis Harder's program is down to 295 bytes ...
Mmm... Let me try to squeeze a little bit more.

First, according to the Quiz, we do not need to take care of multi-mumbers,
so, I change $*.join to $*[0] ==> just take the first number ==> save
me 2 bytes.

Second, I got new idea again ( always base on Jannis Harder's lastest code ),
this time it is down to 264 bytes

( note: run this program without any parameter will fail after replace
$*.join by $*[0],
if this is not acceptable, replace back, so my program will become 266 bytes.
After all, this program likes Jannis Harder's origin, do not do many
error checking;
for example: if input is "lcd.rb -s abc xxx" ==> size invalid will
become size zero,
and any no digit character will become zero digit)

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{|x|$*[0].split('').map{|z|u=d[7*z.to_i,7]
x%2==0 ?u[x/2*3]>o ?f*s+f+f:f+"-"*s+f:(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