Libraryless. Click here for Pure Java version (374L/3K/10K).
1 | !752 |
2 | |
3 | p { |
4 | S s = "Hello world world world"; |
5 | print("Original:\n " + s); |
6 | S transformed = bwt(s, '|'); |
7 | print("BWT Transformed:\n " + transformed); |
8 | S x = inverseBWT(transformed, '|'); |
9 | print("Transformed back:\n " + x); |
10 | print(eq(s, x) ? "Yo!" : "No..."); |
11 | print(); |
12 | |
13 | // Transform it again |
14 | S doublyTransformed = bwt(transformed, '^'); |
15 | print("Doubly Transformed (not very useful):\n " + doublyTransformed); |
16 | } |
17 | |
18 | static S bwt(S input, char endMarker) { |
19 | input += endMarker; |
20 | int n = l(input); |
21 | S[] tbl = new S[n]; |
22 | for i to n: |
23 | tbl[i] = rotateString(input, i); |
24 | sort(tbl); |
25 | new StringBuilder buf; |
26 | for i to n: |
27 | buf.append(last(tbl[i])); |
28 | ret str(buf); |
29 | } |
30 | |
31 | static S rotateString(S s, int n) { |
32 | if (n == 0) ret s; |
33 | ret substring(s, l(s)-n) + substring(s, 0, l(s)-n); |
34 | } |
35 | |
36 | static S inverseBWT(S s, char endMarker) { |
37 | int n = l(s); |
38 | S[] tbl = new S[n]; |
39 | for i to n: |
40 | tbl[i] = ""; |
41 | for i to n: { |
42 | // insert |
43 | for j to n: |
44 | tbl[j] = s.charAt(j) + tbl[j]; |
45 | |
46 | // sort |
47 | sort(tbl); |
48 | } |
49 | for i to n: |
50 | if (last(tbl[i]) == endMarker) |
51 | ret dropLast(tbl[i]); |
52 | |
53 | fail("NEVER FAIL in a perfect world!"); |
54 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1004868 |
Snippet name: | Naive Burrows Wheeler Transform (+ Inverse Transform) |
Eternal ID of this version: | #1004868/1 |
Text MD5: | 56e26874ed3a5d5ce3b44fbcf66dc6e9 |
Transpilation MD5: | a760bac83d2b8c4798a259d590c5d368 |
Author: | stefan |
Category: | javax / fun |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-09-08 17:47:21 |
Source code size: | 1203 bytes / 54 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 526 / 622 |
Referenced in: | [show references] |