CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Golf:
I lended money to my 'coin'puter... (GOLF)

 



freddo
User

Feb 10, 2002, 7:21 AM

Post #1 of 12 (38774 views)
I lended money to my 'coin'puter... (GOLF) Can't Post

Hello Everybody,

This week there was 1_500_000 euros to win in belgium at lotto, and i wanted my computer to participate.... so i came out with this first solution:

Code
perl -e 'while($a<7){$x=int(rand 42)+1;++$l{$x},$a++unless$l{x}}print join" ",keys%l;'


so who's making it shorter?

Freddo

PS: sadly making it shorter wont make it the winner one Crazy...
;---

(This post was edited by freddo on Feb 10, 2002, 7:22 AM)


mhx
Enthusiast / Moderator

Feb 10, 2002, 9:43 AM

Post #2 of 12 (38769 views)
Re: [freddo] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post

What about this one:

Code
perl -e'@_=1..42;print$",splice@_,rand@_,1for 1..7'

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



freddo
User

Feb 14, 2002, 8:39 AM

Post #3 of 12 (38728 views)
Re: [mhx] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post

Hi mhx,

it's damn smart to remove the numbers from the array... i'd never though about such solution... i played with it again and again yesterday, and cannot make it smaller Unsure

I bet i still have a lot of things to learn from perl... and programming, but i guess that's why i like it.

freddo
;---


Paul
Enthusiast

Feb 14, 2002, 9:39 AM

Post #4 of 12 (38726 views)
Re: [mhx] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post


Quote
perl -e'@_=1..42;print$",splice@_,rand@_,1for 1..7'


How about:


Code
@_=1..42;print$_[rand@_-1],for 1..7;



(This post was edited by RedRum on Feb 14, 2002, 9:39 AM)


freddo
User

Feb 14, 2002, 11:03 AM

Post #5 of 12 (38722 views)
Re: [RedRum] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post

Hi Redrum,

Just a note: at lotto you cant get twice the same number... i should have precised it at first probably Tongue
for your version, i found this ($" idea stolen from mhx Wink):
perl -e 'print $",int(rand 42)+1for 1..7'

freddo
;---


Paul
Enthusiast

Feb 14, 2002, 11:22 AM

Post #6 of 12 (38719 views)
Re: [freddo] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post

We don't have lotto in the UK (that's my excuse anyway Smile)


Coderifous
journeyman

Feb 15, 2002, 2:00 PM

Post #7 of 12 (38711 views)
Re: [mhx] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post

I nearly went insane, but I finally did it... it's my crowning achievement in life... I beat mhx (only by three) in perl golf: IF zero's are legal in this lotto. If not, then I just matched him.

$a{int rand 42}=$"while keys%a<7;print%a

If zeroes are not legal then I just matched him at 44.

$a{int rand(42)+1}=$"while keys%a<7;print%a

AND if you want to be real lazy, you could use this one, which is shorter (40), but offers only incremental randomness. Not really what this 'coin'puter was goin for, but technically meets the requirements. I think.

for(1..7){$a+=int rand(6)+1;print"$a "}

So there's my efforts. Feel free to berate me now.

--Jim



mhx
Enthusiast / Moderator

Feb 15, 2002, 3:55 PM

Post #8 of 12 (38707 views)
Re: [Coderifous] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post


Quote
If zeroes are not legal then I just matched him at 44.


Zeroes are not legal... Unsure
And I had 42, actually, while you have 43 Wink


Code
123456789012345678901234567890123456789012345 
@_=1..42;print$",splice@_,rand@_,1for 1..7 # mhx 1
$a{int rand(42)+1}=$"while keys%a<7;print%a # jim 2


What about an optimized (3 strokes less plus a bit more obfuscation) version:


Code
$_{1+~~rand 42}=$"while 7-keys%_;print%_


I even found a way to employ Damian's flashy ~~ operator. It occurred to me that it is a multi-functional operator: it's not only a perfect short form of scalar, but also of the int operator!

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Coderifous
journeyman

Feb 15, 2002, 4:14 PM

Post #9 of 12 (38703 views)
Re: [mhx] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post

PirateNot only have I lost the ability to count, I embarrassed myself too. You have turned my brain to mush marcus. And I thought only my wife could do that.

Marcus: 3524Cool

Jim: 0Frown

Nice use of ~~ It's a perl6 operator right? What the hell does it do? I looked around a bit, but couldn't find anything significant on it. PS - great job again.

--Jim


mhx
Enthusiast / Moderator

Feb 15, 2002, 4:48 PM

Post #10 of 12 (38701 views)
Re: [Coderifous] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post


Quote
Not only have I lost the ability to count, I embarrassed myself too. You have turned my brain to mush marcus. And I thought only my wife could do that.


I'm really sorry for that. I didn't mean to turn your brain to mush. Well, perhaps I did Crazy.


Quote
Nice use of ~~ It's a perl6 operator right?


No, but it looks pretty cool, eh?


Quote
What the hell does it do? I looked around a bit, but couldn't find anything significant on it.


It's actually no special operator (which means it's already available in Perl 5, otherwise my golf entry wouldn't be valid, as we don't play Perl 6 Golf yet). It's just the ~ operator (bitwise negation) applied twice. Damian Conway used this "operator" in his "Extreme Perl" talk I saw two days ago. He used it to force scalar context on an array to get its length. While you'd normally use scalar for that, you can use "~~". One "~" will force the scalar context and the other will just undo what the first one did. So what you get out is the length of the array. What I'm using it for is to force a floating point value into a integer. It works in exactly the same way, just that the "~" forces the conversion to an integer because bitwise operations can only be applied to integers.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Coderifous
journeyman

Feb 15, 2002, 10:42 PM

Post #11 of 12 (38698 views)
Re: [mhx] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post

CoolSmile

Thanks alot Marcus.

--Jim


iM
New User

Jul 14, 2007, 5:40 AM

Post #12 of 12 (36540 views)
Re: [freddo] I lended money to my 'coin'puter... (GOLF) [In reply to] Can't Post

 

Code
# formatted output: 

@m='01'..'42';print$m[rand@m-1].' ',for 1..7;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives