简体   繁体   English

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

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

Imagine a bi-dimensional grid (NxN) where each square has garbage in it, for each movement described by N, S, E, W (for north, south, east and west), the player moves accordingly and grabs the garbage there was in that square.想象一个二维网格 (NxN),其中每个方格中都有垃圾,对于由 N、S、E、W(北、南、东和西)描述的每个移动,玩家相应地移动并抓取那里的垃圾在那个广场。

If the input = 'NESW' the output should be = 4 - 1 for square the player started - 1 going north, 1 going East, 1 going South and 0 going West because he reaches the initial square where he started which is already "cleaned"如果输入 = 'NESW' 则 output 应该 = 4 - 1 玩家开始的方格 - 1 向北,1 向东,1 向南,0 向西,因为他到达了他开始的初始方格,该方格已经“清理” "

For input = 'NSNSNSNS' output = 2 for the same reason.对于输入 = 'NSNSNSNS' output = 2 出于同样的原因。

I'm having trouble counting the garbage and knowing if the player already went to that place.我无法计算垃圾并知道玩家是否已经去过那个地方。

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)

This is what I have so far, it was just to start and try some stuff, but haven't been very successful:/这是我到目前为止所拥有的,只是开始尝试一些东西,但还不是很成功:/

since your grid is two dimensional, model it as a 2D grid - so choose a starting location (say (0,0)) and each direction alters that location - so from (0,0):由于您的网格是二维的,因此 model 将其作为二维网格 - 所以选择一个起始位置(例如(0,0))并且每个方向都会改变该位置 - 所以从(0,0)开始:

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

and so on等等

You store the locations you have visited in a python set - and before you go to a new location, you check if that location isn't already in a set.您将访问过的位置存储在 python 集合中 - 在您将 go 存储到新位置之前,您检查该位置是否已经在集合中。 That is if you want only one piece of garbage per location - if you want eventually to have mutliple pieces, you can model the grid as a dictionary;也就是说,如果您只希望每个位置有一块垃圾 - 如果您最终希望有多个垃圾,您可以将网格 model 作为字典; but as a set -但作为一组——

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)

You could create an array as Ahmet proposed in the comments, but if you do not want to you could do this like this:您可以按照 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)

First, when I run this code, I get 4 as I believe you are looking for.首先,当我运行此代码时,我相信您正在寻找 4。 If you want to know if garbage was already collected in your square, I think you might need to track that in parallel to your movements and collections.. there are a few ways you could do this, I would suggest a list of lists for your grid, if you wanted a 3x3 grid, create something like:如果你想知道你的广场是否已经收集了垃圾,我认为你可能需要在你的动作和 collections 的同时跟踪它。有几种方法可以做到这一点,我会建议你的列表列表网格,如果您想要一个 3x3 网格,请创建如下内容:

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

you can now have something to keep track of where garbage has been picked and if there is any to be picked.您现在可以有一些东西来跟踪垃圾已被拾取的位置以及是否有任何要拾取的东西。

So now, your next step would be to figure out a starting point like所以现在,你的下一步是找出一个起点,比如

grid[0][0]

then if you move up one, you would be in那么如果你上移一个,你就会进入

grid[1][0]

if you visit that spot, in your function, do something like:如果您访问该地点,在您的 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.
}

I hope that was helpful!我希望这有帮助! Good luck祝你好运

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

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