Answer to Question #22719 in Python for James

Question #22719
Given an int count of a number of donuts, return a string of the form 'donut 1,donut 2,..., donut <count>', where <count> is the number passed in. However, if the count is 5 or more, then use the string 'and <num> more donuts' as the last item instead of the actual list of donuts, where <num> is the number of remaining donuts

So
donuts(3) returns 'donut 1,donut 2,donut 3'
donuts(4) returns 'donut 1,donut 2,donut 3,donut 4'
donuts(10) returns 'donut 1,donut 2,donut 3,donut 4,and 6 more donuts'
donuts(20) also returns 'donut 1,donut 2,donut 3,donut 4,and 16 more donuts'
Python functions: range(), str()

I have this so far, but I can't get the donut 1,donut 2, etc.

def donuts(count):
if count >= 5:
count -= 4
numDonuts = 'and ' + str(count) + ' more donuts'
else:
numDonuts = 'donut ' + str(count)
return numDonuts

output:
‘donut 1’
‘donut 1,donut 2,donut 3’
‘donut 1,donut 2,donut 3,donut 4’
‘donut 1,donut 2,donut 3,donut 4 and 6 more donuts’
‘donut 1,donut 2,donut 3,donut 4 and 95 more donut
1
Expert's answer
2013-01-24T08:53:21-0500
Program is written on C++, using files.
#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream in("input.txt");
ofstream out("output.txt");
for (int j=0; j<20; j++) {
int count;
in >> count;
if (count<5) {
for (int i=1;
i<=count; i++) {
if
(i==count) {

out << "donut " << i;
}

else {

out << "donut " << i <<
",";
}
}
}
else {
for (int i=1; i<5;
i++) {
out
<< "donut " << i << ",";
}
count-=4;
out << "and
" << count << " more donuts";
}
out << endl;
}
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS