Diffstat (limited to 'noncore/games/wordgame/calcdist') (more/less context) (show whitespace changes)
-rwxr-xr-x | noncore/games/wordgame/calcdist | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/noncore/games/wordgame/calcdist b/noncore/games/wordgame/calcdist new file mode 100755 index 0000000..faf31f1 --- a/dev/null +++ b/noncore/games/wordgame/calcdist | |||
@@ -0,0 +1,27 @@ | |||
1 | #!/usr/bin/perl | ||
2 | |||
3 | # Usage: cat dictionaries | grep -v '[^a-z]' | calcdist n score | ||
4 | # | ||
5 | # Given a lot of words, find an appropriate distribution | ||
6 | # into n tiles with tile values proportional to the square root | ||
7 | # of the ratio of score to the tile's frequency. | ||
8 | |||
9 | $n = shift; | ||
10 | $score = shift; | ||
11 | |||
12 | while (<>) { | ||
13 | chomp; | ||
14 | for $c ( split "", $_ ) { | ||
15 | $freq{$c}++; | ||
16 | $t++; | ||
17 | } | ||
18 | } | ||
19 | |||
20 | for $c ( sort { $freq{$a} <=> $freq{$b} } keys %freq ) { | ||
21 | #print "$c: $freq{$c}\n"; | ||
22 | $need = int($freq{$c}*$n/$t+0.5) || 1; | ||
23 | $value = int(sqrt($score/($freq{$c}*$n/$t))+0.5) || 1; | ||
24 | $t -= $freq{$c}; | ||
25 | $n -= $need; | ||
26 | print "$need $c $value\n"; | ||
27 | } | ||