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
Y_Kistochka [10]
3 years ago
5

#We've started a recursive function below called #measure_string that should take in one string parameter, #myStr, and returns i

ts length. However, you may not use #Python's built-in len function. # #Finish our code. We are missing the base case and the #recursive call. # #HINT: Often when we have recursion involving strings, we #want to break down the string to be in its simplest form. #Think about how you could splice a string little by little. #Then think about what your base case might be - what is #the most basic, minimal string you can have in python? # #Hint 2: How can you establish the base case has been #reached without the len() function? #You may not use the built-in 'len()' function.

Computers and Technology
1 answer:
Roman55 [17]3 years ago
6 0

Answer:

Here is the Python program:  

def measure_string(myStr): #function that takes a string as parameter  

 if myStr == '': #if the string is empty (base case)  

      return 0  #return 0  

  else: #if string is not empty  

      return 1 + measure_string(myStr[0:-1]) #calls function recursively to find the length of the string (recursive case)  

#in order to check the working of the above function the following statement is used    

print(measure_string("13 characters")) #calls function and passes the string to it and print the output on the screen      

Explanation:

The function works as following:  

Suppose the string is 13 characters  

myStr = "13 characters"  

if myStr == '': this is the base case and this does not evaluate to true because myStr is not empty. This is basically the alternate of  

if len(myStr) == 0: but we are not supposed to use len function here so we use if myStr == '' instead.  

So the program control moves to the else part  

return 1 + measure_string(myStr[0:-1])  this statement is a recursive call to the function measure_string.  

myStr[0:-1] in the statement is a slice list that starts from the first character of the myStr string (at 0 index) to the last character of the string (-1 index)  

This statement can also be written as:  

return 1 + measure_string(myStr[1:])

or  

return 1 + measure_string(myStr[:-1])  This statement start from 1st character and ends at last character  

This statement keeps calling measure_string until the myStr is empty. The method gets each character using a slice and maintains a count by adding 1 each time this statement is returned.The function breaks string into its first character [0:] and all the rest characters [:-1]. and recursively counts the number of character occurrences and add 1. So there are 13 characters in the example string. So the output is:  

13

You might be interested in
Why did LISD had to block the game “among us”?
frosja888 [35]
Lewisville Independent School District blocked the following program: Among Us because the game was a distraction to online education. Normally, school districts disable/block games that students play to make sure they are not just playing games and that they are paying attention during class.
3 0
3 years ago
Read 2 more answers
You are working at the help desk and you get a message that a user cannot access the internet. you open a command prompt, ping t
Kaylis [27]

Answer:

The ethernet cable.

Explanation:

The connection to the switch or router might be disrupted with the disconnection of the cable. Ask the user to check if the ethernet cable is properly connected.

4 0
2 years ago
You can apply several different worksheet themes from which tab?
Lemur [1.5K]
<span>D. Page Layout  i hope this helps </span>
8 0
3 years ago
Read 2 more answers
Whoever answers first gets lots of points
zimovet [89]
Firssstttttt ayyyyyyeeeeeeeeeeeee
8 0
3 years ago
Read 2 more answers
A search engine that crawls uses
RoseWind [281]

Answer:

web crawlers or spider software

Explanation:

A search engine that crawls uses the web crawler or the spider software. And it also uses the server side JavaScript based crawlers and programming languages like Python and PHP. The search engine finds the content category through the meta keywords and directs the page to the index. The index contains details of similar types of pages, And each page has one category, and if category is not found which is rarest, a new category is being created.

7 0
4 years ago
Other questions:
  • A communications system connecting two or more computers is called a(n) ________.
    7·1 answer
  • Which tab provide text formatting features
    11·1 answer
  • An interpreter _______. converts the procedural language one statement at a time converts the source code into machine language
    12·1 answer
  • Give two advantages of representing integers in pure binary.
    5·1 answer
  • 100 tickets are sold in a raffle with one prize. John has 8 tickets, Jamie has 10 tickets, find the probability that John wins
    11·2 answers
  • 14. Which of the following is NOT a
    6·2 answers
  • Because all the IEEE WLAN features are isolated in the PHY and ____________ layers, practically any LAN application will run on
    11·1 answer
  • Why does it keep saying this when I’m putting my right credit card number
    6·1 answer
  • Where can formatting features be found ?! Need help asp !‍♀️
    9·1 answer
  • Computer is a major source of informarion why​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!