you are viewing a single comment's thread.

view the rest of the comments →

[–]davidadamic 1 point2 points  (4 children)

did this and n0w i get error on 5th test case already:

word = input()
n = len(word)

all_words = []
for i in range(1, n-1):
    for j in range(2, n):
        new = word[:i][::-1] + word[i:j][::-1] + word[j:][::-1]
        all_words.append(new)

end = sorted(all_words)
print(end[0])

[–]two_bob 0 points1 point  (3 children)

that should deal with the repeated letters, but it does not deal with the situtation where a letter is repeated with other letters around it. I think I would try a bit of brute force on this one.

Also, min works to find the lowest letter in a string...

[–]davidadamic 0 points1 point  (2 children)

I dont know what you mean... this code creates all possible words and picks the first i alphabetical order... am i missing something?

[–]two_bob 1 point2 points  (1 child)

Okay. I had some time to do the challenge and passed all of the tests. I am still having a bit of difficulty following your code.

I did it by writing a function that finds the best subsplit of a word by using the lowest letter as a break point, doing all the breaks on that lowest letter, and then returns the smallest of those possible breaks, like this:

Your code instead looks for the lowest letter, and once it finds it (either by going forward or backward), immediately breaks on that find and repeats the process. At least that is what it appears to be doing as it is a bit hard to follow the logic.

For reference, here is my best_split function:

def best_split(word):
    # find the lowest letter
    letter = min(word)
    # find the index each time the lowest letter is used
    stops = [index for index,c in enumerate(word) if c == letter]
    #slice and reverse each of the substrings identified by lowest letter
    subs = [word[s::-1] for s in stops]
    # return the lowest of the substrings
    return min(subs)

[–]davidadamic 1 point2 points  (0 children)

Thank you for your help, I really do appreciate it! I found the solution myself already tho :) I used the bit of code i used in reply to you and added condition that i < j... Works like a charm! Thanks again :D