the body of a question Implement the findChar_pos() function by modifying the findChar() function to satisfy the conditions below.
Add a position parameter that receives an integer. The integer transmitted through position indicates the location to start the search. That is, the previously defined findChar() function makes it a special case in which the position is 0.
def findChar_pos(word, char, pos):
idx = 0
while idx < len(word):
if word[idx] == char:
return idx
idx = idx + 1
return -1
assert findChar_pos("hello world", "o", 0) == 4
assert findChar_pos("hello world", "o", 5) == 7
------------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-161-bf375050267a> in <module>()
6 idx = idx + 1
7 return -1
----> 8 assert findChar_pos("hello world", "o", 0) == 4
9 assert findChar_pos("hello world", "o", 5) == 7
AssertionError:
I can't try to implement pos in an if word [idx:pos] way ㅜㅜ
Hello.
If you read the code of the description and the text carefully, it's actually a very simple problem. There's the answer to the question. Please find out how to apply the hint from the problem to the code in the notes you wrote down.
def findChar_pos(word, char, position): # Add a position parameter that receives an integer.
The integer transmitted through idx = position # position indicates the location to start the search.
while idx < len(word):
if word[idx] == char:
return idx
idx = idx + 1
return -1
The findChar
function repeats the idx to the length of the word and finds the character, so you can adjust the value of idx
to make it examine from that part.
567 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
577 PHP ssh2_scp_send fails to send files as intended
597 GDB gets version error when attempting to debug with the Presense SDK (IDE)
572 Understanding How to Configure Google API Key
567 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.