NextDay Procedure

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)

speed of light

speed_of_light = 299792458
billionth = 1.0 /1000000000
nanostick = speed_of_light * billion
print nanostick

variable changes with faster processer, even in the same expression, divede by cycles_per_second.

speed_of_light = 299792458
cycles_per_second = 2700000000. # 2.7GHz

cycle_distance = speed_of_light / cycles_per_second

cycles_per_second = 2800000000. # 2.8GHz
print(cycle_distance)

cycle_distance = speed_of_light / cycles_per_second
print(cycle_distance)

Finding Strings in strings

pythagoras = 'There is geometry in the humming of the string, there is music in the spacing of the spheres'
print(pythagoras.find('string'))
print(pythagoras[40:])
print(pythagoras.find('algebra'))

danton = "De l'audance, encore de l'audace, toujours de l'audace"
print(danton.find('audace'))
print(danton.find('audace', 0))
print(danton.find('audace', 5))
print(danton.find('audace', 6))
print(danton[6:])

link procedure

def get_next_target(page):
  start_link = page.find('<a href=')
  start_quote = page.find('"', start_link)
  end_quote = page.find('"', start_quote + 1)
  url = page&#91;start_quote + 1:end_quote&#93;
  return url, end_quote
  
print(get_next_target('this is a <a href="www.yahoo.co.jp">link</a>'))

How to crawler get a link from webpages.

def print_all_links(page):
  while True:
    url, endpos = get_next_target(page)
    if url:
      print(url)
      page = page[endpos:]
    else:
      break
  
print_all_links(get_page('http://yahoo.co.jp'))

find last position

def find_last(s, t):
  last_pos = -1
  while True:
    pos = s.find(t, last_pos)
    if pos == -1:
      return last_pos
      last_pos = pos

Python基礎

python3とpython2では記法が異なっている点があるので注意が必要です。

print ("hello world!")

変数

msg = "hello world"
print (msg)

整数と小数の演算は小数、整数同士の割り算は、小数点以下切り捨てとなります。

繰り返し処理

print (u"無駄"*10)

\\, \*なども

改行表現

print ("""<html>
<body>
</body>
</html>""")

整数値と文字列はpythonでは明示する

print (5 + int("5"))
print ("i am " + str(20) + "years old.")

配列、存在チェック

sales = [200, 100, 342, 1230, 122]
print (100 in sales)

ソート、reverse

sales = [52, 100, 80, 45]
sales.sort()
print (sales)

タプル:変更不可

a = (2, 5, 8)
print (a * 3)

セット:重複を許さない

a = set([1, 2, 3, 4])
print (a)

差集合

a = set([1, 2, 3, 4])
b = set([4, 5, 6, 7])
print (b - a)

辞書

sales = {"yamada": 200, "yoshida": 300, "sakura": 240}
print (sales)

key、value、items(一覧)

sales = {"yamada": 200, "yoshida": 300, "sakura": 240}
print (sales.values())

文字列へのデータ組み込み

a = 10
b = 123.345
c = "sakaki"
d = {"yamada":200, "yoshimoto": 300}
print ("age: %d" % a)

条件分岐

score = 70
if score > 60:
    print ("ok")

条件分岐2

score = 55
if score > 60:
    print ("ok")
elif score > 50:
    print ("soso")
else:
    print ("NG!")

forループ

sales = [13, 235, 312, 2232]
for sale in sales:
    print (sale)

繰り返し処理 ※インデントに注意

for i in range(10):
    print(i)

空処理

def hello2():
    pass

python module:
https://docs.python.org/3/library/index.html