Code:second occerence

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:

  1. Put water in kettle.
  2. Put coffee in filter.
  3. Put filter on cup.
  4. Boil water.
  5. Pour boiling water on coffee in filter so it goes into the cup.

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:

  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

Now you have the procedure, and can work on converting this to code. How do you do that? Turn this into pseudo-code!

  1. Take your procedure above and write the lines into a file:
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. Turn the numbers at the beginning into comments:
# 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. Each line that “looks like python/code” turn it into a fake loose kind of code by asking yourself, “What code would make this line happen?”
# 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

  1. Now under each of the p-code lines (p-code is short of pseudo code), write out the Python that makes it happen.

  2. 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.

3 Likes

ok thanks for helping i understand now :)))))