Skip to content

Commit 0eaa8e7

Browse files
committed
correct path
1 parent 175cc50 commit 0eaa8e7

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Keep updating.
1515
- Simple Symbols
1616
- First Factorial
1717
- Letter Capitalize
18+
- Correct Path
19+
- I hate using bruteforce!
1820

1921
## Hard
2022
- Kaprekars Constant

coderbyte-CorrectPath.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Challenge (easy)
3+
4+
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.
5+
6+
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.
7+
8+
Sample Test Cases:
9+
10+
Input:"???rrurdr?"
11+
Output:dddrrurdrd
12+
13+
14+
Input:"drdr??rrddd?"
15+
Output:drdruurrdddd
16+
'''
17+
18+
def CorrectPath(s): #bruteforce ftw!
19+
import random
20+
while True:
21+
route=[]
22+
tracepos=[]
23+
x=1;y=5;answer=1
24+
for i in s:
25+
if i=="?":i=random.choice("lrud")
26+
if i=="u":y+=1
27+
elif i=="d":y-=1
28+
elif i=="r":x+=1
29+
elif i=="l":x-=1
30+
if (x,y) in tracepos:
31+
answer=0
32+
break
33+
else: tracepos.append((x,y))
34+
route.append(i)
35+
if x==6 or x==0 or y==0 or y==6:
36+
answer=0
37+
break
38+
if x==5 and y==1 and answer==1:
39+
return "".join(route)
40+
41+
print CorrectPath(raw_input())
File renamed without changes.

0 commit comments

Comments
 (0)