summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-10-24 23:18:13 -0500
committerJesse Luehrs <doy@tozt.net>2012-10-24 23:18:57 -0500
commitd6966208224bb37ca532519a50849c686808582b (patch)
tree2e32f4d55bc11cbc9d9ef0606f2144bfe8ce2ea8
parent8df97a656543017b5485276bdfef3961fa463486 (diff)
downloadrosalind-d6966208224bb37ca532519a50849c686808582b.tar.gz
rosalind-d6966208224bb37ca532519a50849c686808582b.zip
forgot to commit these
-rw-r--r--HAMM.pl8
-rw-r--r--PERM.pl36
2 files changed, 44 insertions, 0 deletions
diff --git a/HAMM.pl b/HAMM.pl
new file mode 100644
index 0000000..0debb4a
--- /dev/null
+++ b/HAMM.pl
@@ -0,0 +1,8 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use 5.016;
+
+chomp(my $line1 = <>);
+chomp(my $line2 = <>);
+say length(($line1 ^ $line2) =~ s/\x00//gr);
diff --git a/PERM.pl b/PERM.pl
new file mode 100644
index 0000000..b9d2d10
--- /dev/null
+++ b/PERM.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use 5.016;
+
+sub factorial {
+ my ($n) = @_;
+ return 1 if $n < 2;
+ return $n * factorial($n - 1);
+}
+
+sub permutations {
+ my ($n) = @_;
+ my $string = join('', 1..$n);
+ return map { _permutation($string, $_) } 0..(factorial($n) - 1);
+}
+
+sub _permutation {
+ my ($string, $index) = @_;
+
+ return '' if $string eq '';
+
+ my $fact = factorial(length($string) - 1);
+
+ my $current_index = int($index / $fact);
+ my $rest = $index % $fact;
+
+ my $first_digit = substr($string, $current_index, 1);
+ substr($string, $current_index, 1, '');
+
+ return $first_digit . _permutation($string, $rest);
+}
+
+my $n = <>;
+say factorial($n);
+say join(' ', split '') for permutations($n);