Lesson 1 : How to Get Started

CS(Computer Science)란 '문제를 어떻게 얼마나 정확하고 효율적으로 푸는가?' 에 관한것이다. 마치 검색엔진이 데이터를 찾아내는것과 같다. 검색엔진은 3가지 주요 부분으로 나뉜다.

  • Find Data : 웹페이지를 크롤링해 데이터를 찾는다.
  • Buid an Index : query들을 빠르게 검색하기 위해서 인덱스를 만든다.
  • Rank Pages : 주어진 query들중 최고의 결과값을 얻기 위해서 순위를 매긴다.

이 수업에서는 검색엔진을 만든다. 위 3가지의 모든 요소를 완벽하게 다루지는 않지만 CS를 이해하기위해 적절히 사용한다.

Extracting links(링크 추출하기)

웹크롤러는 시드(seed)페이지로부터 다른 웹페이지를 크롤링 하기 위해 링크를 수집하고 그 링크에서 또 다른 페이지를 크롤링한다.

ambiguous : 모호한
verbosity : 다변
assume : 사실이라고 생각하다
refer to : 인용하다

Backus Naur Form

<non-treminal> => replacement

sentence => subject verb object

subject => Noun

object => Noun

verb => eat, like(terminal)

sentence
├──subject => noun => i
├──verb => love
└──object => noun => you

Python Grammar for Arithmetic Expressions

expression => expression operator expression

expression => Number, String, ...

expression => (expression)

operator => +, *, /, ...

expression
├──expression
│   ├──expression => Number => 1
│   ├──operator => +
│   └──expression => Number => 1
│    
├──operator => +
└──expression => Number => 1
#(1+1)+1

Quiz : Speed of Light

1나노초에 빛은 몇cm를 이동할수 있을까?

  • 빛의속도(speed of light) = 299,792,458m/s
  • meter = 100 centimeters
  • nanosecond = 1/1000,000,000 second

내컴퓨터의 처리속도는 얼마나 빠를까?

2.3 GHz Intel Core i5

  • 2.3 GHz = 2.3 Billion cycles per second
  • 1 second = 2,300,000,000 cycles
  • 1 second = 1,000,000,000 nanosecond
#1나노초동안 몇cm를 이동할수 있는지
#(빛의속도 * 100) / 1000,000,000 으로 계산
light = 299792458
mToCm = 100
ns = 1000000000

(light * mToCm)/ns # => 29.9792458cm

#1cycle동안 이동할수 있는거리
cycle = 23000000000
light/cycle # => 0.13034454695652173m

Quiz : Spirit Age

Write Python code that defines the variable age to be your age years, and then prints out the number of days you have been alive.

age = 28
days = 365
print age*days

String

문자열(string)은 문자를 나열한 것이다. (sequence of charaters) 문자열은 다른 데이터타입과 함께 사용할수는 없다.

print "!" * 12 # => !!!!!!!!!!!!
print "I'm " + 28 + "years old" # => TypeError: unsupported operand type(s) for +: 'int' and 'str

Indexing Strings

<string>[expression]

'udacity'[0] # => 'u'
'udacity'[1+1] # => 'a'
name = "Minwoo" 
name[0] # => "M"
name+name # => "MinwooMinwoo" 
(name+name)[5] # => "i"

<string>[expression:expression]

    s       start      stop
<string>[expression:expression]
            Number    Number

string that is a subsequence of the charaters in s starting from position start, and ending with position stop-1(stop인덱스를 포함하지 않는다)

word = 'assume'
print word[3] # => 'u'
print word[3:3] # => ''
print word[4:6] # => 'me'
print word[3:] # => 'ume'
print word[:2] # => 'as'
print word[:] # => 'assume'

Finding Strings in Strings

<string>.find(<string>) : number that gives first where the target string appears. If target string is not found, return -1

<string>.find(<string>, <number>) : number that gives first where the target string appears. [at or after number] optional (기본값은 0 인듯하다.)

a = "apple is so good. I love apple"

#<string>.find(<string>)
a.find("apple") # => 0
a[0:5] # => 'apple'
a.find("love") # => 20
a[20:] # => 'love apple'
a.find("banana") # => -1
a.find("banana")+1 # => 0
a.find(a) # => 0
a.find("") # => 0

#<string>.find(<string>,<number>)
a.find("apple",1) # => 25
a[1:].find("apple") # => 24 ("pple is so good. I love apple".find("apple"))
a.find("apple",0) # => 0
a.find("apple",) # => 0 (기본값이 0 인듯하다.)
a[25:] # => 'apple'

Extracting links(링크 추출하기)

이제 진짜 링크 추출하기

#full code
page =('<div id="top_bin"><div id="top_content" class="width960">'
'<div class="udacity float-left"><a href="http://udacity.com">')

start_link = page.find("<a href=") # => start_link == 89
first_quote = page.find('"',start_link) # 첫번째 "의 위치
last_quote = page.find('"',first_quote+1)
# first_quote 이후에 있는 "
# first_quote+1을 하는 이유는 하지않을경우 first_quote의 위치를 반환하기 때문.
url = page[first_quote+1:last_quote]

start_link

start_link = page.find("<a href=") # => start_link == 89
page[89:] # => '<a href="http://udacity.com">'

first_quote

first_quote = page.find('"',start_link) # 첫번째 "의 위치 == 97
page[97:] # => '"http://udacity.com">'

last_quote

last_quote = page.find('"',first_quote+1) # => 마지막 "의 위치 == 116
# first_quote 이후에 있는 "
# first_quote+1을 하는 이유는 하지않을경우 first_quote의 위치를 반환하기 때문.
page[116:] # => '">'

url

                89           116
url = page[first_quote+1:last_quote] 
page[98:116] # => 'http://udacity.com'

완벽했다!

Problem Set

python programming 1

#Write Python code that prints out the number of hours in 7 weeks
hoursOfOneDay = 24
print 7*7*hoursOfOneDay

python programming 2

#Which of the following sequences of statements leaves the value of variable x 
#the same as it was before the statements.
#Assume that both a and x refer to integer values before this code.

1. a = x ; a = a + 1 #true
2. x = x + 1 ; x = x #false
3. z = x; a = z; x = a #true
4. a = x; x = a #true
5. a,x = x,a; a,x= x,a #true

Speed of light

#Write Python code that stores the distance in meters that light travels
#in one nanosecond in the variable, nanodistance.
#Theses variables are defined.
speed_of_light = 299800000 #m/s 
nano_per_sec = 1000000000 #1Billion

nanodistance = speed_of_light/nano_per_sec #0.2998

Strings

#Given any string s, which of the following always have the same value as s ? (Be careful, s could be '')

1. ('a'+s)[1:] # => ''
2. s+'' # => ''
3. s[0]+s[1:]
# => IndexError: string index out of range. 
# if s is the '' empty string, then s[0] will cause an error because there is no character at position 0.
# 특정 인덱스지정은 진짜 해당인덱스가 존재할때만 가능하다.
# 즉 s가 ''일때 s[0:]이나 s[100:200]등은 가능하지만, s[0]은 불가능하다.
4. s[0:] # => ''

Find 1

text = likelion is sucks
text.find("lion") # => 4
text.find("minwoo") # => -1

Find 2

text = "all zip files are zipped" 

first_zip = text.find("zip") # => 4
print text.find("zip",first_zip+1) # => 18

Rounding Numbers

str(<Number>) => <string>

round(<Number>) : 반올림

int(<Number>) : 정수화

# x = 3.14159 
# >>> 3 (not 3.0)
# x = 27.63 
# >>> 28 (not 28.0)
# x = 3.5 
# >>> 4 (not 4.0)

x = 3.14159

# solution 1

def compare(x):
    string_x = str(x) # => '3.14159'
    point = string_x.find('.') # => 1
    return string_x[:point]


if compare(x) == compare(x+0.5):
    answer = compare(x)
else:
    answer = compare(x+0.5)

print(answer)

#solution 2

x = round(x)
string_x = str(x) # => '3'
point = string_x.find('.') # => 1

print string_x[:point]

Palindrome

###############################################
#       Exercise by Websten from forums       #
###############################################
# A palindrome is a word or a phrase that reads 
# the same backwards as forwards. Make a program 
# that checks if a word is a palindrome. 
# If the word is a palindrome, print 0 to the terminal,
# -1 otherwise. 
# The word contains lowercase letters a-z and 
# will be at least one character long.
#
### HINT! ###
# You can read a string backwards with the following syntax:
# string[::-1] - where the "-1" means one step back.
# This exercise can be solved with only unit 1 knowledge
# (no loops or conditions)

word = "madam"
# test case 2
# word = "madman" # uncomment this to test

###
# YOUR CODE HERE. DO NOT DELETE THIS LINE OR ADD A word DEFINITION BELOW IT.
###
if(word == word[::-1]):
    is_palindrome = 0
else:
    is_palindrome = -1

# TESTING
print is_palindrome
# >>> 0  # outcome if word == "madam"
# >>> -1 # outcome if word == "madman"

"string"[::-1]이 역방향이 되는게 이해안됨.

http://stackoverflow.com/questions/41430791/python-list-error-1-step-on-1-slice

results matching ""

    No results matching ""