diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..8687a7d Binary files /dev/null and b/.DS_Store differ diff --git a/HW(Recursion)_final.playground/Contents.swift b/HW(Recursion)_final.playground/Contents.swift new file mode 100644 index 0000000..b29b603 --- /dev/null +++ b/HW(Recursion)_final.playground/Contents.swift @@ -0,0 +1,102 @@ +//: Playground - noun: a place where people can play + +import UIKit +import Foundation + + +/* +Write an iterative (not recursive) fibonacci function that calculates the nth fibonacci number. How does its performance compare with the non-memoized recursive one (see Appendix A below), based on the number of iterations that Swift says it takes in the right margin? + +When n is 4, the iterative function, although running with O(n) complexity, is run 4 times while the recursive is run 9 times. +*/ + +//Appendix A: Recursive Fibonacci from class (non-memoized) +func fib(n: Int) -> Int { + print("X") + if (n == 0 || n == 1) { + return 1 + } + return fib(n - 1) + fib(n - 2) +} + +fib(4) + +// HW ANSWER + +func fibIter(n: Int) -> Int { + //0, 1, 1, 2, 3, 5, 8, 13, 21 + if n == 0{ + return 0; + } + + //if the number isn't 0, proceed + + var x = 0, y = 1, z = 1; + + for _ in 0.. Int { + let stepCount = Int(arc4random_uniform(3)) - 1 + stepNum += stepCount; + switch(stepCount) { + case -1: print("Ouch \(stepNum)") + case 1: print("Yay \(stepNum)") + default: print("Beep \(stepNum)") + } + return stepCount +} + +// HW ANSWER + +func stepUp() { + + switch tryStep() { + case 1: + return + case 0: + stepUp() + case -1: + stepUp() + stepUp() + default: + stepUp() + + } +} + + +/* + +Using the code in Appendix C as a starting point, create a Swift command line project to find files on your computer by name. Your solution should use recursion. Your method should return “NOT FOUND” if it couldn’t find the file, otherwise it should return the full path to that file. + +You can’t use a playground for this because they don’t have filesystem access for some reason. Instead, in XCode, go to File > New > Project, select “Application” under “OS X” in the sidebar, then select “Command Line Tool”; on the next screen, choose “Swift” as the language. + +*/ + + + + + + + + + diff --git a/HW(Recursion)_final.playground/contents.xcplayground b/HW(Recursion)_final.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HW(Recursion)_final.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HW(Recursion)_final.playground/playground.xcworkspace/contents.xcworkspacedata b/HW(Recursion)_final.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HW(Recursion)_final.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HW(Recursion)_final.playground/timeline.xctimeline b/HW(Recursion)_final.playground/timeline.xctimeline new file mode 100644 index 0000000..fc3b703 --- /dev/null +++ b/HW(Recursion)_final.playground/timeline.xctimeline @@ -0,0 +1,11 @@ + + + + + + + diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift new file mode 100644 index 0000000..370cabd --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -0,0 +1,134 @@ +//: Playground - noun: a place where people can play + +import UIKit + +/* +Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw + +1) Get possible numbers in sudoku board + +*/ + +func setValuesToFalse(inout arr:[Bool]) { + for i in 0.. [Int] { + var numberUsed = Array(count: 9, repeatedValue: false) // map each index to a number used + var invalidNumbers = [Int]() + var validNumbers = [Int]() + // add all the numbers in that row to an array + + for pos in (0..=0{ + invalidNumbers.append(num) + + } + } + + // add all the numbers in that column to an array + + for pos in (0..<9) { + let num = sudokuBoard[pos][col] // subtract one to account for offset + if num>=0 && !invalidNumbers.contains(num){ + invalidNumbers.append(num) + } + } + + + // check the 3x3 box that it's in + + for i in(0..<3) { + for j in (0..<3) { + let num = sudokuBoard[row*3+i][col*3+j] + if num>=0 && !invalidNumbers.contains(num){ + invalidNumbers.append(num) + } + + } + } + + setValuesToFalse(&numberUsed) + for i in 0..[[Int]]{ + + let columns = matrix[0].count + let rows = matrix.count + var arr = [[Int]](count: rows, repeatedValue: [Int](count: columns, repeatedValue: 0)) + + for row in 0.. [Int]{ + + var left = values[0...1] + + if left[0] > left [1]{ + let t = left[0] + left[0] = left[1] + left[1] = t + } + + var right = values[2...4] + + if right[0] > right [1]{ + let t = right[1] + right[0] = right[1] + right[1] = t + } + + + +} diff --git a/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground b/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline b/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HWFrom1-28-16(Merge Sort).playground/Contents.swift b/HWFrom1-28-16(Merge Sort).playground/Contents.swift new file mode 100644 index 0000000..6f2c577 --- /dev/null +++ b/HWFrom1-28-16(Merge Sort).playground/Contents.swift @@ -0,0 +1,39 @@ +//: Playground - noun: a place where people can play + +import UIKit + + +func insertionSort(var numbers:[Int], index:Int) -> [Int]{ + + if index == numbers.count{ + return numbers + } + else{ + + var temp = numbers[index], + iter = 0, //final index + iter2 = 0, + maxIter = index - 1 + + while (iter <= maxIter && numbers[iter] < temp) { + + iter++ + } + + while iter2<=maxIter{ + numbers[maxIter-iter2+1] = numbers[maxIter-iter2] + iter2++ + } + + + numbers[iter] = temp; + + return insertionSort(numbers, index: index+1) + } + +} + + + + +insertionSort([9, 3, 4, 2, 1], index: 1) \ No newline at end of file diff --git a/HWFrom1-28-16(Merge Sort).playground/contents.xcplayground b/HWFrom1-28-16(Merge Sort).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-28-16(Merge Sort).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-28-16(Merge Sort).playground/timeline.xctimeline b/HWFrom1-28-16(Merge Sort).playground/timeline.xctimeline new file mode 100644 index 0000000..43b6727 --- /dev/null +++ b/HWFrom1-28-16(Merge Sort).playground/timeline.xctimeline @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift new file mode 100644 index 0000000..fceafe8 --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -0,0 +1,237 @@ +//: Playground - noun: a place where people can play + +import UIKit + +/* +1) + time complexity space complexity +bubble sort O(n^2) O(1) +insertion sort O(n^2) O(1) +selection sort O(n^2) O(1) +merge sort O(nlogn) O(n) +quick sort O(n^2) O(1) +*/ + + +/* 2) What is the advantage of partitioning Quicksort in place? +It has a constant space complexity +*/ + +/* + 3) Without looking, implement quicksort. +*/ + +//func quickSort(inout arr: [Int], first: Int, last: Int) { +// +// if first >= last { return } +// +// let splitPoint = partition(&arr, first: first, last: last) +// +// quickSort(&arr, first: first, last: splitPoint - 1) +// +// quickSort(&arr, first: splitPoint + 1, last: last) +//} +// +//func quickSort(inout arr: [Int]) { +// quickSort(&arr, first: 0, last: arr.count-1) +//} +// +//func partition(inout arr: [Int], first: Int, last: Int) -> Int { +// +// let pivotValue = arr[first] +// var leftMark = first + 1 +// var rightMark = last +// +// while leftMark <= rightMark { +// while arr[leftMark] < pivotValue && leftMark <= rightMark { +// leftMark += 1 +// } +// while arr[rightMark] > pivotValue && leftMark <= rightMark{ +// rightMark -= 1 +// } +// if leftMark < rightMark { +// swap(&arr[leftMark], &arr[rightMark]) +// } +// } +// +// if first != rightMark { +// swap(&arr[first], &arr[rightMark]) +// } +// return rightMark +// +// +//} +// +//var array = [22, 59, 38, 93, 95, 0, 34, 58, 72, 15] +//quickSort(&array) + + +/* 4) Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000. + + +Compare the time it takes to run mergesort, quicksort, and quicksort with the median. +https://gist.github.com/gummibeatz/8ff29bcec54d7e3ef683 +*/ +// +//func quickSort(inout arr: [Int], firstIdx: Int, lastIdx: Int, medianPivot: Bool) { +// // base case +// if firstIdx >= lastIdx { return } +// // partition +// let splitPoint = partition(&arr, firstIdx: firstIdx, lastIdx: lastIdx, medianPivot: medianPivot) +// // quickSort on leftHalf +// quickSort(&arr, firstIdx: firstIdx, lastIdx: splitPoint - 1, medianPivot: medianPivot) +// // quickSort on rightHalf +// quickSort(&arr, firstIdx: splitPoint + 1, lastIdx: lastIdx, medianPivot: medianPivot) +//} +// +//func quickSort(inout arr: [Int], medianPivot: Bool) { +// quickSort(&arr, firstIdx: 0, lastIdx: arr.count-1, medianPivot: medianPivot) +//} +// +//func partition(inout arr: [Int], firstIdx: Int, lastIdx: Int, medianPivot: Bool) -> Int { +// // set pivotValue to firstElement +// var pivotValue = 0 +// if medianPivot { +// var pivotValues:[Int] = [] +// pivotValues.append(arr[0]) +// pivotValues.append(arr[arr.count-1]) +// pivotValues.append(arr[arr.count/2]) +// let maximum = pivotValues.maxElement() +// let minimum = pivotValues.minElement() +// +// for number in pivotValues{ +// if number != maximum && number != minimum{ +// pivotValue = number +// } +// } +// } +// else{ +// pivotValue = arr[firstIdx] +// } +// +// // set leftMark +// var leftMark = firstIdx + 1 +// // set rightMark +// var rightMark = lastIdx +// /* +// as leftMark and rightMark close in on each other, +// swap the items that are greater than the pivot value +// on the left side with the items that are less than the pivot +// value on the right side. Stop when rightMark crosses leftMark +// */ +// while leftMark <= rightMark { +// while arr[leftMark] < pivotValue && leftMark <= rightMark { +// leftMark += 1 +// } +// while arr[rightMark] > pivotValue && leftMark <= rightMark{ +// rightMark -= 1 +// } +// if leftMark < rightMark { +// swap(&arr[leftMark], &arr[rightMark]) +// } +// } +// // set the correct value at the splitPoint +// if firstIdx != rightMark { +// swap(&arr[firstIdx], &arr[rightMark]) +// } +// return rightMark // return the splitPoint +// +// +//} +//func mergeSort(var unsortedArray:Array)->Array{ +// if unsortedArray.count < 2 { +// return unsortedArray +// } +// +// let pivot:Int = unsortedArray.count/2 +// let leftArray:Array = Array(unsortedArray[0.. = Array(unsortedArray[pivot..,B:Array)->Array{ +// var leftIndex = 0 +// var rightIndex = 0 +// var orderedArray:Array = [] +// while leftIndex B[rightIndex] { +// orderedArray.append(B[rightIndex++]) +// } +// else { +// orderedArray.append(A[leftIndex++]) +// orderedArray.append(B[rightIndex++]) +// } +// } +// orderedArray = orderedArray + Array(A[leftIndex..())) { +// let start = NSDate() +// block() +// let end = NSDate() +// print(end.timeIntervalSinceDate(start)) +//} +// +////run your function/code inside the block below +//profile({ +// //quickSort(&numbersArray) //0.00165402889251709 +// //quickSort(&numbersArray, medianPivot: true) //0.00196903944015503 +// //mergeSort(numbersArray) +// print("hi") +// +//}) + + +/* 5) In quicksort, the elements are sorted while the array is being split. In mergesort, the elements are placed in order when we merge two sorted lists. +*/ + + +/* 6) Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced. + Good examples: () [] () ([]()[]) + Bad examples: ( (] ([)] +*/ + +func isBal(inout str: [String]) -> Bool { + var container = [String]() + + for char in str{ + if char == "(" { + container.append(char) + } + else if char == ")" && container.contains("("){ + container.removeAtIndex(container.indexOf("(")!) + } + + else if char == "[" { + container.append(char) + } + else if char == "]" && container.contains("["){ + container.removeAtIndex(container.indexOf("[")!) + } + } + + return container.isEmpty + + +} + +var arr = ["(", ")", "[", "]", "(", ")", "(", "[", "]", "(", ")", "[", "]", ")"] + +isBal(&arr) + diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift new file mode 100644 index 0000000..0f5e88f --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift @@ -0,0 +1,103 @@ +import UIKit + +//1) Write good hash functions for the following data types. Justify your choices and how they avoid collisions. + + +// a) Int + +func hash(x: Int) -> Int { + return x +} + +// this would ensure that there is a unique list of elements that are hashed to their own indexes. + + + +//b) +//struct Point: Hashable { +// let x, y: Int +//} +// +//func hash(point: Point) -> Int { +// +// return (x + y)(x + y)/2 +//} + + + +// c) Array +//sum of all elements in the array + +/* 2) +You moderate a popular mobile device discussion forum. You want to cut down on the vitriol and make your work easier, so you decide to implement a blacklist based system to automatically reject or approve posts. For example: + +moderate("I would never use a crApple product!") // false (reject) +moderate("I wish all these FANDROIDS would just shut up!") // false +moderate("M$ is the worst, Linux rules!") // false +moderate("Can’t we all just get along?") // true (approve) + +Write moderate(message: String) -> Bool, using a built-in Swift Set to manage your blacklist. Make your method case insensitive; it should block the word no matter what combination of upper and lowercase letters is used. + +*/ + +func moderate(input: String) -> Bool { + let badWords = Set(["crapple", "fandroids", "worst", "m$", "shut up"]) + + let cin = Set(input.lowercaseString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "!, "))) + + let containsBadWords = badWords.intersect(cin) + + + return containsBadWords.isEmpty + +} + +moderate("I would never use a crAPPLE product!") +moderate("Can’t we all just get along?") + +//3) + +protocol PhoneBookProtocol { + mutating func addPerson(name: String, phoneNumber: String) + mutating func removePerson(name: String) + mutating func importFrom(oldPhonebook: [(String, String)]) + func findPerson(name: String) -> String // Return phone # +} + +class PhoneBook: PhoneBookProtocol { + var phoneBook = [String: String]() + + func addPerson(name: String, phoneNumber: String) { + phoneBook[name] = phoneNumber + } + + func removePerson(name: String) { + phoneBook.removeValueForKey(name) + } + + func importFrom(oldPhonebook: [(String, String)]) { + for i in 0.. String { + + if ((phoneBook[name]) != nil){ + return phoneBook[name]! + } + else{ + return "not found" + } + + } +} + +var phoneBook = PhoneBook() + +phoneBook.addPerson("Mike", phoneNumber: "21233444") +phoneBook.addPerson("Michael", phoneNumber: "83333840") +phoneBook.addPerson("Jil", phoneNumber: "98397103") +phoneBook.findPerson("Jil") +phoneBook.importFrom([("Henna", "23840278")]) +phoneBook.removePerson("Michael") \ No newline at end of file diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground b/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline b/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..dbf57bb 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -10,14 +10,117 @@ Use the link here to get the questions. Then code your solutions below. If it https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit +*/ -1) +// 1) Given an integer N, there is a list of size N-1 that is missing one number from 1 - N(inclusive). Find that number. +func findMissingNumber(max : Int, numbers : [Int]) -> Int{ + + // for i in 1...max{ + // if !numbers.contains(i){ + // return i + // } + // + // } + // + // return -1 + + let totalSum = max * (max + 1) / 2 + var totalSumOfList = 0 + for i in numbers { + totalSumOfList += i + } + return totalSum - totalSumOfList + +} -2) +findMissingNumber(5, numbers: [1, 4, 3, 2]) -3) -4) +// 2) Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not +func hasDupes(numbers : [Int]) -> Bool{ + // let set = Set(numbers) + // + // return numbers.count == set.count ? true : false + + for var i = 0; i Int { + + // return (arr1.minElement()! > arr2.minElement()! ? arr2.minElement()! : arr1.minElement()!) + + var minArr1 = arr1.first + var minArr2 = arr2.first + + for var i = 0; i < arr1.count; i++ { + + if arr1[i] minArr2! ? minArr2 : minArr1)! +} + +findMin([1, 2, 2, 3, 4], arr2: [-1, 2, 2, 3, 4]) + + +// 4) Check to see if an integer is a palindrome, no casting + + +func isPalindromeInt (var number : Int) -> Bool { + + if number < 0{ + return false + } + + // initialize how many zeros + var div = 1; + while ( number/div >= 10) { + div *= 10 + } + + while (number != 0) { + var left = number / div; + var right = number % 10; + + if (left != right){ + return false + } + + number = (number % div) / 10; + div /= 100; + } + + return true; + +} + +isPalindromeInt(232) \ No newline at end of file diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..81cd23a 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,20 +11,118 @@ Use the link here to get the questions. Then code your solutions below. If it https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/edit#heading=h.za36ai6n5fth -1) +1) Memory access takes 1 picosecond + Math operations take 3 picoseconds + Storing data in memory takes 10 picoseconds + Friends algo takes 200 picoseconds to run. + +How long would my computer take to execute the following code if the input image is 1000px wide by 2000px tall? + 10 3 3 + for (int i = 0; i < width; i++) { + 10 3 3 + for (int j = 0; j < height; j++) { + 1 200 + [image[i][j] makeMoreAwesome]; + } + } + return image; -2) +((10 + 3 + 3) * 1000) * ((10 + 3 + 3 + 201) * 2000) = 16000 * 488000 = 7.8* 10^9 + + What if it’s n by m? + 16n * 217m + +b) O(n^2) + +c) O(n^4) + +2) + a) O(n^4) + b) O(n) + c) O(n^2) + d) O(n) 3) + a) Tree + + b) Hash Table + + c) Tree + + +4)*/ + +func factorial(n : Int) -> Int { + + if n <= 1 { + return 1 + } + + return factorial(n-1)*n +} + + +/* time complexity: O(n) */ + + +/* + +5) */ +func product(number: Int, times: Int) -> Int { + + var totProduct = 0 + + for _ in 1...times{ + totProduct = totProduct + number + } + + return totProduct +} +product(2, times: 3) + +/* complexity is O(n)*/ + +/* -4) +6)Russian Peasant Multiplication -5) +Write each number at the head of a column. +Double the number in the first column, and halve the number in the second column. +If the number in the second column is odd, divide it by two and drop the remainder. +If the number in the second column is even, cross out that entire row. +Keep doubling, halving, and crossing out until the number in the second column is 1. +Add up the remaining numbers in the first column. The total is the product of your original numbers. +source: http://polynomialtimes.com/algorithms/decrease-and-conquer/russian-peasant-multiplication/ +*/ + +func russain(n1: Int, n2: Int) -> Int{ + if n1 == 1 { + return n2 + } + + if n1 % 2 == 1{ + return n2 + russain(n1 / 2, n2: 2 * n2) + } + + return russain(n1 / 2, n2: 2 * n2) -6) +} -7) +russain(4, n2: 2) +/* O(log(n)) + +any time you divide by two its likely a log(n) algorithm */ +/* + +7)Using the technique from exercise 4, profile the built in sorting method in objective C (use an NSMutableArray and google how to sort an array of numbers in objective C). Graph the result. Use spreadsheet formulas to add graph lines for n, n2, and n*log(n). (You’ll have to modify the factors to make them fit in the graph window and to be close to the graph of method execution time). Show that the sort method best fits n * log(n). + + +https://docs.google.com/spreadsheets/d/1CqF_l1DJCnWxTE3uEYb13lvi2KiqtKBONENwqYunqq4/edit?usp=sharing + +*/ + + diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift new file mode 100644 index 0000000..7bd9066 --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -0,0 +1,93 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + + +/* + + +Question 1: https://www.hackerrank.com/challenges/minimum-draws + + +Copy and paste your code: + +let firstLine = readLine(stripNewline: true)! +let T = Int(firstLine)! + +for _ in (0.. + + + \ No newline at end of file diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata b/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline b/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HackkerRankStuff.playground/Contents.swift b/HackkerRankStuff.playground/Contents.swift new file mode 100644 index 0000000..3bee8e5 --- /dev/null +++ b/HackkerRankStuff.playground/Contents.swift @@ -0,0 +1,9 @@ +let testCases = Int(readLine(stripNewline: true)!)! + +for var x = 0; x < testCases; x++ { + + //var first = Int(readLine(stripNewline: true)!)! + + print (readLine(stripNewline: true)!) + +} \ No newline at end of file diff --git a/HackkerRankStuff.playground/contents.xcplayground b/HackkerRankStuff.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HackkerRankStuff.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HackkerRankStuff.playground/playground.xcworkspace/contents.xcworkspacedata b/HackkerRankStuff.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HackkerRankStuff.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HackkerRankStuff.playground/timeline.xctimeline b/HackkerRankStuff.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HackkerRankStuff.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Recursion/Recursion.xcodeproj/project.pbxproj b/Recursion/Recursion.xcodeproj/project.pbxproj new file mode 100644 index 0000000..74977b0 --- /dev/null +++ b/Recursion/Recursion.xcodeproj/project.pbxproj @@ -0,0 +1,246 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + F0CD40CF1C59AA33000BF819 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0CD40CE1C59AA33000BF819 /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + F0CD40C91C59AA33000BF819 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + F0CD40CB1C59AA33000BF819 /* Recursion */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Recursion; sourceTree = BUILT_PRODUCTS_DIR; }; + F0CD40CE1C59AA33000BF819 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + F0CD40C81C59AA33000BF819 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + F0CD40C21C59AA33000BF819 = { + isa = PBXGroup; + children = ( + F0CD40CD1C59AA33000BF819 /* Recursion */, + F0CD40CC1C59AA33000BF819 /* Products */, + ); + sourceTree = ""; + }; + F0CD40CC1C59AA33000BF819 /* Products */ = { + isa = PBXGroup; + children = ( + F0CD40CB1C59AA33000BF819 /* Recursion */, + ); + name = Products; + sourceTree = ""; + }; + F0CD40CD1C59AA33000BF819 /* Recursion */ = { + isa = PBXGroup; + children = ( + F0CD40CE1C59AA33000BF819 /* main.swift */, + ); + path = Recursion; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + F0CD40CA1C59AA33000BF819 /* Recursion */ = { + isa = PBXNativeTarget; + buildConfigurationList = F0CD40D21C59AA33000BF819 /* Build configuration list for PBXNativeTarget "Recursion" */; + buildPhases = ( + F0CD40C71C59AA33000BF819 /* Sources */, + F0CD40C81C59AA33000BF819 /* Frameworks */, + F0CD40C91C59AA33000BF819 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Recursion; + productName = Recursion; + productReference = F0CD40CB1C59AA33000BF819 /* Recursion */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + F0CD40C31C59AA33000BF819 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0710; + LastUpgradeCheck = 0710; + ORGANIZATIONNAME = "Henna Ahmed"; + TargetAttributes = { + F0CD40CA1C59AA33000BF819 = { + CreatedOnToolsVersion = 7.1; + }; + }; + }; + buildConfigurationList = F0CD40C61C59AA33000BF819 /* Build configuration list for PBXProject "Recursion" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = F0CD40C21C59AA33000BF819; + productRefGroup = F0CD40CC1C59AA33000BF819 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + F0CD40CA1C59AA33000BF819 /* Recursion */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + F0CD40C71C59AA33000BF819 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F0CD40CF1C59AA33000BF819 /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + F0CD40D01C59AA33000BF819 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + F0CD40D11C59AA33000BF819 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + F0CD40D31C59AA33000BF819 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + F0CD40D41C59AA33000BF819 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + F0CD40C61C59AA33000BF819 /* Build configuration list for PBXProject "Recursion" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0CD40D01C59AA33000BF819 /* Debug */, + F0CD40D11C59AA33000BF819 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F0CD40D21C59AA33000BF819 /* Build configuration list for PBXNativeTarget "Recursion" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0CD40D31C59AA33000BF819 /* Debug */, + F0CD40D41C59AA33000BF819 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = F0CD40C31C59AA33000BF819 /* Project object */; +} diff --git a/Recursion/Recursion.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Recursion/Recursion.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..9e5d228 --- /dev/null +++ b/Recursion/Recursion.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Recursion/Recursion/main.swift b/Recursion/Recursion/main.swift new file mode 100644 index 0000000..e9ecdf9 --- /dev/null +++ b/Recursion/Recursion/main.swift @@ -0,0 +1,44 @@ +// +// main.swift +// Recursion +// +// Created by Henna Ahmed on 1/27/16. +// Copyright © 2016 Henna Ahmed. All rights reserved. +// + +import Foundation + +//Appendix C: File searching + + +func findFile(name: String, atPath: String) -> String { + let fileManager = NSFileManager.defaultManager() + + let contents = try! fileManager.contentsOfDirectoryAtPath(atPath) + + for fileOrDir in contents { + var isDir = ObjCBool(false); + let fullPath = atPath + "/" + fileOrDir + let exists = fileManager.fileExistsAtPath(fullPath, isDirectory: &isDir) + if exists && Bool(isDir) { + + let result = findFile(name, atPath: fullPath) + if result != "NOT FOUND"{ + return result + } + + } else if exists { + + if fileOrDir == name { + return fullPath + } + + } else { + print("NEITHER: " + fileOrDir) + } + } + return "NOT FOUND" +} + +print(findFile("email-collection.html", atPath: "/Users/hennaahmed/Desktop/")) + diff --git a/Trees.playground/Contents.swift b/Trees.playground/Contents.swift new file mode 100644 index 0000000..36bdf2d --- /dev/null +++ b/Trees.playground/Contents.swift @@ -0,0 +1,61 @@ +//: Playground - noun: a place where people can play + +import UIKit + +//https://docs.google.com/document/d/1te7mLS06MEYwETFSbVBqMrIzJ43GTEo5uuCiWdB0fyE/edit?usp=drivesdk + +class Node { + let value: T + var left: Node? + var right: Node? + init(value: T) { + self.value = value + } +} + +extension Node: CustomStringConvertible { + var description: String { + return "\(value)" + } + var postorderDescription: String { + let lt = left?.postorderDescription ?? "" + let rt = right?.postorderDescription ?? "" + return lt + rt + description + } +} + +//1 + +func parseExpression(input: String) -> Node? { + // Your implementation here! + let operators: Set = ["+", "-", "*", "/"] + var stack: [Node] = [] + for character in input.characters { + if operators.contains(character){ + let new : Node = Node(value: character) + new.left = stack.removeFirst() + new.right = stack.removeFirst() + + stack.append(new) + } + else{ + let new : Node = Node(value: character) + new.left = nil + new.right = nil + stack.append(new) + + } + } + let node = stack.removeFirst(); + return node; +} + +var node = parseExpression("ab+cde+**") +print(node?.postorderDescription) + + + +//Bonus1 + + +//Bonus2 diff --git a/Trees.playground/contents.xcplayground b/Trees.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/Trees.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Trees.playground/playground.xcworkspace/contents.xcworkspacedata b/Trees.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Trees.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Trees.playground/timeline.xctimeline b/Trees.playground/timeline.xctimeline new file mode 100644 index 0000000..c174415 --- /dev/null +++ b/Trees.playground/timeline.xctimeline @@ -0,0 +1,11 @@ + + + + + + + diff --git a/graphs.playground/Contents.swift b/graphs.playground/Contents.swift new file mode 100644 index 0000000..87cc59f --- /dev/null +++ b/graphs.playground/Contents.swift @@ -0,0 +1,37 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + +// 1. Yes, CBAFDEGFCGHC. Every edge is visited once in this path. +// 2. No +//3 + +/* + + A B C D E F +A 0 1 0 1 0 1 +B 1 0 0 0 1 0 +C 0 0 0 1 0 0 +D 1 0 1 0 1 1 +E 0 1 0 1 0 0 +F 1 0 0 1 0 0 + + +*/ + +//4 + +/* + + A B C D E F +A 0 1 0 0 0 1 +B 0 0 0 0 1 0 +C 0 0 0 0 0 0 +D 0 0 1 0 0 0 +E 0 0 0 1 0 0 +F 0 0 0 1 0 0 + + +*/ \ No newline at end of file diff --git a/graphs.playground/contents.xcplayground b/graphs.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/graphs.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/graphs.playground/playground.xcworkspace/contents.xcworkspacedata b/graphs.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/graphs.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/graphs.playground/timeline.xctimeline b/graphs.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/graphs.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + +