繁体   English   中英

编码练习 - 为每个玩家运动抓取垃圾 python

[英]Coding exercise - grabbing garbage for each player movement python

想象一个二维网格 (NxN),其中每个方格中都有垃圾,对于由 N、S、E、W(北、南、东和西)描述的每个移动,玩家相应地移动并抓取那里的垃圾在那个广场。

如果输入 = 'NESW' 则 output 应该 = 4 - 1 玩家开始的方格 - 1 向北,1 向东,1 向南,0 向西,因为他到达了他开始的初始方格,该方格已经“清理” "

对于输入 = 'NSNSNSNS' output = 2 出于同样的原因。

我无法计算垃圾并知道玩家是否已经去过那个地方。

def total_garbage(positions):
  garbage = 0
  N, S, E, W = 0, 0, 0, 0

  for direction in positions:

    if direction.upper() == 'N':
      N += 1
      garbage += 1
    elif direction.upper() == 'S':
      S += 1
      garbage += 1
    elif direction.upper() == 'E':
      E += 1
      garbage += 1
    elif direction.upper() == 'W':
      W += 1
      garbage += 1

  if N == S and E == W and E != 0 and W != 0:
    return garbage
  elif N == S and E == 0 and W == 0:
    return (garbage - min(N,S))

qp = total_garbage(['N','E','S','W'])
print(qp)

这是我到目前为止所拥有的,只是开始尝试一些东西,但还不是很成功:/

由于您的网格是二维的,因此 model 将其作为二维网格 - 所以选择一个起始位置(例如(0,0))并且每个方向都会改变该位置 - 所以从(0,0)开始:

  • N 变为 (0,-1)
  • S 变为 (0,1)
  • E 变为 (1,0)
  • W 变为 (-1,0)

等等

您将访问过的位置存储在 python 集合中 - 在您将 go 存储到新位置之前,您检查该位置是否已经在集合中。 也就是说,如果您只希望每个位置有一块垃圾 - 如果您最终希望有多个垃圾,您可以将网格 model 作为字典; 但作为一组——

def total_garbage(positions):

    # Create a dictionary translating direction to co-ordinate changes
    deltas = {'N':(0,-1),'S':{0,1),'E':(1,0),'W':(-1,0)}

    cleaned_locations = set()
    start_location = (0,0)
    garbage = 0

    for direction in positions:
        dx, dy = deltas[direction]
        x,y = start_location
        nx, xy = x + dx, y+dx

       # Count this site so long as it isn't already cleaned.
       if (nx, ny) not in cleaned:
          garbage += 1
          cleaned.add((nx,ny))
      else:
          return garbage

qp = total_garbage(['N','E','S','W'])
print(qp)

您可以按照 Ahmet 在评论中建议的方式创建一个数组,但如果您不想这样做,可以这样做:

x, y = 0, 0 # this will be our position 
visited_idx = set() # indexes where we have already gathered garbage
for direction in positions:
    if direction.upper() == 'N':
      y += 1
    elif direction.upper() == 'S':
      y -= 1
    elif direction.upper() == 'E':
      x += 1
    elif direction.upper() == 'W':
      x -= 1

    current_idx = (x,y)
    # You wrote that there is no garbage in the first cell hence the second codition
    if current_idx not in visited_idx and current_idx != (0,0):
        visited_idx.add(current_idx)

units_of_garbage_gathered = len(visited_idx)

首先,当我运行此代码时,我相信您正在寻找 4。 如果你想知道你的广场是否已经收集了垃圾,我认为你可能需要在你的动作和 collections 的同时跟踪它。有几种方法可以做到这一点,我会建议你的列表列表网格,如果您想要一个 3x3 网格,请创建如下内容:

grid = [[0,0,0],[0,0,0],[0,0,0]]

您现在可以有一些东西来跟踪垃圾已被拾取的位置以及是否有任何要拾取的东西。

所以现在,你的下一步是找出一个起点,比如

grid[0][0]

那么如果你上移一个,你就会进入

grid[1][0]

如果您访问该地点,在您的 function 中,执行以下操作:

newPos = grid[1][0]
if (newPos != 1) {
     // do your collecting and change grid[1][0] form 0 to 1
} else {
     // don't collect, track your movement or whatever else you want.
}

我希望这有帮助! 祝你好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM