Last active
September 5, 2015 07:35
-
-
Save cslarsen/458043 to your computer and use it in GitHub Desktop.
Revisions
-
cslarsen revised this gist
Dec 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ bool zero(int counts[MAX]) */ bool uniq(int counts[MAX]) { int x = 0; for ( int n=0; n<MAX; ++n ) x ^= counts[n] ^ n; return x == 0; -
cslarsen revised this gist
Dec 8, 2014 . 1 changed file with 6 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /* * Print all permutations of string ``s´´. (C++) * * Solved as a counting problem and coded in a hurry. * @@ -36,18 +36,14 @@ bool zero(int counts[MAX]) } /* * Are all indices unique? */ bool uniq(int counts[MAX]) { int x = 0; for ( int n=0; n<MAX; ++n ) x ^= counts[n] ^ n; return x == 0; } int main() -
cslarsen revised this gist
Jun 30, 2010 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,6 +35,12 @@ bool zero(int counts[MAX]) return true; } /* * Are all numbers unique? * * Note that this is a slow O(n^2) algorithm, * but can be improved to O(n). */ bool uniq(int counts[MAX]) { for ( int p=0; p<MAX; ++p ) -
cslarsen renamed this gist
Jun 30, 2010 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
cslarsen revised this gist
Jun 30, 2010 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,10 +46,11 @@ bool uniq(int counts[MAX]) int main() { int counts[MAX] = {0}; do { if ( uniq(counts) ) print(counts); inc(counts); } while ( !zero(counts) ); } -
cslarsen revised this gist
Jun 30, 2010 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,10 @@ /* * Print all permutations of string ``s´´. (C++) * * Solved as a counting problem and coded in a hurry. * * A more elegant and recursive solution: * http://nicolas-lara.blogspot.com/2009/01/permutations.html */ #include <stdio.h> -
cslarsen revised this gist
Jun 30, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /* * Print all permutations of string ``s´´. (C++) */ #include <stdio.h> -
cslarsen created this gist
Jun 30, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ /* * Print all permutations of string ``s´´. */ #include <stdio.h> static const char s[] = "abc"; const int MAX = (sizeof(s)/sizeof(char)) - 1; void print(int counts[MAX]) { for ( int n=0; n<MAX; ++n ) printf("%c", s[counts[n]]); printf("\n"); } void inc(int counts[MAX], int p = 0) { if ( p < MAX && ++counts[p] >= MAX ) { counts[p] = 0; inc(counts, p+1); } } bool zero(int counts[MAX]) { for ( int p=0; p < MAX; ++p ) if ( counts[p] != 0 ) return false; return true; } bool uniq(int counts[MAX]) { for ( int p=0; p<MAX; ++p ) for ( int i=p+1; i<MAX; ++i ) if ( counts[p] == counts[i] ) return false; return true; } int main() { int counts[MAX] = {0, 0, 0}; do { if ( uniq(counts) ) print(counts); inc(counts); } while ( !zero(counts) ); }