15,117,549 members
0.00/5 (No votes)
See more:
So, I have a function, that gets a call like this:
reversed("abc", "def", "ghi")

And has to return the reversed order like this (it is reversed by 90 degres - by this I mean if you stack them like this:
abc
def
ghi

And then reverse them for 90 degress, you would get:
gda
heb
ifc

And then to upgrade via another function, that would get the same argument and a number of reverses. Like this:
reversed_n(("abc", "def", "ghi"), 1)

And would then return the tuple of what I would get by reversing the same order n times.
For this it would return:
("gda", "heb", "ifc")

What I have tried:

So I made the functions like this:

def reversed(kos):
seznam = []
kos = [i[::-1] for i in kos]
i = 0
while i < len(kos[0]):
s = [beseda[i] for beseda in kos]
seznam.append("".join(s))
i += 1
return tuple(seznam)

def reversed_n(kos, n):
for i in range(0, n):
kos = reversed(kos)
return kos

So it works, but if I try something a lot larger, say a lot larger and put n = 10000001, it will run forever. What would I need to do, can it maybe be done with calculating if it is odd or even number of n?
Posted
Updated 27-Dec-20 19:14pm

## Solution 2

First, I would see if it is indeed taking forever, or maybe something just a little short of forever. Try adding this to your for loop:
if i % 10000 == 0:
print(i)

If you never see i being printed, try cutting down % 10000 by a factor of 1/10 a couple times. If you still never see i being printed, I'd start to look for interpreter maximums or other possible causes like resource limits dictated by your system. For example, there is a resource limit that might affect your function on linux:
getrlimit(2) - Linux manual page[getrlimit]
Good luck.

## Solution 3

def reversed(arr):
kos = []
p = ""
arr_len = len(arr)
item_len = len(arr[0])
for x in range(0, item_len):
for i in range(0, arr_len):
item = arr[arr_len-1-i]
p+=item[x]
kos.append(p)
p=""
return kos
def reversed_n(kos, n):
for i in range(0, n%4):
kos = reversed(kos)
return kos

There can only be 4 outcomes or rotations per se because after 4 rotations it will become same as input, so we can take the modulus of 'n' to 4, and rotate the grid that many times and then return it.
Comments
sbarnes 28-Dec-20 16:43pm

Good observation and answer. But I got the impression https://www.codeproject.com/script/Membership/View.aspx?mid=14631234
was asking about limits and the like. What goes wrong when n gets too big? What is too big and why? But, yes, as a practical matter your shorter way is more performant.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 150 CHill60 120 OriginalGriff 120 phil.o 55 Richard MacCutchan 50
 Richard Deeming 105 OriginalGriff 75 phil.o 55 CHill60 50 Richard MacCutchan 40

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900