It is often helpful to prototype and experiment in Playgrounds, but when you
submit your work, it should be in a Swift file, Lab1.swift, which should compile
using swift Lab1.swift
(which should have no output - for now, Playgrounds
will be the easiest way to test your code).
A language introduction is available here. Another great resource is The Swift Programming Language, Apple's own language reference.
For each of these, use a Swift playground to evaluate each expression, and write the result in a comment, including the type.
Example: 1 + 1 = 2 (Int)
- 1.0 + 1
- 2 * 24
- 2.0 * 3
- 4 / 2
- 4 / 3
- 4 / 3.0
Write all of the following functions.
I've specified method signatures for the functions in this lab, make sure you follow these guidelines so your code can be automatically tested. You'll notice that there are some functions with the same name, but different signatures - this is possible in Swift and standard throughout UIKit and other Swift frameworks - I don't find it to be a great practice but it can be convenient and is good to know about.
- Write a function called
getGreeting
which takes one string argument and returns a greeting string. This function should take aname
String and return the String "Hello, , I am your robot friend!"
This function should be able to be called as follows:
getGreeting(name: "Jake Gyllenhal")
- Write a function called
thermometer
which takes one integer argument, and returns a string describing the weather. Should take a number of degrees, and return the string "It is degrees outside."
This function should be able to be called with no argument labels, like this:
thermometer(77)
- Write a function called
double
which takes one optional integer argument, and either returns nil, or two times the integer passed in, depending on whether or not the argument isSome
orNone
.
This function should be able to be called with no argument labels, like this:
double(77)
- Write a function called
sum
which takes one Int array argument, and returns the sum of all the integers in the array.
This function should be able to be called with no argument labels, like this:
sum([1, 2, 3, 4, 5])
- Write a function called
sum
which takes an array of optional Ints, and returns the sum of all the integers in the array.
This function should be able to be called with no argument labels, like this:
sum([1, nil, 2, 3, nil, 4, 5, nil])
- Write a function called
sumOfOdds
which takes an array of Ints, and returns the sum of all the odd integers in that array.
This function should be able to be called with no argument labels, like this:
sumOfOdds([1, 2, 3, 4, 5])
- Write a function called
average
which takes any number of Double arguments, and returns the average of them.
This function should be able to be called with no argument labels, like this:
average(1.0, 5.5, 3.4)
- Write a function called
minimum
which takes an array of Ints, and returns an optional Int which is nil if the array argument is empty, or the smallest Integer in the array if the array is non-empty.
This function should be able to be called with no argument labels, like this:
minimum([4, 2, -1])
- Write a function called
minimum
which takes an array of Ints and a separate Int, with the argument labelatMost
, which returns the minimum Int in the array, oratMost
, whichever is smaller.
This function should be able to be called with only an argument label for
atMost
, like this:
minimum([4, 2, 5], atMost: 1)
- Write a function called
firstIndex
, which takes an Int argument labelledof
and an Int array labelledfrom
, and returns either the first index ofof
in the arrayfrom
, or nil iffrom
does not containof
.
This function should be called with both argument labels, like this:
firstIndex(of: 4, from: [1, 6, 4, 3])
For this part, you will write a simple class called TaskList
. It should store
a dictionary mapping tasks (strings) to priorities (integers). It should
support these methods:
add(task: priority:)
- adds a task with the given priority to the class.complete(task:)
- mark the given task as complete (remove it from the list)tasks()
-> Returns an alphabetical array of the descriptions of each task in the list.mostUrgent()
- returns a String optional which is the description of the task with the highest priority value.
You shouldn't need to write an initializer for this class, but if you do, make sure it can be called with no arguments for an empty to-do list. As you can see, this class is very simple and limited in usefulness - you can't change the priority of a task, or have subtasks or anything complicated. We will continue to add more features and use this class in future labs.