hey guys i kinda need help on a code:
Given a string that may contain a letter p. Print the index of the second occurrence of p. If the letter p occurs only once, then print -1, and if the string does not contain the letter p, then print -2.
hey guys i kinda need help on a code:
Given a string that may contain a letter p. Print the index of the second occurrence of p. If the letter p occurs only once, then print -1, and if the string does not contain the letter p, then print -2.
So, this problem was probably posed to you by a teacher, and they like to take things that are procedures and strip them of their steps. What I mean is, let’s say I told you this procedure to make coffee:
Now, that makes total sense and you can follow it, but what teachers like to do to make things seem like a hard problem is take the above list and do this:
To make a cup of coffee you must put boiling water into a coffee cup by putting water in a kettle, the coffee in a filter, and pouring the water over the coffee in the filter into the cup.
This “paragraphized” version of the same steps seems to have all the right steps but they’re out of order and there’s some key things missing. (Side note: If you filmed this person making 20 cups of coffee they most likely would do it in exactly the same sequence 90% of the time but claim there’s no procedure.)
The trick is to procedurize that paragraph of garbage, and then solve each step of the procedure. Let’s take your paragraph:
Given a string that may contain a letter p. Print the index of the second occurrence of p. If the letter p occurs only once, then print -1, and if the string does not contain the letter p, then print -2.
Take each comma or period, and make it a new line:
Given a string that may contain a letter p.
Print the index of the second occurrence of p.
If the letter p occurs only once
then print -1
and if the string does not contain the letter p
then print -2
Put numbers next to the lines:
Now you have the procedure, and can work on converting this to code. How do you do that? Turn this into pseudo-code!
1. Given a string that may contain a letter p.
2. Print the index of the second occurrence of p.
3. If the letter p occurs only once
4. then print -1
5. and if the string does not contain the letter p
6. then print -2
# 1. Given a string that may contain a letter p.
# 2. Print the index of the second occurrence of p.
# 3. If the letter p occurs only once
# 4. then print -1
# 5. and if the string does not contain the letter p
# 6. then print -2
# 1. Given a string that may contain a letter p.
# pstr = input()
# 2. Print the index of the second occurrence of p.
# go through each letter of p until I count 2
# print that out
# keep going until I've counted all the p's
# 3. If the letter p occurs only once
# if p_count == 1
# 4. then print -1
# print -1
# elif p_count == 0:
# 5. and if the string does not contain the letter p
# 6. then print -2
# print -2
Now under each of the p-code lines (p-code is short of pseudo code), write out the Python that makes it happen.
As you make each line happen, run it and make sure it does what you think and fix it as you go. If you write 20 lines of code you should run your program 40-100 times while you are working on it.
I leave the rest to you.
ok thanks for helping i understand now :)))))