Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Question 1
- ```swift
- import UIKit
- var firstArray = ["A","B","C","D","E"]
- var secondArray: [String] = ["A","E", "D"]
- func isSubset(firstArray: [String], secondArray: [String]) -> Bool {
- for character in secondArray {
- if !firstArray.contains(character) {
- return false
- }
- }
- return true
- }
- ```
- # Question 2
- The computational complexity to the answer in Problem 1 is O(n^2) as there is a nested loop when each character in the second array is being traversed. The second loop itself traverses each element in the first array.
- # Question 3
- ```swift
- import UIKit
- var fibonnaciSeq: [Int] = [1, 1]
- func computeFibonnacci(request: Int) {
- var lastIndex = fibonnaciSeq.endIndex - 1
- var secondLastIndex = lastIndex - 1
- while fibonnaciSeq[lastIndex] <= request {
- let nextFB = fibonnaciSeq[lastIndex] + fibonnaciSeq[secondLastIndex]
- fibonnaciSeq.append(nextFB)
- lastIndex += 1
- secondLastIndex += 1
- }
- }
- func nextFibonnacci(requests: [Int]) {
- for request in requests {
- if fibonnaciSeq.contains(request) && fibonnaciSeq.index(of: request) != fibonnaciSeq.last! {
- let nextFB = fibonnaciSeq[fibonnaciSeq.index(of: request)! + 1]
- print(nextFB)
- }
- if fibonnaciSeq.contains(request) && fibonnaciSeq.index(of: request) == fibonnaciSeq.last! {
- computeFibonnacci(request: request)
- if let lastItem = fibonnaciSeq.last {
- print(lastItem)
- }
- }
- if !fibonnaciSeq.contains(request) {
- if request > fibonnaciSeq.last! {
- computeFibonnacci(request: request)
- print(fibonnaciSeq.last!)
- } else {
- for number in fibonnaciSeq {
- if number > request {
- print(number)
- break
- }
- }
- }
- }
- }
- }
- ```
- # Question 4
- ```javascript
- function createArrayOfFunctions(y) {
- var arr = [];
- for(var i = 0; i<y; i++) {
- arr[i] = function(x) { return x + i; }
- }
- return arr; }
- ```
- For this problem, x is not well-defined. To fix the problem define an in-scope variable called x like so:
- ```javascript
- function createArrayOfFunctions(y) {
- var arr = [];
- var x = c;
- for(var i = 0; i<y; i++) {
- arr[i] = function(x) { return x + i; }
- }
- return arr; }
- ```
- where c itself is an integer.
Add Comment
Please, Sign In to add comment