aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-24 11:17:54 +0100
committerGitHub <noreply@github.com>2019-06-24 11:17:54 +0100
commit678dc05c13d5fdf2ecdea4ee233a179817577d32 (patch)
tree528c1aea7fc05ebd3c044ecd8a06cc35cc574531
parent8f41f2f4bfc4acc14c0d684362cd783ccb023e9d (diff)
parent77804452f1fc07db3b1c500d31e5ed89cdaf5241 (diff)
downloadperlweeklychallenge-club-678dc05c13d5fdf2ecdea4ee233a179817577d32.tar.gz
perlweeklychallenge-club-678dc05c13d5fdf2ecdea4ee233a179817577d32.tar.bz2
perlweeklychallenge-club-678dc05c13d5fdf2ecdea4ee233a179817577d32.zip
Merge pull request #295 from rvandam/challenge-014
Challenge 014
-rw-r--r--challenge-014/rob-van-dam/README1
-rw-r--r--challenge-014/rob-van-dam/perl5/ch-1.pl40
-rw-r--r--challenge-014/rob-van-dam/perl5/ch-2.pl42
-rw-r--r--challenge-014/rob-van-dam/perl5/ch-3.pl44
4 files changed, 127 insertions, 0 deletions
diff --git a/challenge-014/rob-van-dam/README b/challenge-014/rob-van-dam/README
new file mode 100644
index 0000000000..d0e17767f8
--- /dev/null
+++ b/challenge-014/rob-van-dam/README
@@ -0,0 +1 @@
+Solution by Rob Van Dam
diff --git a/challenge-014/rob-van-dam/perl5/ch-1.pl b/challenge-014/rob-van-dam/perl5/ch-1.pl
new file mode 100644
index 0000000000..613c1250a3
--- /dev/null
+++ b/challenge-014/rob-van-dam/perl5/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use v5.10;
+
+my $M = shift @ARGV || 10;
+
+#slow();
+fast();
+
+# this works but is very slow for large $M
+# as its roughly O(n**2)
+sub slow {
+ my @a = (0);
+ for my $n (0..$M) {
+ my $r = 0;
+ foreach my $m (0..($n-1)) {
+ last if ! defined $a[$n];
+ $r = $n - $m if $a[$m] == $a[$n];
+ }
+ $a[$n + 1] = $r;
+ say $a[$n];
+ }
+}
+
+# this is much faster for large $M
+# by caching the last time we say a given number
+# so its roughly O(n**2) but requires roughly O(2n) space
+sub fast {
+ my @a = (0);
+ my @last = ();
+ for my $n (0..$M) {
+ my $m = $last[ $a[$n] ];
+ my $r = defined $m ? $n - $m : 0;
+ $last[$a[$n]] = $n;
+ $a[$n + 1] = $r;
+ say $a[$n];
+ }
+}
diff --git a/challenge-014/rob-van-dam/perl5/ch-2.pl b/challenge-014/rob-van-dam/perl5/ch-2.pl
new file mode 100644
index 0000000000..95d0f2c6af
--- /dev/null
+++ b/challenge-014/rob-van-dam/perl5/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use v5.10;
+
+use Data::Dumper;
+
+# all state short codes
+my %states = map { $_ => 1 } qw(AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY);
+
+# unique letters contained in these codes (any word matching any letter not in this list is right out)
+my %uniq = map { $_ => 1 } map { split // } keys %states;
+my $uniq = join '', sort { $a cmp $b } keys %uniq;
+my $notok = qr/[^$uniq]/; # by my calculations, should be missing B and Q
+
+my $file = '/usr/share/dict/american-english-insane';
+$file = '/usr/share/dict/american-english-huge' if ! -e $file;
+$file = '/usr/share/dict/words' if ! -e $file;
+
+open my $words, '<', $file or die $!;
+my $longest = '';
+WORD: foreach my $word (<$words>) {
+ chomp $word;
+ $word = uc $word;
+ next if $word =~ $notok; # skip words that contain an invalid character (especially B or Q but also punctuation and accents)
+ next if length($word) % 2 == 1; # skip odd length words, they can't consist of pairs
+ next if length($word) <= length($longest); # skip words that aren't longer than the current longest
+ my @pairs = $word =~ m/../g;
+ foreach my $pair (@pairs) {
+ next WORD if ! $states{$pair}; # skip words that have a pair that doesn't match a state
+ }
+ $longest = $word;
+ #say $word;
+}
+say $longest;
+
+# CONCORDE
+# OR
+# CACOGALACTIA
+#
+# depending on the dictionary available
diff --git a/challenge-014/rob-van-dam/perl5/ch-3.pl b/challenge-014/rob-van-dam/perl5/ch-3.pl
new file mode 100644
index 0000000000..c4aa2e40a3
--- /dev/null
+++ b/challenge-014/rob-van-dam/perl5/ch-3.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use v5.10;
+
+use HTTP::Tiny;
+use URI::Escape;
+use JSON::PP qw(decode_json);
+use Data::Dumper;
+
+my $base = 'http://geodb-free-service.wirefreethought.com';
+my $find = '/v1/geo/cities?limit=10&offset=0&namePrefix=%s';
+my $time = '/v1/geo/cities/%d/time';
+
+my $search = shift @ARGV || 'New Orleans';
+my @cities = find_cities($search);
+
+binmode STDOUT, ":utf8"; # avoid wide character warnings on certain results
+foreach my $city (@cities) {
+ say $city->{'name'}, ', ', $city->{'regionCode'}, ', ', $city->{'countryCode'}, ': ', get_time($city->{'id'});
+}
+
+sub find_cities {
+ my $search = shift;
+
+ my $url = sprintf $base . $find, uri_escape($search);
+ return @{ _get($url) };
+}
+
+sub get_time {
+ my $city_id = shift;
+
+ my $url = sprintf $base . $time, $city_id;
+ return _get($url);
+}
+
+sub _get {
+ my $url = shift;
+
+ my $response = HTTP::Tiny->new->get($url);
+ my $parsed = decode_json($response->{'content'});
+ return $parsed->{'data'};
+}