How to resolve the algorithm De Bruijn sequences step by step in the zkl programming language
How to resolve the algorithm De Bruijn sequences step by step in the zkl programming language
Table of Contents
Problem Statement
The sequences are named after the Dutch mathematician Nicolaas Govert de Bruijn.
A note on Dutch capitalization: Nicolaas' last name is de Bruijn, the de isn't normally capitalized unless it's the first word in a sentence. Rosetta Code (more or less by default or by fiat) requires the first word in the task name to be capitalized.
In combinatorial mathematics, a de Bruijn sequence of order n on a size-k alphabet (computer science) A is a cyclic sequence in which every possible length-n string (computer science, formal theory) on A occurs exactly once as a contiguous substring. Such a sequence is denoted by B(k, n) and has length kn, which is also the number of distinct substrings of length n on A; de Bruijn sequences are therefore optimally short. There are: distinct de Bruijn sequences B(k, n).
For this Rosetta Code task, a de Bruijn sequence is to be generated that can be used to shorten a brute-force attack on a PIN-like code lock that does not have an "enter" key and accepts the last n digits entered.
Note: automated teller machines (ATMs) used to work like this, but their software has been updated to not allow a brute-force attack.
A digital door lock with a 4-digit code would have B (10, 4) solutions, with a length of 10,000 (digits). Therefore, only at most 10,000 + 3 (as the solutions are cyclic or wrap-around) presses are needed to open the lock. Trying all 4-digit codes separately would require 4 × 10,000 or 40,000 presses.
(The last requirement is to ensure that the verification tests performs correctly. The verification processes should list any and all missing PIN codes.) Show all output here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm De Bruijn sequences step by step in the zkl programming language
Source code in the zkl programming language
dbSeq:=Data(); // a byte/character buffer
foreach n in (100){
a,a01,a11 := "%02d".fmt(n), a[0,1], a[1,1];
if(a11
dbSeq.append( if(a01==a11) a01 else a );
foreach m in ([n+1 .. 99]){
if("%02d".fmt(m)[1,1] <= a01) continue;
dbSeq.append("%s%02d".fmt(a,m));
}
}
dbSeq.append("000");
seqText:=dbSeq.text;
println("de Bruijn sequence length: ",dbSeq.len());
println("\nFirst 130 characters:\n",seqText[0,130]);
println("\nLast 130 characters:\n", seqText[-130,*]);
fcn chk(seqText){
chk:=Dictionary();
foreach n in ([0..seqText.len()-1]){ chk[seqText[n,4]]=True }
(9999).pump(List,"%04d".fmt,'wrap(k){ if(chk.holds(k)) Void.Skip else k })
}
println("\nMissing 4 digit PINs in this sequence: ", chk(seqText).concat(" "));
print("Missing 4 digit PINs in the reversed sequence: ",chk(seqText.reverse()).concat(" "));
println("\n4444th digit in the sequence: ", seqText[4443]);
dbSeq[4443]=".";
println("Setting the 4444th digit and reruning checks: ",chk(dbSeq.text).concat(" "));
You may also check:How to resolve the algorithm Return multiple values step by step in the C# programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the Java programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Ruby programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the Wren programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the JavaScript programming language