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



freddo
User

Feb 10, 2002, 7:21 AM


Views: 71713
I lended money to my 'coin'puter... (GOLF)

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


Views: 71708
Re: [freddo] I lended money to my 'coin'puter... (GOLF)

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


Views: 71667
Re: [mhx] I lended money to my 'coin'puter... (GOLF)

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


Views: 71665
Re: [mhx] I lended money to my 'coin'puter... (GOLF)


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


Views: 71661
Re: [RedRum] I lended money to my 'coin'puter... (GOLF)

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


Views: 71658
Re: [freddo] I lended money to my 'coin'puter... (GOLF)

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


Coderifous
journeyman

Feb 15, 2002, 2:00 PM


Views: 71650
Re: [mhx] I lended money to my 'coin'puter... (GOLF)

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


Views: 71646
Re: [Coderifous] I lended money to my 'coin'puter... (GOLF)


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


Views: 71642
Re: [mhx] I lended money to my 'coin'puter... (GOLF)

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


Views: 71640
Re: [Coderifous] I lended money to my 'coin'puter... (GOLF)


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


Views: 71637
Re: [mhx] I lended money to my 'coin'puter... (GOLF)

CoolSmile

Thanks alot Marcus.

--Jim


iM
New User

Jul 14, 2007, 5:40 AM


Views: 69479
Re: [freddo] I lended money to my 'coin'puter... (GOLF)

 

Code
# formatted output: 

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