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
fenix001 [56]
3 years ago
6

Write a function add_spaces(s) that takes an arbitrary string s as input and uses recursion to form and return the string formed

by adding a space between each pair of adjacent characters in the string. If the string has fewer than two characters, it should be returned without any changes. Here are three examples: >>> add_spaces('hello') result: 'h e l l o' >>> add_spaces('hangman') result: 'h a n g m a n' >>> add_spaces('x') result: 'x' This function is somewhat similar to the the replace function from lecture, because it needs to recursively create a new string from an existing string. However, your function will be simpler, because it won’t need to decide what to do after the recursive call returns.
Computers and Technology
2 answers:
kvasek [131]3 years ago
5 0

Answer:

The solution code is written in Python:

  1. def add_spaces(s):
  2.    if len(s) < 2:
  3.        return s  
  4.    else:
  5.        return s[0] + " " + add_spaces( s[1 : ] )

Explanation:

Recursive function is a function that will call itself within the same function.

Let create a function named add_spaces() that take one input string, s (Line 1).

Next, create an if condition to check if the length of the input string is less than 2 (this means if the string has only one character), return the current string (Line 2-3).

Otherwise, it should return the first character of string, s[0] concatenated with a single space " " and concatenated again with the return output from the recursive calling add_spaces() that take s[1: ] as input parameter (Line 4-5). Please note s[1: ] is an expression we get the substring of the current string from position 1 till the end of string.

PtichkaEL [24]3 years ago
5 0

Answer:

C++.

#include <iostream>

#include <string>

using namespace std;

///////////////////////////////////////////////////////////////////

string add_spaces(string input) {

   if (input.length() < 2)

       return input;

   else {

       string minus_input = input.substr(0, input.length()-1);

       return add_spaces(minus_input) + " " + input[input.length()-1];

   }

}

///////////////////////////////////////////////////////////////////

int main() {

   cout<<add_spaces("hangman");

   return 0;

}

///////////////////////////////////////////////////////////////////

Explanation:

A Recursive function calls itself inside its body. To prevent it from running forever, there's a break condition inside recursive functions.

In our example it was a single character string.

We use recursion to solve problems by breaking them into smaller pieces.

In our example, we broke down the original string from the last character, till it only has its starting character. When the last character was encountered, we returned as it is in the break condition.

From there onward, all characters were getting separated by a space, till we reach the first add_spaces function call, where we return the full string with space between them.

You might be interested in
Alpha Technologies, a newly established company, wants to share information about its work with people all over the world. Which
allochka39001 [22]
It should use the web server to be accessible to users of World Wide Web across the world
6 0
4 years ago
What is the maximum ream size on an agile project?
yarga [219]

Most Agile and Scrum training courses refer to a 7 +/- 2 rule, that is, agile or Scrum teams should be 5 to 9 members. Scrum enthusiasts may recall that the Scrum guide says Scrum teams should not be less than 3 or more than 9

Explanation:

6 0
3 years ago
Write a program to sort the (name, age, score) tuples by descending order where name is string, age and score are numbers. The s
zimovet [89]

Answer:

The program in Python is as follows:

from operator import itemgetter

m = int(input("Number of records: "))

print("Name, Age, Score")

my_list =[]

for i in range(m):

   user_details = input()

   my_list.append(tuple((user_details.split(","))))

my_list.sort(key =  itemgetter(0, 1, 2))        

print("Sorted: ", my_list)

Explanation:

This imports the operator function from itemgetter

from operator import itemgetter

This gets the number of records, m

m = int(input("Number of records: "))

This prints the format of input

print("Name, Age, Score")

This initializes the list of tuples

my_list =[]

This iterates through m

for i in range(m):

This gets the details of each person

   user_details = input()

This appends the details to the tuple

   my_list.append(tuple((user_details.split(","))))

This sorts the tuple

my_list.sort(key =  itemgetter(0, 1, 2))        

This prints the sorted tuple

print("Sorted: ", my_list)

6 0
3 years ago
Which of the following is true of two-factor authentication?
dybincka [34]

Answer: D)It relies on two independent proofs of identity.

Explanation: Two factor authentication is technique which uses two steps/stage for the verification or authentication.It is done for the extra security maintenance.IT is also known as 2FA. It consist of two different authentication component so that it penetrates through double protection layer .

Other options are incorrect because It RSA public key cannot be used for the authentication of private content. It does not use both hand geometry at same time for both the levels and It does not utilize only single sign-on technology. Thus, the correct option is option(D).

7 0
4 years ago
The main activity area or the brain of the computer is called the ________
Darya [45]

Motherboard

Its a computer chip



6 0
3 years ago
Other questions:
  • Identify requirements that should be considered when determining the locations and features of firewalls. What are some importan
    5·1 answer
  • In order to plan George’s birthday, his father gave him a list of people who attended his birthday for the last five years. What
    8·2 answers
  • A network that typically reaches a few meters, such as up to 10 meters (33 feet), and consists of personal devices such as mobil
    14·1 answer
  • jeff wants to create a website with interactive and dynamic content. Which programming language will he use?
    11·2 answers
  • Write a program name Dollars that calculates and displays the conversion of an entered number of dollars into currency denominat
    11·1 answer
  • Which of the following might an interior designer in the retail industry specialize in?
    9·1 answer
  • 7.
    15·1 answer
  • Which is the purpose of adding B-Roll footage to a sequence?
    10·1 answer
  • Use your editor to open the cc_data.js file and study the data stored in the staff object to become familiar with its contents a
    6·1 answer
  • Can a dod activity enter into a service contract for a military flight simulator without getting a waiver from the secretary of
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!