• Welcome to the new Internet Infidels Discussion Board, formerly Talk Freethought.

What's that code snippet?

Swammerdami

Squadron Leader
Joined
Dec 15, 2017
Messages
4,897
Location
Land of Smiles
Basic Beliefs
pseudo-deism
There are several programmers here at TFT. Let's play a game. (I might have posted this in the Games subforum but there doesn't seem to be one.)

Post a code snippet (in any programming language) and/or guess the intent or context of a prior submission. Submissions may be made in any language, and with whatever level of obfuscation etc. seem most appropriate. Consider the problem space to be quite flexible: if you wish you can reverse the puzzling and pose a task, asking for a code snippet. Or the snippet might have a bug, with identifying or fixing the bug being the puzzle.

I'll start. The cute and useful algorithm that this snippet helps implement is mentioned in one of Knuth's books. If unfamiliar with that algorithm, careful contemplation of this snippet MIGHT lead you to guess the context and re-discover an elegant algorithm.

Snippet #1
Code:
ITEM item_rand(struct rhtable *rhp, int size)
{
       double f = unif_rand(0.0, 1.0) * size;
       rhp += (int)f;
       return rhp->rh_item[f > rhp->rh_splitp];
       // return rhp->rh_item[f - (int)f > rhp->rh_splitp ? 1 : 0];
}
An alternative (somewhat less obfuscated?) return statement is shown commented-out.
 
unif_rand() looks like a random number generator

It selects random element from structure array and then use relative position and split level to return first or second element of some two element array.

Do I get half prize for that? I have never read Knuth's books.
 
unif_rand() looks like a random number generator

It selects random element from structure array and then use relative position and split level to return first or second element of some two element array.

Do I get half prize for that? I have never read Knuth's books.
You're correct about unif_rand(), but what useful task is being implemented here? While 99+% of programmers would never have reason to use this function, the task for which it provides a simple fast approach has broad application. When might you want to "select a random element from an array"? Why might the task be tediously slow without a special cleverness (as the approach Snippet #1 supports will be seen to possess)?

It would be permitted in the game to write xxx() or foo() instead of unif_rand() to make a snippet more difficult. But guessing the detailed purpose of this particular snippet is already a big challenge, and I didn't want snippet #1 to be too obnoxious. :)

I'll post hints tomorrow, but meanwhile: Please submit other interesting code snippets using any criteria: elegance, efficiency, amusement, concisement, stupidity, novelty, etc.
 
What's the prize anyway? And I don't think commented part is equivalent to the current one. I suspect commented version is correct one.



And the whole thing just selects random subboxes of boxes of equal sizes split unevenly into 2 subboxes.

rh_splitp= Random Hole Split Probability.

I think I have got it.
probability to fall into random sub-hole. :)



Give me my prize!
 
What's the prize anyway? And I don't think commented part is equivalent to the current one. I suspect commented version is correct one.
There is a setup() routine needed to initialize the appropriate values into R[].rh_splitp, R[].rh_item[0] and R[].rh_item[1]. That setup() routine will need to know whether the silly micro-optimization in the uncommented 'return' is used or not.

And the whole thing just selects random subboxes of boxes of equal sizes split unevenly into 2 subboxes.

But WHY? Is there some special reason why a selection would be organized this way? How many distinct ITEMS do you think are in the table anyway?


Let's get some more interesting snippets! No further hints on Snippet #1 until there are more snippets enlivening the thread!
 
I agree that the commented out return statement is not equivalent to the one that's not. The former checks if the decimal part of f is larger than splitp, implying that splitp is a floating point between 0 and 1. The latter checks if f is greater than splitp. Hmm.

snippet.gif

The question of my snippet is, what the heck is it and what does it do?
 
I agree that the commented out return statement is not equivalent to the one that's not. The former checks if the decimal part of f is larger than splitp, implying that splitp is a floating point between 0 and 1. The latter checks if f is greater than splitp. Hmm.

View attachment 32281

The question of my snippet is, what the heck is it and what does it do?

Is your picture supposed to be an answer??


Anyway, my guess: Bleeding together zones of two different colors. Since I refuse to learn C I'm not at all sure of this.
 
I agree that the commented out return statement is not equivalent to the one that's not. The former checks if the decimal part of f is larger than splitp, implying that splitp is a floating point between 0 and 1. The latter checks if f is greater than splitp. Hmm.

View attachment 32281

The question of my snippet is, what the heck is it and what does it do?

Is your picture supposed to be an answer??


Anyway, my guess: Bleeding together zones of two different colors. Since I refuse to learn C I'm not at all sure of this.

No. Just to clarify, the picture is a new snippet, unrelated to the algorithm in the opening post. Should have made that more clear.
 
Snippet #2
View attachment 32281

My initial reaction is that this was a snippet of a Mondrian painting, but Google Images suggests that Piet Mondrian economised on paint and never used esoteric colors like cyan or violet!

In a similar vein, I submit

Snippet #3
primes.gif

17/91; 78/85; 19/51; 23/38; 29/33; 77/29; 95/23; 77/19; 1/17; 11/13; 13/11; 15/2; 1/7; 55/1
 
I agree that the commented out return statement is not equivalent to the one that's not. The former checks if the decimal part of f is larger than splitp, implying that splitp is a floating point between 0 and 1. The latter checks if f is greater than splitp. Hmm.

This was addressed in the post immediately before yours. Let me be more explicit:

(1) item_rand() utilizes TWO random numbers: (a) an integer in the range {0, 1, 2, ..., size-1} and a "splitting" probability 0 < p < 1.
(2) item_rand() derives BOTH the random numbers it needs from a SINGLE call to a random-number generator.
(3) Since item_rand() may be called millions of times while setup() is called only once, a TINY bit of time can be saved by adding K to R[K].rh_splitp. Obviously setup() and item_rand() must agree on whether this micro-optimization is in use or not! setup() is a MUCH longer routine than item_rand() and is NOT shown.

The optimizations (2) and (3) are much less interesting than the algorithm implemented by setup()/item_rand(). It can be used by MANY simulation programs and does what it does MUCH faster than a naive approach.
 
I agree that the commented out return statement is not equivalent to the one that's not. The former checks if the decimal part of f is larger than splitp, implying that splitp is a floating point between 0 and 1. The latter checks if f is greater than splitp. Hmm.

This was addressed in the post immediately before yours. Let me be more explicit:

(1) item_rand() utilizes TWO random numbers: (a) an integer in the range {0, 1, 2, ..., size-1} and a "splitting" probability 0 < p < 1.
(2) item_rand() derives BOTH the random numbers it needs from a SINGLE call to a random-number generator.
(3) Since item_rand() may be called millions of times while setup() is called only once, a TINY bit of time can be saved by adding K to R[K].rh_splitp. Obviously setup() and item_rand() must agree on whether this micro-optimization is in use or not! setup() is a MUCH longer routine than item_rand() and is NOT shown.

The optimizations (2) and (3) are much less interesting than the algorithm implemented by setup()/item_rand(). It can be used by MANY simulation programs and does what it does MUCH faster than a naive approach.

Optimized Monte-Carlo generator?
 
More snippets please! Surely there are lots of code snippets that are amusing, instructive, scary, puzzling, disgusting or worth looking at for some reason!
I think Jayjay's Mondrian snippet prints "Hello world." Am I close?


... the algorithm implemented by setup()/item_rand() [] can be used by MANY simulation programs and does what it does MUCH faster than a naive approach.

Optimized Monte-Carlo generator?

Yes!
Among how many distinct arbitrary-probability items will item_rand() choose from?
(I barely remember my English. Is "choose from among" an English idiom? Did I illegally split a preposition?)

The following was output by a program that called item_rand() for each character. What did that program do? Be specific.
To say,
'Thy past know,
Feed'st is't not my still shown.
To whom ther's gainst that kept strange, had night,
For them thin my loss, he roses return'd loves test high defections fix'd that pour'st though win my heart,
As he leave memory
My spirit, now untains as play as your nature hap to the golder ther with my truth defection the was those jacks truth's and mock to hath canst my in tabless bold,
Bare flower pale same;
And hooks, that thy name to shadows I be fair far that their special neight in thee hourse her with from their body's ending thy fate,
And not by all my babe, youthful doth flies
When so sullence he trial love to the posters brain widow'd chaste me I not love which his grow shall carcely and error and it lame, thou show!

Thou pine and the thought kill I saw you not beauty grief, all mayst hope.

Like a heart stransfix the word
No profane, and age,
Which glory;
That a fortify the guides to large? Is thy powers with taugh'd each borrows not could many lamb he leaguest, 'tis fell keeps your never speed,
The worst others been
To spends
For if thou not keep,
Laid best in my loves, thy see them bore thus vainly charg'd the eyes shall be deceiv'd;
But yet whit doth fingers not able arigour fix'd'?
Be scandles their we, what thoughts of my lays shame,
Commit the world with three shall I still I this writ and stol'n from the love ere iniquid place?
I have his purple same;
Coral be so it thier wrongs
That shady sport.

There I boast thou did example married, delight,
 
Snippet #3
View attachment 32282

17/91; 78/85; 19/51; 23/38; 29/33; 77/29; 95/23; 77/19; 1/17; 11/13; 13/11; 15/2; 1/7; 55/1
I am not a Mensa material.

That "tiny" program contains the Sieve of Eratosthenes; and outputs the entire sequence of prime numbers!
(In the form 22, 23, 25, 27, 211, ... with non-power-of-two numbers ignored. ... If you wait long enough.)

Who created that "programming language"? Google "Genius who died of Covid-19"
 
char conundrum(char x){
if( (x >= 65) && (x <= 90) ) return (x | 0x20);
if( (x >= 97) && (x <= 122) ) return x(x & 0xDF);
return(x);
}

0x is hexadecimal
 
char conundrum(char x){
if( (x >= 65) && (x <= 90) ) return (x | 0x20);
if( (x >= 97) && (x <= 122) ) return x(x & 0xDF);
return(x);
}

0x is hexadecimal

I'm pretty sure this flips the case of the character, doesn't alter anything else.

And anyone that can follow that code would know what 0x means.
 
Back
Top Bottom