Skip to content

Instantly share code, notes, and snippets.

@sahrendt0
Created October 24, 2012 06:37
Show Gist options
  • Save sahrendt0/3944407 to your computer and use it in GitHub Desktop.
Save sahrendt0/3944407 to your computer and use it in GitHub Desktop.
GEN 220 Problem Set 4
#!/usr/bin/perl -w
use strict;
my $infile = shift;
open(IN,"<$infile");
my $i = 1;
foreach my $line (<IN>)
{
print "$i : $line";
$i++;
}
close(IN);
#!/usr/bin/perl -w
use strict;
my $f=$[;
my $ch=0;
sub l {
length
}
sub r{join"", reverse split
("",$_[$[])}
sub ss{substr($_[0]
,$_[1],$_[2])}
sub be{$_=$_[0];
p(ss($_,$f,1));
$f+=l()/2;
$f%=l();
$f++ if$ch%2;
$ch++}
my$q=r("\ntfgpfdfal,thg?bngbj"."naxfcixz");
$_=$q;
$q=~tr/f[a-z]/ [l-za-k]/;
my@ever=1..&l;
my$mine=$q;
sub p {print@_;}
be $mine for @ever
#!/usr/bin/perl
# calculate the sum of an array
use strict; use warnings;
my @data = (1..100);
my $sum = 0;
foreach my $i (@data){
$sum += $i;
}
print "Sum is $sum\n";
#!/usr/bin/perl -w
## Added library usage
use strict;
use Temperature;
my $arg = $ARGV[0] || '-c20';
if ($arg =~ /^\-(c|f)((\-|\+)*\d+(\.\d+)*)$/) {
my ($deg, $num) = ($1, $2);
my ($in, $out) = ($num, $num);
if ($deg eq 'c') {
$deg = 'f';
$out = Temperature::c2f($num);
} else {
$deg = 'c';
$out = Temperature::f2c($num);
}
$out = sprintf('%0.2f', $out);
$out =~ s/^((\-|\+)*\d+)\.0+$/$1/;
print "$out $deg\n";
} else {
print "Usage: $0 -[c|f] num\n";
}
exit;
#!/usr/bin/perl -w
use strict;
my $arg = $ARGV[0] || '-c20';
if ($arg =~ /^\-(c|f)((\-|\+)*\d+(\.\d+)*)$/) {
my ($deg, $num) = ($1, $2);
my ($in, $out) = ($num, $num);
if ($deg eq 'c') {
$deg = 'f';
$out = &c2f($num);
} else {
$deg = 'c';
$out = &f2c($num);
}
$out = sprintf('%0.2f', $out);
$out =~ s/^((\-|\+)*\d+)\.0+$/$1/;
print "$out $deg\n";
} else {
print "Usage: $0 -[c|f] num\n";
}
exit;
sub f2c {
my $f = shift;
my $c = ($f - 32)*(5/9);
return $c;
}
sub c2f {
my $c = shift;
my $f = $c * (9/5) + 32;
return $f;
}
package Temperature;
use strict; use warnings;
sub f2c {
my $f = shift;
my $c = ($f - 32)*(5/9);
return $c;
}
sub c2f {
my $c = shift;
my $f = $c * (9/5) + 32;
return $f;
}
1;
#!/usr/bin/perl -w
use strict;
my ($value, $from, $to, $prefix, %allprefixes);
%allprefixes =
(
"mega" => 6, #use these as exponents, so down below I can just subtract exponents to get the answer
"kilo" => 3,
"milli" => -3,
"micro" => -6,
"nano" => -9,
"pico" => -12,
"femto" => -15,
"atto" => -18,
"zepto" => -21,
"yocto" => -24,
);
print "Enter the unit that you are starting with: ";
$from = <STDIN>;
print "Enter your target unit: ";
$to = <STDIN>;
print "Enter the numerical amount: ";
$value = <STDIN>;
chomp ($from);
chomp ($to);
chomp ($value);
if($value==0){
print "0 $from is ",0, $to, "\n";
}
if (not exists $allprefixes{$to}) {
die "My programmer has not yet supplied me with $to as a prefix\n";
}
if (not exists $allprefixes{$from}) {
die "My programmer has not yet supplied me with $from as a prefix\n";
}
$prefix = $allprefixes{$from} - $allprefixes{$to};
my $mod = 1;
for(my $i=0;$i<abs($prefix);$i++)
{
if($prefix > 0)
{
$mod *= 10;
}
else
{
$mod /= 10;
}
}
print "$value $from is ",$value*$mod," $to. \n";
__END__
Sample input:
Enter the unit that you are starting with: mega
Enter your target unit: kilo
Enter the numerical amount: 10
10 mega is 10000 kilo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment