Python Forum
Strings inside other strings - substrings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strings inside other strings - substrings
#1
I have to write a function that takes a string argument str, returns the longest substring of str in which the
letters occur in alphabetical order. For example if the string is 'azcbobobegghakl', then the function
should return 'beggh' In the case of ties, print the rst substring. For example, if s = 'abcbcd', then the
function should return 'abc'.

I wrote this so far, this only returns True if there is a substring inside a string:
import sys
#s1 is the small string, s2 is the bigger string, and k is the index
s1 = sys.argv[1]
s2= sys.argv[2]
k = 0
#k=0, same as the first index of a list with the index 0
def substring(s1, s2, k): #len stands for length, which is the number of characters in a word
	if s1 in s2[k:]: #this function finds the substring s1 in s2
		return True
	return False

print(substring(s1,s2,k)) #Prints out the presence of s1 in s2 (boolean value)
Reply
#2
I don't understand. The function you have to write takes only one input and searches for alphabetical letters. Why did you write a function that searches for a specific substring?

To solve the problem you stated, you will want to loop through characters in the string (for loop). Check each one against the previous one for being alphabetical (ord() or less than operator), and keep track of the longest one so far (if condition using len).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
As @ichabod801 said, your function needs only one parameter (the source string), and should return a string. The return string can be:
  • Empty if source string is empty
  • One character long if only one character in source string
  • One character long if no alphabetical sequences
  • Up to the same length as the source string

(You could decide on alternative rules). You also need to consider whether or not to take account of upper and lower case (or initial upper case, or initialisation of individual words).

Like @ichabod801, I don't understand the substring check. What I'd expect to see is a loop that walks through each character of the source string, with a candidate return string building up until the required sequence is broken. You would store that candidate whilst checking for another sequence. If a new sequence is larger, it will replace the previous candidate.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Padlock of alphabetical strings Men 11 3,978 Aug-06-2022, 07:48 AM
Last Post: mariasmith1122
  help comparing strings xander 3 1,788 Jan-04-2022, 04:09 PM
Last Post: BashBedlam
  Removing all strings in a list that are of x length Bruizeh 5 3,133 Aug-27-2021, 03:11 AM
Last Post: naughtyCat
  Calculate mean only for match strings ranbarr 1 2,650 Jun-01-2021, 10:03 AM
Last Post: ranbarr
  Find Mismatch Between Two Strings Omid 8 5,024 Oct-19-2020, 02:41 PM
Last Post: perfringo
  Can someone help me optimize this game for large number of strings Emekadavid 13 4,873 Jul-06-2020, 06:16 PM
Last Post: deanhystad
  "Slicing and dicing strings" - - PyBite #105 Drone4four 8 4,293 Jun-11-2020, 09:28 PM
Last Post: knackwurstbagel
  list of strings to list of float undoredo 3 2,668 Feb-19-2020, 08:51 AM
Last Post: undoredo
  Convert a list of integers to strings? Cornelis 3 2,246 Nov-15-2019, 12:13 PM
Last Post: perfringo
  Print strings enclosed with a character with different characters aylmaoxd 1 1,903 Aug-17-2019, 03:35 PM
Last Post: Resistance

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020