Swift Flashcards
When to use a For loop or While loop?
For loop: when you know the number of iterations ahead of time.
While loop: when you don’t know the number of iterations ahead of time
== VS ===
== tests for the Equatable protocol. ie does “abc” equal “abc”
=== tests for address matching. Do two instances of an object have the same address?
break vs return
Break: will exit loop or switch
Return: will exit the function
What’s the syntax for replacing an element in an array with another element?
array[x] = y
What’s the syntax for assigning a value to a dictionary?
dictionary[x] = y
How do you check if a dictionary contains a value?
- dict.keys.contains(key)
- if let value = dict[key]
- if dict[key] != nil
How do you determine how many times you want to repeat an operation?
First ask: What data do i need to see? all of it? some of it?
start from base case and develop from there. Consider writing out options.
How to iterate both index and value of an array?
for (index, value) in array.enumerated()
How to iterate elements of a dictionary?
for (key, value) in dictionary
How to cast from Int to String?
Int(string)
returns optional
How to cast from string to array?
Array(string)
How would you instantiate the absolute minimum as an Integer?
Int.min
How would you instantiate the absolute max as an Integer?
Int.max
How to return the greater of two values?
max(x,y)
How to return the lesser of two values?
min(x,y)
How to return a lowercase version of string?
string.lowercased()
this returns a new object!
How would you break a sentence into an array of strings?
string.components(seperatedBy: “ “)
How to return the prefix of an array?
array.prefix(count)
How to join an array of strings?
array.joined(separator: “”)
How to remove and return an element in an array?
array.remove(at: index)
Are swift parameters passed in as variables or constants?
Constants
What does inout do?
Allows you to modify an object in place, instead of returning a new object
What syntax do you use to pass an argument as inout?
- func test(inout string: String)
- test(string: &string)
How would you insert an element in an array?
array.insert(value, at: index)
How to clean a string of non alpha characters?
string.filter { $0.isLetter }
How to convert a string to an array?
let array = Array(string)
What does the type Int32 do?
It stores an integer with a length of 32 bits. This is different than the Int type which stores the default system value, which could be 32 or 64.
How to join an array of strings?
let result = array.joined(separator: “”)
What is the inout syntax for a func signature?
ie func reverseString(string: inout String)
What is the syntax for getting the first N elements of an array?
array.prefix(n)
How would you sort an array of this type? [[Int: Int]]
array.sort { $0.values.first! < $1.values.first! }
What is the diff between Sort, Sorted, and Sorted(by: )
Sort: called in place
Sorted: returns a copy
Sorted(by: ): returns a copy, accepts >
or <
as an argument
How to swap 2 values?
(a, b) = (b, a)
What does the Continue keyword do?
Prevents a loop from iterating after entering an If block
How do you calculate an exponent?
let result = pow(x,y)
What type does pow(x,y) return and how do you use it?
It returns Decimal type and it should be converted to Int/Float by returning:
Int(NSDecimalNumber(decimal: n))
How do you pop the last element of an array?
- .removeLast() to remove it
What’s the syntax for set?
let set: Set<Int> = [1,2,3]</Int>
What numbers will this iterate over?
for index in 1…5
1,2,3,4,5
How many times will this run?
for i in 0..<4
4
How do you get the length of an array?
array.count
What is the syntax to perform a swap?
(a,b) = (b,a)
How can you combine two strings?
stringA + stringB
How would you iterate an array starting at index N?
for i in N…array.count
How to create a Set in Swift?
let set: Set = [1,2,3]
How can you check if a set contains a value?
set.contains(value)
How to add an element to a Set?
set.insert(value)
How to declare a function argument as mutable?
write inout before the type
func sortColors(_ nums: inout [Int])
What is the diff between .reverse() and .reversed() ?
.reveresed() returns new value
.reverse() in place
What does continue
do in a loop?
skips an iteration of the loop
What does break
do in a loop?
ends the loop entirely
What does Fallthrough
do in a loop?
allows for a switch case to proceed to the next case
How can you create N number of arrays without instantiating them manually?
var arrays: [[Int]] = []
let n = 5
for _ in 0..<n {
arrays.append([])
}
How can you create N number of arrays without instantiating them manually? Do NOT use a for loop
let n = 5var arrays = [[Int]](repeating: [1,2,3], count: n)
How to get absolute number in swift?
abs(n)
What is the time complexity of .count
?
o(1) on an Array or any object that conforms to Random Access Collection
o(n) on non RAC conforming objects
How would you iterate an array starting at indexN while also having access to the array’s element?
for element in array[1…]
Whats the diff between readLine() and CommandLine.arguments?
- readLine() prompts the user for input
- commandLine pulls in arguments that are passed in at runtime
How would you access the last element of an array?
array.last
How to access strings by index?
Convert to array
let stringArray = string.map{ String($0) }
Are strings an instance of Array?
No, they are an instance of Collection. They are a collection of characters
What is the syntax to create a Set of Integers?
let set = Set<Int>()</Int>
How to access tuple elements?
let tuple = (a,b)
let a = tuple.0
let b = tuple.1
Continue vs break
Continue skips an iteration
break ends the loop
How to slice array?
array[x…y]
How to clear whitespace in Swift?
import Foundation
string.trimmingCharacters(in: .whitespaces)
What is the expected output after trimming whitespace on this string:
let originalString = “ Remove whitespaces “
Output: “Remove whitespaces”
How to filter out non-alphanumeric chars?
let output = string.filter { $0.isLetter || $0.isNumber }
How to get Int from pow() ?
let integer = Int(pow(Double(a), Double(b)))
How to pop the first element in an array?
array.removeFirst()
What will 0…5 print?
0,1,2,3,4,5
What will 0..<5 print?
0,1,2,3,4
What is the name of this range syntax: 0…5
closed
What is the name of this range syntax 0..<5
half open
let nums = [1,2,3]
what does nums[0..<0]) return?
An empty array [ ]
What numbers does this return?
let array = [1,2,3,4,5]
array[0…3]
1,2,3,4
What numbers does this return?
let array = [1,2,3,4,5]
array[0..<3]
0,1,2,3
Is this operator “…” inclusive or exclusive?
ie array[0…array.count]
inclusive
What will this return?
let array = [1,2,3,4,5]
array[0..<array.count]
1,2,3,4,5
What will this return?
let array = [1,2,3,4,5]
array[0…array.count]
Fatal, index out of range