From 9243782e5c4fe456e60eff6b8565f66af495c4dc Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 8 Sep 2024 18:11:41 +0100 Subject: Add Python solution to challenge 053 --- challenge-053/paulo-custodio/python/ch-1.py | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 challenge-053/paulo-custodio/python/ch-1.py (limited to 'challenge-053/paulo-custodio/python/ch-1.py') diff --git a/challenge-053/paulo-custodio/python/ch-1.py b/challenge-053/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..c323b6f3a8 --- /dev/null +++ b/challenge-053/paulo-custodio/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# Challenge 053 +# +# TASK #1 +# Rotate Matrix +# Write a script to rotate the followin matrix by given 90/180/270 degrees +# clockwise. +# +# [ 1, 2, 3 ] +# [ 4, 5, 6 ] +# [ 7, 8, 9 ] +# For example, if you rotate by 90 degrees then expected result should be like +# below +# +# [ 7, 4, 1 ] +# [ 8, 5, 2 ] +# [ 9, 6, 3 ] + +m = [[ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ]]; + +def rotate90(m): + return [[ m[2][0], m[1][0], m[0][0] ], + [ m[2][1], m[1][1], m[0][1] ], + [ m[2][2], m[1][2], m[0][2] ]] + +def rotate180(m): + return rotate90(rotate90(m)) + +def rotate270(m): + return rotate90(rotate90(rotate90(m))) + +def display(m): + for row in m: + print("[ "+", ".join([str(x) for x in row])+" ]") + print("") + +display(rotate90(m)) +display(rotate180(m)) +display(rotate270(m)) -- cgit