bacek's Journal
http://use.perl.org/~bacek/journal/
bacek's use Perl Journal
en-us
use Perl; is Copyright 1998-2006, Chris Nandor. Stories, comments, journals, and other submissions posted on use Perl; are Copyright their respective owners.
2012-01-25T02:46:45+00:00
pudge
pudge@perl.org
Technology
hourly
1
1970-01-01T00:00+00:00
bacek's Journal
http://use.perl.org/images/topics/useperl.gif
http://use.perl.org/~bacek/journal/
-
Scripting Games, part 3
http://use.perl.org/~bacek/journal/38148?from=rss
<p>Next one: <a href="http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent2.mspx">find winner of skater contest</a>.</p><p>
# Calculate score for single result<br>
sub calculate_score(@scores) {<br>
# Sort scores<br>
my @sorted = sort @scores;<br>
# Drop highest and lowest<br>
shift @sorted;<br>
pop @sorted;<br>
# Calculate final score<br>
([+] @sorted)/ +@sorted;<br>
};</p><p>
# Parse line in form "Ken Myer,55,66,76,67,59,70,54", and return (score, name)<br>
sub parse_line($line) {<br>
my @parts = $line.split(',');<br>
reverse(shift @parts, calculate_score(@parts));<br>
};</p><p>
# Main cycle. Read file, parse lines, build mapping score=>name<br>
my $fh = open('skaters.txt') or die;<br>
my %results = map &parse_line, =$fh;</p><p>
# Get 3 top results<br>
my @top = %results.keys.sort.reverse[0..2];<br>
# And show them<br>
say %results{$_}, ': ', $_ for @top;</p><p>I don't know what to say. Solution is straight forward.</p>
bacek
2008-12-24T07:31:03+00:00
journal
-
Scripting Games, part 2
http://use.perl.org/~bacek/journal/38147?from=rss
<p>Beginner's events are way too easy. Let's take a look at "advanced".</p><p>http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent1.msp<nobr>x<wbr></nobr> </p><p>
# First thing first: build letters-to-digits map<br>
my $pos = 2;<br>
my %l2d;<br>
map { %l2d{$^a} = $pos; %l2d{$^b} = $pos; %l2d{$^c} = $pos++ }, 'a'..'p', 'r'..'y';</p><p>
# Calculate number representation of word<br>
sub read_word($word) {<br>
# join digit for given letters<br>
[~] map { %l2d{ lc $^a } }, $word.split('');<br>
};</p><p>
# Read wordlist and returns hash (number => word).<br>
# Also skips all non-7 chars words<br>
sub read_wordlist($filename) {<br>
my $fh = open($filename) or die;<br>
map { read_word($^a) => $^a }, grep { $^a.chars == 7 }, =$fh;<br>
};</p><p>
# Read wordlist file.<br>
my %words = read_wordlist('wordlist.txt');</p><p>
# And say word corresponded to number.<br>
for =$*IN {<br>
say uc %words{$_};<br>
}</p><p>Funny note: there is no 'scripts' in wordlist.txt<nobr> <wbr></nobr>:)</p>
bacek
2008-12-24T07:01:03+00:00
journal
-
First post.
http://use.perl.org/~bacek/journal/38146?from=rss
<p>Hi.</p><p>In response to Pm's <a href="http://use.perl.org/~pmichaud/journal/38134">"Scripting games in Perl6"</a> I've decided to implement some of them in Perl6.</p><p>Let's start with easiest <a href="http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/bevent1.mspx"> "2008: Beginners Event 1"</a></p><p>Task is to calculate number of different pairs of cards.</p><p>
sub fact($n) {<br>
$n <= 1 ?? 1 !! $n*fact($n-1);<br>
}</p><p>
sub calculate_pairs(@pairs) {<br>
my %h;<br>
# Calculate number of distinct cards.<br>
++%h{$_} for @pairs;<br>
# Sum of C^N_2<br>
[+] map { int(fact($^a) / 2) }, %h.values;<br>
};</p><p>Trivial mathematical solution: number of different pairs for given amount of cards is C^n_2. So, I group cards, calculate number of combinations in different group and sum them.</p><p>Little trick: if number of cards in group equals to 1 than int(...) will return 0.</p><p>Update: masak++ for pointing to use.perl.org strange reaction on <=</p>
bacek
2008-12-24T06:11:24+00:00
journal