-2
def functionOne(shortWord, longWord):
    character = shortWord[0]

I am at a loss right now, I need to find the index value of the first letter of shortWord's first occurrence in longWord but I can't use concepts such as find() or index(). How would I compare the two to get the value?

for example: if the arguments were abcdefghijklmnop and cdef I need to return an index value of 2.

Edit: I can only use slicing and basic indexing concepts such as getting the index value or range using [#]. I cannot use external functions.

Edit #2: The question posed is as follows: Using string slicing and indexing concepts find the location of the second argument within the first. The index value of the beginning of the second argument will be returned by the function.

  • Shouldn't that be *"if the arguments were `cdef` and `abcdefghijklmnop`"*? – wjandrea Mar 10 '22 at 21:10
  • Welcome back to Stack Overflow! Please take the [tour] if you haven't already. What have you already tried? Do you know how to keep track of an index in a loop, for example [with `enumerate()`](/q/522563/4518341)? (Are you allowed to use `enumerate()`? Please be specific about the requirements.) Please read [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341). You're expected to make an attempt at the problem yourself first. If you want more tips, see [ask]. – wjandrea Mar 10 '22 at 21:13
  • 1
    Define "such as". I'm not gonna write an alternative only for you to then say "I can't use that". – Kelly Bundy Mar 10 '22 at 21:15
  • I have tried this for around an hour now with no real idea of how I would get it to work. I can only use string slicing and light indexing such as variable[5]. I can't use enumerate and Im sorry if my formatting is wrong. – Noah Preston Mar 10 '22 at 21:17
  • @Noah Can you use `range`? Please be specific, and post your best attempt. The formatting's fine now, I fixed it for you :) – wjandrea Mar 10 '22 at 21:19
  • 1
    So you can't use loops or recursion? – Kelly Bundy Mar 10 '22 at 21:19
  • I can use range and loops such as for loops or while loops but my best attempt so far has been nothing as I cant even comprehend how I might do it with such a limited toolset. – Noah Preston Mar 10 '22 at 21:20
  • @Noah OK, then [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341) has this advice: "**Ask about *specific* problems with your *existing* implementation.** If you can't do that yet, try some more of your own work first or searching for more general help; your professor is likely to be a better resource at this stage than Stack Overflow." Sorry to turn you away, but SO is not built to teach the language basics. – wjandrea Mar 10 '22 at 21:25
  • If it helps, that question is asking you to write your own version of `str.find()` or `str.index()`. – wjandrea Mar 10 '22 at 21:25

1 Answers1

-1

Maybe you can loop trow the string while comparing. Some thing like this:

def functionOne(shortWord, longWord):
    for position in range(len(longWord) - len(shortWord)):
        if shortWord == longWord[position: position + len(shortWord)]:
            return position
Mayo
  • 36
  • 5
  • I tried the solution that you gave but the output of the function afterwards returned as "None", why would that be? – Noah Preston Mar 10 '22 at 21:28
  • @NoahPreston Either because that's correct or because of the bug. – Kelly Bundy Mar 10 '22 at 21:30
  • @Noah If `shortWord` is not in `longWord`, the behaviour is undefined. This function doesn't cover it, so the function implicitly returns `None`. For comparison, `str.find()` would return `-1` and `str.index()` would raise an error. – wjandrea Mar 10 '22 at 21:38
  • if this function returned None is because the string you are looking for is not in the other string – Mayo Mar 10 '22 at 21:42
  • @Mayo Or, as mentioned before, because of your bug. – Kelly Bundy Mar 10 '22 at 21:47