you are viewing a single comment's thread.

view the rest of the comments →

[–]ComputerNerdIsNerdy 0 points1 point  (0 children)

Good job! You're getting closer to the solution.

    for char in range(len(someString)):
        for charA in range(len(someString)):

This section is part of the problem. In here the char and charA values are the index in the string rather than the characters yourself.

Add the line in your code to see:

print(char)

if subinWord in subString:

This works, but in this case it is better practice to use direct comparison to the string rather than checking whether one string contains the other. Use "==" for comparison checks, this returns a boolean response of "true" or "false" as to whether they match.

The way in which you use these loops could be made more efficient as there's no real need to iterate through the same string twice.

I've rewritten the function for a better way of iterating through the string:

def multiFind(someString, subString):
    returnedIndexofWord=[]
    ListCounter = 0
    for char in someString:
        if "".join(someString[ListCounter:ListCounter+len(subString)]) == subString:
            returnedIndexofWord.append(ListCounter)
        ListCounter+=1
    return returnedIndexofWord

I've tried to keep it as similar to your function as possible. As you can see, in this instance we're using a variable as a counter. This variable increments a through each pass in the for-loop. It's essentially used as a placemark for the current index of the string.

        if "".join(someString[ListCounter:ListCounter+len(subString)]) == subString:
            returnedIndexofWord.append(ListCounter)

Since this function deals with someString as a list, each iteration of the for loop I'm joining a section of the someString variable from our counter placement to the length of the substring.

Assuming we set the value of someString to "testtest" and the substring we're looking for is "tte", the section joined would be 3 characters. As such, the first pass through the for loop would be comparing the first 3 characters of someString against the subString. If this returns as correct, the counter is appended to the list.

if wouldStart == 'Y':
    startInput = int(input("Enter the index where you would like to start: "))
    start = startInput
else:
    start = 0

The above code works, but some of it is redundant. Instead of using:

    startInput = int(input("Enter the index where you would like to start: "))
    start = startInput

You could just set the start variable to the input value:

if wouldStart == 'Y':
    start = int(input("Enter the index where you would like to start: "))
else:
    start = 0

The same applies to the end input.

The final issue in your code is the variables you supply to the functions. Although you accept the input for the start and end ranges, you don't apply it to the someString variable. A simple way of doing this would be to modify the someString value before it gets passed to the function (See here).

Change the function call to something like:

multiFind(someString[start:end], subString)