回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

Really Health 電面 game of life

全局:

2019(1-3月) 码农类General 硕士 全职@Really Health - 内推 - 技术电面  | | Other | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
剛剛面完這家店面,韓國小哥蠻有耐心的。
在網路上找不到這家公司的題目、也不確定是否有題庫。
就在此分享題目給大家做參考

使用的平台是 codinghire ,  界面挺難用的。幾乎等於純文字編輯(code highlight 至少for python是完全沒有)

# 被問到的題目是 Ga
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
是 O(1)
如果我理解有錯的話歡迎指正,謝謝

(需要大米,給點大米是對樓主發文的一點鼓勵,也不會消耗您分數!)





  1. # /*
  2. # Conway’s Game of Life

  3. # Rules
  4. # 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:

  5. # 1) Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  6. # 2) Any live cell with two or three live neighbors lives on to the next generation.
  7. # 3) Any live cell with more than three live neighbors dies, as if by overcrowding.
  8. # 4) Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.


  9. # Example:
  10. # -------
  11. # ______________
  12. # |X = Dead Cell |
  13. # |O = Alive Cell|
  14. # --------------

  15. # 1 2 3 1 2 3 1 2 3
  16. # _____ _____ _____
  17. # A |O O O| A |O O O| A |X O X|
  18. # B |X O X| => B |X X X| => B |X X X| => ...
  19. # C |O O O| C |O O O| C |X O X|
  20. # ----- ----- -----

  21. # 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.
  22. # */

  23. DEAD = 0
  24. LIVE = 1
  25. DEAD_TO_DEAD = 2
  26. DEAD_TO_LIVE = 3
  27. LIVE_TO_DEAD = 4
  28. LIVE_TO_LIVE = 5

  29. def update_current_cell_to_transient(i, j, board):
  30. live_cnt = 0
  31. for next_i in {i-1,i,i+1}:
  32. for next_j in {j-1,j,j+1}:
  33. # skip current cell:
  34. if next_i==i and next_j==j:
  35. continue
  36. # check the position is on the board.
  37. 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}:
  38. live_cnt+=1
  39.    
  40. # update to transient state
  41. if board[i][j] == LIVE:
  42. if live_cnt < 2:
  43. board[i][j] = LIVE_TO_DEAD
  44. elif live_cnt in {2,3}:
  45. board[i][j] = LIVE_TO_LIVE
  46. else:
  47. board[i][j] = LIVE_TO_DEAD
  48. elif board[i][j] == DEAD:
  49. if live_cnt ==3 :
  50. board[i][j] = DEAD_TO_LIVE
  51. else:
  52. board[i][j] = DEAD_TO_DEAD
  53.             
  54. def update_transient_to_final_status(i, j, board):
  55. if board[i][j] in {DEAD_TO_LIVE, LIVE_TO_LIVE}:
  56. board[i][j] = LIVE
  57. else:
  58. board[i][j] = DEAD
  59.             

  60. # board: 2D array
  61. # 0: DEAD
  62. # 1: LIVE
  63. def game_of_live(board):
  64. # // validate input
  65. if not board:
  66. return []
  67.         
  68. # check each cell on this baord and update the current state to transient state ( because i don't want to loss info)
  69.    
  70. for i in range(len(board)):
  71. for j in range(len(board)):
  72. update_current_cell_to_transient(i,j, board)
  73.             
  74. # update transient status to final status
  75. for i in range(len(board)):
  76. for j in range(len(board)):
  77. update_transient_to_final_status(i,j, board)
  78.             
  79.    
  80. # update the transient state to next state.
  81. # print(board)
  82. return board
  83.    

  84. # 1 2 3 1 2 3 1 2 3
  85. # _____ _____ _____
  86. # A |O O O| A |O O O| A |X O X|
  87. # B |X O X| => B |X X X| => B |X X X| => ...
  88. # C |O O O| C |O O O| C |X O X|
  89. # ----- ----- -----

  90. import unittest
  91. class MyTestCase(unittest.TestCase):
  92. def test_something(self):
  93. input = [
  94. [1,1,1],
  95. [0,1,0],
  96. [1,1,1]
  97. ]
  98. expect = [
  99. [1,1,1],
  100. [0,0,0],
  101. [1,1,1]
  102. ]
  103. self.assertEqual(expect, game_of_live(input))

  104. input = [
  105. [1,1,1],
  106. [0,1,0],
  107. [1,1,1]
  108. ]

  109. game_of_live(game_of_live(game_of_live(input)))

  110. # round 1
  111. [[1, 1, 1],
  112. [0, 0, 0],
  113. [1, 1, 1]
  114. ]

  115. # round 2
  116. [[0, 1, 0],
  117. [0, 0, 0],
  118. [0, 1, 0]
  119. ]
  120. # round 3
  121. [[0, 0, 0],
  122. [0, 0, 0],
  123. [0, 0, 0]
  124. ]
  125.    
复制代码



上一篇:Amzon VO 3/28
下一篇:亚麻凉经
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表