diff options
| author | Francis Whittle <fudje@belladonna.lan> | 2019-06-30 17:49:36 +1000 |
|---|---|---|
| committer | Francis Whittle <fudje@belladonna.lan> | 2019-06-30 17:49:36 +1000 |
| commit | 851c45f89fa6cc63d7fd002edff2d4dd07ceea9e (patch) | |
| tree | 6e30729bcf4c7d6bac8f10f9bc01f581514959c8 /challenge-014 | |
| parent | 2afb83c14948f6c18c7f6bd18116582d0541ffd8 (diff) | |
| download | perlweeklychallenge-club-851c45f89fa6cc63d7fd002edff2d4dd07ceea9e.tar.gz perlweeklychallenge-club-851c45f89fa6cc63d7fd002edff2d4dd07ceea9e.tar.bz2 perlweeklychallenge-club-851c45f89fa6cc63d7fd002edff2d4dd07ceea9e.zip | |
fjwhittle Challenge 014 solutions
Diffstat (limited to 'challenge-014')
| -rw-r--r-- | challenge-014/fjwhittle/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-014/fjwhittle/perl6/ch-1.p6 | 23 | ||||
| -rw-r--r-- | challenge-014/fjwhittle/perl6/ch-2.p6 | 32 | ||||
| -rw-r--r-- | challenge-014/fjwhittle/perl6/ch-3.p6 | 42 |
4 files changed, 98 insertions, 0 deletions
diff --git a/challenge-014/fjwhittle/blog.txt b/challenge-014/fjwhittle/blog.txt new file mode 100644 index 0000000000..e7aad306d4 --- /dev/null +++ b/challenge-014/fjwhittle/blog.txt @@ -0,0 +1 @@ +https://rage.powered.ninja/2019/06/30/hashed-up-sequencing.html diff --git a/challenge-014/fjwhittle/perl6/ch-1.p6 b/challenge-014/fjwhittle/perl6/ch-1.p6 new file mode 100644 index 0000000000..2b4f3cf279 --- /dev/null +++ b/challenge-014/fjwhittle/perl6/ch-1.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env perl6 + +use v6.d; + +#| Generate <count> elements of Van Eck's Sequence or a Variation thereof +#| starting at <start>. +unit sub MAIN ( + UInt $count = 19, #= Count of elements to generate; defaults to 19. + UInt :$start = 0 #= Starting number of the Variation; defaults to 0. +); + +my \Van_Eck := gather { + take $start; + my %m = $start => 0; + + for ^∞ -> $n { + take %m{Van_Eck[$n]}:exists ?? $n - %m{Van_Eck[$n]} !! 0; + %m{Van_Eck[$n]} = $n; + } +} + +# 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5 ... +Van_Eck[^$count].put; diff --git a/challenge-014/fjwhittle/perl6/ch-2.p6 b/challenge-014/fjwhittle/perl6/ch-2.p6 new file mode 100644 index 0000000000..4773d59f06 --- /dev/null +++ b/challenge-014/fjwhittle/perl6/ch-2.p6 @@ -0,0 +1,32 @@ +#!/usr/bin/env perl6 + +#| Print the longest word in the given <dict> that can be spelt with only the +#| ANSI abbreviations of US states. +unit sub MAIN ( + $dict = '/usr/share/dict/words' #= Dictionary of words; defaults to + #= /usr/share/dict/words. +); + +my %states = <AL AK AZ AR CA CO CT DE DC 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> Z=> ('Alabama', 'Alaska', 'Arizona', + 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', + 'District of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', + 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', + 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', + 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', + 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', + 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', + 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', + 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', + 'West Virginia', 'Wisconsin', 'Wyoming'); + +my $states = set %states.keys; + +my @words = $dict.IO.words\ + .grep({.chars ≥ 4 && .chars %% 2})».uc\ + .unique.sort(*.chars <=> *.chars).reverse; + +given @words.first(*.comb(2) ⊂ $states) { + say %states{.comb(2)}.join(' + ') ~ ' = ' ~ $_; +} diff --git a/challenge-014/fjwhittle/perl6/ch-3.p6 b/challenge-014/fjwhittle/perl6/ch-3.p6 new file mode 100644 index 0000000000..c950d9e8dd --- /dev/null +++ b/challenge-014/fjwhittle/perl6/ch-3.p6 @@ -0,0 +1,42 @@ +use Cro::HTTP::Client; + +sub url-part-encode (Str(Cool) $in --> Str) { + $in.trans: (|<! # $ % & ' ( ) * + , / : ; = ? @ [ ]>, ' ') => (|<%21 %23 %24 %25 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D>, '+') +} + +#| Find the time in <city> +unit sub MAIN( + $city is copy; +); + +my $geodb-client = Cro::HTTP::Client.new: base-uri => 'http://geodb-free-service.wirefreethought.com'; + +my $city-encoded = url-part-encode($city); + +my &formatter = { "%02d:%02d %s".sprintf: .hour % 12, .minute, .hour >= 12 ?? 'pm' !! 'am' } + +my $result = + await $geodb-client\ + .get('/v1/geo/cities?limit=1&offset=0&namePrefix=%s'.sprintf: $city-encoded)\ + .then({ await .result.body })\ + .then({ if my $cityid = .result<data>[0]<id> { + $city = .result<data>[0]<name region country>.join(', '); + $cityid = url-part-encode($cityid); + await $geodb-client.get('/v1/geo/cities/%s/dateTime'.sprintf($cityid)); + } else { + die "$city not found." + } + })\ + .then({ DateTime.new(await(.result.body).<data>, :&formatter)}); + + +say "The time in $city is $result"; + +CATCH { + when X::Cro::HTTP::Error { + die "Error in request: ‘$_’"; + } + default { + die $_; + } +} |
