This commit is contained in:
Niels G. W. Serup 2013-02-11 21:55:34 +01:00
parent 286588366a
commit 071b3aa821
3 changed files with 24 additions and 19 deletions

View File

@ -21,11 +21,11 @@ word whose length does not exceed 8 characters, although it is a bit slow.
But why not more than 8 characters? My view of memory might have been a bit
naive back then, because the first step in my algorithm is to generate and
store all permutations of all subsequences of the given word. That is, if the
string is "me", my program stores the array `{ "m", "e", "me", "em" }` in
string is "me", my program stores the array ={ "m", "e", "me", "em" }= in
memory before going on to reading the dictionary and looking for matches.
If the string is "you", the program stores `{ "y", "o", "yo", "oy", "u", "yu",
"uy", "ou", "uo", "you", "yuo", "oyu", "ouy", "uyo", "uoy" }`.
If the string is "you", the program stores ={ "y", "o", "yo", "oy", "u", "yu",
"uy", "ou", "uo", "you", "yuo", "oyu", "ouy", "uyo", "uoy" }=.
If the string is "computer", the program stores the 109600 permutations of the
subsequences of "computer".
@ -51,11 +51,11 @@ Note that this code doesn't actually compile, because of all the wrong
code. However, it did compile back in 2008 which means that either I added the
wrong code after I had compiled it, or I used an overfriendly compiler (I don't
remember which compiler it was, but it ran on Windows). I have run the old
executable with `wine`, and that works.
executable with ~wine~, and that works.
It's not necesarry to know C to laugh at this code, but it helps.
We'll start with some basic `#include`s.
We'll start with some basic ~#include~s.
#+BEGIN_SRC c
#include <stdio.h>
@ -74,9 +74,9 @@ char os[0],s[0],r[0],t[0];
int l,c,rc,k,sk,i,ii,iii,ri;
#+END_SRC
The next step is to define our own version of C's builtin `strstr` function
The next step is to define our own version of C's builtin ~strstr~ function
(almost). I was used to PHP, so I wanted the same return values as PHP's
`strpos`.
~strpos~.
#+BEGIN_SRC c
int strpos (const char *haystack, const char *needle) {
@ -116,9 +116,9 @@ int main(int argc, char *argv[])
}
#+END_SRC
Wait, what? We use `strcpy` to copy the string `argv[1]`, which contains the
word we want to permute, into the statically allocated `os` with length 0? Or we
read a line from standard in and save in `os`? And almost the same for `s`?
Wait, what? We use ~strcpy~ to copy the string ~argv[1]~, which contains the
word we want to permute, into the statically allocated ~os~ with length 0? Or we
read a line from standard in and save in ~os~? And almost the same for ~s~?
That's... not good.
At least these two lines aren't that bad.
@ -148,7 +148,7 @@ dynamically-generated ints as lengths?
char ra[rc][l+1];
#+END_SRC
And then some more assignments and `while` loops...
And then some more assignments and ~while~ loops...
#+BEGIN_SRC c
ri=0;
@ -165,7 +165,7 @@ This formula does something. I'm not sure what.
ca[ii]=floor(i/pow(l,l-ii-1))-floor(i/pow(l,l-ii))*l;
#+END_SRC
More `while` loops, now also with `if` statements.
More ~while~ loops, now also with ~if~ statements.
#+BEGIN_SRC c
iii=0;
@ -182,8 +182,8 @@ More `while` loops, now also with `if` statements.
strncpy(t,s+ca[ii],1);
#+END_SRC
Let's concatenate `t` onto `ra[ri]`, a string which hardly exists due to the
`char ra[rc][l+1];` magic above.
Let's concatenate ~t~ onto ~ra[ri]~, a string which hardly exists due to the
~char ra[rc][l+1];~ magic above.
#+BEGIN_SRC c
strcat(ra[ri],t);
@ -192,7 +192,7 @@ Let's concatenate `t` onto `ra[ri]`, a string which hardly exists due to the
#+END_SRC
And why not concatenate an end-of-string mark onto a string which, if it
doesn't have an end-of-string mark, will make `strcat` fail miserably?
doesn't have an end-of-string mark, will make ~strcat~ fail miserably?
#+BEGIN_SRC c
strcat(ra[ri],"\0");
@ -217,7 +217,7 @@ And then more junk.
//printf("\nOrd: %s\nOrdl\x91ngde: %d\nOrdkombinationer: %d\n",os,l,ri);
#+END_SRC
Phew... At this point, I'm certain that `ra` is supposed to be an array of all
Phew... At this point, I'm certain that ~ra~ is supposed to be an array of all
word permutations. So let's open our dictionary "ord.txt" and look for matches.
#+BEGIN_SRC c
@ -250,7 +250,7 @@ end-of-string mark.
while (ii<ri && k==0) {
#+END_SRC
The magical core of the matching logic, using our own `strpos`:
The magical core of the matching logic, using our own ~strpos~:
#+BEGIN_SRC c
if (strpos(ra[ii],wrd)>-1) {k=1;}
@ -357,9 +357,9 @@ fun nPermutations len = foldl op+ 0 (map (fn n => factorial n * binomc len n)
(upTo 1 len))
(* Gives the size in bytes for storing all word subsequence permutations for a
* given word length in a space-saving way: there are `len` arrays, each taking
* given word length in a space-saving way: there are ~len~ arrays, each taking
* up space for the pointer to the array and the permutations of subsequences of
* length n where `1 <= n <= len` and n is unique.
* length n where ~1 <= n <= len~ and n is unique.
*)
fun nSize len = 8 * len + foldl op+ 0 (
map (fn n => (n + 1) * factorial n * binomc len n)

View File

@ -4,6 +4,8 @@
#+startup: showall
#&toc
* Sound Programming
Much can be programmed, and that includes sound. In the digital world, sound is
typically represented by sequences of about 90 kB per second, so "printing"
sound is merely a matter of printing bytes. As such, any general purpose

View File

@ -3,9 +3,12 @@
How I've set up StumpWM on Trisquel
#&
#+license: wtfpl
#+startup: showall
* My StumpWM setup
GNOME
I use StumpWM instead of e.g. Gnome. StumpWM is a tiling window manager, which
means that it's a good window manager.