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
Why would you use a database system instead of traditional file system
iris [78.8K]

Answer:

Traditional database system is the system that contains all the data in the computer files on a computer system. The organization of the data is done in the form of files sets. In this system, large amount of data is not properly accessible due to inefficiency and also consumes lot of time.

Instead of the traditional system,database system is used, which has the collection of the data that gets stored in the database management system(DBMS).They can be managed by the operating system.

They have qualities like consuming large amount of data, any type of information, independent to work ,less time-taking development of application for etc. which is not present in traditional system.Thus database system are chosen over the traditional system.

4 0
3 years ago
Use the drop-down menus to select the type of goal described in each statement. I want to finish all of my social studies homewo
nalin [4]

Answer:

I want to finish all of my social studies homework by dinner. - <u>Short term goal. </u>

Short term goals are targets that are to be achieved in the near and immediate future such as in a day or in a week. As the goal listed is to be done on the same day, it is a short term goal.

I want to graduate in three years in the top 15% of my class.  - <u>Normative Goal</u>

Normative goals are the goals people set based on what society looks upon as desirable and a good thing to do. Academic excellence is a normative goal as it is looked upon favorably by society.

I want to earn a good grade on my math test tomorrow. - <u>Intrapersonal goals.</u>

Intrapersonal goals are goals that we set within ourselves to motivate ourselves to do better. The writer here wants to do a good job in the math test which makes it intrapersonal.

I want to qualify for Honors Spanish next year. - <u>Long term goal</u>

A long term goal is one that is to be achieved in the future most likely after a period of a year. Qualifying for an Honors in Spanish in the next year is therefore a long term goal.

4 0
4 years ago
OBJECTIVE This project will familiarize you with the use of pthreads, pthread mutexes and pthread condition variables. Your prog
zysi [14]

Answer:

Check the explanation

Explanation:

/*

This program will simulate a tunnel with a limit of how many cars may move through it,

and will also simulate the cars going north and south-bound through the tunnel. We will

use pthreads, pthread mutexes, and pthread condition variables to simulate this.

*/

#include <iostream>

#include <fstream>

#include <sstream>

#include <string>

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <pthread.h>

#include <string.h>

#include <signal.h>

#include <sys/types.h>

using namespace std;

#define MAXTHREADS 128

static int maxCarsInTunnel;

static int maxNCarsInTunnel;

static int maxSCarsInTunnel;

static int totalNCars;

static int totalSCars;

static int currentCarsInTunnel;

static int currentNCarsInTunnel;

static int currentSCarsInTunnel;

static int numCarsWaiting;

static pthread_mutex_t lockAccess = PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *northCar(void *arg);

void *southCar(void *arg);

struct car {

int carNo;

int travelTime;

bool waited;

};

int main() {

pthread_t cartid[MAXTHREADS];

int storage = 0;

cin >> maxCarsInTunnel;

cin >> maxNCarsInTunnel;

cin >> maxSCarsInTunnel;

cout << "Maximum number of cars in the tunnel: " << maxCarsInTunnel << endl;

cout << "Maximum number of northbound cars: " << maxNCarsInTunnel << endl;

cout << "Maximum number of southbound cars: " << maxSCarsInTunnel << endl;

unsigned int arrival_time;

char direction;

unsigned int traversal_time;

struct car argList;

while (cin >> arrival_time >> direction >> traversal_time) {

sleep(arrival_time);

 

argList.travelTime = traversal_time;

argList.waited = false;

if (direction == 'N') {

totalNCars++;

argList.carNo = totalNCars;

pthread_create(&cartid[storage], NULL, northCar, (void *) &argList);

}

else if (direction == 'S') {

totalSCars++;

argList.carNo = totalSCars;

pthread_create(&cartid[storage], NULL, southCar, (void *) &argList);

}

storage++;

}

for (int i = 0; i < storage; i++) {

pthread_join(cartid[i], NULL);

}

cout << totalNCars << " northbound car(s) crossed the tunnel." << endl;

cout << totalSCars << " southbound car(s) crossed the tunnel." << endl;

cout << numCarsWaiting << " car(s) had to wait." << endl;

return 0;

}

void *northCar(void* arg) {

int tunnelTime;

int carNum;

bool wait;

struct car *argptr;

argptr = (struct car *) arg;

tunnelTime = argptr->travelTime;

carNum = argptr->carNo;

wait = argptr->waited;

pthread_mutex_lock(&lockAccess);

cout << "Northbound car # " << carNum << " arrives at the tunnel." << endl;

while (currentNCarsInTunnel >= maxNCarsInTunnel || currentCarsInTunnel >= maxCarsInTunnel) {

if (wait == false) {

wait = true;

numCarsWaiting++;

}

pthread_cond_wait(&cond, &lockAccess);

}

 

currentNCarsInTunnel++;

currentCarsInTunnel++;

cout << "Northbound car # " << carNum << " enters the tunnel." << endl;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&lockAccess);

sleep(tunnelTime);

pthread_mutex_lock(&lockAccess);

cout << "Northbound car # " << carNum << " exits the tunnel." << endl;

currentNCarsInTunnel--;

currentCarsInTunnel--;

 

pthread_cond_broadcast(&cond);

pthread_mutex_unlock(&lockAccess);

}

void *southCar(void* arg) {

int tunnelTime;

int carNum;

bool wait;

struct car *argptr;

argptr = (struct car *) arg;

tunnelTime = argptr->travelTime;

carNum = argptr->carNo;

wait = argptr->waited;

pthread_mutex_lock(&lockAccess);

cout << "Southbound car # " << carNum << " arrives at the tunnel." << endl;

while (currentSCarsInTunnel >= maxSCarsInTunnel || currentCarsInTunnel >= maxCarsInTunnel) {

if (wait == false) {

wait = true;

numCarsWaiting++;

}

pthread_cond_wait(&cond, &lockAccess);

}

currentSCarsInTunnel++;

currentCarsInTunnel++;

cout << "Southbound car # " << carNum << " enters the tunnel." << endl;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&lockAccess);

sleep(tunnelTime);

pthread_mutex_lock(&lockAccess);

cout << "Southbound car # " << carNum << " exits the tunnel." << endl;

currentSCarsInTunnel--;

currentCarsInTunnel--;

pthread_cond_broadcast(&cond);

pthread_mutex_unlock(&lockAccess);

}

Kindly check the attached images below to see the code screenshot and code output.

8 0
4 years ago
The component of an information system consisting of raw facts is the definition for which of the following?
julsineya [31]

Answer:

The answer to this question is: Information

An information system is a set of command that i used to analyze available information in order to create a better decision for the company.

Which means that information will not be able to do any function of it does not possess the initial information, to begin with, so we can conclude that information is a major component of the information systems. hope this makes sense in your mind lol

Explanation:

4 0
3 years ago
Read 2 more answers
When does the following while-loop stop running?
8090 [49]

The loop never stops running because the value of var is always 1.

5 0
3 years ago
Other questions:
  • How much power is consumed by a 1.4A motor connected to a 12V battery?​
    11·1 answer
  • In your presentation you added a text box to?
    5·1 answer
  • What is the name of the process that is used to establish whether or not a user’s identity is legitimate?
    7·1 answer
  • I need to create a function that returns Pascal's Triangle with n rows, where n is an int argument.
    12·1 answer
  • What is the value of vals[4][1]? double[][] vals = {{1.1, 1.3, 1.5}, {3.1, 3.3, 3.5}, {5.1, 5.3, 5.5}, {7.1, 7.3, 7.5}};
    6·1 answer
  • What is the function of napier's bones<br>​
    8·1 answer
  • 2min speech on can teachers be replace by technology​
    5·1 answer
  • How many passes will it take to find the five in this list?
    14·2 answers
  • True or False. The 'C' programming language guarantees that R0 will always contain a zero value on the ATmega328P
    6·1 answer
  • 10. Site-to-Site VPN architecture is also known as
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!