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
melomori [17]
3 years ago
8

#Write a function called 'string_type' which accepts one #string argument and determines what type of string it is. # # - If the

string is empty, return "empty". # - If the string is a single character, return "character". # - If the string represents a single word, return "word". # The string is a single word if it has no spaces. # - If the string is a whole sentence, return "sentence". # The string is a sentence if it contains spaces, but # at most one period. # - If the string is a paragraph, return "paragraph". The # string is a paragraph if it contains both spaces and # multiple periods (we won't worry about other # punctuation marks). # - If the string is multiple paragraphs, return "page". # The string is a paragraph if it contains any newline # characters ("\n"). # #Hint: think carefully about what order you should check #these conditions in. # #Hint 2: remember, there exists a count() method that #counts the number of times a string appears in another #string. For example, "blah blah blah".count("blah") #would return 3.

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

Answer:

I am writing a Python program:

def string_type(string):

   if string=="":  //if the string is empty

       return "empty"

   elif string.count(".")>1:  #if the period sign occurs more than once in string

       if string.count("\n"):  #checks if the new line occurs in the string

           return "page"  #if both the above cases are true then its a page

       return "paragraph"  # if the period sign condition is true then its a para

   elif string.count(" ")>=1:  #if no of spaces in string occur more than once

       return "sentence"  #returns sentence

   elif len(string)==1:  # if length of the string is 1 this

       return "character"  #returns character

   else:  #if none of the above conditions is true then its a word

       return "word" #returns word

Explanation:

def string_type(string):  this is the definition of method string_type which takes a string as argument and determines whether the type of string is a word, paragraph, page, sentence or empty.

if string=="" this if condition checks if the string is empty. If this condition is true then the method returns "empty"

elif string.count(".")>1  This condition checks if the string type is a paragragh

string.count(".")>1   and if string.count("\n") both statements check if the string type is a page.

Here the count() method is used which is used to return the number of times a specified string or character appears in the given string.

Suppose the string is "Paragraphs need to have multiple sentences. It's true.\n However, two is enough. Yes, two sentences can make a paragraph."

The if condition first checks if count(".")>1 which means it counts the occurrence of period i.e. "." in the string. If the period occurs more than once this means it could be a page. But it could also be a paragraph so in order to determine the correct string type another if statement if string.count("\n") inside elif statement determines if the string is a page or not. This statement checks the number of times a new line appears in the string. So this distinguishes the string type paragraph from string type page.

elif string.count(" ")>=1: statement determines if the string is a sentence. For example if the string is "i love to eat apples." count() method counts the number of times " " space appears in the string. If the space appears more than once this means this cannot be a single word or a character and it has more than one words. So this means its a sentence.

  elif len(string)==1:  this else if condition checks the length of the string. If the length of the string is 1 this means the string only has a single character. Suppose string is "!" Then the len (string) = 1 as it only contains exclamation mark character. So the method returns "character" . If none of the above if and elif conditions evaluates to true then this means the string type is a word.

You might be interested in
What is the similarities between traditional and modernized presentation​
Ivenika [448]

Answer:

“Traditional” refers to those societies or elements of societies that are small-scale, are derived from indigenous and often ancient cultural practices. “Modern” refers to those practices that relate to the industrial mode of production or the development of large-scale often colonial societies.

6 0
2 years ago
I am trying to code a lifting simulator game on ro-blox. These problems listed in the third picture tells you the problems. If y
ipn [44]

Answer:

cool

Explanation:

3 0
3 years ago
Read 2 more answers
People often want to save money for a future purchase. You are writing a program to determine how much to save each week given t
GenaCL600 [577]
.................................
7 0
4 years ago
Read 2 more answers
You tell me if I only have middle school education then why do I have a 34 thousand dollar student loan bill?
motikmotik

Answer:

Explanation:

The number one thing that comes to my mind would be identity theft.

3 0
4 years ago
Which windows tool lets you perform automatic backups at regular times (such as on specific days at specific times)?.
arlik [135]

Answer:

Windows Automatic Backup Tool Which Windows tool lets you perform automatic backups at regular times? For regular backups, using third-party automatic file backup program is highly suggested. However, some of you choose to use File History or Windows Backup and Restore (Windows 7).

Explanation:

8 0
2 years ago
Other questions:
  • What are the three most important jobs of an operating system?
    8·1 answer
  • Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code? String element = "red"; f
    12·1 answer
  • A student sits down at a computer and presses the power button. the computer does not turn on. what is a logical explanation as
    8·1 answer
  • Why is information so important?
    6·2 answers
  • Jenae helps maintain her school web site and needs to create a web site poll for students. Which tool will she use? JavaScript H
    15·2 answers
  • Software refers to the physical parts of a computer.<br> a. True<br> b. False
    11·2 answers
  • Which is the purpose of adding B-Roll footage to a sequence?
    10·1 answer
  • I was designed to meet the computing needs of an individual. I was originally referred to as ___________________.
    7·1 answer
  • Imagine you are the human resource director for a small video game company that employs around 20 coders, managers, artists, etc
    12·1 answer
  • Select the correct locations on the image. Adrian wants to delve into database administration. Which certifications would help h
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!