ive been practicing on codingbat trying to figure out the problems, and things were going well until I got to the level 2 problems, now I spend a lot of time on one problem and still cant seem to figure it out. Then once I've given up on trying to solve it, and search for the solution so I can hopefully understand how the problem is solved, I don't really understand how all the code is working line by line. So here's the one I am stuck on today, hope you all can help me understand and correct where I'm wrong. (The code works, I just don't fully understand how)
https://codingbat.com/prob/p139564 Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad".
Please always tell us what goes wrong with code. Also please supply a link to the original codingbat question, so we shall know what you are supposed to do.
You usually cannot solve that sort of question by writing code. You have to work out what the algorithm will be, starting on paper, and then proceeding to pseudo‑code.
You should credit the place you found the solution, maybe with a link to it.
I don't "understand" this either, meaning I'm not sure why the author wrote it, but if I were to put the code into a sentence, I would say:
If this is not the first character and the character is '*' and the character to the left of it is not '*', then strip the last character off of the result string.
Does that help?
All things are lawful, but not all things are profitable.