开发者

Finding a substring inside of a string and its slice?

I've been trying to help my friend with his assignment and I can't even figure out where to start. I can get it to identify the substring inside of the string but giving where it starts is troubling me. I've been at it for hours. Any help is welcome. Here is everything I have about the assignment below.

PS. his entire class doesn't know how to do this. The teacher gave them something way out of their and my league.

Write a program that asks the user to input two strings stringA and stringB.

  1. The program must first find all starting positions in stringA where stringB appears.

    For example if stringA is "the car is in that carport", and stringB is "car", the program should report that stringB occurs in stringA starting at positions 4 and 19 (remember that the string starts at position 0).

  2. Next the program must find all the starting positions in stringA where stringB almost appears.

    • What 'almost' means is that one of the letters of stringB does not match.
    • For example if stringA is the same as above, and stringB is "the", the program should report that stringB almost appears starting at position 14, which is the start of the string "tha".
    • Notice that it does not report that it开发者_C百科 almost appears starting at position 0, since there it appears with zero mismatches.
  3. Finally, the program must find all the starting positions in stringA where the reverse of stringB appears.

    • For example, if stringA is the same as above, and stringB is "rop", then the program should report that the reverse of stringB appears in stringA starting at position 22.

The program equalsubstrings.py is helpful in understanding how to write this program.


stringA = "the car is in that carport"
stringB = "the"

exact = []
reverse = []
almost = []

lenA = len(stringA)
lenB = len(stringB)
limit = lenA - lenB
reversedB = stringB[::-1]

pos = stringA.find(stringB)
while pos >= 0:
    exact.append(pos)
    pos = stringA.find(stringB, pos + 1)

pos = stringA.find(reversedB)
while pos >= 0:
    exact.append(pos)
    pos = stringA.find(reversedB, pos + 1)

for i in range(limit):
    substrA = stringA[i:i+lenB]
    misses = 0
    for j in range(lenB):
        if substrA[j] != stringB[j]:
            misses += 1
    if misses == 1:
        almost.append(i)

print exact, almost, reverse


I'll give you two clues:

In order to check if a string is a substring of another one, you can do:

  1. Use find:

a="this is a test"

b="test"

a.find(b,0,14)

10

  1. use the operator in like in the following example:

a="this is a test" b="test" c="not related text" b in a True b in c False

To reverse a string, do something like the following:

>>> a="this is a test"
>>> a[::-1]
'tset a si siht'

Good luck with your homework


I'd use the re module.

  • re.finditer allows you to go through all the matches of a substring in a string.
  • Each match will provide you with a "match object".
  • Use mo.start() on any match object to find the index of that match into the string.
  • For the "almost" matches, go through stringB char-by-char, replacing the current character with [^<current_character>], and repeat the match procedure for each resulting regex, recording all matches.
  • Lastly, do the simple search with the reverse of stringB (see elsewhere).

The benefit of this approach: You get a good grasp on re :-) .

BTW: What is the mysterious equalsubstrings.py program? ;-).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜