Ad
  • Custom User Avatar

    Woah. I hadn't thought of all that. I'll try to update the kata around the weekend, when I have time. Thanks for your in-depth feedback!

  • Custom User Avatar

    Thanks for your feedback! I will fix the test cases, and clarify the description. For the voting system, there isn't a way to deal with having 3 drawing votes at the first round. I suppose that it should just be a draw in that case (i.e. returning "no winner").

  • Custom User Avatar

    This solution wasn't designed to be elegant, only to do the job.

  • Custom User Avatar

    This is a very good kata, but my code doesn't work in CodeWars but it does on my computer. I'm using Python 3.6 vs CodeWars' Python 3.4, but I don't think that should have any difference?

    Anyway, here's my solution. Thanks if you can tell me why it doesn't work :)

    # stores the fastest routes
    fastestRoute = []
    fastestRouteCost = 100000000000
    
    # for finding a square in the route list
    def findListInList(needle, haystack):
        for i in range(len(haystack)):
            yes = True
            for j in range(len(needle)):
                if needle[j] != haystack[i][j]:
                    yes = False
            if yes:
                return i
    
        return False
    
    # the recursive solving function
    def nextStep(route, wires):
        global fastestRoute, fastestRouteCost
    
        # possible directions to trabel in
        directions = [[1,0], [1,1], [0,1], [-1,0], [-1, -1], [0, -1], [-1, 1], [1,-1]]
        
        lastStep = route[len(route)-1]
    
        # loop through directions
        for dir in directions:
            try:
                # we don't want negative indices, or squares that are already in the route
                if lastStep[0]+dir[0] < 0 or lastStep[1]+dir[1] < 0 or findListInList([lastStep[0]+dir[0],lastStep[1]+dir[1]], route):
                    continue
    
                # a traversable square has been found; recursively call to find solutions with it
                if wires[lastStep[0]+dir[0]][lastStep[1]+dir[1]] == ".":
                    newTempRoute = route+[[lastStep[0]+dir[0],lastStep[1]+dir[1]]]
                    nextStep(newTempRoute, wires)
    
                # a solution has been found; save it if it's the shortest 
                elif wires[lastStep[0]+dir[0]][lastStep[1]+dir[1]] == "G":
                    newTempRoute = route+[[lastStep[0]+dir[0],lastStep[1]+dir[1]]]
                    if len(newTempRoute) < fastestRouteCost:
                        fastestRoute = newTempRoute
                        fastestRouteCost = len(newTempRoute)
            except:
                None
              
        return True
    
    # the main function
    def wire_DHD_SG1(existingWires):
        global fastestRoute, fastestRouteCost
    
        # turn the wires into a searchable list
        lines = [list(line) for line in existingWires.split("\n")]
        start = [0,0]
    
        # find the start square
        for i in range(len(lines)):
            if "S" in lines[i]:
                start = [i, lines[i].index("S")]
                break # for slight optimisation
    
        # the main call of the solving loop
        nextStep([start], lines)
    
        # make sure a route has been found (there will always be at least 2 squares in a solved route)
        if fastestRoute != []:
            newLines = lines.copy()
            for square in fastestRoute[1:len(fastestRoute)-1]:
                chars = list(newLines[square[0]])
                chars[square[1]] = "P"
                newLines[square[0]] = "".join(chars)
                
            return "\n".join(["".join(line) for line in newLines])
    
        # Oh for crying out loud...
        return "Oh for crying out loud..."
    
  • Custom User Avatar

    Ah, thanks!

  • Custom User Avatar

    Thanks for making it! It was a good challenge :)

    Out of interest, I'm quite new and I'm not sure how I can translate Katas. Do you know at all?

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution