Ad
  • Custom User Avatar

    Ok, fine. Advise: you should unpublish the kata in the meantime. ;)

    Keep me posted when you'll republish it, please. :)

  • 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

    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").

    Reading this makes me think that the tests aren't precise enough: currently, my solution does not match this type of behavior at all! Example:

    "a" = 100, "b" = 100, "c" = 20, "d" = 10
    

    With that, my solution will keep a, b and c for the secund round. Well acutally, that will end up with the exact same result... Let me think a little bit about it...

    Ok...:

    10: a, c
    10: b, c
    5:  c, a
    

    With this, my code will return "no winner" because it will keep c for the secund round, but with what you describe, a should win on the second round.

    I believe your kata/description needs reenforcement... ;)

    EDIT: you should explore this type of subcases too:

    10: a, c
    8:  b, c
    8:  c, a
    
    => result should be a
    
    
    10: a, c
    8:  b, c
    8:  c, a
    7:  d, c
    
    => result should be c
    

    BTW: the "with majority" condition: is that synonym of "won at the first round" or is it real calculation at the second round? Because in that case...:

    10: a, c
    8:  b, c
    8:  c, a
    7:  d, c
    7:  e, c
    

    c wins at the second tour but he has better than the majority at this one. So? If you just want to have the answer "1st/2nd round", put it that way in the description, if not, you should emphasize this a bit more, I believe (here again, my solution answer to "first/second round" only...).

  • 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

    Hi,

    Interesting kata. For a moment, I feared it was a duplicate of the "instant runoff voting", but it's not. :)

    Some suggestions:

    • You should (actually "have to") put the Test.it or Test.describe before the call to the user's function: currently, when printing something to the console, the display of the current test is done before the message related to it is displayed, that's misleading... I ended up debugging the first test for a draw with the test.it message "should return bill and majority"!
    • no need to do the second assertion (test.expect), the test.assert_equals perfectly does the job

    EDIT: the description might miss one thing though: if you get a draw after the first round, what are the remaining candidates on the second round? Only those who were draw or those and the ones who got the second highest score too? (currently, my solution does that. It passed the tests, but is that the wanted behavior?)

  • Custom User Avatar

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

  • Custom User Avatar

    Not an issue, a question. ;p (issues are when kata is not working correctly or contains wrong informations/tests/...)

    One important thing is missing in your post: what is the problem on cw? time out? or failure? (I guess the first one) Be aware that your solution has to pass more than 300 tests with very large grids (300 random ones, size of the grid is range(3,75,5) (NxN) and you have 20 tests for each size), so your code has to be optimized/efficient to get throuth them. Abut python verison, 3.4 is a bit faster but good solutions manage the tests in 5s, so that couldn't be an issue.

    'Looked quickly at your code: it's really not optimized. One of your function is actually reeeeeally slow and that's where lays your troubles.

  • 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

    Fair comment.

  • Custom User Avatar

    The use of short or obvious variable names in the Kata Test Cases in Python increases the chance of problems due to collisions with user solutions' choices for their variable names, and it's difficult to debug when this happens. Please try to use as few global variables as possible, and use long, non-obvious variable names and/or store them inside of a single variable or object.

  • Custom User Avatar

    Ah, thanks!

  • Custom User Avatar

    Once you've solved a kata you can click on the language drop down menu from the Details Page and select + Add New. This should open the kata for editing. Once complete the Kata Sensei can approve/reject the translations. Hope this helps :-)

  • 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

    Cool, nice one :-), thanks for solving.

  • Custom User Avatar

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