Exercises 5 β
Create a package named exercises-5, delete its existing source file, and create a main.swift file. Put your solutions in this file, and when youβre done, compare your work with the solutions bundle.
These exercises focus on using arrays and dictionaries. As with the previous exercises, whenever youβre asked to declare a function, itβs up to you to choose appropriate parameters, argument labels, and return values for your functions, and to try out each function by calling it with various arguments.
Exercise 5.1 β
Declare a function to calculate the sum of an array of numbers.
Exercise 5.2 β
Declare a function that takes an array of numbers and returns a new array where the order of the elements is reversed.
This is exactly what the reversed
method of type Array
does. Your solution shouldnβt rely on this method; the exercise is to implement it yourself.
Exercise 5.3 β
Declare a function that takes an array of numbers and returns a new array where the elements are sorted from small to large.
This is exactly what the sorted
method of type Array
does. Your solution shouldnβt rely on this method; the exercise is to implement it yourself.
Exercise 5.4 β
Declare a function that takes an integer and checks whether or not this integer is a palindrome.
Exercise 5.5 β
Declare a function characters(in:)
that checks which characters occur in a string and returns the number of times each of these characters occur.
Like arrays and dictionaries, strings are collections. This means you can use a for-in
loop to iterate over the characters in a string:
var string = "some string"
for character in string {
// ...
}
Youβll need this to solve the exercise.
Exercise 5.6 β
Declare a function that takes two strings and checks whether or not these strings are anagrams.
Tip
You can reuse the characters(in:)
function from the previous exercise.
Like arrays and dictionaries, strings also have properties, methods, and initializers. If you want your function to ignore the difference between lowercase and uppercase letters, look for a method that can help you achieve that.
Exercise 5.7 β
Declare a function words(in:)
that checks which words occur in a string and returns the number of times each of these words occur.
To split a string into words, pick a set of whitespace and punctuation characters that you want to support, then split the string any time you encounter one of these characters.
Exercise 5.8 β
Declare a function stats(for:)
that returns the number of lines, words, and characters in a string.
Exercise 5.9 β
Declare a function that can encrypt a message by replacing every βaβ with a βzβ, every βbβ with a βyβ, and so on, for the entire alphabet. Store this mapping of characters in a dictionary and use it in your function.
This particular mapping is symmetrical, so encrypting an already encrypted message effectively decrypts it. Alternatively, you could build any mapping you want, then provide separate dictionaries and functions for encrypting and decrypting.
Exercise 5.10 β
Declare the following type alias to represent a date:
typealias Date = (day: Int, month: Int, year: Int)
Next, declare a function to calculate the number of days between two dates.
Use an array to store the number of days in each month. You can then use this array as a lookup table to simplify your code.
Exercise 5.11 β
Declare a function that can fully spell out any integer. For example, for an input of -100212, the function should return βminus one hundred thousand two hundred twelveβ.
Recall from Introduction to Computing that the largest value a signed 64-bit integer can hold is about 9 quintillion.
Tip
The last three digits require the most care. After that, rely on recursion to do most of the work for you.