amnesty_puppy has asked for the wisdom of the Perl Monks concerning the following question:
Hey, I'm trying to Perl some BIO past papers (a British programming contest) but I have some results that I can't explain. The program has to take in a string where b, i and o are functions and have coefficients and groupings such as b3(io) does b then io 3 times. Full explanation is here (question two):
BIO Past Papersmy script so far is as follows, you can probaby work out what I'm trying to do here:
#!/usr/bin/perl # Shuffles cards @pack = (1, 2, 3, 4, 5, 6, 7, 8); sub b { push(@pack, shift @pack); } sub i { my ($a, $b, $c, $d, $e, $f, $g, $h) = @pack; @pack = ($e, $a, $f, $b, $g, $c, $h, $d); } sub o { my ($a, $b, $c, $d, $e, $f, $g, $h) = @pack; @pack = ($a, $e, $b, $f, $c, $g, $d, $h); } sub parse { my ($string) = @_; print "\nParse $string: "; foreach $offset (0..(length($string)-1)) { $char = substr($string, $offset, 1); if ($char =~ /b|i|o/) { print "bio "; &{$char}; } elsif ($char =~ /\d/) { print "digit "; # foreach (1..$char) { &parse(substr($string, $offset+1)); +} } elsif ($char =~ /\(/) { print "open "; &parse(substr($string, $offset+1, (rindex($string, ")", $o +ffset)-$offset)+1) ); } elsif ($char eq ")") { print "close "; } } } chomp($shuffle = <STDIN>); &parse($shuffle); print "\n@pack\n";#
I don't know why this prints:
oib(oib) Parse oib(oib): bio bio bio open Parse o: bio bio bio bio close 1 4 5 8 2 3 6 7
when it should print
oib(oib) Parse oib(oib): bio bio bio open Parse oib: bio bio bio # whatever here
Firstly the string doesn't seem to get passed correctly, and then it parses it wrong anyway (too many bios). Anyone wanna try this one?
Thanks, Dom.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Shuffling cards
by blazar (Canon) on Feb 22, 2005 at 14:30 UTC | |
by amnesty_puppy (Novice) on Feb 22, 2005 at 15:17 UTC | |
by blazar (Canon) on Feb 22, 2005 at 15:38 UTC | |
|
Re: Shuffling cards
by blazar (Canon) on Feb 22, 2005 at 15:43 UTC | |
|
Re: Shuffling cards
by Anonymous Monk on Feb 22, 2005 at 17:14 UTC | |
|
Re: Shuffling cards
by blazar (Canon) on Feb 23, 2005 at 12:55 UTC | |
by Anonymous Monk on Feb 23, 2005 at 14:11 UTC | |
by blazar (Canon) on Feb 23, 2005 at 14:36 UTC | |
by Anonymous Monk on Feb 23, 2005 at 15:46 UTC | |
by blazar (Canon) on Feb 24, 2005 at 09:16 UTC | |
|