Sunday, August 10, 2008

Ihad started a href="http://www.rosettacode.org/wiki/Monty_Hall_simulation">newtopic on Rosetta Code on the Monty Hall problem, using anexample I had laying around that I had written in Python to simulatethe effects of different player strategies. The game goes like this:

Thecontestant in in front of three doors that he cannot seebehind..

The three doors conceal oneprize and the rest being booby prizes, arranged randomly.

TheHost asks the contestant to choose a door.

The hostthen goes behind the doors where only he can see what is concealed,then alwaysopens onedoor, out of the other s not chosen by the contestant, thatmust reveal a booby prize to the contestant.

Thehost then asks the contestant if he would like either to stick with hisprevious choice, or switch and choose the other remaining closed door.

Itturns out that if the contestant follows a strategy of always switchingwhen asked, then he will maximise his chances of winning..

Ithen re-wrote the simulator in AWK (gawk), so there are nowat least two implementations of the simulator on Rosetta Code.,

Tuesday, August 05, 2008

Thisexercise I thought up myself after doing href="http://paddy3118.blogspot.com/2008/08/zig-zag.html">ZigZag."Produce a spiral array.. A spiral array is a square arrangement of thefirst N2 integers, where the numbers increasesequentially as you go around the edges of the arrayspiralling inwards.

TheAlgorithm

Again I choose to define the x axis as the columnnumbers increasing linearly left to right; and the y axis being the rownumbers increasing linearly top to bottom; and with the top left cornerbeing (0,0).If you think of the outer part of the spiral, itcan be defined as how x and y change, i.e. deltas. First along the toprow dx,dy =(1,0) meaning that for successive places x changes by 1and y does not.

Similarlyfor the next straight run , down the RHS: style="font-family: monospace;"> dx,dy = (0,1).Alongthe bottom: dx,dy =(-1,0).And back to the top along the LHS: style="font-family: monospace;">dx,dy = (0,-1).

Thenext time around the loop, dx,dy take on those same values.

Whento stop going in a straight line and change direction?

style="margin-left: 40px;">

When youwould go off the edge of the array

If you are aboutto 'step' on a position that is already filled.

Whento stop?

Whenyou have placed the last integer.

The Program

I have two ways of generating successive dx,dyvalues; a formula in lines 19 and 34, and a commented out use ofitertools.cycle in lines 14,17,18 and 33 which could be used instead.They generate the same sequence.When to change direction isdone by initializing myarray with None values in line 22 and theconditional statement at line 29When to stop is handled bythe outer while loop at line 24.

I read all the other answers and saw a very short example in the J language, which looked like line noise to me, as well as a fairly short example in Haskel a programming language I don't know, but I got a hint as to how it is done by the mention of sorting indeces.

I printed a sheet of graph paper, drew a 6 by 6 grid and put in the zig-zag numbers, after a few false starts I realised that what distinguishes a diagonal is if the top left is coordinate 0,0; x increases to the right, and y increases going down the rows, then the sum of the induces is constant for a diagonal.

If we are going to do some sorting then we need to sort first on the sum of the indeces.

Next, you find that when the sum is odd you are 'going' down the diagonal so increasing y gives the sort order and vice versa, so secondly sort on -y if the sum of the coordinates is even, otherwise +y.

The sort will give the order of the coordinates enumerate the order and you get what integer goes in which location et voila!.

The code:

The sort is split between the iterable which simply generates the coordinate pairs and the key function which returns a tuple of the tho elements highlighted above for sorting the coordinates (Its equivalent to the decorate-sort-undecorate or Schwartzian sort method for any non-Pythoneers). It is the main part of the program:

Sunday, August 03, 2008

Whilst browsing Rosetta Code, I came across a good algorithm for training students, SEDOL, so coded up a version in what I would call a very litteral translation of itsexplanation.I changed the Python example on Wikipedia, as it did not have a validation function as other examples have, and added my code to the Rosetta page.

Note: I came back to the code after a few hours of sleep and thought initially that I would have trouble in the return statement of checksum() as I thought I would need a dictionary doing the full reverse matching of what sedol2value, from ints to characters. sedolchars nolonger includes the vowels so using that method for a value over 9 will not work.Luckily, the result of the calculation is always in the range 0<= n >=9, and within that range, sedolchars[n] works.