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 Papers
Ok, I finally checked the full explanation. Here's my sample code to solve the assignment:
#!/usr/bin/perl -l
use strict;
use warnings;
my @pack=1..8;
my %disp;
@disp{qw/b i o/}=map {
my $t=$_;
sub { @_[@$t] };
} [1..7,0],
[4,0,5,1,6,2,7,3],
[0,4,1,5,2,6,3,7];
sub parse {
for (@_) {
my $c;
s/[()]/
$& eq '(' ?
'(=' . $c++ . '=' :
'=' . --$c . '=)' /ge;
s/(\d)([bio])/$2 x $1/ge;
1 while s/(\d)
\(=(\d+)=
(.+?)
=\2=\)/$3 x $1/gex;
}
}
while (<>) {
parse $_;
@pack=$disp{$_}->(@pack) for /./g;
print "@pack";
}
__END__
Please note that:
- I don't do any input validation, for the rules partly explicitly partly implicitly state that I must trust it,
- this is certainly not the most efficient way to do this, but it is IMHO adequate to the nature of the problem, as of the description,
- Also, the sub parse() is by no means necessary, but I wanted to keep it separate it from the main loop.
HTH.
- Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
- Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
-
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
-
Please read these before you post! —
-
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
-
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
- Link using PerlMonks shortcuts! What shortcuts can I use for linking?
-
See Writeup Formatting Tips and other pages linked from there for more info.