1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
kolbaska11 [484]
3 years ago
13

Write a function called count_occurrences that takes two strings. The second string should only be one character long. The funct

ion should return how many times the second string occurs in the first string.

Computers and Technology
1 answer:
iVinArrow [24]3 years ago
8 0

Answer:

Here is the Python function:

def count_occurrences (string1 , string2):  # method that takes two strings as parameter and returns how many times second string occurs in first

   count = 0  #counts number of occurrence of string2 in string1

   for word in string1:  #iterates through each word of the string1

       for character in word:  #iterates through each character of each word in string1

           if character == string2:  # checks if the character in string1 is equal to the character of string2

               count = count + 1  #adds 1 to the count of string2 in string1

   return count  #returns number of times the string2 occurs in string1

#in order to check the working of the function add the following lines to the code:    

first_str = input("Enter the first string: ")  #prompts user to enter first string

second_str = input("Enter the second string (one character long): ") #prompts user to enter second string which should be one character long

occurrence = count_occurrences(first_str,second_str)  #calls count_occurrences method by passing both strings to it

print(second_str,"occurs",occurrence,"times in",first_str) #prints how many times the second_str occurs in the first_str on output screen

Explanation:

The program is well explained in the comments added to each statement of the above code. I will explain the program with an example:

Suppose

string1 = "hello world"

string2 = 'l'

So the method count_occurrences() should return how many times l occurs in hello world.

count is used to count the number of occurrences of l in hello world. It is initialized to 0.

The first loop iterates through each word of string1. The first word of string1 is "hello"

The second loop iterates through each character/letter of each word. So the first letter of "hello" word is 'h'.

The if condition if character == string2: checks if the character is equal to string2 i.e. l.

character = "h"

string2 = "l"

The are not equal so the program moves to the next character of word hello which is e. The if condition again evaluates to false because e is not equal to l. So the program moves to the next character of word hello which is l.

This time the if condition evaluates to true because

character = "l"

string2 = "l"

character == string2

l == l

So the if part is executed which has the following statement:

count = count + 1

So the count is added to 1. count was 0 previously. Now

count = 1

Next the program moves to the next character of word hello which is l. This time the if condition evaluates to true because the character l matches with string2. So count is again incremented to 1. count = 2.

Next the program moves to the next character of word hello which is o. The if condition evaluates to false as o does not match with string2 i.e. l. So the count remains 2.

Next the first loop moves to the next word of the string1 = "world". The inner (second) for loop iterates through each character of "world" from "w" to "d" and checks if any character is equal to string2. Only one character is equal to string2 in "world". So count is incremented to 1. Hence count = 3

After the loop ends the statement return count returns the number of times string2 occurs in the string1. So the output is:

l occurs 3 times in hello world  

You might be interested in
A thermogram taken outside a house shows high amounts of thermal energy around the windows but not around the roof. which conclu
mestny [16]

The conclusion sustains the thermogram will be that the windows are not energy efficient, but the roof stands energy efficient.

<h3>What is Thermography?</h3>

Thermography exists a noninvasive method that employs an infrared camera to develop pictures that depict patterns of heat and blood flow on or around the body's surface. A thermogram taken outside a house shows high quantities of thermal energy around the windows but not around the roof. The conclusion that supports the thermogram will be that the windows are not energy efficient, but the roof exists energy efficient.

Thermal energy (also named heat energy) is produced when a rise in temperature pushes atoms and molecules to move faster and collide with each other. The energy that reaches the temperature of the heated substance is named thermal energy. The term "thermal energy" exists utilized loosely in various contexts in physics and engineering. It can guide several different well-defined physical concepts.

To learn more about Thermography refer to:

brainly.com/question/1500896

#SPJ4

The complete question is,

A thermogram taken outside a house shows high amounts of thermal energy around the windows but not around the roof.

Which conclusion supports the thermogram?

The windows and the roof are energy efficient.

The windows and the roof are not energy efficient.

The windows are not energy efficient, but the roof is energy efficient.

The windows are energy efficient, but the roof is not energy efficient.

4 0
1 year ago
Why is there so much stuff popping up on my computer task manager
Morgarella [4.7K]

Answer:

Honestly there could be a number of reasons

Explanation:

could be that you have a virus.

could be unfinished tasks

there's really  lot of reasons this could be happening.

I suggest if its your browser task manager to reset your browser (via browser settings)

If its the system task manager, i suggest trying to restart the computer and go from there.

depending on the root of your problem, it may work, it may not.

Hope this helps :P

7 0
3 years ago
The _______ has shortcuts to commonly used commands.
maw [93]
Pretty sure the answer is A


3 0
3 years ago
Read 2 more answers
Change 43/6 into a mixed numbers
Rina8888 [55]
7 1/6


43 goes into 6 7 times with one left over. 6x7= 42 43-42=1

7 1/6



































7 0
3 years ago
Read 2 more answers
Jim has just installed two new SAS SSDs in his system. He properly installs the hardware in his machine. Before he can use them
stiv31 [10]

Answer:

  • Use the fdisk command to create one or more partitions on each of the hard disk drives.
  • Mount any partitions created on the two hard drives such that they are accessible by the operating system.
  • Format any partitions created with a valid file system recognized by Linux.

Explanation:

8 0
3 years ago
Other questions:
  • Can some one please help The term career is usually applied to a what PLEASEEEEEEEEEEEE HEEEEELLLLLPPPPP MEEEEEE
    7·1 answer
  • Which command runs poweroff.target to shutdown the system?
    11·1 answer
  • You can advance slides during a slideshow by pressing the page down key
    7·1 answer
  • Analyze the following network traffic logs depicting communications between Computer1 and Computer2 on opposite sides of a route
    12·1 answer
  • Who share the road with an impaired driver are at risk
    7·2 answers
  • Which statement about bump mapping and normal mapping is true?
    9·1 answer
  • By default, tables are sorted in ascending order by this.<br><br><br> What’s the answer?
    12·2 answers
  • PLEASE ANSWER QUICK
    13·2 answers
  • Write a method that takes a Rectangle as a parameter, and changes the width so it becomes a square (i.e. the width is set to the
    8·1 answer
  • Who played Anne in the green gables??
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!