A.6. Chapter 6

A.6.1. Exercise 1 solution

-- Calculate the first 20 Fibonacci numbers

-- Define F1 and F2

set fibList to {0, 1}

-- Now calculate the next 18 numbers

repeat 18 times
    set end of fibList to (item −1 of fibList) + (item −2 of fibList)
end repeat

-- log the result

log fibList

A.6.2. Exercise 2 solution

set USCities to {{"Boston", 574000}, {"Chicago", 2784000}, {"Dallas", 1007000}, ¬
     {"Houston", 1631000}, {"Los Angeles", 3485000}, {"Philadelphia", 1586000}, ¬
     {"San Diego", 1111000}, {"San Francisco", 724000}, {"New York", 7323000}}

-- set the mix and max to the population of the first city

set min to item 2 of item 1 of USCities
set max to min
-- keep track of the list item that contains the max and min populations

set minIndex to 1
set maxIndex to 1

repeat with i from 2 to count USCities
    set population to item 2 of (item i of USCities)
    if population > max then
        set maxIndex to i
        set max to population
    end if

    if population < min then
        set minIndex to i
        set min to population
    end if
end repeat

display dialog "The city with the smallest population is " & item 1 of (item ¬
     minIndex of USCities) & return & "The city with the largest population is " ¬
     & item 1 of (item maxIndex of USCities)

A.6.3. Exercise 3 solution

set USCities to {¬ {name:"Boston", population:574000}, ¬ {name:"Chicago", population:2784000}, ¬ {name:"Dallas", population:1007000}, ¬ {name:"Houston", population:1631000}, ¬ {name:"Los Angeles", population:3485000}, ¬ {name:"Philadelphia", population:1586000}, ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.