simple nextDay procedure, that assuming every month has 30 days.
For example:
### nextDay(1999, 12, 30) => (2000, 1, 1)
### nextDay(2013, 1, 30) => (2013, 2, 1)
### nextDay(2012, 12, 30) => (2013, 1, 1) (even though December really has 31 days)
def nextDay(year, month, day): """warning: this version incorrectly assumues all months have 30days!""" if month == 12 and day == 30: return year + 1, 1, 1 elif month < 12: return year, month + 1, 1 else: return year, month, day + 1
day between date
def nextDay(year, month, day): if day < 30: return year, month, day + 1 else: if month == 12: return year + 1, 1, 1 else: return year, month + 1, 1 def deteIsBefore(year1, month1, day1, year2, month2, day2): if year1 < year2: return True if year1 == year2: if month1 < month2: return True if month1 == month2: return day1 < day2 return False def daysBetweenDates(year1, month1, day1, year2, month2, day2): days = 0 while dateIsBefoer(year1, month1, day1,year2, month2, day2): year1, month1, day1 = nextDayMonth(year1, month1, day1) days += 1 return days
how_many_days, representing a month, and returns the number of days in that month.
days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31] def how_many_days(month_number): return days_in_month[month_number - 1]
capital of Indiaby accessing the list
countries = [['China','Beijing',1350], ['India','Delhi',1210], ['Romania','Bucharest',21], ['United States','Washington',307]] print(countries[1][1])
replace spy code name with list
spy = [0,0,7] def replace_spy(p): p[2] = p[2] + 1 replace_spy(spy) print(spy)
List operation
p = [1, 2] q = [3, 4] p.append(q) q[1] = 5 print(p)
2GB memory that means "2 ** 30 * 2 * 8(bit)", 1 bit have a light switch , so kind of this shows 17 billion light switch
print 2 ** 10 print 2 ** 20 print 2 ** 30 print 2 ** 40
Memory Hierarchy
Look at the latency-distance, speed of light is about 300,000km, CPU Register is 0.12m, and DRAM is 3.6m, harddrive is 2.98km.
So, if you writing a program, you have to think about the latency-distance of each memory that, harddrive is much much far than register and DRAM.
sum list
def sum_list(p): result = 0 for e in p: result = result + e return result print(sum_list([1, 4, 7]))
define first character U by for loop
def measure_udacity(p): count = 0 for e in p: if p[0] == 'U': count = count + 1 return count
find element in the list procedure
def find_element(p, t) i = 0 for i < len(p): if p[i] == t: return i i = i + 1 return -1
find_element, using index that takes as its inputs a list and a value of any type, and returns the index of the first element in the input list that matches the value.
def find_elment(p, t): if t in p: return p.index(t) else: return -1
union that takes as inputs two lists. It should modify the first input list to be the set union of the two lists, assume the first list is a set, that is, it contains no repeated elements.
def union(p, q): for e in q: if e not in p: p.append(e)