Created
June 24, 2019 21:24
-
-
Save kevincolyer/86fd2cbfc88b559d854091c8a9c2bcca to your computer and use it in GitHub Desktop.
Perl weekly challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl6 | |
use v6; | |
use Test; | |
# challenge 14.1 | |
# Write a script to generate Van Eck’s sequence. | |
my @testEck=0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5; | |
is vanEck(17),@testEck,"First 18 items Van Eck's sequence correct"; | |
sub vanEck(int $n where $n>=0 ) { | |
# Let a0 = 0. Then, for n ≥ 0, if there exists an m < n such that am = an, take the largest such m and set an+1 = n − m; otherwise an+1 = 0. | |
my int $max_m=-1; # set to a value lower than 0 to raise max_m | |
state int @a=0; | |
return 0 if $n==0; # base case | |
vanEck($n-1) if @a[$n]:!exists; | |
for 0..^$n -> int $m { | |
if @a[$m]==@a[$n] { | |
$max_m=max($max_m,$m); | |
} | |
} | |
if $max_m>=0 { | |
@a[$n+1]=$n-$max_m; | |
return @a; | |
} | |
@a[$n+1]=0; | |
return @a; | |
} | |
# Challenge 14.2 | |
#Using only the official postal (2-letter) abbreviations for the 50 U.S. states, write a script to find the longest English word you can spell? | |
my @words="/usr/share/dict/words".IO.lines.map(*.lc.trans('\'' => '', :delete, 'éåö' => 'eao').uc).grep(*.chars > 3) ; | |
say "Longest word is: " ~ wordsearch(@words); | |
# CAMPINAS | |
sub wordsearch(@words) { | |
my @codes=<AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA AL 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 AS GU MP PR VI FM MH PW AA AE AP CM CZ NB PI TT>.sort; | |
my $best=""; | |
for @words -> $word { | |
my $found=True; | |
for $word.comb(2) -> $xx { | |
if not ( $xx (elem) @codes ) { | |
$found=False; | |
last; | |
}; | |
} | |
if $found { | |
#say "> Found $word"; | |
$best=$word if $word.chars>$best.chars ; | |
} | |
} | |
return $best; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment