aboutsummaryrefslogtreecommitdiff
path: root/challenge-014
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-014')
-rw-r--r--challenge-014/archargelod/README1
-rwxr-xr-xchallenge-014/archargelod/nim/ch_1.nim52
-rwxr-xr-xchallenge-014/archargelod/nim/ch_2.nim38
-rw-r--r--challenge-014/zapwai/README1
-rw-r--r--challenge-014/zapwai/c/ch-1.c37
-rw-r--r--challenge-014/zapwai/javascript/ch-1.js24
-rw-r--r--challenge-014/zapwai/perl/ch-1.pl29
-rw-r--r--challenge-014/zapwai/perl/ch-2.pl94
-rw-r--r--challenge-014/zapwai/python/ch-1.py20
-rw-r--r--challenge-014/zapwai/rust/ch-1.rs33
10 files changed, 329 insertions, 0 deletions
diff --git a/challenge-014/archargelod/README b/challenge-014/archargelod/README
new file mode 100644
index 0000000000..6cd57e1074
--- /dev/null
+++ b/challenge-014/archargelod/README
@@ -0,0 +1 @@
+Solution by archargelod
diff --git a/challenge-014/archargelod/nim/ch_1.nim b/challenge-014/archargelod/nim/ch_1.nim
new file mode 100755
index 0000000000..f5f4009e0e
--- /dev/null
+++ b/challenge-014/archargelod/nim/ch_1.nim
@@ -0,0 +1,52 @@
+#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
+import std/tables
+
+proc vanEckNth*(nth: Natural): int =
+ ## returns nth term of Van Eck's sequence
+ ## starting from 0-th term: [a(0) = 0]
+ var lastPos: Table[int, int]
+
+ var prev = 0
+ for n in 1 .. nth:
+ if prev notin lastPos:
+ lastPos[prev] = n - 1
+ prev = 0
+ continue
+
+ let m = lastPos[prev]
+ lastPos[prev] = n - 1
+ prev = n - 1 - m
+
+ result = prev
+
+proc vanEckSequence*(count: Positive): seq[int] =
+ ## blazingly fast!
+ ## sub 1ms with 20_000 terms
+ result = newSeq[int](count)
+ var lastPos: Table[int, int]
+
+ for n in 1 ..< count:
+ let prev = result[n - 1]
+ if prev notin lastPos:
+ lastPos[prev] = n - 1
+ continue
+
+ let m = lastPos[prev]
+ lastPos[prev] = n - 1
+ result[n] = n - 1 - m
+
+when isMainModule:
+ import std/unittest
+
+ const
+ Count = 97
+ Expected = [
+ 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, 2, 9, 0, 4, 9,
+ 3, 6, 14, 0, 6, 3, 5, 15, 0, 5, 3, 5, 2, 17, 0, 6, 11, 0, 3, 8, 0, 3, 3, 1, 42, 0,
+ 5, 15, 20, 0, 4, 32, 0, 3, 11, 18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3, 6, 3,
+ 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0, 5, 37, 0, 3, 8, 8, 1
+ ]
+
+ suite "Van Eck's sequence":
+ test "first 97 terms":
+ check vanEckSequence(Count) == Expected
diff --git a/challenge-014/archargelod/nim/ch_2.nim b/challenge-014/archargelod/nim/ch_2.nim
new file mode 100755
index 0000000000..cd87276a58
--- /dev/null
+++ b/challenge-014/archargelod/nim/ch_2.nim
@@ -0,0 +1,38 @@
+#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off
+import std/[sets]
+
+const USStates = [
+ "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"
+].toHashSet()
+
+proc isComposableFrom(word: string, syllables: HashSet[string]): bool =
+ if word.len mod 2 != 0:
+ return false
+
+ for i in countUp(0, word.len - 2, 2):
+ if word[i .. i + 1] notin syllables:
+ return false
+
+ true
+
+proc longestWordFromSyllables(dictFile: string, syllables: HashSet[string]): string =
+ let dict = open(dictFile)
+ defer:
+ dict.close()
+
+ for word in lines dict:
+ if word.len < result.len:
+ continue
+ if word.isComposableFrom syllables:
+ result = word
+
+when isMainModule:
+ import std/strformat
+
+ const DictPath = "/usr/share/dict/words"
+ let res = DictPath.longestWordFromSyllables(USStates)
+
+ echo &"Longest word composable from US 2-letter State names is '{res}'"
diff --git a/challenge-014/zapwai/README b/challenge-014/zapwai/README
new file mode 100644
index 0000000000..037b3777ef
--- /dev/null
+++ b/challenge-014/zapwai/README
@@ -0,0 +1 @@
+Solutions by David Ferrone.
diff --git a/challenge-014/zapwai/c/ch-1.c b/challenge-014/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..d0fce061cd
--- /dev/null
+++ b/challenge-014/zapwai/c/ch-1.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <stdbool.h>
+#define N 20
+
+void vaneck(int* seq, int* len) {
+ if (*len == 1) {
+ (*len)++;
+ seq[1] = 0;
+ }
+ else {
+ int pop = seq[*len - 1];
+ bool done_flag = false;
+ int j = *len - 2;
+ while (j >= 0) {
+ if ((!done_flag) && (pop == seq[j])) {
+ done_flag = true;
+ seq[*len] = *len - 1 - j;
+ (*len)++;
+ }
+ j--;
+ }
+ if (!done_flag) {
+ seq[*len] = 0;
+ (*len)++;
+ }
+ }
+}
+
+int main() {
+ int seq[N] = {};
+ int len = 1;
+ for (int i = 0; i < N; i++) {
+ vaneck(seq, &len);
+ printf("%d ", seq[i]);
+ }
+ printf("\n");
+}
diff --git a/challenge-014/zapwai/javascript/ch-1.js b/challenge-014/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..1e48037b13
--- /dev/null
+++ b/challenge-014/zapwai/javascript/ch-1.js
@@ -0,0 +1,24 @@
+function vaneck(seq) {
+ if (seq.length == 1)
+ seq.push(0);
+ else {
+ let pop = seq[seq.length - 1];
+ let done_flag = false;
+ let j = seq.length - 2;
+ while (j >= 0) {
+ if (!done_flag && (pop == seq[j])) {
+ done_flag = true;
+ seq.push(seq.length - 1 - j);
+ }
+ j--;
+ }
+ if (!done_flag)
+ seq.push(0);
+ }
+}
+let N = 19;
+let seq = [0];
+for (let i = 0; i < N; i++) {
+ vaneck(seq);
+}
+console.log(seq);
diff --git a/challenge-014/zapwai/perl/ch-1.pl b/challenge-014/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..483577c297
--- /dev/null
+++ b/challenge-014/zapwai/perl/ch-1.pl
@@ -0,0 +1,29 @@
+use v5.38;
+my @seq = (0);
+my $n = 0;
+do {
+ vaneck(\@seq);
+ $n++;
+} while ($n < 19);
+say @seq;
+
+sub vaneck($r) {
+ if ($#$r == 0) {
+ push @$r, 0;
+ } else {
+ my $pop = $$r[$#$r];
+ my $done_flag = 0;
+ my $j = $#$r - 1;
+ do {
+ if (($done_flag == 0) && ($$r[$j] == $pop)) {
+ $done_flag = 1;
+ push @$r, $#$r - $j;
+ }
+ $j--;
+ } while ($j >= 0);
+ unless ($done_flag) {
+ push @$r, 0;
+ }
+ }
+}
+
diff --git a/challenge-014/zapwai/perl/ch-2.pl b/challenge-014/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..19826d4316
--- /dev/null
+++ b/challenge-014/zapwai/perl/ch-2.pl
@@ -0,0 +1,94 @@
+use v5.38;
+my $str = <<ENDL;
+ Alabama AL
+ Kentucky KY
+ Ohio OH
+ Alaska AK
+ Louisiana LA
+ Oklahoma OK
+ Arizona AZ
+ Maine ME
+ Oregon OR
+ Arkansas AR
+ Maryland MD
+ Pennsylvania PA
+ Massachusetts MA
+ California CA
+ Michigan MI
+ RhodeIsland RI
+ Colorado CO
+ Minnesota MN
+ SouthCarolina SC
+ Connecticut CT
+ Mississippi MS
+ SouthDakota SD
+ Delaware DE
+ Missouri MO
+ Tennessee TN
+ Montana MT
+ Texas TX
+ Florida FL
+ Nebraska NE
+ Georgia GA
+ Nevada NV
+ Utah UT
+ NewHampshire NH
+ Vermont VT
+ Hawaii HI
+ NewJersey NJ
+ Virginia VA
+ Idaho ID
+ NewMexico NM
+ Illinois IL
+ NewYork NY
+ Washington WA
+ Indiana IN
+ NorthCarolina NC
+ WestVirginia WV
+ Iowa IA
+ NorthDakota ND
+ Wisconsin WI
+ Kansas KS
+ Wyoming WY
+ENDL
+open my $fh, "<words" or die;
+my @dict = <$fh>;
+close $fh;
+chomp @dict;
+my @words;
+foreach (@dict) {
+ if (length($_) % 2 == 0) {
+ push @words, uc $_ unless ($_ =~ /[b|q]/);
+ }
+}
+my %h = split(" ", $str);
+my @letters = sort values %h;
+my @composites = @letters;
+my $n = 0;
+my $len = @letters;
+do {
+ my @newlist;
+ foreach my $item (@composites) {
+ for my $i (0 .. $#letters) {
+ push @newlist, $item.$letters[$i];
+ }
+ }
+ $n++;
+ push @composites, @newlist;
+} while ( $n < 3 ); # max length of words is 2(n+1)
+
+my @list = grep { /[AEIOU]+/ } @composites;
+
+my $words = join(" ", @words);
+my $let = "A";
+for my $comp (@list) {
+ say $comp if ($words =~ / $comp /);
+ if ($let ne substr($comp,0,1)) {
+ say $let;
+ $let = substr($comp,0,1);
+ }
+}
+
+# MAINLAND
+# MANDARIN
+# MEMORIAL
diff --git a/challenge-014/zapwai/python/ch-1.py b/challenge-014/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..130d5406a2
--- /dev/null
+++ b/challenge-014/zapwai/python/ch-1.py
@@ -0,0 +1,20 @@
+def vaneck(seq):
+ if len(seq) == 1:
+ seq.append(0)
+ else:
+ pop = seq[len(seq)-1]
+ done_flag = False
+ j = len(seq) - 2
+ while j >= 0:
+ if (not done_flag) and seq[j] == pop:
+ done_flag = True
+ seq.append(len(seq) - 1 - j)
+ j -= 1
+ if not done_flag:
+ seq.append(0)
+seq = [0]
+N = 19
+for i in range(N):
+ vaneck(seq)
+print(seq)
+
diff --git a/challenge-014/zapwai/rust/ch-1.rs b/challenge-014/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..d40f814867
--- /dev/null
+++ b/challenge-014/zapwai/rust/ch-1.rs
@@ -0,0 +1,33 @@
+fn vaneck(seq :&mut Vec<i32>) {
+ if seq.len() == 1 {
+ seq.push(0);
+ }
+ else {
+ let pop = seq[seq.len() - 1];
+ let mut done_flag = false;
+ let mut j = seq.len() - 2;
+ while j > 0 {
+ if !done_flag && pop == seq[j] {
+ done_flag = true;
+ seq.push((seq.len()- 1 - j).try_into().unwrap());
+ }
+ j -= 1;
+ }
+ if !done_flag && pop == seq[0] {
+ done_flag = true;
+ seq.push((seq.len()- 1).try_into().unwrap());
+ }
+ if !done_flag {
+ seq.push(0);
+ }
+ }
+}
+
+fn main() {
+ let n = 19;
+ let mut seq = vec![0];
+ for _i in 0 .. n {
+ vaneck(&mut seq);
+ }
+ println!("{:?}", seq);
+}