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
Schach [20]
3 years ago
14

Given an alphanumeric string made up of digits and lower case ASCII characters only, find the sum of all the digit characters in

the string. The function signature for 'sum Digits' is -
(define sumDigits ...)

1. Constraints -
- Not allowed to use library functions. This means no import statement.
- No loops or list comprehension. The solution must be recursive.
- Do not use length of string in any function.


2. Input to function 'sumDigits -
- An alphanumeric string.


3. Output of function -
-Sum of all the digits in the string.


4. Sample test case
Test case 1
calling sumDigits ab1c2d3e54 outputs 15 = 1 + 2 + 3 + 5 + 4
Computers and Technology
1 answer:
lara31 [8.8K]3 years ago
3 0

Answer:

C++.

Explanation:

#include <iostream>

#include <string>

using namespace std;

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

int sumDigits(string alphanumeric) {

   if (alphanumeric.length() == 1) {

       if ((int(alphanumeric[0]) >= 48) && (int(alphanumeric[0]) <= 57)) {

           cout<<int(alphanumeric[0]) - 48<<endl;

           return (int(alphanumeric[0]) - 48);

       }

       else

           return 0;

   }

   else {

       if ((int(alphanumeric[0]) >= 48) && (int(alphanumeric[0]) <= 57)) {

           cout<<int(alphanumeric[0]) - 48<<endl;

           return int(alphanumeric[0]) - 48 + sumDigits(alphanumeric.substr(1, alphanumeric.length()-1));

       }

       else

           return 0 + sumDigits(alphanumeric.substr(1, alphanumeric.length()-1));

   }

}

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

int main() {

   cout<<"Sum: "<<sumDigits("ab1c2d3e54");

   return 0;

}

You might be interested in
I think these might be the answers but im not to sure, so am I right? or wrong? please help
mina [271]
Number 1 and 4 are the same? I agree with your answers.
4 0
3 years ago
Read 2 more answers
Papa Mario of Mario's Pizzeria has baked a huge pizza and cut it into n slices, but he is clumsy and the pizza wasn't evenly sli
rosijanka [135]

Suppose there are n student: 1, 2, 3, ..., i, ..., n.

Now, let's say each want slice size to be: t1, t2, ..., ti, ..., tn.

Let's pizza slices be : s1, s2, ..., si, ..., sn.

Now, it can be said that a student ' i ' will accept a slice of pizza ' si ' only if the size of slice is more then ' ti '.

Logic to distribute: what can be done is we can sort the demand i.e ti of students and also sort the size of pizza slices si. Now, Papa Mario can distribute the sorted slices to students sorted demand(smallest to heighest) one by one as they appear in sorted lists. Now, it will only be possible to distribute the slices to make everyone happy, if and only if in sorted lists of both s and t for each index k, it holds the condition: tk <= sk.

Let me explain you with example:

Lets says size of whole pizza is 100.

Now number of students n = 4.

Let there demands be : 20, 35, 15, 10.

This means 1st student atleast require the slice size of 20 and so on.

Case 1: Papa Mario divides the pizza into slices of size: 25, 20, 20, 35.

=> Now, we sort both lists.

Thus t => 10, 15, 20, 35

and s => 20, 20, 25, 35

Now, as we can see that for all index  tk <= sk, hence pizza can be distrubeted so that everyone can be happy.

Case 2: Papa Mario divides the pizza into slices of size: 30, 20, 20, 30.

=> Now, we sort both lists.

Thus t => 10, 15, 20, 35

and s => 20, 20, 30, 30

Now, as we can see that, for last student with demand of 35, the pizza slice alotted is of size 30, thus he is unhappy, and pizza cannot be distributed so that everyone becomes happy.

Now, after the approach, let's discuss question.

(a) Greedy algorithm paradigm is most appropriate for this problem, as what we are doing is greedily distributing small slices to those students which have least demands first then tackling bigger demands of students, by remaining bigger slices.

(b) As we are mainly sorting thel ist of sizes of pizza slices and student demands, thus we need to use an efficient algorithm to sort the lists. Such an efficient algorithm can be QuickSort() or MergeSort().

(c) Asymptotic running time of our algorithm would be O(n*logn).

3 0
3 years ago
A consumer-directed digital or computer device that has a video output signal for displaying a video game is called a .......
Allushta [10]
I think the first problem is B. and the second problem is D.
4 0
4 years ago
Hexadecimal (19BACDFE)H into Octal. Please write step by step.
lubasha [3.4K]
The H idk, but till E is i think 3156546776
4 0
4 years ago
Discuss the advantages of Database approach to systemdevelopment
Sergeu [11.5K]

Answer:

Explained

Explanation:

Improved Data Sharing and Data Security

Proper database management systems help increase organizational accessibility to data, which in turn helps the end users share the data quickly and effectively across the organization.  A management system helps get quick solutions to database queries, thus making data access faster and more accurate.

Effective Data Integration

Implementing a data management system enhances an integrated picture of an organization’s operations. It becomes easy to see how processes in one segment of the organization affect other segments.

Increased productivity of end user

Deploying a database management system, will always result in the increased productivity of users.It empowers the end user to make well informed and quick decisions that can contribute in success and failure of any organization.

4 0
3 years ago
Other questions:
  • What are two characteristics of a scalable network? (choose two.)?
    5·1 answer
  • If you had a chance to see what your life would be like in 20 years
    8·1 answer
  • What are the two main advantages to a well-designed relational database over other types?
    10·1 answer
  • Pick the simplest line of code to test if the word "BASIC" is stored in the variable text1.
    12·1 answer
  • What information should the subtitles in the clips of your video per folio provide?
    14·1 answer
  • What is the advantage of postfix notation?
    6·1 answer
  • OkkKkkkkkEYEYYEYEYEY BYEYYEYEYEyeeyeyyeEYEy if you know you know
    11·1 answer
  • Ask the user to input their grade percentage (e.g. the use will enter 98 if they had an overall grade of 98% in their course) fo
    6·1 answer
  • Who is the owner of apple company??​
    13·2 answers
  • Carl wants to add two new characters to the extraterrestrial battleship game he's
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!