Hi Im new to coding and I'm trying my hand at a question from MIT MOOC 6.00x Qn 3... | Sololearn: Learn to code for FREE!

0

Hi Im new to coding and I'm trying my hand at a question from MIT MOOC 6.00x Qn 3...

So basically, the question asks to produce the longest string in a random sequence of alphabets that actually presents itself in alphabetical order, as well as to print such a sequence that first presents itself in the random string. For example, if we had a string abcbcd, it should print abc instead of bcd and if it were abcbcde it should print bcde. if it were abbccdeehcvsn it should print abbccddee. This is a portion of my code which i am trying to run: s = 'azcbobobegghakl' x=1 y=2 z=str('Longest substring in alphabetical order is: ') Alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] for letter in s: if (str(letter) == s[x:y]): z+=str(letter) x+=1 y+=1 elif (str(letter) != s[x:y]): if ((Alphabet.index(str(letter))-Alphabet.index(s[x:y]))==1): z+=str(letter) x+=1 y+=1 else: x+=1 y+=1 continue else: x+=1 y+=1 continue print(z) I've not yet figured out a method to sniff out the longest string yet but that comes later. What i need help in is that every time i try to run the code in the sololearn interpreter, it produces this error message: Traceback (most recent call last): File "..\Playground\", line 36, in <module> if ((Alphabet.index(str(letter))-Alphabet.index(s[x:y]))==1): ValueError: '' is not in list Do take note that the line of code is on line 36 on the interpreter. I've been pondering over this for quite a long time and i don't see any value that is denoted as a blank in s as suggesred by the value error Anyone can explain why the value error is as such,how my code is actually being read and what new code to input to get an accurate output. Any new suggestions to solve this problem are welcome as well.

10/8/2018 2:22:30 PM

Loo Wee Sing

2 Answers

New Answer

+3

If you run the code like this, you will see what happens: s = 'azcbobobegghakl' x=1 y=2 z=str('Longest substring in alphabetical order is: ') Alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', ''] for letter in s: print(letter, s[x:y], x, y) if (str(letter) == s[x:y]): z+=str(letter) x+=1 y+=1 elif (str(letter) != s[x:y]): if ((Alphabet.index(str(letter))-Alphabet.index(s[x:y]))==1): z+=str(letter) x+=1 y+=1 else: x+=1 y+=1 continue else: x+=1 y+=1 continue print(z)

+1

hmm this is interesting thank you so much!