the list of number

product_list, that takes as input a list of numbers, and returns a number that is the result of multiplying all those numbers together.

def product_list(p):
 result = 1
 for i in p
    result =  result * i
    return result

A greatest, that takes as input a list of positive numbers, and returns the greatest number in that list. If the input list is empty, the output should be 0.

def greatest(p):
  biggest = 0
  for i in p:
    if i > biggeset:
      biggest = i
  return biggest

total_enrollment, that takes as an input a list of elements, where each element is a list containing three elements: a university name, the total number of students enrolled, and the annual tuition fees.
The procedure return two numbers, giving the total number of students enrolled at all of the universities in the list, and the total tuition fees (which is the sum of the number of students enrolled times the
# tuition fees for each university).

usa_univs = [ ['California Institute of Technology',2175,37704],
              ['Harvard',19627,39849],
              ['Massachusetts Institute of Technology',10566,40732],
              ['Princeton',7802,37000],
              ['Rice',5879,35551],
              ['Stanford',19535,40569],
              ['Yale',11701,40500]  ]

def total_enrollment(p):
    total_student = 0
    total_tuition = 0
    for i in p:
        total_sutudent = i[1] + total_sudent
        total_tuition = i[1] * i[2] + total tuition
    return total_student, total_tuition

check_sudoku, that takes as input a square list of lists representing an n x n sudoku puzzle solution and returns the boolean True if the input is a valid sudoku square and returns the boolean False otherwise.

def check_sudoku(p):
    n = len(p)
    digit = 1
    while digit <= n:
      i = 0
      while i < n:
        row_count = 0
        col_count = 0
        j = 0
        while j < 0
          if p[i][j] == digit:
            row_count = row_count + 1
          if p[j][i] = digit:
            col_count = col_count + 1
          j = j + 1
        if row_count != 1 or col_count != 1:
          return False
        i = i + 1
      digit = digit + 1
    return True

divided by number by bumbers showing dicimal

def list_mean(p):
    return float(sum(p)) / len(p)

better data structure indexing keyword, thinking data structure is the most important in computer science.

[[<keyword>,[<url>,<url>]]
 [<keyword>,[<url>,<url>]]
 ...]

web crawler

needs the list to crawl and the list crawled.

Basic concept of crawling is here.

start with tocrawl = [seed]
 crawled = []
 while there more pages tocrawl:
    pick a page from tocrawl
    add that page to crawled
    add all the link target on
     this page to tocrawl
    return crawled

this is a procedure of get links.

def get_all_links(page);
  links = []
  while True:
    url, endpos = get_next_target(page)
    if url:
      links.append(url)
      page = page[endpos:]
    else:
      break
  return links

starting from the seedpage. The important point is web crawler follow last page of link using python pop.

 def crawl_web(seed):
   tocrawl = [seed]
   crawled = []
   while tocrawl:
     page = tocrawl.pop()
def union(p,q):
    for e in q:
        if e not in p:
            p.append(e)


def get_all_links(page):
    links = []
    while True:
        url,endpos = get_next_target(page)
        if url:
            links.append(url)
            page = page[endpos:]
        else:
            break
    return links

def crawl_web(seed):
    tocrawl = [seed]
    crawled = []
    while tocrawl:
        page = tocrawl.pop()
        if page not in crawled:
        union(tocrawl, get_all_links(get_page(page)))
        crawled.append(page)

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

browser app

screen-shot-2016-11-26-at-07-06-42

import UIKit

class ViewController: UIViewController, UIWebViewDelegate, UITextFieldDelegate {
    
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var backButton: UIBarButtonItem!
    @IBOutlet weak var forwardButton: UIBarButtonItem!
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let topBorder = CALayer()
        topBorder.frame = CGRectMake(0, 0, self.webView.frame.size.width, 1)
        topBorder.backgroundColor = UIColor.lightGrayColor().CGColor
        self.webView.layer.addSublayer(topBorder)
        self.webView.delegate = self
        self.textField.delegate = self
        
        self.webView.delegate  = self
        self.textField.delegate = self
        
        // string -> NSURL -> NSURLRequest -> webView.loadRequest
        let startUrl = "http://google.com"
//        if let url = NSURL(string: startUrl){
//            let urlRequest = NSURLRequest(URL: url)
//            self.webView.loadRequest(urlRequest)
//        }
        self.jumpToUrl(startUrl)
        self.setupButtonsEnabled()
        self.activityIndicatorView.hidesWhenStopped = true
    }
    
    func jumpToUrl(urlString: String){
        if let url = NSURL(string: urlString){
            let urlRequest = NSURLRequest(URL: url)
            self.webView.loadRequest(urlRequest)
        } else {
            self.showAlert("Invalid URL")
        }
    }
    
    func showAlert(message: String){
        let alertController = UIAlertController(title: "Error", message:message, preferredStyle: .Alert)
        let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        var urlString = self.textField.text
        urlString = urlString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
        if urlString == "" {
            // alert
            self.showAlert("please enter URL")
        } else {
            if urlString.hasPrefix("http://") && !urlString.hasPrefix("https://") {
                urlString = "http://" + urlString
            }
            self.jumpToUrl(urlString)
            self.setupButtonsEnabled()
        }
        self.textField.resignFirstResponder()
        return true
    }
    
    func setupButtonsEnabled() {
        self.backButton.enabled = self.webView.canGoBack
        self.forwardButton.enabled = self.webView.canGoForward
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
        self.webView.stopLoading()
        self.activityIndicatorView.stopAnimating()
        
        if error.code != NSURLErrorCancelled {
            self.showAlert("Network Error")
        }
        self.updateUrlLocation()
    }
    
    func webViewDidStartLoad(webView: UIWebView) {
        self.activityIndicatorView.startAnimating()
    }
    
    func updateUrlLocation(){
        if let urlString = self.webView.request?.URL?.absoluteString {
            self.textField.text = urlString
        }
    }
    
    
    func webViewDidFinishLoad(webView: UIWebView) {
        self.activityIndicatorView.stopAnimating()
        self.setupButtonsEnabled()
        self.updateUrlLocation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    @IBAction func goBack(sender: AnyObject) {
        self.webView.goBack()
    }
    
    @IBAction func goForward(sender: AnyObject) {
        self.webView.goForward()
    }
    
    @IBAction func reload(sender: AnyObject) {
        self.webView.reload()
    }

}

string -> NSURL -> NSURLRequest -> webView.loadRequest

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var backButton: UIBarButtonItem!
    @IBOutlet weak var forwardButton: UIBarButtonItem!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // string -> NSURL -> NSURLRequest -> webView.loadRequest
        let startUrl = "http://hogehoge.com"
        if let url = NSURL(string: startUrl){
            let urlRequest = NSURLRequest(URL: url)
            self.webView.loadRequest(urlRequest)
        }
        
    }

姓名診断 app

screen-shot-2016-11-26-at-02-38-19
tips
ctl + button -> segue: show
seque identifier -> class name
command + = -> size調整
file -> new file -> Cocoa Class : 新しいclassを生成 -> utility custom classでclass設定

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.textField.text = ""
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func showAlert() {
        let VERSION: Float = (UIDevice.currentDevice().systemVersion as NSString).floatValue
        if VERSION >= 8.0 {
            let alertController = UIAlertController(title: "Error", message: "Please enter your name", preferredStyle: .Alert)
            let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
            alertController.addAction(defaultAction)
            self.presentViewController(alertController, animated: true, completion: nil)
        } else {
            let alert = UIAlertView()
            alert.title = "Error"
            alert.message = "Please enter your name"
            alert.addButtonWithTitle("OK")
            alert.show()
        }
    }
    
    override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
        if identifier == "showResult" {
            if self.textField.text == "" {
                self.showAlert()
                return false
            }
            return true
        }
        return true
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showreResult" {
            let resultViewController: ResultViewController = segue.destinationViewController as! ResultViewController
            resultViewController.myName = self.textField.text
            self.textField.resignFirstResponder()
        }
    }
    
}


import UIKit

class ResultViewController: UIViewController {

    var myName: String = ""
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var scoreLabel: UILabel!
    
    override func viewDidLoad(){
        super.viewDidLoad()
        self.nameLabel.text = "\(self.myName)の点数は..."
        
        let score = arc4random_uniform(101)
        self.scoreLabel.text = "\(score)点"
        
    }
    
    
}

omikuji app

swiftではarcNrandom_uniform(UInt32(hoge.count))でランダムに数値を生成します。

Tips
command + option + 0: utility 非表示
launch screen -> main.storyboardに変更可能

screen-shot-2016-11-26-at-01-32-40

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var myLabel: UILabel!
    
    
    @IBAction func getOmikuji(sender: AnyObject) {
        let results = [
            "大吉",
            "吉",
            "中吉",
            "凶",
            "大凶"
        ]
        
        // arc4random_uniform(n + 1)
        let random = arc4random_uniform(UInt32(results.count))
        
        if random == 0 {
            self.myLabel.textColor = UIColor.redColor()
        } else {
            self.myLabel.textColor = UIColor.blackColor()
        }
        
        self.myLabel.text = results[Int(random)]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Swift coding

untitled

//var s: String?
//s = nil

let name: String? = "yamada"
let msg = "hello " + name!

if name != nil {
    let msg = "hello " + name!
}

if let s = name {
 let msg = "hello " + s
}

var label: String!
label = "score"
println(label)

function

//func sayHi(){
//    println("hi")
//}
//sayHi()

func sayHi(name: String){
    println("hi " + name)
}
sayHi("satoshi")

func sayHi(myname name: String){
    println("hi " + name)
}
sayHi(myname: "satoshi")

func sayHi(#name: String = "jiro"){
    println("hi " + name)
}
sayHi(name: "satoshi")

列挙型

enum Result {
    case Success
    case Error
    func getMessage() -> String {
        switch self {
        case .Success:
            return "OK"
        case .Error:
            return "OK"
        }
    }
}

var r: Result

r = Result.Success
r = .Success

Result.Error.rawValue

class

class User {
    var name: String
    var score: Int = 0
    init(name: String){
        self.name = name
    }
    func updrade() {
        score++
    }
}

var tom = User(name: "Tom")
tom.name
tom.score
tom.updrade()
tom.score

継承

class User {
    var name: String
    var score: Int = 0
    init(name: String){
        self.name = name
    }
    final func upgrade() {
        score++
    }
}

class AdminUser: User {
    func reset(){
        score = 0
    }
    override func upgrade(){
        score += 3
    }
}

protocol

protocol Student {
    var studentId: String { get set }
    func study()
}

class User: Student {
    var name: String
    var score: Int = 0
    var studentId: String = "hoge"
    func study(){
        println("studying.....")
    }
    init(name: String){
        self.name = name
    }
    func upgrade() {
        score++
    }
}



var tom = User(name: "Tom")
tom.name
tom.score
tom.upgrade()
tom.score

willSet, didSet

class User {
    var name: String
    var score: Int = 0 {
        willSet{
            println("\(score) -> \(newValue)")
        }
        didSet {
            println("\(oldValue) -> \(score)")
        }
    }
    var level: Int {
        get{
            return Int(score / 10)
        }
        set {
            score = Int(newValue * 10)
        }
    }
    init(name: String){
         self.name = name
        }
    func upgrade() {
        score++
    }
}

optional chaining

class User {
    var blog: Blog?
}

class Blog {
    var title = "My Blog"
}

var tom = User()
tom.blog = Blog()
tom.blog?.title

if let t = tom.blog?.title {
    println(t)
}

type casting

class User {
    var name: String
    init(name: String){
        self.name = name
    }
}
class AdminUser: User {}

let tom = User(name: "Tom")
let bob = AdminUser(name: "Bob")
let steve = someUser()

let users: [AnyObject] = [tom, bob, steve]

for user in users {
    if let u = user as? AdminUser {
        println(u.name)

構造体

struct UserStruct {
    var name: String
    var score: Int = 0
    init(name: String){
        self.name = name
    }
    mutating func upgrade(){
        score++
    }
}

拡張

extension String {
    var size: Int {
        return countElements(self)
    }
    func dummy() -> String {
        return "dummy"
    }
}

var s: String = "hoge"
s.size
s.dummy()

generics

func getArray(item: T, count: Int) -> [T]
{
    var result = [T]()
    for _ in 0..
	

getting started Swift

X-codeのplaygroundでコードと実行結果を表示します。

//print("hello world")
print("hello world")

変数

//var msg: String
//msg = "hello world"
//変更ができない変数 let

//var msg = "hello world"

var msg = "hello"
let s = "hoge"
//s = "bar"
println("msg: \(msg), s: \(s)")

データ型

// String, Int, Float/Double, Bool, nil
// + - * / %
let x: Float = 8.0 % 2.5
var y = 0
y++
y

let s = "hello " + "world"
true && true
true || false
!true

let a = "hkt"
let b = 48
let c = a + String(b)

タプル: _xxx はxxxを破棄

//let error = (40, "not found")
//error.0
//error.1

let error = (code:40, msg:"not found")
error.code
error.msg

let error = (40, "not found")
//let (code, msg) = error
let (code, _msg) = error
code
msg

swift

var colors: [String] = ["blue", "pink"]
colors[0]
colors[1] = "red"
colors

colors.count
colors.isEmpty

colors.append("green")
colors.insert("gray", atIndex: 1)

let secondColor = colors.removeAtIndex(1)

dictionary

var users: [String: Int] = [
  "yamada": 500,
    "tanaka" : 800
]

users["yamada"]

users.count
users.isEmpty

users["ito"] = 900
users

users.removeValueForKey("ito")
users

users.updateValue(1000, forKey: "tanaka")
let keys = Array(users.keys)
let values = Array(users.values)

var emptyDictionary = [String: Int]()

if

let score = 72
var result = ""

if score > 80 {
    result = "Great"
} else if score > 60 {
   result = "good"
} else {
    result = "soso..."
}

result = score > 80 ? "great" : "so so..."

switch

let num = 7

switch num {
case 0:
    println("zero")
case 1, 2, 3:
    println("small")
case 4...6:
    println("4/5/6")
case 7..<9:
    println("7/8")
case let n where n > 10:
    println("huge")
default:
    println("n.a.")
}

while


var n = 0
//while n < 10 { // println(n) // n++ //} do { println(n) n++ } while n < 10 [/code] for

for var i = 0; i < 10; i++ {
 println(i)
}

for i in 0...9 {
    println(i)
}

let a = [5, 3, 10]
for i in a {
    println(i)
}