Python Forum
Creating a circular matrix with one input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a circular matrix with one input
#11
(Sep-29-2020, 05:27 PM)DPaul Wrote: For the time being it works only for 3 and 5, perhaps with some work it will do all odd numbers.

If you are using "peeling the onion" approach then number of layers (cycles) can be determined with:

cycles = sum(divmod(n, 2)) 
# n = 5 -> 3
# n = 3 -> 2
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#12
Quote:If you are using "peeling the onion" approach then number of layers (cycles) can be determined with:
Interesting, obviously i do something similar, but this is more "straightforward".
It counts the centre as a layer; this is where i cheat a little. i know where the centre is, so i put the
first block in the middle outside of any loop; one layer less to worry about. Hence the odd numbers. :-)
From that i can derive where the inner layer's start is. (But it needs a lot more work.)

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#13
Original problem statement was to start from left upper. So starting from center is derivative of the original.

As per conditions this is always square matrice. So if the n is even, then we will have center layer of four items and if it's odd then it is only one item. This can be used when starting from center.

If n is odd then starting point indice for both row and column is sum(divmode(n, 2)), if it is even then starting point will be low-left of inner layer (rowwise n // 2 + 1, columnwise n // 2 -1)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#14
(Sep-30-2020, 01:11 PM)perfringo Wrote: Original problem statement was to start from left upper.

I read that the printing must start at the left upper.
(Only my calculations start in the middle )
Anyway, considering the keen intrest of TS, i branched out
to do a 3D spiral, using a device called "cube:bit", that
is available in various formats. Nice multi-colored effect :-)
Doing that, i found a different way of programming it, which
might take me back to the 2D print problem.
Keeps one busy.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#15
For those interested, all odd numbers are OK:
No recursion. Number = 9
Paul
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9] [32, 33, 34, 35, 36, 37, 38, 39, 10] [31, 56, 57, 58, 59, 60, 61, 40, 11] [30, 55, 72, 73, 74, 75, 62, 41, 12] [29, 54, 71, 80, 81, 76, 63, 42, 13] [28, 53, 70, 79, 78, 77, 64, 43, 14] [27, 52, 69, 68, 67, 66, 65, 44, 15] [26, 51, 50, 49, 48, 47, 46, 45, 16] [25, 24, 23, 22, 21, 20, 19, 18, 17]
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#16
Here are the increments between two consecutive lines (in the 9x9 case)
Output:
1 2 3 4 5 6 7 8 9 31 31 31 31 31 31 31 31 1 -1 23 23 23 23 23 23 1 1 -1 -1 15 15 15 15 1 1 1 -1 -1 -1 7 7 1 1 1 1 -1 -1 -1 -1 -3 1 1 1 1 -1 -1 -1 -11 -11 -11 1 1 1 -1 -1 -19 -19 -19 -19 -19 1 1 -1 -27 -27 -27 -27 -27 -27 -27 1
This could be the starting point of a non recursive algorithm.
Reply
#17
(Sep-25-2020, 11:35 AM)omnisierra Wrote: Could anyone help me to solve the question without advanced methods which wouldn't be very complicated for me?

There is interesting discussion Smile but no feedback from OP whether it's helpful or not.

I believe that in simplest for OP is to read/analyse carefully what deanhystad proposed and act accordingly.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#18
Previous posts have shown that whatever algorithm, you need a kind of "GPS" to navigate
through the different turns in the matrix. Fortunately these "moves" are predictable.
In order to help our TS even further, this is my GPS:
These are not coordinates, but index increments (decrements) that are associated with the move.
dictMoves = {'LEFT':(0,-1),'DOWN':(1,0),'RIGHT':(0,1),'UP':(-1,0)} 
And these are the "rails" you need to move along. (The vector is initiated dynamically, based on the seed number.
[2, 2, 2, 2, 4, 4, 4, 4, 6, 6, 6, 6]
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#19
There are several ways to solve this problem and there is possibility to apply 'moves' to indices directly without need to moves dictionary.

My thinking was something like - how to fill outer line with consecutive numbers. I solved it with four for-loops using size of n. Then I added for-loop for 'layers/cycles' (how many times to repeat this operation) on top of that and index adjustment (inner layer boundaries are smaller). This solution does not need any manual adjustment - just value of n and it works on square matrices of all sizes.

To emit consecutive values I used iterator and calling next() on it. This may qualify as 'non-beginner'. It can be easily refactored though.

For this particular solution I can provide a hint - "if we go right then going left is reverse and if we are going down then going up is reverse, but keep in mind how range works"
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with a Matrix nxn with black and white input Rosko 3 1,966 Nov-24-2019, 07:39 AM
Last Post: buran
  Creating a wavelet matrix pingaan 4 2,505 May-26-2019, 06:25 PM
Last Post: heiner55
  define a diagonal matrix from a matrix amalalaoui 1 2,345 May-15-2019, 01:12 PM
Last Post: ichabod801
  Code for a circular wire?? hiphopopot0mous 11 6,568 Dec-03-2017, 06:23 PM
Last Post: micseydel
  Creating lists or arrays as the input nickalvar 4 4,202 May-03-2017, 04:46 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020