Mailing List Archive: 49091 messages

# A Rebol Challenge. The Monty Hall Puzzle IN 0 BY TES

### [1/24] from: reichart:prolific at: 17-Dec-2001 10:24

There are some pretty cool examples here! What is really nice about this little "detour" is that we are all learning some interesting interpretations of the Rebol words, and their use. However, we need to keep in mind that we can't loose focus on the goal, which is to write a model of the real problem...in the least number of bytes. I condensed even further this one: w: 100 print [loop w [w: w - first random [0 0 1]]] Which, although a VERY COOL example of Rebol, is not a good example of the problem. This exact same problem in other languages would be just: Matlab: sum(rand(100,1)>1/3) C: for(int i=0,j=0;i<100;i++)j+=rand(3)>1;cout<<j; In Perl: \$i=0;\$j=0;for(;\$i<100;\$i++){\$j+=rand(3)>1;}print \$j Rebol: w: 100 print [loop w [w: w - first random [0 0 1]]] Does not look good for Rebol. Not that I judge a language by its compatibility at all, but it is just plain fun to compare, we are mostly boys after all. So... The problem stated clearly is: Monty shows you three curtains. Behind one curtain is a prize, the others nothing. REBOL: Set up THREE variables in Rebol, 1 "different" than the other two. Pick a door at random. REBOL: Pick a variable at random. Once picked, one of the doors will be revealed to you, which is guaranteed to be empty. REBOL: Search through the variables until an empty one is found, remove it from the list. Or mark it in some manner. Offer chance to switch. REBOL: Always switch, and if percentage chance of receiving prize ("different") of the remaining two variables, print a statement, or even the simple number which is the percentage chance of receiving the prize. We have to include the spaces and the "rebol[]_" in our count. Just this header is 8, since Rebol requires spaces. So use 1 letter variable names. If you write something "tricky" then simply break it down in your email. This will both prove that it is a good model, and teach the rest of us more about the nature of writing good programs, and make yourself a god, all at the same time! If you think you have really modeled the problem correctly, and in the LEAST number of bytes, then append the statement to the Subject line "IN n bytes." Where n is the actual number you were able to achieve. Reichart... [Reichart--Prolific--com] Be useful.

### [2/24] from: joel::neely::fedex::com at: 17-Dec-2001 15:44

Hi, Reichart, Just a minor quibble and a major coincidence... Reichart wrote:
...
> w: 100 print [loop w [w: w - first random [0 0 1]]] > Which, although a VERY COOL example of Rebol, is not a good example of the
<<quoted lines omitted: 4>>
> In Perl: \$i=0;\$j=0;for(;\$i<100;\$i++){\$j+=rand(3)>1;}print \$j > Rebol: w: 100 print [loop w [w: w - first random [0 0 1]]]
Actually, it can be written in Perl as for(1..100){\$j+=rand(3)>1}print \$j dropping 17 bytes from 51 to 34 (concidentally 2/3!!!) -jn-

### [3/24] from: sunandadh:aol at: 17-Dec-2001 19:58

Hi Reichart,
> This exact same problem in other languages would be just: > Matlab: sum(rand(100,1)>1/3) > C: for(int i=0,j=0;i<100;i++)j+=rand(3)>1;cout<<j; > In Perl: \$i=0;\$j=0;for(;\$i<100;\$i++){\$j+=rand(3)>1;}print \$j > Rebol: w: 100 print [loop w [w: w - first random [0 0 1]]] > > Does not look good for Rebol.
We can use the same algorithm as you have for Perl and C, and get something like: s: 0 n: 100 loop n[if 1 < Random 3 [s: s + 1]]s Which is 47 bytes -- exactly the same as your C (actually C++) example And shorter than your Perl, though longer than Joel's improvement. (You ask that we count the Rebol [] as an extra 8 bytes overhead. Fair enough, but shouldn't your C++ have a standard header include?) Unlike Perl and C, Rebol needs an explicit type cast to go from true/false to an integer, so to follow exactly the Perl/C++ examples we'd have to write something like: s: 0 n: 100 loop n[s: s + to-integer (1 < Random 3)] or s: 0 n: 100 loop n[s: s + pick[0 1](2 < Random 3)] but they are longer. Rebol "loses out" on sheer shortness here because it needs spaces between operators, and its RAND function is spelt RANDOM. Some people would see these as sufficient compensation for coming second in such a race. An, of course, bear in mind that the entire Rebol environment needed to run these programs is under 252K (Rebol/Core, Windows 3.1). If I am allowed to have a slightly larger environment (though still way smaller than C or Perl -- I don't know about Matlab), then I'd define: L: :Loop R: :Random And roll my code home in 39 bytes: s: 0 n: 100 L n[if 1 < R 3 [s: s + 1]]s Sunanda.

## Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BYTES

Incedently, This is 3 bytes shorter: w: 100 print loop w [w: w - last random [0 0 1]] Of course more could be shave off, at the loss of readablility, but would better match the examples from other languages given. --Ryan Reichart wrote:
> There are some pretty cool examples here! > What is really nice about this little "detour" is that we are all learning
<<quoted lines omitted: 104>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Ryan Cole Programmer Analyst www.iesco-dms.com 707-468-5400 The contradiction so puzzling to the ordinary way of thinking comes from the fact that we have to use language to communicate our inner experience which in its very nature transcends lingistics. -D.T. Suzuki

### [5/24] from: sunandadh:aol at: 18-Dec-2001 2:45

Hi Ryan,
> Incedently, > This is 3 bytes shorter: > > w: 100 print loop w [w: w - last random [0 0 1]] > > Of course more could be shave off, at the loss of readablility, but would > better > match the examples from other languages given. >
And, losing the readability as you suggest, this is 8 bytes shorter still: w: 100 loop w[w: w - last random[0 0 1]] 40 bytes, and counting, Sunanda.

## Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BY TES

Hey Sunanda and Ryan, All very cool. But the goal is to not keep making the example shown with the C, MatLab, and Pearl smaller, but to make a more verbose accurate portrayal of the original puzzle in Rebol. I'm then going to go see if they can do the same in Pearl, or language of choice. In agreement Sunanda, making us count Rebol[] should mean they count #includes, etc. But those parts of the arguments hold little meaning to me over all. One simply offers them up and states the facts. 145 with, 138 without for example. I really enjoy reading these examples though. I have learned about three new things about Rebol I did not know! Reichart... [Reichart--Prolific--com] Be useful.

## Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BYTES

On 18-Dec-01, Reichart wrote:
> Hey Sunanda and Ryan, > All very cool. > But the goal is to not keep making the example shown with the C, > MatLab, and Pearl smaller,
Aw gee... But see below... (:
> I really enjoy reading these examples though. I have learned about > three new things about Rebol I did not know!
Me too.
>> And, losing the readability as you suggest, this is 8 bytes shorter >> still: >> w: 100 loop w[w: w - last random[0 0 1]] >> 40 bytes, and counting, >> Sunanda.
w: 100 loop w[w: w - any random[0 0 1]] 39... -- Carl Read

### [8/24] from: james:mustard at: 18-Dec-2001 23:01

It seems everyone since Gabriele has removed the randomness factor from their equations! random/seed now or some similar quasi-random seed value needs to be chosen or you repeat the same pattern every time! just my 2c :) James.

### [9/24] from: al:bri:xtra at: 18-Dec-2001 23:12

James pointed out:
> random/seed now
Equivalent code needs to be added to the C/C++ versions too. Andrew Martin ICQ: 26227169 http://valley.150m.com/

### [10/24] from: lmecir:mbox:vol:cz at: 18-Dec-2001 12:07

Hi Carl, having fun, aren't you? This one is shorter: loop w: 100[w: w - any random[0 0 1]]

## Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BY TES

Hi Riechart,
> But the goal is to not keep making the example shown with the C, MatLab, and > Pearl smaller, but to make a more verbose accurate portrayal of the
original
> puzzle in Rebol. > > I'm then going to go see if they can do the same in Pearl, or language of > choice.
Okay, I'm game. Here's a version of the Monty Simulator that plays the game 100 times, and prints the result for each play, and a final total. Our competitor swaps each time. To compare like with like, other programs shou;d exactly replicate the format of the printed lines. The program as presented is about 350 bytes I reserve the right to reduce it to under 250 bytes for a "final version". I am also fairly confident that I can do a full GUI implementation in under 1K Rebol [] N: 100 Loop N [ D: random/secure [Red Green Blue] C: pick D random 3 prin["I pick"D/1] M: next D if find M C[alter M C] prin[". M shows"M/1] either 1 = length? M [prin [". I swap to"C]] [prin [". I swap to"M/2]] prin[". Car was behind" C] either D/1 = C [print ". I lose."N: N - 1] [Print ". I win."] ] print ["Wins:" N] Notes on variables: D - for Doors array C - colour of door that hides the car M - for Monty's two doors N for loop count and wins (using Ryan's ingenious two-for-the-price of one trick) Print line format is: I pick [col]. M shows [col]. I swap to [col]. Car was behind [col]. I [win/lose]. where [col] = colour name. last line: wins: nn Sunanda.

### [12/24] from: mario:cassani:icl at: 18-Dec-2001 12:00

Hallo Sunanda, Rebolers all, it looks like you are having a lot of fun with this kind of puzzles. Maybe this site will interest you: Monty: http://www.cut-the-knot.com/hall.html Home: http://www.cut-the-knot.com/ Other similar links: http://cartalk.cars.com/Tools/monty.pl http://www.shodor.org/interactivate/activities/monty3/ http://www.google.com/search?q=Monty+Hall
<<quoted lines omitted: 3>>
> confident that I > can do a full GUI implementation in under 1K
Uhm, sounds interesting! A small interactive math puzzles with animated man and sliding doors... Mario

## Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BYTES

> w: 100 loop w[w: w - last random[0 0 1]] > > 40 bytes, and counting, >
But w: 100 loop w[w: w - random 2 / 3] is 34 bytes... Limbo, anyone? ;-) -jn- -- What a distressing contrast there is between the radiant intelligence of the child and the feeble mentality of the average adult. -- Sigmund Freud joel}dot}neely}at}FIX}PUNCTUATION}fedex}dot}com

### [14/24] from: deadzaphod:flyingparty at: 18-Dec-2001 5:25

> But > > w: 100 loop w[w: w - random 2 / 3] > > is 34 bytes...
doesn't really compute correctly, it gives a constant result.
>> w: 100 loop w[w: w - random 2 / 3]
== 33.3333333333332
>> w: 100 loop w[w: w - random 2 / 3]
== 33.3333333333332
>> w: 100 loop w[w: w - random 2 / 3]
== 33.3333333333332 if that counted we could use: w: random 2 / 3 ?? w 20 bytes ;-)

### [15/24] from: joel:neely:fedex at: 18-Dec-2001 1:44

Hi, James, Interesting point... James Marsden wrote:
> It seems everyone since Gabriele has removed the randomness > factor from their equations! > > random/seed now > > or some similar quasi-random seed value needs to be chosen or > you repeat the same pattern every time! >
That's why Perl adopted the convention that, if you haven't seeded the PRNG before the first use, it will do so automatically, based on TOD and other hard-to-predict-or-replicate data. The other side of the coin is that you may WANT your PRNG to give the same values every time you run your code (at least during development and testing) so that you can see the effect of any changes in a reliable way. Finally, whether freshly seeded or not, if the PRNG is unbiased the long-term statistics will converge to the same answer. -jn- -- Now when you say that souls don't develop because people become distracted... hey, has anyone noticed that building there before? -- Steve Chapel joel|dot|FIX|PUNCTUATION|neely|at|fedex|dot|com

### [16/24] from: joel:neely:fedex at: 18-Dec-2001 1:50

Hi, Cal, You caught me with my tongue planted firmly in my cheek! Cal Dixon wrote:
> > But > > > > w: 100 loop w[w: w - random 2 / 3] > > > > is 34 bytes... > > doesn't really compute correctly, it gives a constant result. >
But it's the CORRECT constant result! ;-)
> >> w: 100 loop w[w: w - random 2 / 3] > == 33.3333333333332
<<quoted lines omitted: 5>>
> w: random 2 / 3 ?? w > 20 bytes ;-)
Or even 2 / 3 FIVE BYTES!!!!! -- In some countries being a foriegner who is likely to be carrying cash is a traffic violation. -- Aaron Watters joel'dot'neely'at'fedex'FIX'PUNCTUATION'dot'com

### [17/24] from: lmecir:mbox:vol:cz at: 18-Dec-2001 23:50

Hi, <<Joel>> But w: 100 loop w[w: w - random 2 / 3] is 34 bytes... Limbo, anyone? ;-) -jn- -- What a distressing contrast there is between the radiant intelligence of the child and the feeble mentality of the average adult. -- Sigmund Freud joel}dot}neely}at}FIX}PUNCTUATION}fedex}dot}com <</Joel>> what should the (random 2 / 3) do? Cheers Ladislav

### [18/24] from: joel:neely:fedex at: 18-Dec-2001 20:11

> But > > w: 100 loop w[w: w - random 2 / 3] > > is 34 bytes... >
...
> what should the (random 2 / 3) do? >
It returns a random real number bounded (on both sides ;-) by 0.666666666666667 The post was, of course, tongue-in-cheek. I was simply demonstrating a program that was so compact that all traces of reasoning about its behavior and correctness had long since evaporated. As to what (random 2 / 3) *should* do, as opposed to what it actually *does* do, we find the following: USAGE: RANDOM value /seed /secure /only DESCRIPTION: Returns a random value of the same datatype. RANDOM is an action value. ARGUMENTS: value -- Maximum value of result (Type: any) which might lead one to assume that (random 2 / 3) would yield a random decimal value between 0.0 and 0.66666... However, I've been vigorously scolded for believing that zero is a number and that consistency is a virtue, so I'll have to say simply that I have no officially publishable opinion on what it *should* do. ;-) -jn-

### [19/24] from: brett:codeconscious at: 19-Dec-2001 12:56

> But the goal is to not keep making the example shown with the C, MatLab,
and
> Pearl smaller, but to make a more verbose accurate portrayal of the
original
> puzzle in Rebol.
Here is a very verbose version of my original solution with Sunanda's explanation idea incorporated: REBOL [] box: [Box1 Box2 Box3] wins: 0 loop number-of-games: 100 [ pick-a: func [what] [copy/part random what 1] prize: pick-a box choice1: pick-a box remaining-box: exclude box opened-box: exclude box union prize choice1 choice2: pick-a remaining-box if win: equal? prize choice2 [ wins: wins + 1 ] print join "I choose " [ choice1 ". " opened-box " opened. I switch to " choice2 ". Prize in " prize ". I " either win ["win"]["lose"] "." ] ] print wins / number-of-games
> I'm then going to go see if they can do the same in Pearl, or language of > choice.
The choice of variable name is going to impact on the goal of the smallest verbose program. For this goal I believe one should use a meaningful name - something that does not require a legend to understand it immediately in it's own context. Brett.

## Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BY TES

1. Q: Sunanda wrote: To compare like with like, other programs shou;d exactly replicate the format of the printed lines. A: We agree! Very nicely done. Actually "simulates" the problem. Several others have written good versions as well. Brett's for example. It is rare, even on this mailing group, that we can all try our hand at the same basic problem. Of course even in the same language there are infinite numbers of ways to express oneself. But this is what makes this so useful. I have grabbed a chunk of these emails for others (friends I'm slowly convincing to join the alliance) so they can learn faster. Coincidently, I'm currently working on a product I designed which will eventually become a new slot game in Vegas (and other gaming places). I have been doing combinatory statistics for the past 2 weeks. One of the mathematicians is working on Linux, and our front end is in Director PC. It would be SOOOOOOOOO much easier if everything were just Rebol. But both sides have their own problems. Rebol crashes on the Linux side, and the Director programmer (lingo) will NOT learn a new language. I'm slowly chipping away at the linux guy. Trying to get him to accept that Rebol is so great that he shouyld try to figure out why it crashes on his machine. Reichart... [Reichart--Prolific--com] Be useful.

## Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BYTES

Hi Brett
> The choice of variable name is going to impact on the goal of the smallest > verbose program. > For this goal I believe one should use a meaningful name - something that > does not require a legend to understand it > immediately in it's own contex
Good point. I suppose part of the problem is that it is much easier to measure program conciseness (which is what Reichart challenged the list about) than it is to measure program clarity. And, as we all know from our school days onwards, if something can be measured it often assumes a priority over the things that cannot be so easily measured. So thanks for the timely reminder that Rebol is a messaging language, not an SMS txt msg lngg, Sunanda,

## Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BY TES

Hi Reichart, Thanks for the compliment, I have to "fess up" though must last posted solution (the verbose one) has a bug. Found it last night just after I sent it to a friend to try to convince him of the 2/3 result. :-/ The problem was that the switch strategy was not always assured. Strangely the result is close to 2/3 for the bugged version. Anyway the new (longer) version, attached below, fixes the problem by simulating Monty's part more closely. I have spent an amazing amout of time on this Monty Hall problem and I'm currently trying to convince two of my good friends of the result. And yes one is a statistician. I'm not sure whether to curse you for the amount of time I've had to allocate on the problem or thank you for the stimulating thinking and useful techniques it has produced. :) REBOL [] box: [Box1 Box2 Box3] wins: 0 loop number-of-games: 100 [ pick-a: func [what] [copy/part random what 1] prize: pick-a box choice1: pick-a box montys-choices: exclude box union prize choice1 opened-box: pick-a montys-choices choice2: exclude box union choice1 opened-box if win: equal? prize choice2 [ wins: wins + 1 ] print join "I choose " [ choice1 ". " opened-box " opened. I switch to " choice2 ". Prize in " prize ". I " either win ["win"] ["lose"] "." ] ] print wins / number-of-games Brett.

### [23/24] from: greggirwin:mindspring at: 19-Dec-2001 17:29

<< To compare like with like, other programs should exactly replicate the format of the printed lines. >> Yes, as long as the design of the printed format isn't designed with any particular language in mind. :) I.e. the first implmentation shouldn't define the format. --Gregg

### [24/24] from: sunandadh:aol at: 19-Dec-2001 20:34

Hi Mario,
> Hallo Sunanda, Rebolers all, > it looks like you are having a lot of fun with this > kind of puzzles. > Maybe this site will interest you:
Thanks for the references.
> I am also fairly > > confident that I > > can do a full GUI implementation in under 1K > > Uhm, sounds interesting! > A small interactive math puzzles with animated man and > sliding doors... >