aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-09-15 16:06:48 +0100
committerGitHub <noreply@github.com>2019-09-15 16:06:48 +0100
commitf52d535ceb578f0c4633100acac326d6e28d3bd0 (patch)
tree09f819e0ceadf94f1fc830765820e737383d6057
parent999c0c38c29a4502784e434530cd76a83169ffde (diff)
parent29c821656dbaad5d1c1a874079da967b81a61a46 (diff)
downloadperlweeklychallenge-club-f52d535ceb578f0c4633100acac326d6e28d3bd0.tar.gz
perlweeklychallenge-club-f52d535ceb578f0c4633100acac326d6e28d3bd0.tar.bz2
perlweeklychallenge-club-f52d535ceb578f0c4633100acac326d6e28d3bd0.zip
Merge pull request #628 from drclaw1394/master
drclaw/ruben solutions for w25 ch1 ch2. p5 and p6
-rw-r--r--challenge-025/ruben-westerberg/README17
-rwxr-xr-xchallenge-025/ruben-westerberg/perl5/ch-1.pl48
-rwxr-xr-xchallenge-025/ruben-westerberg/perl5/ch-2.pl40
-rwxr-xr-xchallenge-025/ruben-westerberg/perl6/ch-1.p639
-rwxr-xr-xchallenge-025/ruben-westerberg/perl6/ch-2.p626
5 files changed, 161 insertions, 9 deletions
diff --git a/challenge-025/ruben-westerberg/README b/challenge-025/ruben-westerberg/README
index bd6dab73ef..e7a3e41132 100644
--- a/challenge-025/ruben-westerberg/README
+++ b/challenge-025/ruben-westerberg/README
@@ -2,16 +2,15 @@ Solution by Ruben Westerberg
ch-1.pl and ch-1.p6
===
-Shortest script that doesn't throw an error...
- usage
- perl ch-1.pl
- perl6 ch-1.pl
- Scripts are 0 bytes in size
+Longest Pokemon name sequence.
+Simply run the program to see the sequence update. Last one printed in the longest one
+
ch-2.pl and ch-2.p6
===
-Builds and prints out a inverted index of all words in in put documents. Text files are provided as command line arguments.
-The output of the program show the word, the locations of this word in each of the files.
-usage example:
- ./ch-2.pl ../text/{beowulf.txt,pride.txt}
+Implements the chaochiper
+usage example (encode and decode pipeline reading from standard input)
+ ch-2.pl | ch-2.pl decode
+
+ ch-2.p6 | ch-2.p6 --decode
diff --git a/challenge-025/ruben-westerberg/perl5/ch-1.pl b/challenge-025/ruben-westerberg/perl5/ch-1.pl
new file mode 100755
index 0000000000..60e2e9bf57
--- /dev/null
+++ b/challenge-025/ruben-westerberg/perl5/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+#
+use strict;
+use warnings;
+use List::Util qw<all none any>;
+
+my @pokemon= do { open my $fh, "<", "../pokemon.txt"; <$fh>};
+chop @pokemon;
+#@pokemon=@pokemon[0..49];
+
+my @stack;
+my ($max, $node)=(0, undef);
+
+my $root={name=>"", parent=>undef, depth=>0};
+push @stack, $root;
+
+while (@stack){
+ my @result= process(shift @stack);
+ push @stack, @result;
+}
+
+print join ",", ppath($node);
+sub process {
+ my $tree=shift;
+ my @path=ppath($tree);
+ my @next=grep { !defined($tree->{parent}) || (substr($_, 0,1) eq substr($tree->{name},-1,1 ))} @pokemon;
+ @next=sort grep { my $n=$_; none {$n eq $_ } @path } @next;
+ if (@next && $tree->{depth} > $max) {
+ #print "We have max \n";
+ $max= $tree->{depth};
+ $node=$tree;
+ print "@path\n";
+ }
+
+ map {{name=>$_, parent=>$tree, depth=>$tree->{depth}+1} } @next;
+}
+
+
+sub ppath {
+ my $node=shift;
+ my @res;
+ while (defined $node->{parent}) {
+ my $n=$node;
+ $node=$node->{parent};
+ unshift @res, $n->{name};
+ }
+ @res;
+}
diff --git a/challenge-025/ruben-westerberg/perl5/ch-2.pl b/challenge-025/ruben-westerberg/perl5/ch-2.pl
new file mode 100755
index 0000000000..2410e796c3
--- /dev/null
+++ b/challenge-025/ruben-westerberg/perl5/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+my $decode=$ARGV[0]//undef;
+my @alpha= ("a".."z","A".."Z"," ", qw(? ! . : >));
+my @alpha1=@alpha;
+my @alpha2=@alpha;
+$|=1;
+while (<STDIN>) {
+ chomp;
+ for (split "", $_) {
+ print chaochiper(\@alpha1,\@alpha2, $_, $decode);
+ }
+ print "\n";
+}
+sub chaochiper {
+
+ my ($a1,$a2,$c,$decode)=@_;
+ my ($p,$ct);
+ if ($decode) {
+ $p= (grep { $$a2[$_] eq $c } 0..@$a2-1)[0];
+ $ct=$$a1[$p];
+ }
+ else {
+ $p= (grep { $$a1[$_] eq $c } 0..@$a1-1)[0];
+ $ct=$$a2[$p];
+ }
+
+ push @$a1, splice @$a1, 0, $p+1;
+ push @$a2, splice @$a2, 0, $p;
+
+ {
+ use integer;
+ splice(@$a1, @$a1 / 2,0, splice(@$a1,2,1));
+ splice(@$a2, @$a2 / 2,0 ,splice(@$a2,1,1));
+ }
+ $ct;
+}
diff --git a/challenge-025/ruben-westerberg/perl6/ch-1.p6 b/challenge-025/ruben-westerberg/perl6/ch-1.p6
new file mode 100755
index 0000000000..c95233a645
--- /dev/null
+++ b/challenge-025/ruben-westerberg/perl6/ch-1.p6
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl6
+#
+my @pokemon="../pokemon.txt".IO.lines;
+#@pokemon=@pokemon[^50];
+my %root=(name=>Nil, parent=>Nil, depth=>0);
+my $max=0;
+my $node=Nil;
+my @stack.push: %root;
+my $stopFlag=False;
+my $channel=Channel.new;
+my $counter=0;
+signal(SIGINT).tap({$stopFlag=True});
+
+while @stack and !$stopFlag {
+ @stack.append: process @stack.shift;
+}
+
+say pokepath($node);
+
+sub process(%tree) {
+ my $next:=@pokemon.grep: {!%tree<parent>.defined||.starts-with: %tree<name>.substr: *-1,1 };
+ my @path=pokepath(%tree);
+ $next:= sort keys $next (-) @path;
+ if %tree<depth> > $max {
+ $max=%tree<depth>;
+ $node=%tree;
+ say @path;
+ }
+
+ $next.map({
+ %( name=>$_, parent=>%tree, depth=>%tree<depth>+1 );
+ }) ;
+}
+sub pokepath($node is copy) {
+ reverse gather while $node<parent>.defined {
+ take $node<name>;
+ $node=$node.<parent>;
+ }
+}
diff --git a/challenge-025/ruben-westerberg/perl6/ch-2.p6 b/challenge-025/ruben-westerberg/perl6/ch-2.p6
new file mode 100755
index 0000000000..aed270d8da
--- /dev/null
+++ b/challenge-025/ruben-westerberg/perl6/ch-2.p6
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl6
+
+sub MAIN (Bool :$decode=False) {
+ $*OUT.out-buffer=0;
+ my @alpha=("a".."z","A".."Z"," ", <? ! . : >)[*;*];
+ my @alpha1=@alpha;
+ my @alpha2=@alpha;
+ for $*IN.lines {
+ put join "", .comb.map: { chaochiper(@alpha1,@alpha2, $_, :$decode) };
+ };
+}
+
+sub chaochiper(@alpha1,@alpha2, $c, :$decode=False){
+ my $p=($decode??@alpha2!!@alpha1).grep($c,:k)[0];
+ my $ct=($decode??@alpha1!!@alpha2)[$p];
+
+ given @alpha1 {
+ .=rotate($p+1);
+ .splice(.elems div 2, 0, .splice(2,1));
+ }
+ given @alpha2 {
+ .=rotate($p);
+ .splice(.elems div 2, 0, .splice(1,1));
+ }
+ $ct;
+}