[Tutor] range question

James Reynolds eire1130 at gmail.com
Thu Sep 22 16:37:47 CEST 2011


On Thu, Sep 22, 2011 at 10:27 AM, Joel Knoll <mindarson at live.com> wrote:

>  Given a range of integers (1,n), how might I go about printing them in the
> following patterns:
>
> 1 2 3 4 ... n
> 2 3 4 5 ... n 1
> 3 4 5 6 ... n 1 2
>
> etc., e.g. for a "magic square". So that for the range (1,5) for example I
> would get
>
> 1 2 3 4
> 2 3 4 1
> 3 4 1 2
> 4 1 2 3
>
> I just cannot figure out how to make the sequence "start over" within a
> row, i.e. to go from 4 down to 1 in this example.
>
> I have been grappling with this problem for 2.5 days and have gotten
> nowhere!
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

I would probably do something like this:

a = [1,2,3,4]
> print a
> b = a.pop(0)
> a.append(b)
> print a


Probably an easier way of doing it though.

If you wanted to do that four times:

for item in a:
>     print a
>     b = a.pop(0)
>     a.append(b)




You just need to think of it as a register and shifting bits left or right
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110922/e52bc297/attachment.html>


More information about the Tutor mailing list