Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connectionsPath: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: DFS Newsgroups: comp.os.linux.advocacy Subject: Re: OT programming challenge: fastest/best/shortest C program to jumble a sentence, then restore it Date: Fri, 8 Mar 2024 15:22:06 -0500 Organization: A noiseless patient Spider Lines: 133 Message-ID: References: <65e9cad3$0$4689$882e4bbb@reader.netnews.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 8 Mar 2024 20:22:02 -0000 (UTC) Injection-Info: dont-email.me; posting-host="fab2b49de04fb903622181ec797b4864"; logging-data="1998726"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BWRpJwDD7HS++WQGK7LhO" User-Agent: Betterbird (Windows) Cancel-Lock: sha1:rNm9Q33nw5HrF/T/KjjpFnxjwbE= In-Reply-To: Content-Language: en-US Bytes: 5054 On 3/8/2024 2:48 PM, rbowman wrote: > On Fri, 8 Mar 2024 08:09:33 -0500, DFS wrote: > >> It's harder than you think to randomly shuffle the words in a shorter >> sentence, so that each word ends up in a different position than it >> started in. >> >> Bring it! Thanks for doing it. FYI: -------------------------------------------------------------------------- ~$ gcc jumble_rbowman.c -o jumble jumble_rbowman.c: In function ‘main’: jumble_rbowman.c:9:29: warning: missing terminating " character 9 | char* sentence = strdup("Once you try it, you'll see it doesn't need | ^ jumble_rbowman.c:9:29: error: missing terminating " character 9 | char* sentence = strdup("Once you try it, you'll see it doesn't need | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ jumble_rbowman.c:10:1: error: ‘spice’ undeclared (first use in this function) 10 | spice."); | ^~~~~ jumble_rbowman.c:10:1: note: each undeclared identifier is reported only once for each function it appears in jumble_rbowman.c:10:7: warning: missing terminating " character 10 | spice."); | ^ jumble_rbowman.c:10:7: error: missing terminating " character 10 | spice."); | ^~~ jumble_rbowman.c:11:5: error: expected identifier before ‘char’ 11 | char* tokens[20]; | ^~~~ jumble_rbowman.c:54:1: error: expected ‘,’ or ‘;’ before ‘}’ token 54 | } | ^ jumble_rbowman.c:54:1: error: expected declaration or statement at end of input -------------------------------------------------------------------------- > #include > #include > #include > #include > > int main(int argc, char** argv) > { > char* sentence = strdup("Once you try it, you'll see it doesn't need > spice."); > char* tokens[20]; > int token_count = 0; > int i; > int j; > int slots; > int candidate; > int* indices; > > for (tokens[token_count] = strtok(sentence, " "); > tokens[token_count]; > tokens[token_count] = strtok(NULL, " ")) > { > token_count++; > } > > indices = malloc(token_count * sizeof(int)); > for (i=0; i indices[i] = -1; > } > srand((unsigned int) time(NULL)); > for (i=0, slots=0; slots candidate = rand() % token_count; > for (j=0; j if (indices[j] == candidate) { > break; > } > else if (indices[j] == -1) { > indices[slots++] = candidate; > break; > } > } > } > printf("\nshuffled:\n"); > for (i=0; i printf("%s ", tokens[indices[i]]); > } > printf("\noriginal: \n"); > for (i=0; i printf("%s ", tokens[i]); > } > printf("\n"); > > return 0; > } > > ./shuffle > > shuffled: > doesn't you spice. try Once it, it need see you'll > original: > Once you try it, you'll see it doesn't need spice. > > ./shuffle > > shuffled: > need it, spice. it you you'll see Once try doesn't > original: > Once you try it, you'll see it doesn't need spice. > > Get enough monkeys running it and the shuffled sentence may be the same as > the original. > > For production, I'd first count the tokens and allocate the tokens array > but I'm lazy. Further enhancements, allow the string to be entered on the > command line, read strings from a file and write shuffled strings to a > file, and so on. > > Of course in Python you could use split and shuffle to abstract away all > the messiness. > > > >