[Tutor] another for loop question - latin square (was: Tutor Digest, Vol 106, Issue 74)

Dave Angel d at davea.name
Wed Jan 2 03:07:08 CET 2013


On 01/01/2013 08:27 PM, Brandon Merritt wrote:
> Sorry, I should have been more clear in what I was trying to accomplish. I
> am trying to build the latin square as follows:
>
> 1234567
> 2345671
> 3456712
> 4567123
> 5671234
> 6712345
> 7123456
>
> So, the "scaleorder" is the n*n scale of the square - in this case, a 7x7.
> The key is that there should be no duplicate number for each row and for
> each column. And the "topleft" variable is the number in the topleft that
> sets how the rest of the square will look - in the example I gave above, it
> is 1.
>
> The link below is the original exercise - I am not in school, purely for my
> own education as I am really trying hard to become a passable Python
> programmer. I've been working at it for 6 month now, and I don't know any
> other languages:
>
> http://www.cse.msu.edu/~cse231/PracticeOfComputingUsingPython/02_Control/LatinSquares/Project03.pdf
>
>
> Thank you,
> Brandon

Please reply to a single message, not to some digest.  This way your new
message will be threaded together with the previous ones.  And for those
unable to see the threads, at least the subject line will match.  Note
the first line below of the quote you used:

>
> On Mon, Dec 31, 2012 at 3:00 AM, <tutor-request at python.org> wrote:
>
>> <snip>
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of Tutor digest..."
>>
>>
>>

So you don't just want a latin-square, you want a particular one.  You
picked the most trivial one, where all the spots are numeric, and each
line is simply sequentially chosen from a ring, 1 through scaleorder.

Assuming scaleorder is less than 10, the following should work.

table = [str(i) for i in range(1, scaleorder+1) * 3]

for row in range(topleft-1, topleft+scaleorder-1):
    line = table[row:row+scaleorder]
    print "".join(line)

If you have to deal with scaleorders bigger than 9, then you'll need a
separator between the numbers.  Probably the easiest place to fix that
would be in constructing the table.  But a q&d fix might be to use  "
".join() on the last line.

-- 

DaveA



More information about the Tutor mailing list