
Where one string contains the alphabets in order and the other contains the test string.
#Mad daedalus key code full
, up to the full string of alphabets and then stores the longest substring encountered in the check.įor the sake of completeness, here is the code that would work for any longest common substring: def longestSubstring(str1, str2): This iterates through every possible substring from 'a', 'ab', 'abc'. This works on the assumption that the substring should always begin with a. Print(longestAlphaSubstring('asdklfjalkdfjabcdefghijklmnopqrstuvwxyzaaabcasdkfjl kasdf')) Print(longestAlphaSubstring('jamabcdaskl')) If str1 in str2 and len(str1)>len(longest): It seems like the above code doesn't work for all cases, so I had to redesign the function. Print('longest substring:', longestSubstring(s,alphabets)) This is a variant of the Longest Common Substring Problem. TL DR: the last code after the edit solves the problem If I could skip one loop when the first if condition is true, then I think it might work in my code. The problem with my current code, is that it gives bccddeezzzzz. In the case of xyzbcdezzz, it will extract 'bcdezzz' because that's the longest possible alphabetical order substring, not bcde. What I meant was that if I have a string, it extracts the longest possible substring in alphabetical order. To fix this, I've been attempting to make the loop skip the next character if it's in alphabetical order already, and check the next one by increasing the value of 'n' once the condition is met, but this doesn't seem to do anything at all.Īdvice, tips, corrections and harsh criticism are all welcomed.ĮDIT: It appears I've misled some of you, so I apologise.

Then it checks b, comparing to c and adds bc together, and then adds "bc" to "ab". This is because it checks the character a, comparing it to the next in the sequence, and then adds a to b giving "ab".

When I run this code, it gives 'abbccd' as the longest substring, when it's actually 'abcd'. If len(longest_string) > len(current_string): If len(current_string) > len(longest_string): If len(current_string) = len(longest_string): Print('the longest string checked is:', longest_string, ', count reset') If len(current_string) > len(longest_string) : The goal of the code is to find the longest alphabetical substring within a string.
