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

  • Thread starter email55555 email55555
  • Start date
E

email55555 email55555

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
 
J

Jannis Harder

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

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

email55555 email55555

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
 
F

Florian Gross

email55555 said:
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
 
J

Jannis Harder

-----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+tXn6V2tMahXhOACgx3Km
5tUh3zTWjIpRjbZDgQ6WUX8=
=HbPU
-----END PGP SIGNATURE-----
 
J

Jannis Harder

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

email55555 email55555

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
 
J

Jannis Harder

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

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

Jannis Harder

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

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

Jannis Harder

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

email55555 email55555

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
 
E

email55555 email55555

264 bytes.

:-( gmail wrap again my program.

the 2 lines
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}

should be one line instead of two ( it should join together like)
... :(u[h=x*4/3]>o ?f:"|")+ ...


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
 
D

Dick Davies

That's what I love about ruby - the readability of its code :D

* email55555 email55555 said:
264 bytes.

:-( gmail wrap again my program.

the 2 lines
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}

should be one line instead of two ( it should join together like)
... :(u[h=x*4/3]>o ?f:"|")+ ...
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
 
T

Thomas Kirchner

--PNTmBPCT7hxwcZjr
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

* On Jan 10 21:02, Dick Davies ([email protected]) thusly jotted:
That's what I love about ruby - the readability of its code :D

I know that's what I love about it - that both are equally possible and
come naturally to different people. We have both the Jannis Harders and
the David Heinemeier Hanssons. :)

I tend to switch between the two extremes based on the task at hand.
Concise code is to be valued for certain things, elegant code for others,
and if you can get both - well, then you're golden. Not many languages
other than Ruby can accomplish that ;)
Tom

--PNTmBPCT7hxwcZjr
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFB4n1x/rVdTqQq7OwRAstJAKDNEC7jtjOuyzrwIr4ShDgjbjO/fwCdFMMv
khkQ76URv/E0O3vWZF1sbbw=
=PoL/
-----END PGP SIGNATURE-----

--PNTmBPCT7hxwcZjr--
 
J

James Edward Gray II

* On Jan 10 21:02, Dick Davies ([email protected]) thusly jotted:

I know that's what I love about it - that both are equally possible and
come naturally to different people. We have both the Jannis Harders
and
the David Heinemeier Hanssons. :)

Just to be clear, I'm more in line with David's approach and so is Ruby
Quiz, by extension.

I'm not trying to stop or degrade the the golf game going on here. I
have enjoyed looking over the ever shrinking offerings and I've listed
every one of them on the quiz page.

However, that's probably all the attention they'll get from me. I
wouldn't break them down in my summary, for example. It's nothing
personal. My interests are just different.

Don't let that stop you though. Golf away, as long as your enjoying
yourself!

(This has been in the Ruby Quiz FAQ since day one, but I wanted to make
my position clear since this is the first time it has come up.)

James Edward Gray II
 
T

Thomas Kirchner

--2fHTh5uZTiUOsy+g
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* On Jan 11 0:02, James Edward Gray II ([email protected]) thusly jo=
tted:
Just to be clear, I'm more in line with David's approach and so is Ruby= =20
Quiz, by extension.
=20
[...]
=20
Don't let that stop you though. Golf away, as long as your enjoying=20
yourself!

That was my idea - Ruby allows everyone to express themselves and have fun
programming. I wasn't (necessarily) encouraging byte-size code for the
Quiz, nor overly wordy code for other things. I haven't yet found another
language that allows the user to decide their own style as well as Ruby.

This ties in to the thread about braces - I personally prefer {} to do/end,
where possible. Just another nicety. (Thanks Matz!)
Tom

--2fHTh5uZTiUOsy+g
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFB4rId/rVdTqQq7OwRAsFpAKCLeFiPXR7AOB1VuRsRZLxsaUGk3QCfRZoH
NIW0Yvb4ZlFF2xIg2f++rI0=
=GH35
-----END PGP SIGNATURE-----

--2fHTh5uZTiUOsy+g--
 
D

David Tran

256 bytes.

Shrik again from 264 bytes. New idea again.
I agree, at this point, this program is not readable at all.

If you like a readable version,
please check my solution #1
( maybe it is not readable either :) )

What I did is I try to make a as small as possible program for the solution.
I have fun for this solution is about its size,
not the readable / maintenace / performance ... etc.

There are many ways to play, I just try to take other way and have fun.
(Of couse, if this is my job, I would not solve as it right now :) )

OK, anyway, like it or not; here is the 256 bytes program.
( Not the gmail wrap line, it is this mailing list wrap my code.
So this time, I list the line number as [n],
if the program wrapped, join the line together yourself,
sometime the line is wrap when a blank character to separate "word",
so when join line, you may need add a blank character)

[1] s=(s=$*.index"-s")?$*.slice!(s,2)[1].to_i: 2
[2] d,="\21\265\22H\245\10-\0\23".unpack"B*"
[3] f=" "
[4] o=48
[5] 5.times{|x|((t=x%2==0)?1:s).times{puts
$*[0].split('').map{|z|u=d[7*z.to_i,7]
[6] t ?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}}
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top