How to resolve the algorithm Four is the number of letters in the ... step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Four is the number of letters in the ... step by step in the zkl programming language
Table of Contents
Problem Statement
The Four is ... sequence is based on the counting of the number of letters in the words of the (never─ending) sentence:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Four is the number of letters in the ... step by step in the zkl programming language
Source code in the zkl programming language
// Built the sentence in little chucks but only save the last one
// Save the word counts
fcn fourIsThe(text,numWords){
const rmc="-,";
seq:=(text - rmc).split().apply("len").copy(); // (4,2,3,6...)
szs:=Data(numWords + 100,Int).howza(0).extend(seq); // bytes
cnt,lastWords := seq.len(),"";
total:=seed.len() - 1; // don't count trailing space
foreach idx in ([1..]){
sz:=szs[idx];
a,b := nth(sz,False),nth(idx+1); // "two","three hundred sixty-seventh"
lastWords="%s in the %s, ".fmt(a,b);
ws:=lastWords.counts(" ")[1]; // "five in the forty-ninth " --> 4
cnt+=ws; total+=lastWords.len();
lastWords.split().pump(szs.append,'-(rmc),"len");
if(cnt>=numWords){
if(cnt>numWords){
z,n:=lastWords.len(),z-2;
do(cnt - numWords){ n=lastWords.rfind(" ",n) - 1; }
lastWords=lastWords[0,n+1]; total-=(z - n);
}
break;
}
}
return(lastWords.strip(),szs,total);
}
fcn lastWord(sentence){ sentence[sentence.rfind(" ")+1,*] }
var seed="Four is the number of letters in the first word of this sentence, ";
sentence,szs,total := fourIsThe(seed,201);
print(" 1:");
foreach n,x in ([1..201].zip(szs)){
print("%3d".fmt(x));
if(0 == n%25) print("\n%3d:".fmt(n+1));
}
println("\nLength of above sentence: ",total);
n:=1000; do(5){
sentence,x,total := fourIsThe(seed,n);
word:=lastWord(sentence);
println("%,d words: \"%s\" [%d]. Length=%,d"
.fmt(n,word,word.len(),total));
n*=10;
}
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the PureBasic programming language
You may also check:How to resolve the algorithm 100 doors step by step in the PL/I programming language
You may also check:How to resolve the algorithm Convert decimal number to rational step by step in the Ada programming language
You may also check:How to resolve the algorithm Date manipulation step by step in the D programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the PureBasic programming language