98 925
Assignments Done
98.6%
Successfully Done
In April 2021

# 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 &lt;count&gt;', where &lt;count&gt; is the number passed in. However, if the count is 5 or more, then use the string 'and &lt;num&gt; more donuts' as the last item instead of the actual list of donuts, where &lt;num&gt; 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 &gt;= 5:
count -= 4
numDonuts = 'and ' + str(count) + ' more donuts'
else:
numDonuts = 'donut ' + str(count)
return numDonuts

output:
&lsquo;donut 1&rsquo;
&lsquo;donut 1,donut 2,donut 3&rsquo;
&lsquo;donut 1,donut 2,donut 3,donut 4&rsquo;
&lsquo;donut 1,donut 2,donut 3,donut 4 and 6 more donuts&rsquo;
&lsquo;donut 1,donut 2,donut 3,donut 4 and 95 more donut
1
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!