Skip to main content
Tweeted twitter.com/StackCodeReview/status/1213067503225724929
Became Hot Network Question
Tags, capitalization, corrected function name
Source Link
AJNeufeld
  • 35.3k
  • 5
  • 41
  • 103

I've completed the following coderbyte question and would like feedback as to whether it follows pythonPython best practices, is efficient and so forth.

Have the function CorrectPath(str)calcpath(str) read the str parameter being passed, which will represent the movements made in a 5x5 grid of cells starting from the top left position The characters in the input string will be entirely composed of: r, l, u, d, ?. Each of the characters stand for the direction to take within the grid, for example: r = right, l = left, u = up, d = down. Your goal is to determine what characters the question marks should be in order for a path to be created to go from the top left of the grid all the way to the bottom right without touching previously travelled on cells in the grid.

I've completed the following coderbyte question and would like feedback as to whether it follows python best practices, is efficient and so forth.

Have the function CorrectPath(str) read the str parameter being passed, which will represent the movements made in a 5x5 grid of cells starting from the top left position The characters in the input string will be entirely composed of: r, l, u, d, ?. Each of the characters stand for the direction to take within the grid, for example: r = right, l = left, u = up, d = down. Your goal is to determine what characters the question marks should be in order for a path to be created to go from the top left of the grid all the way to the bottom right without touching previously travelled on cells in the grid.

I've completed the following coderbyte question and would like feedback as to whether it follows Python best practices, is efficient and so forth.

Have the function calcpath(str) read the str parameter being passed, which will represent the movements made in a 5x5 grid of cells starting from the top left position The characters in the input string will be entirely composed of: r, l, u, d, ?. Each of the characters stand for the direction to take within the grid, for example: r = right, l = left, u = up, d = down. Your goal is to determine what characters the question marks should be in order for a path to be created to go from the top left of the grid all the way to the bottom right without touching previously travelled on cells in the grid.

added 4 characters in body
Source Link
Dave
  • 753
  • 8
  • 16

Input: "???rrurdr?", Output: dddrrurdrd"dddrrurdrd"

Input: "drdr??rrddd?", Output: drdruurrdddd"drdruurrdddd"

Input: "???rrurdr?", Output: dddrrurdrd

Input: "drdr??rrddd?", Output: drdruurrdddd

Input: "???rrurdr?", Output: "dddrrurdrd"

Input: "drdr??rrddd?", Output: "drdruurrdddd"

Source Link
Dave
  • 753
  • 8
  • 16

Find the correct path through a 5 x 5 grid (coderbyte 'Correct path')

I've completed the following coderbyte question and would like feedback as to whether it follows python best practices, is efficient and so forth.

The coderbyte problem:

Have the function CorrectPath(str) read the str parameter being passed, which will represent the movements made in a 5x5 grid of cells starting from the top left position The characters in the input string will be entirely composed of: r, l, u, d, ?. Each of the characters stand for the direction to take within the grid, for example: r = right, l = left, u = up, d = down. Your goal is to determine what characters the question marks should be in order for a path to be created to go from the top left of the grid all the way to the bottom right without touching previously travelled on cells in the grid.

For example: if str is "r?d?drdd" then your program should output the final correct string that will allow a path to be formed from the top left of a 5x5 grid to the bottom right. For this input, your program should, therefore, return the string rrdrdrdd. There will only ever be one correct path and there will always be at least one question mark within the input string.

Examples

Input: "???rrurdr?", Output: dddrrurdrd

Input: "drdr??rrddd?", Output: drdruurrdddd

Code:

import itertools import numpy as np def calcpath(s): _map = {"d": 1, "u": -1, "l": -1, "r": 1} q_count = s.count("?") move_combinations = itertools.product(_map.keys(), repeat=q_count) while True: arry = np.array([[0]*5 for i in range(5)]) arry[0][0] = 1 # start at 0, 0 current_x = 0 current_y = 0 try: moves = next(move_combinations) except StopIteration: return None ss = s[::].replace("?", "{}").format(*moves) for move in ss: if move in "lr": current_x += _map.get(move) elif move in "du": current_y += _map.get(move) if current_x < 0 or current_x > 4: break elif current_y < 0 or current_y > 4: break elif arry[current_x][current_y]: break else: arry[current_x][current_y] = 1 # check for completion if (current_x, current_y) == (4, 4): return ss assert calcpath("???rrurdr?") == "dddrrurdrd" assert calcpath("drdr??rrddd?") == "drdruurrdddd" assert calcpath("??ddd??lrlddd?") == None