Data Structure and algorithm Python language description Chapter 2 first question (python version), Introduction to algorithms Chapter 2 answer

Source: Internet
Author: User

Data Structure and algorithm Python language description Chapter 2 first question (python version), Introduction to algorithms Chapter 2 answer

Question:
Defines a Time class
A) Time (hours, minutes, seconds) to create a Time object;
B) t. hours (), t. minutes (), t. seconds () respectively return the hour, minute, and second values of the time object t.
C) define addition and subtraction operations for the Time object (using operators + and -)
D) define the equal and less than relational objects of time objects (using the operators = and <)

 

1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 "5 defines a Time class 6 a) Time (hours, minutes, seconds) create a time object; 7 B) t. hours (), t. minutes (), t. seconds () respectively returns the hour of the Time object t, the minute and the second value 8 c) defines addition and subtraction operations for the Time object (using the operator + and-) 9 d) defines the equal and less than the relational objects of a Time object (with the operator ==and <) 10 11 ADT Time: # defines the abstract data type of Time 12 Time (self, int hours, int minutes, int seconds) # construct a Time object 13 + (self, Time t2) # obtain the result of adding t2 to this object 14-(self, Time t2) # obtain the result 15 = (self, Time t2) # Whether the object is equal to t2 16 <(self, Time t2) # This object is smaller than t2 17 hours (self) # retrieve the object's hour 18 minutes (self) # retrieve the object's minute 19 seconds (self) # retrieve the object's second value 20 "21" 22 Author: Minion Xu 23 Time: "25 26 class Time (object): 27 _ slots _ = ('_ urs',' _ minutes ',' _ seconds ') 28 29 # creation time object 30 def _ init _ (self, hours = 0, minutes = 0, seconds = 0): 31 # type judgment 32 if not isinstance (hours, int) or not isinstance (minutes, int) o R not isinstance (seconds, int): 33 raise TypeError 34 35 if (0 <= hours <= 23): 36 self. _ hours = hours 37 if (0 <= minutes <= 59): 38 self. _ minutes = minutes 39 if (0 <= seconds <= 59): 40 self. _ seconds = seconds 41 else: 42 raise ValueError ("% d is not valid seconds! "% Seconds) 43 else: 44 raise ValueError (" % d is not valid minutes! "% Minutes) 45 46 else: 47 raise ValueError (" % d is not valid hours! "% Hours) 48 49 50 # + 51 def _ add _ (self, other): 52 seconds_add = self. _ seconds + other. _ seconds 53 seconds_carry = seconds_add // 60 # Carry 54 seconds = seconds_add % 60 # second value 55 minutes_add = self. _ minutes + other. _ minutes + seconds_carry 56 minutes_carry = minutes_add // 60 # carry more than 60 minutes 57 minutes = minutes_add % 60 # minute 58 hours_add = self. _ hours + other. _ hours + minutes_carry 59 if (hours_add <24): 60 hours = hours_add 61 else: 62 hours = hours_add-24 #63 return Time (hours, minutes, seconds) 64 #-65 def _ sub _ (self, other): 66 if self. _ seconds <other. _ seconds: 67 self. _ minutes-= 1 68 seconds = self. _ seconds + 60-other. _ seconds 69 else: 70 seconds = self. _ seconds-other. _ seconds 71 if self. _ minutes <other. _ minutes: 72 self. _ hours-= 1 73 minutes = self. _ minutes + 60-other. _ minutes 74 else: 75 minutes = self. _ minutes-other. _ minutes 76 if self. _ hours <other. _ hours: 77 hours = self. _ hours + 24-other. _ hours 78 else: 79 hours = self. _ hours-other. _ hours 80 return Time (hours, minutes, seconds) 81 # = 82 def _ eq _ (self, other ): 83 bool1 = False 84 bool2 = False 85 bool3 = False 86 if self. _ hours = other. _ hours: 87 bool1 = True 88 if self. _ minutes = other. _ minutes: 89 bool2 = True 90 if self. _ seconds = other. _ seconds: 91 bool3 = True 92 return bool1 and bool2 and bool3 93 94 #<95 def _ lt _ (self, other): 96 if self. _ hours <other. _ hours: 97 return True 98 elif self. _ hours = other. _ hours: 99 if self. _ minutes <other. _ minutes: 100 return True101 elif self. _ minutes = other. _ minutes: 102 if self. _ seconds <other. _ seconds: 103 return True104 else: 105 return False106 else: 107 return False108 else: 109 return False110 111 def hours (self): 112 return self. _ hours113 114 def minutes (self): 115 return self. _ minutes116 117 def seconds (self): 118 return self. _ seconds119 120 def print (self): 121 print (self. _ hours, ":", self. _ minutes, ":", self. _ seconds) 122 123 def _ str _ (self): 124 return str (self. _ hours) + ":" + str (self. _ minutes) + ":" + str (self. _ seconds) 125 126 if _ name _ = '_ main _': 127 t = Time (128, 31, 59) t1 = Time (, 41) 129 he = t + t1130 cha = t-t1131 print (he) 132 print (cha) 133 print (t = t1) 134 print (t <t1) 135 print (he. hours ())

 

 
 
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.