Skip to content

Commit d1debfc

Browse files
authored
moo: raise error for size>10
* The maximum number of distinct digits is 10 * This version didn't raise an error for larger values of size parameter * Based on commit f3248c8 in bin/fish, add entropy thanks to hash keys being "unordered"
1 parent 0a69975 commit d1debfc

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

bin/moo

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ use strict;
1515

1616
use List::Util qw(shuffle);
1717

18-
my ($VERSION) = '1.3';
18+
my ($VERSION) = '1.4';
1919

2020
sub usage {
2121
die "usage: moo [size]\n";
2222
}
2323

24+
sub get_secret {
25+
my $size = shift;
26+
27+
my $i = $size - 1;
28+
my %digits = map { $_ => 1 } (0 .. 9);
29+
my @s1 = keys %digits;
30+
my @s2 = shuffle(@s1);
31+
return @s2[0 .. $i];
32+
}
33+
2434
sub has_dupe {
2535
my $guess = shift;
2636
my %chars;
@@ -34,12 +44,12 @@ sub has_dupe {
3444
my $size = shift;
3545
$size = 4 unless defined $size;
3646
usage() if $size !~ m/\A[0-9]+\Z/ or !$size;
47+
die "secret size must be within range: 1-10\n" if $size > 10;
3748
usage() if @ARGV;
3849

3950
print "MOO\n";
4051
{
41-
my @secret = shuffle(0 .. 9);
42-
@secret = splice @secret, 0, $size;
52+
my @secret = get_secret($size);
4353
my @secret_by_value = (0) x 10;
4454
foreach my $i (@secret) {
4555
$secret_by_value[$i] = 1;

0 commit comments

Comments
 (0)