# [Tutor] Tutor Digest, Vol 106, Issue 74

Brandon Merritt merrittb7 at gmail.com
Wed Jan 2 02:27:49 CET 2013

```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

On Mon, Dec 31, 2012 at 3:00 AM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>    1. Re: help (Mike G)
>    2. another for loop question - latin square (Brandon Merritt)
>    3. Re: another for loop question - latin square (Steven D'Aprano)
>    4. Re: another for loop question - latin square (Dave Angel)
>    5. Re: another for loop question - latin square (Mitya Sirenef)
>    6. Re: another for loop question - latin square (Alan Gauld)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 30 Dec 2012 04:30:21 -0800
> From: Mike G <msg.ufo at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] help
> Message-ID:
>         <CAHD43mpeT44WytjM=
> PqyxQp14vG608u5rY-6nR2fY-X6m1-z-g at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Hi Randy
>
> > I am an older newbie teaching myself Python programming.
> >
>
> Me too :)
>
> > My problem is I hear no system bell; the enter doesn't respond by
> quitting the program;
> > The problem with the program code the enter key hasn't worked in earlier
> programs.
> >
> > I appreciate any advice I may recieve with this coding glitch.
> >
>
> I copied the code into a blank .py file and ran it from cmd in Windows
> XP x86 using Python 273, it worked fine - including the beep. As well,
> hitting "Enter" exited the program.
>
> It sounds like (no pun intended) it may be how you're running the
> program. I would use a py file and run it using cmd - holler if you
> need help, you may if the path to Python isn't good to go.
>
> Mike
>
>
> ------------------------------
>
> Message: 2
> Date: Sun, 30 Dec 2012 15:59:14 -0800
> From: Brandon Merritt <merrittb7 at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] another for loop question - latin square
> Message-ID:
>         <
> CAEHLpHWnwwZ82bV52M3BpBbYOQjtPFvxn2WULBsio_t3CMnNpQ at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> I am having trouble figuring out a solution after a couple hours now of
> playing with the code. I'm trying to make a latin square using the code
> below:
>
> scaleorder = int(raw_input('Please enter a number for an n*n square: '))
>
>
> topleft = int(raw_input('Please enter the top left number for the square:
> '))
>
> firstrow = range((topleft),scaleorder+1)
>
> count = 0
>
> while count < 8:
>     for i in firstrow:
>         print i
>         count += 1
>         firstrow[i+1]
>
>
> -----------------------------------------------------------------------
>
> It seemed like I could make the for loop work by doing something like this:
>
> for i in firstrow:
>         print i, i+2
>
>
> but  that obviously is not a solution either. Any ideas?
>
> Thanks,
> Brandon
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20121230/0d206b45/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 3
> Date: Mon, 31 Dec 2012 11:27:22 +1100
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] another for loop question - latin square
> Message-ID: <50E0DBEA.5060009 at pearwood.info>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> On 31/12/12 10:59, Brandon Merritt wrote:
> > I am having trouble figuring out a solution after a couple hours now of
> > playing with the code. I'm trying to make a latin square using the code
> > below:
> >
> > scaleorder = int(raw_input('Please enter a number for an n*n square: '))
> > topleft = int(raw_input('Please enter the top left number for the
> square: '))
> > firstrow = range((topleft),scaleorder+1)
> >
> > count = 0
> > while count<  8:
> >      for i in firstrow:
> >          print i
> >          count += 1
> >          firstrow[i+1]
> >
> >
> > -----------------------------------------------------------------------
> >
> > It seemed like I could make the for loop work by doing something like
> this:
> >
> > for i in firstrow:
> >          print i, i+2
> >
> >
> > but  that obviously is not a solution either. Any ideas?
>
>
> Absolutely none. I don't understand your question. What's a latin square?
> Is it the same as a magic square? What do you mean, "make the for loop
> work"?
> The for loop already works: it iterates over the range topleft to
> scaleorder
> inclusive. You say "that obviously is not a solution", but a solution to
> what? What is it supposed to do?
>
> I think that you need to think a bit more carefully about what you are
> trying to accomplish. Probably the most valuable piece of advice I ever
> received was that *writing code* should be the last thing you do when
> trying to solve a problem. When I have a tricky problem to accomplish,
> I will sometimes spend hours designing my code with pencil and paper
> before writing my first line of code.
>
> Can YOU solve a latin square using pen and paper? If you can't, how do you
> expect to write a program to do it?
>
> Start by writing down the steps that you would take to make a latin square.
> I suggest with starting with a fixed size, say, 5x5:
>
> * pick a number for the top left corner, say, 3
>
> * write down the first row: 3 ? ? ? ?
>    [you need to come up with a rule for making the row]
>
> * write down the second row: ? ? ? ? ?
>    [again, you need to come up with a rule for making the next row]
>
> ... and so on for the other three rows. Now you have an algorithm for
> making 5 x 5 latin squares.
>
> Now you can write some code to do that!
>
> Once that is working, you can start thinking about changing from fixed
> 5 x 5 squares to arbitrary N x N sizes.
>
>
>
> --
> Steven
>
>
> ------------------------------
>
> Message: 4
> Date: Sun, 30 Dec 2012 19:44:27 -0500
> From: Dave Angel <d at davea.name>
> To: tutor at python.org
> Subject: Re: [Tutor] another for loop question - latin square
> Message-ID: <50E0DFEB.5000909 at davea.name>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On 12/30/2012 06:59 PM, Brandon Merritt wrote:
> > I am having trouble
>
> Please tell us what Python version you're targeting.  It looks like 2.7,
> but you really should specify it for us.
>
> Next, you should tell us your skill level;  are you experienced at
> another language and learning Python, or what?  Is this latin square an
> assignment, from a tutorial or book, or just for fun?  If from a
> tutorial, have you been doing the earlier problems?  Have you completed
> any other Python projects which were non-trivial?  Do you realize just
> how tricky a latin square is to create?
>
> >  figuring out a solution after a couple hours now of
> > playing with the code. I'm trying to make a latin square
>
> For other readers, an nxn latin square (not a magic square) has the same
> n symbols in each row and in each column, with no duplicates in any row,
> nor in any column.  For a 4x4 square, one solution might be
>
>      A B C D
>      B D A C
>      C A D B
>      D C B A
>
>
> >  using the code
> > below:
> >
> > scaleorder = int(raw_input('Please enter a number for an n*n square: '))
> >
> >
> > topleft = int(raw_input('Please enter the top left number for the square:
> > '))
> >
> > firstrow = range((topleft),scaleorder+1)
>
> You do realize that this won't necessarily create a list of the
> requested size?  Specifically, if the user didn't specify a top-left of
> exactly 1, then the size will be wrong.
>
> > count = 0
> >
> > while count < 8:
> >     for i in firstrow:
> >         print i
> >         count += 1
> >         firstrow[i+1]
>
> Just what did you expect the last line to accomplish?  And what was the
> 8 supposed to mean?
>
> >
> > -----------------------------------------------------------------------
> >
> > It seemed like I could make the for loop work by doing something like
> this:
> >
> > for i in firstrow:
> >         print i, i+2
> >
> >
> > but  that obviously is not a solution either. Any ideas?
> >
> > Thanks,
> > Brandon
> >
> >
> >
>
> Have you constructed a latin square by hand, of any size ?  (try 4 for
> your first attempt)  Do you realize that you could build 3 lines and
> then discover that there is no valid fourth line for those 3?
>
> Have you learned how and why to build functions in your code?   I
> maintain this problem is too tricky to do without at least factoring
> each row into a function.
>
>
>
> --
>
> DaveA
>
>
>
> ------------------------------
>
> Message: 5
> Date: Sun, 30 Dec 2012 19:49:23 -0500
> From: Mitya Sirenef <msirenef at lightbird.net>
> To: tutor at python.org
> Subject: Re: [Tutor] another for loop question - latin square
> Message-ID: <50E0E113.2020403 at lightbird.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 12/30/2012 06:59 PM, Brandon Merritt wrote:
> > I am having trouble figuring out a solution after a couple hours now
> > of playing with the code. I'm trying to make a latin square using the
> > code below:
> >
> > scaleorder = int(raw_input('Please enter a number for an n*n square: '))
> >
> >
> > topleft = int(raw_input('Please enter the top left number for the
> > square: '))
> >
> > firstrow = range((topleft),scaleorder+1)
> >
> > count = 0
> >
> > while count < 8:
> >     for i in firstrow:
> >         print i
> >         count += 1
> >         firstrow[i+1]
> >
> >
> > -----------------------------------------------------------------------
> >
> > It seemed like I could make the for loop work by doing something like
> > this:
> >
> > for i in firstrow:
> >         print i, i+2
>
>
> It's a bit hard to understand what you're trying to do, but if
> my guess is anywhere close to truth, you might be trying
> to make the first line of a latin square by incrementing
> from a specified number. In this case, you should make a range
> from topleft to scaleorder+topleft, and if you want the
> top row randomized, you can use random.shuffle():
>
> from random import shuffle
>
> scaleorder = int(raw_input('Please enter a number for an n*n square: '))
> topleft    = int(raw_input('Please enter the top left number for the
> square: '))
> firstrow   = range(topleft, scaleorder+topleft)
> shuffle(firstrow)
>
> print firstrow
>
>
> Does this help?   -m
>
>
>
> --
> Lark's Tongue Guide to Python: http://lightbird.net/larks/
>
>
>
> ------------------------------
>
> Message: 6
> Date: Mon, 31 Dec 2012 00:51:07 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] another for loop question - latin square
> Message-ID: <kbqnhp\$kto\$1 at ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 31/12/12 00:27, Steven D'Aprano wrote:
> > On 31/12/12 10:59, Brandon Merritt wrote:
> >> I am having trouble figuring out a solution after a couple hours now of
> >> playing with the code. I'm trying to make a latin square using the code
> >> below:
>
> I totally agree with everything Steven said.
> However there is one glaring mistake in your code....
>
>
> >> count = 0
> >> while count<  8:
> >>      for i in firstrow:
> >>          print i
> >>          count += 1
> >>          firstrow[i+1]
>
> What do you think that last row is doing?
> Whatever it is, you are almost certainly wrong.
> But eventually it is likely to throw an index error...
>
> >> -----------------------------------------------------------------------
> >>
> >> It seemed like I could make the for loop work by doing something like
> >> this:
> >>
> >> for i in firstrow:
> >>          print i, i+2
> >>
> >> but  that obviously is not a solution either. Any ideas?
>
> It works perfectly. It prints out i and i+2 for every member of
> firstrow. Whether that's what you want to do is another matter.
>
> As Steven said, try working out what you want to do first,
> then write the code.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 106, Issue 74
> **************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130101/74df182b/attachment-0001.html>
```