aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/abigail/python/ch-1.py
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-03-15 18:13:51 +0800
committer冯昶 <seaker@qq.com>2021-03-15 18:13:51 +0800
commit8b6be37fe4dac8b4c6489a95e55514b76b298d15 (patch)
treeae36c8ec2c71f606c0e36adaa19dba366a68a0b4 /challenge-101/abigail/python/ch-1.py
parent865acfd056fb6f409ec6b1a81d60b931cbcb69fe (diff)
parentc9aec2da6bcb04b488183f09ca94bee488557aff (diff)
downloadperlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.gz
perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.bz2
perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.zip
Merge branch 'master' of github.com:seaker/perlweeklychallenge-club
Diffstat (limited to 'challenge-101/abigail/python/ch-1.py')
-rw-r--r--challenge-101/abigail/python/ch-1.py110
1 files changed, 110 insertions, 0 deletions
diff --git a/challenge-101/abigail/python/ch-1.py b/challenge-101/abigail/python/ch-1.py
new file mode 100644
index 0000000000..22553ccb0b
--- /dev/null
+++ b/challenge-101/abigail/python/ch-1.py
@@ -0,0 +1,110 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as python ch-1.py < input-file
+#
+
+from math import floor, sqrt
+import sys
+
+RIGHT = 0
+UP = 1
+LEFT = 2
+DOWN = 3
+
+#
+# Fetch data
+#
+elements = input () . split ()
+
+#
+# Find the optimal width and height
+#
+count = len (elements)
+height = floor (sqrt (count))
+while count % height:
+ height = height - 1
+
+width = floor (count / height)
+
+
+#
+# Initialize the matrix
+#
+min_x = 0
+max_x = height - 1
+min_y = 0
+max_y = width - 1
+
+matrix = [[0] * width for x in range (height)]
+
+
+#
+# Fill the matrix, starting at the bottom left
+#
+x = max_x
+y = min_y
+direction = RIGHT
+for element in elements:
+ matrix [x] [y] = element
+ turn = False
+ if direction == RIGHT:
+ if y >= max_y:
+ turn = True; x = x - 1; max_x = max_x - 1
+ else:
+ y = y + 1
+
+ if direction == UP:
+ if x <= min_x:
+ turn = True; y = y - 1; max_y = max_y - 1
+ else:
+ x = x - 1
+
+ if direction == LEFT:
+ if y <= min_y:
+ turn = True; x = x + 1; min_x = min_x + 1
+ else:
+ y = y - 1
+
+ if direction == DOWN:
+ if x >= max_x:
+ turn = True; y = y + 1; min_y = min_y + 1
+ else:
+ x = x + 1
+
+ if turn:
+ direction = direction + 1
+ direction = direction % 4
+
+
+#
+# Find the widths of each column
+#
+widths = [0] * width
+
+for y in range (width):
+ max = 0
+ for x in range (height):
+ if max < len (matrix [x] [y]):
+ max = len (matrix [x] [y])
+ widths [y] = max
+
+
+#
+# Print the matrix
+#
+for x in range (height):
+ for y in range (width):
+ if y > 0:
+ sys . stdout . write (" ")
+ #
+ # Format the entry. Format will look something like '{:>Ws}',
+ # where W is the width of the column.
+ #
+ sys . stdout . write (("{:>" + str (widths [y]) + "s}") .
+ format (matrix [x] [y]))
+ sys . stdout . write ("\n")