简体   繁体   中英

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.

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"

For input = 'NSNSNSNS' output = 2 for the same reason.

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):

  • N becomes (0,-1)
  • S becomes (0,1)
  • E becomes (1,0)
  • W becomes (-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. 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; 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:

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. 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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