r/learnpython • u/yusuke_guy • 5d 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
3
u/desrtfx but other languages pro 5d 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?
2
u/kilkil 5d ago
your solution looks mostly right. the issue is in lines 6-8, specifically here:
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:
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)2
u/POGtastic itertools evangelist 4d ago edited 4d 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
returnlike that in the MOOC. Either wrap your code in a function, which will allowreturnto short-circuit the function, or use a conditional to avoid printing the other message.Another hint to make your code a little cleaner:
str.findlets 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 callstr.findagain. That way you don't even have to do slicing.2
u/Insomnia_Calls 4d 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 4d ago edited 4d ago
To get around the problems that using slicing introduces (having to adjust find() indices) why not use the
startoptional argument tofind(). 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.