Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I was working on a problem set and I came across this problem:

Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print:

Longest substring in alphabetical order is: beggh
Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc
Longest substring in alphabetical order is: abc

I actually was able to get a solution, but it was the most inefficient ever! And here's what I came up with:

def test():
 index = 1
 prev_index = 0
 count = 0
 global largest
 largest = ''
 test = s[prev_index]
 while count < len(s):
 if ord(s[index]) > ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 elif ord(s[index]) == ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 else:
 if len(largest) < len(test):
 largest = test[:]
 test = s[index]
 prev_index += 1
 index += 1
 count += 1
 return largest
try:
 test()
except IndexError:
 pass
finally:
 print largest

Can anyone give me an example of better code that produces the same output?

I was working on a problem set and I came across this problem:

Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print:

Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc

I actually was able to get a solution, but it was the most inefficient ever! And here's what I came up with:

def test():
 index = 1
 prev_index = 0
 count = 0
 global largest
 largest = ''
 test = s[prev_index]
 while count < len(s):
 if ord(s[index]) > ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 elif ord(s[index]) == ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 else:
 if len(largest) < len(test):
 largest = test[:]
 test = s[index]
 prev_index += 1
 index += 1
 count += 1
 return largest
try:
 test()
except IndexError:
 pass
finally:
 print largest

Can anyone give me an example of better code that produces the same output?

I was working on a problem set and I came across this problem:

Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print:

Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc

I actually was able to get a solution, but it was the most inefficient ever! And here's what I came up with:

def test():
 index = 1
 prev_index = 0
 count = 0
 global largest
 largest = ''
 test = s[prev_index]
 while count < len(s):
 if ord(s[index]) > ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 elif ord(s[index]) == ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 else:
 if len(largest) < len(test):
 largest = test[:]
 test = s[index]
 prev_index += 1
 index += 1
 count += 1
 return largest
try:
 test()
except IndexError:
 pass
finally:
 print largest

Can anyone give me an example of better code that produces the same output?

deleted 15 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Improve Improving efficiency: Finding for finding longest contiguous nondecreasingnon-decreasing substring

So, I was working on a problem set and I came across this problem:

Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print:

Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc

I actually was able to get a solution, but it was the most inefficient ever! And here's what I came up with:

def test():
 index = 1
 prev_index = 0
 count = 0
 global largest
 largest = ''
 test = s[prev_index]
 while count < len(s):
 if ord(s[index]) > ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 elif ord(s[index]) == ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 else:
 if len(largest) < len(test):
 largest = test[:]
 test = s[index]
 prev_index += 1
 index += 1
 count += 1
 return largest
try:
 test()
except IndexError:
 pass
finally:
 print largest

Can anyone give me an example of a better code producingthat produces the same output? Thanks. :)

Improve efficiency: Finding longest contiguous nondecreasing substring

So, I was working on a problem set and I came across this problem:

Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print:

Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc

I actually was able to get a solution but was the most inefficient ever! And here's what I came up with:

def test():
 index = 1
 prev_index = 0
 count = 0
 global largest
 largest = ''
 test = s[prev_index]
 while count < len(s):
 if ord(s[index]) > ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 elif ord(s[index]) == ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 else:
 if len(largest) < len(test):
 largest = test[:]
 test = s[index]
 prev_index += 1
 index += 1
 count += 1
 return largest
try:
 test()
except IndexError:
 pass
finally:
 print largest

Can anyone give me an example of a better code producing the same output? Thanks. :)

Improving efficiency for finding longest contiguous non-decreasing substring

I was working on a problem set and I came across this problem:

Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print:

Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc

I actually was able to get a solution, but it was the most inefficient ever! And here's what I came up with:

def test():
 index = 1
 prev_index = 0
 count = 0
 global largest
 largest = ''
 test = s[prev_index]
 while count < len(s):
 if ord(s[index]) > ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 elif ord(s[index]) == ord(s[prev_index]):
 test += s[index]
 index += 1
 prev_index += 1
 else:
 if len(largest) < len(test):
 largest = test[:]
 test = s[index]
 prev_index += 1
 index += 1
 count += 1
 return largest
try:
 test()
except IndexError:
 pass
finally:
 print largest

Can anyone give me an example of better code that produces the same output?

edited title and tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

How can this code be made more efficient? Improve efficiency: Finding longest contiguous nondecreasing substring

format the problem statement; remove irrelevant instructions to students
Source Link
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 210
Loading
Tweeted twitter.com/#!/StackCodeReview/status/394171451898875904
added 24 characters in body
Source Link
Loading
deleted 13 characters in body
Source Link
Loading
Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /