Thread Buchstaben generieren: Bruteforce-aehnlich (14 answers)
Opened by styx-cc at 2006-11-03 00:46

Ronnie
 2006-11-14 17:59
#71326 #71326
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Hier noch eine etwas aufgepumpte OOPerl Variante:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/perl

use strict;
use warnings;

package Wheel;

sub new {
my $class = shift;
my @items = @_;
my $self = {
items => \@items,
position => 0,
notch => [],
};
bless $self, $class;
return $self;
}

sub on_notch {
my $self = shift;
my $call = shift || sub { print "Notch!\n" };
push @{$self->{notch}}, $call;
}

sub succ {
my $self = shift;
if ($self->{position}++ < $#{$self->{items}}) {
$self->{position} and return $self->{items}->[$self->{position}];
} else {
$self->{position} = 0;
$_->() for @{$self->{notch}};
return $self->{items}->[$self->{position}];
}

}

sub to_s {
my $self = shift;
return $self->{items}->[$self->{position}];
}

package Odometer;
use Data::Dumper;

my $DONE = 0;

sub new {
my $class = shift;
my $self = [];
return bless $self, $class;
}

sub add_wheel {
my $self = shift;
my @items = @_;
my $w = Wheel->new(@items);
push @$self, $w;
$self->[$#$self-1]->on_notch(sub {$w->succ}) if $#$self > 0;
}

sub succ {
my $self = shift;
my $temp = $self->to_s;
$self->[0]->succ;

if ($DONE == 2) {
return undef;
} else {
$DONE++ if $DONE;
return $temp;
}
}

sub to_s {
my $self = shift;
return join ' ', map { $_->to_s } reverse @$self;
}

sub add_termination {
my $self = shift;
$self->[-1]->on_notch( sub { $DONE = 1} );
}

package main;

my $o = Odometer->new;
#$o->add_wheel(qw/red green blue yellow/);
$o->add_wheel(1, 3, 5) for (1..3);
#$o->add_wheel(2, 4);
$o->add_termination;
while (my $cur = $o->succ) {
print $cur . "\n";
};

View full thread Buchstaben generieren: Bruteforce-aehnlich