Another quick code over breakfast inspired by 'spying' the other forums.
Rotate sequences left or right
Gribouillis commented: nice +13
""" Rotate sequences right or left
>>> rot_right(arr)
[(7, 4, 1), (8, 5, 2), (9, 6, 3)]
>>> p(rot_right(arr))
(7, 4, 1)
(8, 5, 2)
(9, 6, 3)
----------------------------------------
>>> p(rot_right(rot_right(arr))
)
(9, 8, 7)
(6, 5, 4)
(3, 2, 1)
----------------------------------------
>>> p(rot_right(rot_right(rot_right(arr))))
(3, 6, 9)
(2, 5, 8)
(1, 4, 7)
----------------------------------------
>>> p(rot_right(rot_right(rot_right(rot_right(arr)))))
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
----------------------------------------
>>> p(rot_right(rot_left(arr)))
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
----------------------------------------
>>> a =['***',
' * ',
' ']
>>> p(a)
***
*
----------------------------------------
>> p(map(''.join, rot_right(a)))
*
**
*
>>> p(map(''.join, rot_left(a)))
*
**
*
----------------------------------------
>>> p(map(''.join, rot_left(rot_left(a))))
*
***
----------------------------------------
"""
def p(a, sep=40*'-'):
""" print sequences in separate lines and line separation sep """
for line in a: print(line)
print(sep)
def rot_left(a):
return list(zip(*a)[::-1])
def rot_right(a):
return list(zip(*a[::-1]))
if __name__ == '__main__':
arr = [[1,2,3],[4, 5, 6], [7, 8, 9]]
p(arr)
p(rot_right(rot_right(rot_right(arr))))
p(rot_right(rot_right(rot_right(arr))))
TrustyTony 888 pyMod Team Colleague Featured Poster
TrustyTony 888 pyMod Team Colleague Featured Poster
Gribouillis 1,391 Programming Explorer Team Colleague
TrustyTony 888 pyMod Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.