Question: Rewrite increment the same way. -- The reading for this question is po
ID: 3619805 • Letter: Q
Question
Question: Rewrite increment the same way.--
The reading for this question is posted below:
---
Prototype development versus planning:
In this chapter, we demonstrated an approach to program development that we call
prototype development. In each case, we wrote a rough draft (or prototype)
that performed the basic calculation and then tested it on a few cases, correcting
flaws as we found them.
Although this approach can be effective, it can lead to code that is unnecessarily
complicated—since it deals with many special cases—and unreliable—since it is
hard to know if you have found all the errors.
An alternative is planned development, in which high-level insight into the
problem can make the programming much easier. In this case, the insight is that
a Time object is really a three-digit number in base 60! The second component is
the “ones column,” the minute component is the “sixties column,” and the hour
component is the “thirty-six hundreds column.”
When we wrote addTime and increment, we were effectively doing addition in
base 60, which is why we had to carry from one column to the next.
This observation suggests another approach to the whole problem—we can convert
a Time object into a single number and take advantage of the fact that the computer knows how to do arithmetic with numbers. The following function converts a Time object into an integer:
def convertToSeconds(t):
minutes = t.hours * 60 + t.minutes
seconds = minutes * 60 + t.seconds
return seconds
Now, all we need is a way to convert from an integer to a Time object:
def makeTime(seconds):
time = Time()
time.hours = seconds // 3600
time.minutes = (seconds%3600) // 60
time.seconds = seconds%60
return time
You might have to think a bit to convince yourself that this function is correct.
Assuming you are convinced, you can use it and convertToSeconds to rewrite
addTime: def addTime(t1, t2):
seconds = convertToSeconds(t1) + convertToSeconds(t2)
return makeTime(seconds)
This version is much shorter than the original, and it is much easier to demonstrate
that it is correct. Question: Rewrite increment the same way.
--
The reading for this question is posted below:
---
Prototype development versus planning:
In this chapter, we demonstrated an approach to program development that we call
prototype development. In each case, we wrote a rough draft (or prototype)
that performed the basic calculation and then tested it on a few cases, correcting
flaws as we found them. Although this approach can be effective, it can lead to code that is unnecessarily
complicated—since it deals with many special cases—and unreliable—since it is
hard to know if you have found all the errors.
An alternative is planned development, in which high-level insight into the
problem can make the programming much easier. In this case, the insight is that
a Time object is really a three-digit number in base 60! The second component is
the “ones column,” the minute component is the “sixties column,” and the hour
component is the “thirty-six hundreds column.”
When we wrote addTime and increment, we were effectively doing addition in
base 60, which is why we had to carry from one column to the next.
This observation suggests another approach to the whole problem—we can convert
a Time object into a single number and take advantage of the fact that the computer knows how to do arithmetic with numbers. The following function converts a Time object into an integer: def convertToSeconds(t):
minutes = t.hours * 60 + t.minutes
seconds = minutes * 60 + t.seconds
return seconds Now, all we need is a way to convert from an integer to a Time object:
def makeTime(seconds):
time = Time()
time.hours = seconds // 3600
time.minutes = (seconds%3600) // 60
time.seconds = seconds%60
return time You might have to think a bit to convince yourself that this function is correct.
Assuming you are convinced, you can use it and convertToSeconds to rewrite
addTime: def addTime(t1, t2):
seconds = convertToSeconds(t1) + convertToSeconds(t2)
return makeTime(seconds) This version is much shorter than the original, and it is much easier to demonstrate
that it is correct.
Explanation / Answer
class time:hours = 12;
minutes = 00;
seconds = 00;
def convertToSeconds(t):
minutes = t.hours * 60 + t.minutes
seconds = minutes * 60 + t.seconds
return seconds;
def makeTime(seconds):
time1 = time()
time1.hours = (seconds/3600)
time1.minutes = (seconds%3600)/60
time1.seconds = (seconds%3600)%60
return time1
def addTime(t1, t2):
seconds = convertToSeconds(t1) + convertToSeconds(t2)
return makeTime(seconds)
t2= time();
t1= time();
t1.hours = 2
t2.minutes=10
t1.minutes = 11;
t1.seconds = 11;
t2.hours = 1;
t2.seconds = 34;
t3 = addTime(t1,t2)
print "Result Time:"+str(t3.hours)+":"+str(t3.minutes)+":"+str(t3.seconds);
raw_input();
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.