diff options
| author | LapVeesh <rabbiveesh@gmail.com> | 2019-07-02 23:43:11 +0300 |
|---|---|---|
| committer | LapVeesh <rabbiveesh@gmail.com> | 2019-07-02 23:43:11 +0300 |
| commit | eaffc119a2a02e35db2e93521ce1a43bbd5b5ebc (patch) | |
| tree | 78972bc45cfe7a17ee9869cf6c4bb20d5c742328 | |
| parent | d058cc897094733ded2988aa03af754c92e7ab92 (diff) | |
| download | perlweeklychallenge-club-eaffc119a2a02e35db2e93521ce1a43bbd5b5ebc.tar.gz perlweeklychallenge-club-eaffc119a2a02e35db2e93521ce1a43bbd5b5ebc.tar.bz2 perlweeklychallenge-club-eaffc119a2a02e35db2e93521ce1a43bbd5b5ebc.zip | |
Finished this weeks, finally submitted some old ones I did
| -rwxr-xr-x | challenge-009/veesh-goldman/perl5/ch-1.pl | 6 | ||||
| -rw-r--r-- | challenge-009/veesh-goldman/perl5/ch-2.pl | 3 | ||||
| -rw-r--r-- | challenge-009/veesh-goldman/perl5/ch-3.pl | 3 | ||||
| -rw-r--r-- | challenge-011/veesh-goldman/perl5/ch-01.pl | 5 | ||||
| -rwxr-xr-x | challenge-011/veesh-goldman/perl5/ch-02.pl | 23 | ||||
| -rwxr-xr-x | challenge-015/veesh-goldman/perl5/ch-01.pl | 79 | ||||
| -rwxr-xr-x | challenge-015/veesh-goldman/perl5/ch-02.pl | 93 | ||||
| -rwxr-xr-x | challenge-015/veesh-goldman/perl5/ch-extra.pl | 51 |
8 files changed, 263 insertions, 0 deletions
diff --git a/challenge-009/veesh-goldman/perl5/ch-1.pl b/challenge-009/veesh-goldman/perl5/ch-1.pl new file mode 100755 index 0000000000..4c47b1a33c --- /dev/null +++ b/challenge-009/veesh-goldman/perl5/ch-1.pl @@ -0,0 +1,6 @@ +#!/usr/bin/perl + +use feature 'say'; +use List::Util qw/uniq first/; + +say first { 5 <= uniq split //, $_ **2 } 1..10000 diff --git a/challenge-009/veesh-goldman/perl5/ch-2.pl b/challenge-009/veesh-goldman/perl5/ch-2.pl new file mode 100644 index 0000000000..7c9f08cb9d --- /dev/null +++ b/challenge-009/veesh-goldman/perl5/ch-2.pl @@ -0,0 +1,3 @@ +#! /usr/bin/perl + + diff --git a/challenge-009/veesh-goldman/perl5/ch-3.pl b/challenge-009/veesh-goldman/perl5/ch-3.pl new file mode 100644 index 0000000000..7c9f08cb9d --- /dev/null +++ b/challenge-009/veesh-goldman/perl5/ch-3.pl @@ -0,0 +1,3 @@ +#! /usr/bin/perl + + diff --git a/challenge-011/veesh-goldman/perl5/ch-01.pl b/challenge-011/veesh-goldman/perl5/ch-01.pl new file mode 100644 index 0000000000..96209d367b --- /dev/null +++ b/challenge-011/veesh-goldman/perl5/ch-01.pl @@ -0,0 +1,5 @@ +#! /usr/bin/env perl + +#programatically compute the equal point in fahrenheit and celsius +# it's -40, by the way + diff --git a/challenge-011/veesh-goldman/perl5/ch-02.pl b/challenge-011/veesh-goldman/perl5/ch-02.pl new file mode 100755 index 0000000000..824ac0d88d --- /dev/null +++ b/challenge-011/veesh-goldman/perl5/ch-02.pl @@ -0,0 +1,23 @@ +#! /usr/bin/env perl + +# create the identity matrix of size n for any input n + +my $usage = <<"EOF"; +USAGE - $0 size + size - the size of the desired identity matrix +EOF +my $size = shift or die $usage; + +die "size must be a positive integer" unless $size > 0; + +my @matrix; +for my $y ( 0 .. $size - 1 ) { + push @matrix, []; + for my $x ( 0 .. $size - 1 ) { + print $matrix[$y][$x] = 1 if $y == $x; + print $matrix[$y][$x] = 0 if $y != $x; + } + print "\n"; +} + + diff --git a/challenge-015/veesh-goldman/perl5/ch-01.pl b/challenge-015/veesh-goldman/perl5/ch-01.pl new file mode 100755 index 0000000000..0959351397 --- /dev/null +++ b/challenge-015/veesh-goldman/perl5/ch-01.pl @@ -0,0 +1,79 @@ +#! /usr/bin/env perl + + +use v5.22; + +#TASK: write a script to generate the first 10 weak and strong +#prime numbers + +#we start off our list of primes with 2, so that 1 isn't included +my @primes = (2); + +#a function to add primes to the list. Acts as a generator by +#remembering $bottom, the number up til which we have primes for +sub generate_primes_till { + my $max = shift; + state $bottom = 3; + for ($bottom .. $max) { + push @primes, $_ if is_prime($_) + } + $bottom = $max; +} + +#checks if divisible by any other prime +sub is_prime { + my $number = shift; + for (@primes) { + #bail out on first number with no remainder + return 0 unless $number % $_; + } + #any truthy value would do, but hey, it's self documenting. + #... he said as he documents it + return 'prime' +} + +#an interface to our generator. Returns the prime at number n if it exists, +#and otherwise expands the list until there is a prime at that number +sub p { + my $n = shift; + state $top = 0; + #if there's an entry at this number, then return it + return $primes[$n] if $primes[$n]; + #otherwise, generate more numbers and then return it + until ($primes[$n]) { + generate_primes_till ($top+=1000) + } +} + +sub is_strong { + my $n = shift; + p($n) > ( p($n-1) + p($n+1) ) /2 +} + +sub is_weak { + my $n = shift; + p($n) < ( p($n-1) + p($n+1) ) /2 +} + +my @strongs; +my $n; +until (@strongs == 10 ) { + $n++; + push @strongs, p($n) if is_strong $n; +} + +$n = 0; +my @weaks; +until (@weaks == 10 ) { + $n++; + push @weaks, p($n) if is_weak $n; +} + +say <<"OUTPUT"; +The strong numbers are: +@strongs + +The weak numbers are: +@weaks + +OUTPUT diff --git a/challenge-015/veesh-goldman/perl5/ch-02.pl b/challenge-015/veesh-goldman/perl5/ch-02.pl new file mode 100755 index 0000000000..56c4dbe5b4 --- /dev/null +++ b/challenge-015/veesh-goldman/perl5/ch-02.pl @@ -0,0 +1,93 @@ +#!/usr/bin/env perl + +use v5.22; + +#TASK: imlement a vigenere cipher. + +my $usage =<<"USAGE"; +$0 encode|decode key [message] + + Encodes or decodes the message given at the command line. + Requires a key to be passed in after the command. + + With no message given, enters REPL mode, + encoding/decoding the input, one line at a time. +USAGE + +#pull of the arguments. We need to shift them so <> works later +my ($command, $key, $message) = (shift, shift, shift); + + +#we use one list to represent the table, so we can get the start +#value and the length +my @abc; +#we use all printable ascii characters... 'cause we can +push @abc, $_ for ord(' ') .. ord('~'); + +#we take each letter from the message to en/decode, and then get +#the correct letter from the cipher, and pass it to a callback +#which does the shifting +sub vignere_loop { + my @encrypted; + my ($message, $cb) = @_; + for my $letter (split //, $message) { + #keep a local copy of where we're holding in the key + state $key_index = 0; + $key_index %= length $key; + #get the letter at our position + my $cipher = substr $key, $key_index, 1; + + push @encrypted, $cb->($cipher, $letter); + $key_index++; + } + @encrypted +} + +sub letter_by_offset { + my $offset = shift; + chr( $offset % @abc + $abc[0] ) +} + +sub offset { + ord(shift) - $abc[0] +} + + +my %execute; +$execute{encode} = sub { + vignere_loop shift, sub { + my ($cipher, $letter) = @_; + letter_by_offset( offset($letter) + offset($cipher) ) + }; +}; + +$execute{decode} = sub { + vignere_loop shift, sub { + my ($cipher, $letter) = @_; + letter_by_offset( offset($letter) - offset($cipher) ); + }; +}; + +#i recently found out that you can take a reference to a function +#inside of an interpolation, so it's creeping in everywhere I +#write code +sub die_usage { die "${\shift} \n $usage" } + +#CLI app parameter checking +die_usage "Command required" unless $command; +#we check for available commands dynamically so that we DRY +die_usage "Unrecognized command: $command" + unless grep { $command eq $_ } keys %execute; +die_usage "Need a key in order to $command" unless $key; + +#either process this one, or enter the REPL +if ($message) { + #in case the message has a space, pull the rest off of ARGV + say $execute{$command}->(join ' ', $message, @ARGV); +} else { + say "Now entering REPL mode, press CTRL-D to finish"; + while (<>) { + say $execute{$command}->($_) + } +} + diff --git a/challenge-015/veesh-goldman/perl5/ch-extra.pl b/challenge-015/veesh-goldman/perl5/ch-extra.pl new file mode 100755 index 0000000000..2b8c22abf0 --- /dev/null +++ b/challenge-015/veesh-goldman/perl5/ch-extra.pl @@ -0,0 +1,51 @@ +#! /usr/bin/perl + +use v5.22; + +use Mojo::UserAgent; +use Mojo::URL; +use Mojo::File qw/path/; +use Mojo::Util qw/encode/; +use utf8; + +#task: use the language detection api + +sub api_path { + Mojo::URL->new('https://ws.detectlanguage.com/0.2/')->path(shift) +} +say api_path('detect'); + +my $auth_key = path('.langdetectkey')->slurp; +chomp $auth_key; +say $auth_key; + +my $headers = { Authorization => "Bearer $auth_key" }; + +my $ua = Mojo::UserAgent->new; + +sub detect { + $ua->post( api_path('detect'), + $headers, json => { q => shift } )->res->json +} + +sub user_status { + $ua->get( api_path('user/status') , $headers)->res->json +} + +sub languages { + $ua->get( api_path('languages'))->res->json +} + +use Data::Printer; + +my $res; +$res = detect('how are you gentlemen?'); +p $res; +$res = detect([ 'All your base are belong to us', '戦いが始まりました', 'שלום עליכם, רבותי' ]); +p $res; + +$res = user_status(); +p $res; + +$res = languages(); +p $res; |
