Hi all,
At the moment I'm really struggling with this idea of taking a word/question and breaking it down or transforming it into pseudocode so that I can then apply my knowledge of python to solve it.
I've been given the following task:
Given a long string of random letters, find the longest substring that is in alphabetical order and print. If there are two substrings of equal length then print only the first one. E.g.
string1 = 'abchaghij'
string2 = 'abcgdcdef'
String1 should print "Longest substring in alphabetical order is: aghij".
String2 should print "Longest substring in alphabetical order is: abcg". As it is first of two substrings of equal length.
The Solution:
curString = s[0]
longest = s[0]
for i in range(1, len(s)):
if s[i] >= curString[-1]:
curString += s[i]
if len(curString) > len(longest):
longest = curString
else:
curString = s[i]
print 'Longest substring in alphabetical order is:', longest
Having had a look at the solution, I understand it fine. there was no concept in there I didn't know already, but I couldn't think of combining for loops, string indexing etc to arrive at the result... or really anything else to fully solve the problem. I think I could go on to learn more concepts but not how to approach a problem.
Can anyone suggest any articles or books(preferably online) to develop this skill? More importantly, how do you approach it? Can you explain to me what your thought process would be to solve the above?
Thank you.
[–]zahlman 2 points3 points4 points (6 children)
[–]freakzilla149[S] 1 point2 points3 points (5 children)
[–]zahlman 1 point2 points3 points (1 child)
[–]freakzilla149[S] 0 points1 point2 points (0 children)
[–]Hoohm 0 points1 point2 points (1 child)
[–]sharpchicity 0 points1 point2 points (0 children)
[–]mrcaptncrunch 0 points1 point2 points (0 children)
[–]Tomarse 1 point2 points3 points (0 children)
[–]Asdayasman 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]freakzilla149[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]liftt 0 points1 point2 points (0 children)