Python Forum
accumulator pattern - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: accumulator pattern (/thread-2357.html)



accumulator pattern - python_learner - Mar-09-2017

Write a programm which sums up the first 100 even numbers. + comments


RE: accumulator pattern - wavic - Mar-09-2017

Show us what have you done so far.


RE: accumulator pattern - Ofnuts - Mar-09-2017

x=2+4+6+8+[....]+198+200
Filling the dots is left as an exercise to the reader.

If you are mathematically inclined:
x=2*((100*101)*2)



RE: accumulator pattern - zivoni - Mar-09-2017

Somehow I doubt that it will be accepted as a satisfactory solution of programming homework (even if OP's name is Carl Gauss). The assignment is not too precise, so there is a question of allowing zero.

Last * probably means /.


RE: accumulator pattern - Skaperen - Mar-10-2017

(Mar-09-2017, 11:20 PM)zivoni Wrote: The assignment is not too precise, so there is a question of allowing zero.

Output:
>>> sum([x for x in range(2,102,2)]) == sum([x for x in range(0,102,2)]) True
it gets the same sum, either way.


RE: accumulator pattern - Ofnuts - Mar-10-2017

(Mar-09-2017, 11:20 PM)zivoni Wrote: Somehow I doubt that it will be accepted as a satisfactory solution of programming homework (even if OP's name is Carl Gauss). The assignment is not too precise, so there is a question of allowing zero.
A philosophy teacher would have excuses, but not a programming teacher.

Quote:Last * probably means /.
Yes


RE: accumulator pattern - zivoni - Mar-10-2017

I was thinking about some "pragmatic" teacher -  one who have just explained for (or while) loop in a class and expects to see it used in the following homework.

Quote:
Output:
>>> sum([x for x in range(2,102,2)]) == sum([x for x in range(0,102,2)]) True
it gets the same sum, either way.
The right expression is a sum of one more number than the left one.

But yes, it was nitpicking. Anyone who is able to sum numbers from 1 to 100 should be able to sum numbers from 0 to 99.

EDIT: I have misunderstood Ofnuts' comment - it was about zero, not about accepting solution given by a single expression. And he is right, it shouldnt matter from the programming point of view.


RE: accumulator pattern - Ofnuts - Mar-10-2017

(Mar-10-2017, 09:36 AM)zivoni Wrote: I was thinking about some "pragmatic" teacher -  one who have just explained for (or while) loop in a class and expects to see it used in the following homework.

Quote:
Output:
>>> sum([x for x in range(2,102,2)]) == sum([x for x in range(0,102,2)]) True
it gets the same sum, either way.
The right expression is a sum of one more number than the left one.

But yes, it was nitpicking. Anyone who is able to sum numbers from 1 to 100 should be able to sum numbers from 0 to 99.

EDIT: I have misunderstood Ofnuts' comment - it was about zero, not about accepting solution given by a single expression. And he is right, it shouldnt matter from the programming point of view.

No, your first understanding was the correct one :). If you teach computer science you have to be accurate. What the specs don't forbid is allowed. Nothing should be "implicit". A long time ago I worked on a proposal for a device automatically calling for assistance in case of a car crash. The car speed was sampled at 2kHz or so, so the acceleration was the difference between two consecutive samples and the "crash" itself was detected when the average of the last 200 accelerations was above some value. Computing this fast enough wasn't that easy on the available microcomputers of the era. Until you figure out that the average of all the deltas is just the last value minus the first value divided by the number of samples.


RE: accumulator pattern - zivoni - Mar-10-2017

OK :)

Yes, OP's demand for program and comment is fullfilled by
print(50*202)  # sums even numbers from 2 to 200
But if his post is not a precise transcription of homework specs, then there is possibility that OP gets what he asks for, not what he wants. And without his cooperation its rhetorical question.