r/learnpython • • 3d ago

Stuck at Helsinki mooc.fi exercise

I have been doing one of the exercises in the mooc fi course : https://programming-26.mooc.fi

Specifically part 3 (working with strings - 15, the last one in that section), here's the question : https://programming-26.mooc.fi/part-3/2-working-with-strings#programming-exercise-the-second-occurrence

but sadly been days and couldn't get what's wrong with my solution, here it's as well : https://pastebin.com/kmHf6jjm

I tried to check for their discord server at :

https://programming-26.mooc.fi/support-and-assistance

But it keeps redirecting me to student of helsinki's login portal asking for a uni account, is the discord server public or restricted to those who attend the Helsinki University....or maybe I am missing something out

would appreciate the help whether for solving it or for how to access the discord server (or shedding some light on if it's accessible or not)

thanks in advance

2 Upvotes

13 comments sorted by

3

u/desrtfx but other languages pro 3d ago

Show your code and precisely describe your problems.

If you have no code yet, describe how you would approach the task in plain English.

A hint: it's a combination of .find() and slicing.

Can you find the first instance of a letter?

1

u/yusuke_guy 3d ago

yes ofc, i added a pastebin link for it (sorry if not the best way to share code), here it's : https://pastebin.com/kmHf6jjm

my solution with indeed using find then slicing after the first instance then using find again, and it seems to work when i run my own test and their test cases, but it fails to pass the exercise :(

2

u/desrtfx but other languages pro 3d ago

I am not entirely sure what the problem is. Your logic is solid and basically the same approach that I used.

My assumption is the return is causing some problem (where it actually shouldn't).

2

u/kilkil 3d ago

your solution looks mostly right. the issue is in lines 6-8, specifically here:

wordshort = word[cut:]

when you do that, because you're changing the length of the word, you're messing up all the indexes.

for example if I had input "abcabcabc", and substring "abc", your approach would do this:

  • find first match at index 0, correct

  • cut off the first 3 letters, going from "abcabcabc" (original input) to "abcabc" (shortened input)

  • find the first match in the now-shortened string is at index 0

the problem is, index "0" of your shortened string is actually supposed to be bigger. in my example, it should be index "3" not "0".

how would you fix this?

(hint: check the documentation for str.find(), on https://docs.python.org)

3

u/yusuke_guy 3d ago

thanks for taking time to look at my solution. and yeah indeed, I already faced that exact problem first time trying the exercise as u mentioned

that's why if you look at the formatted print line you will notice i fixed it by doing {occ2 + cut} so it takes the already sliced part into account, and sadly the solution still fails

1

u/kilkil 3d ago

ah shit, well that's embarassing. I totally missed that lol

could you post the input for which it is failing, the expected output, and the actual output? trying to see if my own solution will have the same bug

1

u/yusuke_guy 2d ago

Nah nothing embarrassing, if anything is embarrassing that is the problem did lie in me using return while not having the code wrapped inside a function, that was the problem, thanks for taking time to help

2

u/desrtfx but other languages pro 3d ago

Hint: check OP's code. They are accounting for that.

2

u/POGtastic itertools evangelist 2d ago edited 2d ago

I worked through all of the MOOC to unlock this problem and see what their environment was like.

My 7-year-old daughter did take interest in what I was doing because it looked like some of her computer e-learning stuff that she does at school, so I hope you're happy with introducing my Dearest Beloved to this wretched profession.


You cannot use return like that in the MOOC. Either wrap your code in a function, which will allow return to short-circuit the function, or use a conditional to avoid printing the other message.

Another hint to make your code a little cleaner: str.find lets you specify a starting index for the search. So after finding the index of the first occurrence, you can add an offset to that occurrence and call str.find again. That way you don't even have to do slicing.

2

u/yusuke_guy 2d ago

thanks for taking the time to look into it, and.....sorry for your daughter 😭 (best of luck for her)

Yeah, you are quite right the problem really did lie in the return, i guess that's one downside of learning a second language, I got so used to working in the C++'s main() that I became used to terminating anything with the return, much appreciated!!

And yeah, did notice others suggesting that find() takes a second argument for the index where to start searching from, but since I was not yet introduced to it in the MOOC, I tried to keep it as primitive as how someone who still didn't acquire that knowledge would tackle the exercise first time, ADDED to the arsenal

Thanks again

2

u/Insomnia_Calls 3d ago

Your code looks fine. I can only recommend you to take a look in the src file for the exercise (you can find it in the src/ subfolder in VSCode), if I remeber correctly there should be a file “test.py” or something. There, you can see what tests are performed on your code to pass the exercise. Oftentimes, it is something as simple as added/missing period in your print statement.

2

u/lakseol 2d ago edited 2d ago

To get around the problems that using slicing introduces (having to adjust find() indices) why not use the start optional argument to find(). That makes finding the second non-overlapping occurrence of a substring much easier.

Caveat: Haven't done that course so I don't know what it shows you about the str.find() method or what limitations the problems impose.