Reupload
This commit is contained in:
BIN
cli/__pycache__/algo.cpython-36.pyc
Normal file
BIN
cli/__pycache__/algo.cpython-36.pyc
Normal file
Binary file not shown.
415
cli/algo-test.py
Normal file
415
cli/algo-test.py
Normal file
@@ -0,0 +1,415 @@
|
||||
import unittest
|
||||
import algo
|
||||
import random
|
||||
|
||||
|
||||
class TestAlgo(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.seat = [[1, 2, 3, 4, 5, 6, 7],
|
||||
[8, 9, 10, 11, 12, 13, 14],
|
||||
[15, 16, 17, 18, 19, 20, 21],
|
||||
[22, 23, 24, 25, 26, 27, 28],
|
||||
[29, 30, 31, 32, 33, 34, 35],
|
||||
[0, 0, 36, 37, 38, 0, 0]]
|
||||
self.ran = random.Random()
|
||||
|
||||
# to test if it can generate correct form of seat
|
||||
def testGen(self):
|
||||
seat = algo.generateSeats()
|
||||
|
||||
def check(arr):
|
||||
num_list = []
|
||||
if len(arr) != 6 or len(arr[0]) != 7:
|
||||
return False
|
||||
if arr[5][0] != 0 or arr[5][1] != 0 or arr[5][5] != 0 or arr[5][5] != 0 or arr[5][6] != 0:
|
||||
return False
|
||||
for i in range(len(arr)):
|
||||
for j in range(len(arr[0])):
|
||||
if arr[i][j] != 0:
|
||||
num_list.append(arr[i][j])
|
||||
num_list = set(num_list)
|
||||
return len(num_list) == 38
|
||||
self.assertTrue(check(seat))
|
||||
|
||||
# to test shift left on row 1
|
||||
def testShiftLeftRow1(self):
|
||||
col = self.ran.randint(2, 7)
|
||||
new = algo.shiftLeft(1, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(1, 6):
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[0] == [2, 3, 4, 5, 6, 7, 1]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift left on row 2
|
||||
def testShiftLeftRow2(self):
|
||||
col = self.ran.randint(2, 7)
|
||||
new = algo.shiftLeft(2, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
if i != 1:
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[1] == [9, 10, 11, 12, 13, 14, 8]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift left on row 3
|
||||
def testShiftLeftRow3(self):
|
||||
col = self.ran.randint(2, 7)
|
||||
new = algo.shiftLeft(3, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
if i != 2:
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[2] == [16, 17, 18, 19, 20, 21, 15]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift left on row 4
|
||||
def testShiftLeftRow4(self):
|
||||
col = self.ran.randint(2, 7)
|
||||
new = algo.shiftLeft(4, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
if i != 3:
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[3] == [23, 24, 25, 26, 27, 28, 22]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift left on row 5
|
||||
def testShiftLeftRow5(self):
|
||||
col = self.ran.randint(2, 7)
|
||||
new = algo.shiftLeft(5, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
if i != 4:
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[4] == [30, 31, 32, 33, 34, 35, 29]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift left on row 6
|
||||
def testShiftLeftRow6(self):
|
||||
col = self.ran.randint(4, 5)
|
||||
new = algo.shiftLeft(6, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(5):
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[5] == [0, 0, 37, 38, 36, 0, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift right on row 1
|
||||
def testShiftRightRow1(self):
|
||||
col = self.ran.randint(1, 6)
|
||||
new = algo.shiftRight(1, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(1, 6):
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[0] == [7, 1, 2, 3, 4, 5, 6]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift right on row 2
|
||||
def testShiftRightRow2(self):
|
||||
col = self.ran.randint(1, 6)
|
||||
new = algo.shiftRight(2, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
if i != 1:
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[1] == [14, 8, 9, 10, 11, 12, 13]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift right on row 3
|
||||
def testShiftRightRow3(self):
|
||||
col = self.ran.randint(1, 6)
|
||||
new = algo.shiftRight(3, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
if i != 2:
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[2] == [21, 15, 16, 17, 18, 19, 20]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift right on row 4
|
||||
def testShiftRightRow4(self):
|
||||
col = self.ran.randint(1, 6)
|
||||
new = algo.shiftRight(4, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
if i != 3:
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[3] == [28, 22, 23, 24, 25, 26, 27]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift right on row 5
|
||||
def testShiftRightRow5(self):
|
||||
col = self.ran.randint(1, 6)
|
||||
new = algo.shiftRight(5, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
if i != 4:
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[4] == [35, 29, 30, 31, 32, 33, 34]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test shift right on row 6
|
||||
def testShiftRightRow6(self):
|
||||
col = self.ran.randint(3, 4)
|
||||
new = algo.shiftRight(6, col, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(5):
|
||||
if new[i] != old[i]:
|
||||
return False
|
||||
return new[5] == [0, 0, 38, 36, 37, 0, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test left bound
|
||||
def testLeftBound(self):
|
||||
new = algo.shiftLeft(1, 1, 1, self.seat.copy())
|
||||
self.assertEqual(self.seat, new)
|
||||
|
||||
# to test right bound
|
||||
def testRightBound(self):
|
||||
new = algo.shiftRight(1, 7, 1, self.seat.copy())
|
||||
self.assertEqual(self.seat, new)
|
||||
|
||||
# to test move up on col 1
|
||||
def testUpCol1(self):
|
||||
row = self.ran.randint(2, 5)
|
||||
new = algo.shiftUp(row, 1, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(1, 7):
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][0] for i in range(6)] == [8, 15, 22, 29, 1, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move up on col 2
|
||||
def testUpCol2(self):
|
||||
row = self.ran.randint(2, 5)
|
||||
new = algo.shiftUp(row, 2, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 1:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][1] for i in range(6)] == [9, 16, 23, 30, 2, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move up on col 3
|
||||
def testUpCol3(self):
|
||||
row = self.ran.randint(2, 6)
|
||||
new = algo.shiftUp(row, 3, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 2:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][2] for i in range(6)] == [10, 17, 24, 31, 36, 3]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move up on col 4
|
||||
def testUpCol4(self):
|
||||
row = self.ran.randint(2, 6)
|
||||
new = algo.shiftUp(row, 4, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 3:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][3] for i in range(6)] == [11, 18, 25, 32, 37, 4]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move up on col 5
|
||||
def testUpCol5(self):
|
||||
row = self.ran.randint(2, 6)
|
||||
new = algo.shiftUp(row, 5, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 4:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][4] for i in range(6)] == [12, 19, 26, 33, 38, 5]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move up on col 6
|
||||
def testUpCol6(self):
|
||||
row = self.ran.randint(2, 5)
|
||||
new = algo.shiftUp(row, 6, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 5:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][5] for i in range(6)] == [13, 20, 27, 34, 6, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move up on col 7
|
||||
def testUpCol7(self):
|
||||
row = self.ran.randint(2, 5)
|
||||
new = algo.shiftUp(row, 7, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 6:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][6] for i in range(6)] == [14, 21, 28, 35, 7, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move down on col 1
|
||||
def testDownCol1(self):
|
||||
row = self.ran.randint(1, 4)
|
||||
new = algo.shiftDown(row, 1, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(1, 7):
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][0] for i in range(6)] == [29, 1, 8, 15, 22, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move down on col 2
|
||||
def testDownCol2(self):
|
||||
row = self.ran.randint(1, 4)
|
||||
new = algo.shiftDown(row, 2, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 1:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][1] for i in range(6)] == [30, 2, 9, 16, 23, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move down on col 3
|
||||
def testDownCol3(self):
|
||||
row = self.ran.randint(1, 5)
|
||||
new = algo.shiftDown(row, 3, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 2:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][2] for i in range(6)] == [36, 3, 10, 17, 24, 31]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move down on col 4
|
||||
def testDownCol4(self):
|
||||
row = self.ran.randint(1, 5)
|
||||
new = algo.shiftDown(row, 4, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 3:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][3] for i in range(6)] == [37, 4, 11, 18, 25, 32]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move down on col 5
|
||||
def testDownCol5(self):
|
||||
row = self.ran.randint(1, 5)
|
||||
new = algo.shiftDown(row, 5, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 4:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][4] for i in range(6)] == [38, 5, 12, 19, 26, 33]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move down on col 6
|
||||
def testDownCol6(self):
|
||||
row = self.ran.randint(1, 4)
|
||||
new = algo.shiftDown(row, 6, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 5:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][5] for i in range(6)] == [34, 6, 13, 20, 27, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test move down on col 7
|
||||
def testDownCol7(self):
|
||||
row = self.ran.randint(1, 4)
|
||||
new = algo.shiftDown(row, 7, 1, self.seat.copy())
|
||||
|
||||
def check(old, new):
|
||||
for i in range(6):
|
||||
for j in range(7):
|
||||
if j != 5:
|
||||
if old[i][j] != new[i][j]:
|
||||
return False
|
||||
return [new[i][6] for i in range(6)] == [35, 7, 14, 21, 28, 0]
|
||||
self.assertTrue(check(self.seat, new))
|
||||
|
||||
# to test upper bound
|
||||
def testUpperBound(self):
|
||||
new = algo.shiftUp(1, 1, 1, self.seat.copy())
|
||||
self.assertEqual(self.seat, new)
|
||||
|
||||
# to test left side lower bound
|
||||
def testLeftLowerBound(self):
|
||||
new = algo.shiftDown(5, 1, 1, self.seat.copy())
|
||||
self.assertEqual(self.seat, new)
|
||||
|
||||
# to test right side lower bound
|
||||
def testRightLowerBound(self):
|
||||
new = algo.shiftDown(5, 7, 1, self.seat.copy())
|
||||
self.assertEqual(self.seat, new)
|
||||
|
||||
# to test center lower bound
|
||||
def testCenterLowerBound(self):
|
||||
new = algo.shiftDown(6, 3, 1, self.seat.copy())
|
||||
self.assertEqual(self.seat, new)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
199
cli/algo.py
Normal file
199
cli/algo.py
Normal file
@@ -0,0 +1,199 @@
|
||||
from random import randrange
|
||||
|
||||
'''
|
||||
Original from secminhr
|
||||
modified by t510599
|
||||
'''
|
||||
|
||||
# init seats
|
||||
def generateSeats():
|
||||
seats = []
|
||||
numbers = [i for i in range(1, 39)]
|
||||
for i in range(6):
|
||||
seats.append([])
|
||||
for j in range(7):
|
||||
seats[len(seats)-1].append(0)
|
||||
if i == 5:
|
||||
if 0<=j<=1 or 5<=j<= 6:
|
||||
continue
|
||||
index = randrange(0, 38)
|
||||
while(numbers[index] == 0):
|
||||
index = randrange(0, 38)
|
||||
seats[i][j] = numbers[index]
|
||||
numbers[index] = 0
|
||||
return seats
|
||||
|
||||
# print seats
|
||||
def printArr(seats):
|
||||
for i in range(len(seats)):
|
||||
for j in range(len(seats[0])):
|
||||
num = seats[i][j]
|
||||
if 0 <= num <= 9:
|
||||
num = '0' + str(num)
|
||||
print(num, end=" ")
|
||||
print()
|
||||
print() # newline
|
||||
|
||||
def _arrShiftLeft(index, arr, num):
|
||||
element = arr[index]
|
||||
zero_index = []
|
||||
while 0 in arr:
|
||||
index = arr.index(0)
|
||||
zero_index += [index]
|
||||
del arr[index]
|
||||
zero_index = zero_index[::-1]
|
||||
index = arr.index(element)
|
||||
if num > index:
|
||||
num = index
|
||||
front_list = arr[:num]
|
||||
arr = arr[num:len(arr)] + front_list
|
||||
|
||||
for i in zero_index:
|
||||
arr.insert(i, 0)
|
||||
return arr
|
||||
|
||||
def _arrShiftRight(index, arr, num):
|
||||
element = arr[index]
|
||||
zero_index = []
|
||||
while 0 in arr:
|
||||
index = arr.index(0)
|
||||
zero_index += [index]
|
||||
del arr[index]
|
||||
zero_index = zero_index[::-1]
|
||||
index = arr.index(element)
|
||||
if num > len(arr) - index - 1:
|
||||
num = len(arr) - index - 1
|
||||
later_list = arr[len(arr) - num:len(arr)]
|
||||
arr = later_list + arr[:len(arr)-num]
|
||||
for i in zero_index:
|
||||
arr.insert(i, 0)
|
||||
return arr
|
||||
|
||||
|
||||
######### Actions ############
|
||||
|
||||
def shiftRight(row, col, num, seats):
|
||||
row_list = seats[row - 1]
|
||||
row_list = _arrShiftRight(col - 1, row_list, num)
|
||||
seats[row - 1] = row_list
|
||||
return seats
|
||||
|
||||
def shiftLeft(row, col, num, seats):
|
||||
row_list = seats[row - 1]
|
||||
row_list = _arrShiftLeft(col - 1, row_list, num)
|
||||
seats[row - 1] = row_list
|
||||
return seats
|
||||
|
||||
def shiftUp(row, col, num, seats):
|
||||
col_list = [seats[i][col - 1] for i in range(len(seats))]
|
||||
col_list = _arrShiftLeft(row - 1 ,col_list, num)
|
||||
for i in range(len(seats)):
|
||||
seats[i][col - 1] = col_list[i]
|
||||
return seats
|
||||
|
||||
def shiftDown(row, col, num, seats):
|
||||
col_list = [seats[i][col - 1] for i in range(len(seats))]
|
||||
col_list = _arrShiftRight(row - 1 ,col_list, num)
|
||||
for i in range(len(seats)):
|
||||
seats[i][col - 1] = col_list[i]
|
||||
return seats
|
||||
|
||||
# get list of element from left bottom to right top
|
||||
def _getUpperCross(row, col, arr):
|
||||
target_list = []
|
||||
current_row = row
|
||||
current_col = col
|
||||
highest_pos = (0, 0)
|
||||
#get all right up corner
|
||||
while current_row >= 1 and current_col <= len(arr[0]):
|
||||
target_list.insert(0, arr[current_row-1][current_col-1])
|
||||
current_row -= 1
|
||||
current_col += 1
|
||||
highest_pos = (current_row + 1, current_col - 1)
|
||||
current_row = row + 1
|
||||
current_col = col - 1
|
||||
#get all left bottom corner
|
||||
while current_row <= len(arr) and current_col >= 1:
|
||||
target_list.append(arr[current_row-1][current_col-1])
|
||||
current_row += 1
|
||||
current_col -= 1
|
||||
return (target_list, highest_pos)
|
||||
|
||||
# get list of element from left top to right bottom
|
||||
def _getLowerCross(row, col, arr):
|
||||
target_list = []
|
||||
current_row = row
|
||||
current_col = col
|
||||
highest_pos = (0, 0)
|
||||
#get all left up corner
|
||||
while current_row >= 1 and current_col >= 1:
|
||||
target_list.insert(0, arr[current_row-1][current_col-1])
|
||||
current_row -= 1
|
||||
current_col -= 1
|
||||
highest_pos = (current_row + 1, current_col + 1)
|
||||
current_row = row + 1
|
||||
current_col = col + 1
|
||||
#get all right down corner
|
||||
while current_row <= len(arr) and current_col <= len(arr[0]):
|
||||
target_list.append(arr[current_row-1][current_col-1])
|
||||
current_row += 1
|
||||
current_col += 1
|
||||
return (target_list, highest_pos)
|
||||
|
||||
def shiftRightUp(row, col, num, seats):
|
||||
# the list is follows the predicate: n.row > (n+1).row
|
||||
corner_list, highest_pos = _getUpperCross(row, col, seats)
|
||||
target = seats[row-1][col-1]
|
||||
corner_list = _arrShiftLeft(corner_list.index(target) ,corner_list, num)
|
||||
#fill in
|
||||
current_row = highest_pos[0]
|
||||
current_col = highest_pos[1]
|
||||
while corner_list:
|
||||
seats[current_row-1][current_col-1] = corner_list[0]
|
||||
del corner_list[0]
|
||||
current_row += 1
|
||||
current_col -= 1
|
||||
return seats
|
||||
|
||||
def shiftRightDown(row, col, num, seats):
|
||||
# the list is follows the predicate: n.row > (n+1).row
|
||||
corner_list, highest_pos = _getLowerCross(row, col, seats)
|
||||
target = seats[row-1][col-1]
|
||||
corner_list = _arrShiftRight(corner_list.index(target) ,corner_list, num)
|
||||
#fill in
|
||||
current_row = highest_pos[0]
|
||||
current_col = highest_pos[1]
|
||||
while corner_list:
|
||||
seats[current_row-1][current_col-1] = corner_list[0]
|
||||
del corner_list[0]
|
||||
current_row += 1
|
||||
current_col += 1
|
||||
return seats
|
||||
|
||||
def shiftLeftUp(row, col, num, seats):
|
||||
corner_list, highest_pos = _getLowerCross(row, col, seats)
|
||||
target = seats[row-1][col-1]
|
||||
corner_list = _arrShiftLeft(corner_list.index(target) ,corner_list, num)
|
||||
#fill in
|
||||
current_row = highest_pos[0]
|
||||
current_col = highest_pos[1]
|
||||
while corner_list:
|
||||
seats[current_row-1][current_col-1] = corner_list[0]
|
||||
del corner_list[0]
|
||||
current_row += 1
|
||||
current_col += 1
|
||||
return seats
|
||||
|
||||
def shiftLeftDown(row, col, num, seats):
|
||||
corner_list, highest_pos = _getUpperCross(row, col, seats)
|
||||
target = seats[row-1][col-1]
|
||||
corner_list = _arrShiftRight(corner_list.index(target) ,corner_list, num)
|
||||
#fill in
|
||||
current_row = highest_pos[0]
|
||||
current_col = highest_pos[1]
|
||||
while corner_list:
|
||||
seats[current_row-1][current_col-1] = corner_list[0]
|
||||
del corner_list[0]
|
||||
current_row += 1
|
||||
current_col -= 1
|
||||
return seats
|
||||
83
cli/seat.py
Normal file
83
cli/seat.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from algo import generateSeats, printArr, shiftUp, shiftDown, shiftLeft, shiftRight, shiftLeftDown, shiftLeftUp, shiftRightDown, shiftRightUp
|
||||
from random import choice
|
||||
import sys
|
||||
import json
|
||||
|
||||
def load():
|
||||
f = open('seats.txt','r',encoding="utf-8")
|
||||
seats = json.load(f)
|
||||
f.close()
|
||||
return seats
|
||||
|
||||
def parse(seats,requirements):
|
||||
funcs = {"f": shiftUp,"b": shiftDown, "l": shiftLeft, "r": shiftRight, "fr": shiftRightUp, "br": shiftRightDown, "fl": shiftLeftUp, "bl": shiftLeftDown} # f: forward, b: backward, l:left, r: right
|
||||
name = {"f": "向前","b": "向後", "l": "向左", "r": "向右", "fr": "向右前", "br": "向右後", "fl": "向左前", "bl": "向左後"}
|
||||
order = sorted([int(n) for n in requirements.keys()])
|
||||
luckier = choice(order)
|
||||
print("幸運兒: " + str(luckier) + "\n")
|
||||
order = order[order.index(luckier):] + order[:order.index(luckier)]
|
||||
|
||||
for req in order:
|
||||
no,direction,steps = requirements[str(req)]
|
||||
no = no
|
||||
steps = steps
|
||||
row,col = find(seats,no)
|
||||
if col != None and row != None:
|
||||
if direction in funcs.keys():
|
||||
funcs[direction](row,col,steps,seats)
|
||||
print(no, name[direction], steps)
|
||||
printArr(seats)
|
||||
print()
|
||||
return seats
|
||||
|
||||
def find(seats,no):
|
||||
for y in range(len(seats)):
|
||||
if no in seats[y]:
|
||||
col = seats[y].index(no)
|
||||
row = y
|
||||
return (row+1,col+1) # start from 0 -> start from 1
|
||||
return (None,None)
|
||||
|
||||
help_msg = '''Please provide mode!
|
||||
generate - generate a random seat
|
||||
run - apply requirements
|
||||
print - print the current seat
|
||||
debug - dry run'''
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) <= 1 or sys.argv[1] == "help":
|
||||
print(help_msg)
|
||||
sys.exit(1 if len(sys.argv) <= 1 else 0)
|
||||
else:
|
||||
mode = sys.argv[1]
|
||||
if mode == "generate":
|
||||
seats = generateSeats()
|
||||
printArr(seats)
|
||||
n = open('seats.txt','w',encoding="utf-8")
|
||||
n.write(str(seats))
|
||||
n.close()
|
||||
print("Done!")
|
||||
elif mode == "run" or mode == "debug":
|
||||
seats = load()
|
||||
print("Original:")
|
||||
printArr(seats)
|
||||
#load requirements
|
||||
requirements = dict()
|
||||
r = open('requirements.txt','r',encoding="utf-8")
|
||||
for line in r.readlines():
|
||||
no,direction,steps = line.strip().split(" ")
|
||||
requirements[no] = (int(no),direction,int(steps))
|
||||
parse(seats,requirements)
|
||||
print("Result:")
|
||||
printArr(seats)
|
||||
if mode == "run":
|
||||
#save
|
||||
n = open('seats.txt','w',encoding="utf-8")
|
||||
n.write(str(seats))
|
||||
n.close()
|
||||
print("Done!")
|
||||
elif mode == "show":
|
||||
printArr(load())
|
||||
else:
|
||||
print(help_msg)
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user