注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
剛剛面完這家店面,韓國小哥蠻有耐心的。
在網路上找不到這家公司的題目、也不確定是否有題庫。
就在此分享題目給大家做參考
使用的平台是 codinghire , 界面挺難用的。幾乎等於純文字編輯(code highlight 至少for python是完全沒有)
# 被問到的題目是 Ga是 O(1)
如果我理解有錯的話歡迎指正,謝謝
(需要大米,給點大米是對樓主發文的一點鼓勵,也不會消耗您分數!)
- # /*
- # Conway’s Game of Life
- # Rules
- # The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
- # 1) Any live cell with fewer than two live neighbors dies, as if caused by under-population.
- # 2) Any live cell with two or three live neighbors lives on to the next generation.
- # 3) Any live cell with more than three live neighbors dies, as if by overcrowding.
- # 4) Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
- # Example:
- # -------
- # ______________
- # |X = Dead Cell |
- # |O = Alive Cell|
- # --------------
- # 1 2 3 1 2 3 1 2 3
- # _____ _____ _____
- # A |O O O| A |O O O| A |X O X|
- # B |X O X| => B |X X X| => B |X X X| => ...
- # C |O O O| C |O O O| C |X O X|
- # ----- ----- -----
- # Exercise: In any language you like, please implement a version of this game. You can assume any data structure and method of calling your app that you want.
- # */
- DEAD = 0
- LIVE = 1
- DEAD_TO_DEAD = 2
- DEAD_TO_LIVE = 3
- LIVE_TO_DEAD = 4
- LIVE_TO_LIVE = 5
- def update_current_cell_to_transient(i, j, board):
- live_cnt = 0
- for next_i in {i-1,i,i+1}:
- for next_j in {j-1,j,j+1}:
- # skip current cell:
- if next_i==i and next_j==j:
- continue
- # check the position is on the board.
- if 0<= next_i < len(board) and 0<= next_j< len(board[0]) and board[next_i][next_j] in {LIVE, LIVE_TO_DEAD, LIVE_TO_LIVE}:
- live_cnt+=1
-
- # update to transient state
- if board[i][j] == LIVE:
- if live_cnt < 2:
- board[i][j] = LIVE_TO_DEAD
- elif live_cnt in {2,3}:
- board[i][j] = LIVE_TO_LIVE
- else:
- board[i][j] = LIVE_TO_DEAD
- elif board[i][j] == DEAD:
- if live_cnt ==3 :
- board[i][j] = DEAD_TO_LIVE
- else:
- board[i][j] = DEAD_TO_DEAD
-
- def update_transient_to_final_status(i, j, board):
- if board[i][j] in {DEAD_TO_LIVE, LIVE_TO_LIVE}:
- board[i][j] = LIVE
- else:
- board[i][j] = DEAD
-
- # board: 2D array
- # 0: DEAD
- # 1: LIVE
- def game_of_live(board):
- # // validate input
- if not board:
- return []
-
- # check each cell on this baord and update the current state to transient state ( because i don't want to loss info)
-
- for i in range(len(board)):
- for j in range(len(board)):
- update_current_cell_to_transient(i,j, board)
-
- # update transient status to final status
- for i in range(len(board)):
- for j in range(len(board)):
- update_transient_to_final_status(i,j, board)
-
-
- # update the transient state to next state.
- # print(board)
- return board
-
- # 1 2 3 1 2 3 1 2 3
- # _____ _____ _____
- # A |O O O| A |O O O| A |X O X|
- # B |X O X| => B |X X X| => B |X X X| => ...
- # C |O O O| C |O O O| C |X O X|
- # ----- ----- -----
- import unittest
- class MyTestCase(unittest.TestCase):
- def test_something(self):
- input = [
- [1,1,1],
- [0,1,0],
- [1,1,1]
- ]
- expect = [
- [1,1,1],
- [0,0,0],
- [1,1,1]
- ]
- self.assertEqual(expect, game_of_live(input))
- input = [
- [1,1,1],
- [0,1,0],
- [1,1,1]
- ]
- game_of_live(game_of_live(game_of_live(input)))
- # round 1
- [[1, 1, 1],
- [0, 0, 0],
- [1, 1, 1]
- ]
- # round 2
- [[0, 1, 0],
- [0, 0, 0],
- [0, 1, 0]
- ]
- # round 3
- [[0, 0, 0],
- [0, 0, 0],
- [0, 0, 0]
- ]
-
复制代码
|