diff options
| author | 冯昶 <seaker@qq.com> | 2021-03-15 18:13:51 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-03-15 18:13:51 +0800 |
| commit | 8b6be37fe4dac8b4c6489a95e55514b76b298d15 (patch) | |
| tree | ae36c8ec2c71f606c0e36adaa19dba366a68a0b4 /challenge-101/abigail/python | |
| parent | 865acfd056fb6f409ec6b1a81d60b931cbcb69fe (diff) | |
| parent | c9aec2da6bcb04b488183f09ca94bee488557aff (diff) | |
| download | perlweeklychallenge-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')
| -rw-r--r-- | challenge-101/abigail/python/ch-1.py | 110 | ||||
| -rw-r--r-- | challenge-101/abigail/python/ch-2.py | 48 |
2 files changed, 158 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") diff --git a/challenge-101/abigail/python/ch-2.py b/challenge-101/abigail/python/ch-2.py new file mode 100644 index 0000000000..1ed4dc151f --- /dev/null +++ b/challenge-101/abigail/python/ch-2.py @@ -0,0 +1,48 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as python ch-2.py < input-file +# + +import fileinput + +# +# This determines on which side of the line through (x1, y1) and +# (x2, y2) the origin lies. If > 0, then the origin lies to the left +# of the line, if < 0, the origin lies to the right of the line, if +# = 0, the origin lies on the line. +# +def side (x1, y1, x2, y2): + return (y2 - y1) * x2 - (x2 - x1) * y2 + + +for line in fileinput . input (): + # + # Parse input. We need an explicite conversion from strings to floats + # + x1, y1, x2, y2, x3, y3 = map (lambda f: float (f), line . split ()) + + # + # Determine where the origin is relative to the three lines + # through the vertices of the triangle. Note we have to go + # in a specific order through the points. (Either clock wise, + # or counter clockwise, as long as we're consistent). + # + s1 = side (x2, y2, x3, y3) + s2 = side (x3, y3, x1, y1) + s3 = side (x1, y1, x2, y2) + + # + # If the origin either lies to the left (or on) each of the + # lines, or to the right (or on) each of the lines, the origin + # lies inside the triangle. If not, it does not. + # + if s1 <= 0 and s2 <= 0 and s3 <= 0 or \ + s1 >= 0 and s2 >= 0 and s3 >= 0: + print (1) + else: + print (0) |
