From 220050de0bbb2317571fa54e7c97051f467e3abf Mon Sep 17 00:00:00 2001 From: Varindra Date: Wed, 13 Jan 2016 21:36:51 -0500 Subject: [PATCH 1/9] swift hw done --- .../Contents.swift | 108 +++++++++++++++++- .../contents.xcplayground | 2 +- 2 files changed, 104 insertions(+), 6 deletions(-) diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..2aba62a 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -9,15 +9,113 @@ var str = "Hello, playground" Use the link here to get the questions. Then code your solutions below. If it does not require code, just write your answer in comments. https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit +*/ +//: ### Questions +//: 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. +//: +//: 2. Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not +//: +//: 3. Given two lists, find the smallest value that exists in both lists. +//: +//: L1 = [1,2,5,9] L2 = [9, 20 , 5] +//: +//: 4. Check to see if an integer is a palindrome don’t use casting +//: ### Solutions + +//: #1 + +func sumOfNConsecutives(n : Int) -> Int{ + return (n*(n+1))/2 +} + +func findMissingNumberFrom(list : [Int]) -> Int { + + let sumOfConsecutives = sumOfNConsecutives(list.count) + var sumOfList : Int = 0 + for i in list { + sumOfList += i + } + + return sumOfConsecutives - sumOfList +} + +//: #2 + +func hasDuplicates(var list: [Int]) -> Bool { + + for i in 0.. Int? { + + guard list1.count != 0 && list2.count != 0 else{ + return nil + } + + let set1 = Set(list1) + let set2 = Set(list2) + let intersection = Array(set1.intersect(set2)) + + return findMin(intersection) + +} + +func findMin(list: [Int]) -> Int { + + var min: Int = list[0] + + for i in list{ + if i < min { + min = i + } + } + return min +} + +//: #4 + +func isIntegerPalindrome(var value : Int) -> Bool { + + if value < 10 { + return true + } + + var n: Int = 0 + + while n <= value { + + if n == value { + return true + } + + n = n*10 + value%10 + value = value/10 + } + + return false + +} -1) -2) -3) -4) -*/ diff --git a/HWfrom1-09-16(SwiftIntro).playground/contents.xcplayground b/HWfrom1-09-16(SwiftIntro).playground/contents.xcplayground index 5da2641..3596865 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/contents.xcplayground +++ b/HWfrom1-09-16(SwiftIntro).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file From 0c5c2236fa54e54bbc7884ed38ba9ecde8afd2ab Mon Sep 17 00:00:00 2001 From: Varindra Date: Thu, 14 Jan 2016 01:14:10 -0500 Subject: [PATCH 2/9] algo section done.. sorta, didnt do 7 --- .../Contents.swift | 231 +++++++++++++++++- .../contents.xcplayground | 2 +- 2 files changed, 225 insertions(+), 8 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..45fddaf 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -10,21 +10,238 @@ 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 +Big O Homework +//: 1. With my new top of the line XJ452 supercomputer, memory access takes 1 picosecond, math operations take 3 picoseconds, and storing data in memory takes 10 picoseconds. My friend wrote a filter that makes a pixel more awesome, and 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? What if it’s n by m? +*/ + +/* +Pixel **awesomeFilter(Pixel image[][], int width, int height) { +for (int i = 0; i < width; i++) { +for (int j = 0; j < height; j++) { +[image[i][j] makeMoreAwesome]; +} +} +return image; +} +*/ + 10 + (1 3 1 1 3 ) x1000 10 + 5*1001 + 4*1000 +for (int i = 0; i < width; i++) { + 10 (1 3 1 1 3) x2000 (10 + 5*2001 + 207*2000)*1000, 414,000,000 + for (int j = 0; j < height; j++) { + 1 1 1 200 + [image[i][j] makeMoreAwesome]; + } +} +return image; 1 +1 +} + +10 + 5005 + 4000 + (10 + 10005 + 414,000)*1000 + 1 +9,015 + 10,000 + 10,005,000 + 414,000,000 + 1 = 424,024,016 + +Without last bool check: 9010 + 10,000,000, 414,000,000 + 1 = 424,009,011 + +n x m = 10 + 5*(n+1) +4*(n) + 10*(n) + 5*(m+1)(n) +207(m)*(n) + 1 +n x m = 10 + 9(n) + 10(n) + 212(m)(n) + 1 + +/* +//: b) What is the time complexity of this method, expressed in big O notation? Assume the image is square, and both dimensions are ‘n’ +*/ +Answer: O(n^2) +/* +//: c) My friend sends me an improved version of his algorithm, makeEvenMoreAwesome, that takes into account the pixels around the image. He says it’s O(n2) in the amount of pixels in the image. What is the new time complexity of the method? +*/ +Answer: O(n^4) if it is a square + +//: ##Question 2 +/* +//: a) If foo(xs) is a function with time complexity n (where n is the size of the input array), and bar(xs) is a function with time complexity n^2, what is the time complexity of each of the following snippets of code or algorithms? +*/ + +for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + foo(xs); + } +} +for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + bar(xs); + } +} +for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + // do cool stuff + } +} + +Answer: O(n^4) + +//: b) +int frobnicate(ys, m) { + if (m == 0) { + return 0; + } + return ys[m] + frobnicate(ys, m - 1); +} + +frobnicate(xs, n); + +/* +Tip: Write down a table with n from 0 to 5 and trace through to find out how many times frobnicate is called with each value of n. +*/ + +Answer: O(n) + + +/* +//: c) An algorithm that takes as its input a list of friends of length n, filters out duplicates using a method similar to our hasDuplicates method, sorts the list using merge sort (see bigocheatsheet.com), then prints each item to the screen. +*/ + +Answer: O(n^2) + +/* + +//: d) An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). +*/ + +Answer: O(n) + + +//: 3. Look at the complexities for some common data structures at bigocheatsheet.com. Pick a good data structure for each of the following scenarios (there are sometimes multiple answers): + +/* +//: a) You get a large dataset of points of interest from an API when your app first runs. You build it once at the beginning, and then have to search it many times while the user pans around a map. +*/ + +Answer: tree + +/* +//: b) You get a small dataset of points of interest from an API every time the user pans the map. You construct the data set many times and only render it once, then you discard it and do another API search. +*/ + +Answer: Hash Table -1) +/* +Tip: Constructing a dataset of size n means you have to call the data structure’s insert method n times. So if the data structure has an insert method that takes O(n2), the time to build it all from scratch is O(n3). + +*/ + +/* +//: c) You used a linked list for your music app’s playlist feature, but now when people search their playlist, there’s a noticeable lag before loading results. Your competitor’s app is buttery smooth when searching, even showing results as you type. What data structure would allow you to more quickly search without compromising too much on the speed of inserting and deleting tracks, even in the worst case? +*/ + +Answer: Tree + +//: ### 4. Write an algorithm using one of the methods from exercise 1 (your choice) to calculate the factorial of a number n. What is the time complexity of your method in terms of the input value? + +//Non recursive +func nonRecFactorial(n : Int) -> Int { + + guard n>=1 else{ + return 1 + } + + var product = 1 + + for i in 1...n{ + product *= i + } + + return product + +} + +// Recursive +func factorial(n : Int) -> Int { + + if n <= 1 { + return 1 + } + + return factorial(n-1)*n +} + +O(n) + + +//: 5.Write an Objective C or Swift function to multiply two numbers without using the * operator. Use the grade school method of multiplying by doing repeated addition. For instance, 5 * 8 = 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 = 40. Find the big O of your function in terms of n and m (the two operands). -2) +func productByAddition(x: Int,_ y: Int) -> Int { -3) + guard x != 0 || y != 0 else{ + return 0 + } -4) + return x + productByAddition(x, y-1) -5) +} -6) +func nonRecProdByAddition(x: Int, _ y: Int) -> Int { -7) + let isNegative = y < 0 + var sum = 0 + + let multCap = isNegative ? 0-y : y + + for var i = 1; i <= multCap; i++ { + sum += x + } + + return isNegative ? 0-sum : sum + +} + +O(n) +/* +//: 6. Look up Russian Peasant Multiplication. It’s a faster way to multiply numbers, especially on a binary computer (like yours!). Implement a new multiplication function using this technique and find the big O of your method. If you have trouble with implementing this, write a flow chart and find the big O based on that. (But it’s more satisfying to implement it and run it) + +Tip: Run through the method by hand a few times to see how it works and verify to yourself that it does. It’s a non-intuitive algorithm. This will hopefully also make the time complexity more clear. +*/ + +func russianPeasantMultiplication(first: Int, second: Int) -> Int { + + var decreasingArray = [first] + var increasingArray = [second] + var sum = 0 + + //Appending + while true { + + decreasingArray.append(decreasingArray.last! / 2) + increasingArray.append(increasingArray.last! * 2) + + if decreasingArray.last! == 1{ + break + } + } + + //removal + for var i = 0 ; i < decreasingArray.count; { + + if decreasingArray[i]%2 == 0 { + decreasingArray.removeAtIndex(i) + increasingArray.removeAtIndex(i) + continue + } + + i++ + } + + //sum + for numb in increasingArray { + sum += numb + } + + return sum + +} + +O(n) + +/* +//: 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). */ diff --git a/HWfrom1-10-016(BigO).playground/contents.xcplayground b/HWfrom1-10-016(BigO).playground/contents.xcplayground index 5da2641..89da2d4 100644 --- a/HWfrom1-10-016(BigO).playground/contents.xcplayground +++ b/HWfrom1-10-016(BigO).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file From 60769e19553267113be2159e6c3da083a4b990df Mon Sep 17 00:00:00 2001 From: Varindra Date: Sat, 16 Jan 2016 01:53:22 -0500 Subject: [PATCH 3/9] complete --- .../Contents.swift | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..5590e01 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -10,20 +10,51 @@ var str = "Hello, playground" Question 1: https://www.hackerrank.com/challenges/minimum-draws Copy and paste your code: +*/ +func maxDraws(pairs: Int) -> Int? { + + guard pairs>0 else{ + return nil + } + + if pairs == 1{ + return 2 + } + else{ + return pairs/2 + 1 + } -What is the big O runtime of your code?: +} +/* +What is the big O runtime of your code?: O(1) Question 2: https://www.hackerrank.com/challenges/handshake Copy and paste your code: - -What is the big O runtime of your code?: +*/ +func handshakes(people: Int)-> Int{ + return (people-1)*(people)/2 +} +/* +What is the big O runtime of your code?: O(1) Question 3: https://www.hackerrank.com/challenges/connecting-towns Copy and paste your code: +*/ +func totalPaths(seq : [Int]) -> Int { + + var paths = seq.count>0 ? 1 : 0 -What is the big O runtime of your code?: + for n in seq { + paths *= n + } + + return paths +} + +/* +What is the big O runtime of your code?: O(n) */ From e98b6c3db4a34eaa1aef0221851149cbf69bf733 Mon Sep 17 00:00:00 2001 From: Varindra Date: Thu, 21 Jan 2016 01:26:03 -0500 Subject: [PATCH 4/9] upstream pull --- HWfrom1-10-016(BigO).playground/Contents.swift | 2 +- HWfrom1-10-016(BigO).playground/contents.xcplayground | 2 +- HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 45fddaf..d5d5c2a 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -238,7 +238,7 @@ func russianPeasantMultiplication(first: Int, second: Int) -> Int { } -O(n) +O(logn) /* //: 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). diff --git a/HWfrom1-10-016(BigO).playground/contents.xcplayground b/HWfrom1-10-016(BigO).playground/contents.xcplayground index 89da2d4..3596865 100644 --- a/HWfrom1-10-016(BigO).playground/contents.xcplayground +++ b/HWfrom1-10-016(BigO).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 5590e01..f6fdf38 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -25,6 +25,7 @@ func maxDraws(pairs: Int) -> Int? { } } + /* What is the big O runtime of your code?: O(1) From 7e3dd3b5e467db6bd46cac3598aa039599cda71f Mon Sep 17 00:00:00 2001 From: Varindra Date: Thu, 21 Jan 2016 17:28:34 -0500 Subject: [PATCH 5/9] sorta done --- .../Contents.swift | 87 ++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 5d51051..88659a8 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -11,16 +11,99 @@ Work on your solutions here. Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw -1) +1) +*/ +let board : [[Int?]] = [ [1, 7, 9, 8, 5, 4, 3, nil, 6], + [nil, 1, 4,nil,5, 4, 3, nil, 6], + [1, nil, 9, 8, 5, 4, 3, nil, 6], + [1, 2, 9, 8, 5, 4, 3, nil, 6], + [1, nil, 9, 8, 5, 4, 3, nil, 6], + [1, nil, 9, 8, 5, 4, 3, nil, 6], + [1, nil, 9, 8, 5, 4, 3, nil, 6], + [1, nil, 9, 8, 5, 4, 3, nil, 6], + [1, nil, 9, 8, 5, 4, 3, nil, 6] ] +func getValidNumbers(sudokuBoard:[[Int?]], row:Int, col:Int) -> [Int] { + let completeSet = Set.init(arrayLiteral: 1,2,3,4,5,6,7,8,9) + var alreadyUsedSet = Set() + + for i in 0.. [[Int]] { + var rotatedAray = [[Int]]() + for i in 0..= 0; j-- { + colToRow.append(matrix[j][i]) + } + rotatedAray.append(colToRow) + } + + return rotatedAray +} + +let array = [[1,3,4], + [5,2,6], + [9,8,4]] +rotateNinetyDegrees(array) + +/* 3) +*/ + +func sortFour(inout array: [Int]) { + + if array[0] > array[1] { + let temp = array[1] + array[1] = array[0] + array[0] = temp + } + + if array[2] > array[3] { + let temp = array[3] + array[3] = array[2] + array[2] = temp + } + + +} + -*/ \ No newline at end of file From 1831de321efa9be2fc567598b79c16e57abbdce8 Mon Sep 17 00:00:00 2001 From: Varindra Date: Sun, 31 Jan 2016 01:29:18 -0500 Subject: [PATCH 6/9] upstreamfetch --- .../Contents.swift | 86 +++++- Hw1-23/Hw1-23.xcodeproj/project.pbxproj | 246 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + Hw1-23/Hw1-23/main.swift | 33 +++ 4 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 Hw1-23/Hw1-23.xcodeproj/project.pbxproj create mode 100644 Hw1-23/Hw1-23.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Hw1-23/Hw1-23/main.swift diff --git a/HWfrom1-23-16(Recursion).playground/Contents.swift b/HWfrom1-23-16(Recursion).playground/Contents.swift index 4776cab..ba15f9e 100644 --- a/HWfrom1-23-16(Recursion).playground/Contents.swift +++ b/HWfrom1-23-16(Recursion).playground/Contents.swift @@ -17,4 +17,88 @@ var str = "Hello, playground" -//3 \ No newline at end of file +//3 + +// Calebs Questions: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit# + +//1 + +func iterativeFib(index: Int) -> Int { + + guard index >= 2 else { + return 1 + } + + var fibBackTwo = 1 + var fibBackOne = 1 + var fibOfIndex = Int() + for _ in 2...index { + fibOfIndex = fibBackTwo + fibBackOne + fibBackTwo = fibBackOne + fibBackOne = fibOfIndex + } + + return fibOfIndex +} + +//2 + +import Foundation + +var stepNum = 0 +func tryStep() -> 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 +} + +func stepUp() { + + let progress = tryStep() + + switch progress { + case 0: + stepUp() + case 1: + return + case -1: + stepUp() + stepUp() + default: + return + } +} + +stepUp() +print(stepNum) +stepUp() +print(stepNum) + +//3 + +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) { + // YOUR CODE HERE + print("DIR: " + fileOrDir) + } else if exists { + // YOUR CODE HERE + print("FILE: " + fileOrDir) + } else { + print("NEITHER: " + fileOrDir) + } + } + return "NOT FOUND" +} + diff --git a/Hw1-23/Hw1-23.xcodeproj/project.pbxproj b/Hw1-23/Hw1-23.xcodeproj/project.pbxproj new file mode 100644 index 0000000..636ab17 --- /dev/null +++ b/Hw1-23/Hw1-23.xcodeproj/project.pbxproj @@ -0,0 +1,246 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + C7E23E831C56F2CD003512E3 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7E23E821C56F2CD003512E3 /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + C7E23E7D1C56F2CD003512E3 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + C7E23E7F1C56F2CD003512E3 /* Hw1-23 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Hw1-23"; sourceTree = BUILT_PRODUCTS_DIR; }; + C7E23E821C56F2CD003512E3 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + C7E23E7C1C56F2CD003512E3 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + C7E23E761C56F2CD003512E3 = { + isa = PBXGroup; + children = ( + C7E23E811C56F2CD003512E3 /* Hw1-23 */, + C7E23E801C56F2CD003512E3 /* Products */, + ); + sourceTree = ""; + }; + C7E23E801C56F2CD003512E3 /* Products */ = { + isa = PBXGroup; + children = ( + C7E23E7F1C56F2CD003512E3 /* Hw1-23 */, + ); + name = Products; + sourceTree = ""; + }; + C7E23E811C56F2CD003512E3 /* Hw1-23 */ = { + isa = PBXGroup; + children = ( + C7E23E821C56F2CD003512E3 /* main.swift */, + ); + path = "Hw1-23"; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + C7E23E7E1C56F2CD003512E3 /* Hw1-23 */ = { + isa = PBXNativeTarget; + buildConfigurationList = C7E23E861C56F2CD003512E3 /* Build configuration list for PBXNativeTarget "Hw1-23" */; + buildPhases = ( + C7E23E7B1C56F2CD003512E3 /* Sources */, + C7E23E7C1C56F2CD003512E3 /* Frameworks */, + C7E23E7D1C56F2CD003512E3 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "Hw1-23"; + productName = "Hw1-23"; + productReference = C7E23E7F1C56F2CD003512E3 /* Hw1-23 */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + C7E23E771C56F2CD003512E3 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0710; + LastUpgradeCheck = 0710; + ORGANIZATIONNAME = "Varindra Hart"; + TargetAttributes = { + C7E23E7E1C56F2CD003512E3 = { + CreatedOnToolsVersion = 7.1; + }; + }; + }; + buildConfigurationList = C7E23E7A1C56F2CD003512E3 /* Build configuration list for PBXProject "Hw1-23" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = C7E23E761C56F2CD003512E3; + productRefGroup = C7E23E801C56F2CD003512E3 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + C7E23E7E1C56F2CD003512E3 /* Hw1-23 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + C7E23E7B1C56F2CD003512E3 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + C7E23E831C56F2CD003512E3 /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + C7E23E841C56F2CD003512E3 /* 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; + }; + C7E23E851C56F2CD003512E3 /* 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; + }; + C7E23E871C56F2CD003512E3 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + C7E23E881C56F2CD003512E3 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + C7E23E7A1C56F2CD003512E3 /* Build configuration list for PBXProject "Hw1-23" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C7E23E841C56F2CD003512E3 /* Debug */, + C7E23E851C56F2CD003512E3 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + C7E23E861C56F2CD003512E3 /* Build configuration list for PBXNativeTarget "Hw1-23" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C7E23E871C56F2CD003512E3 /* Debug */, + C7E23E881C56F2CD003512E3 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = C7E23E771C56F2CD003512E3 /* Project object */; +} diff --git a/Hw1-23/Hw1-23.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Hw1-23/Hw1-23.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ba867fa --- /dev/null +++ b/Hw1-23/Hw1-23.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Hw1-23/Hw1-23/main.swift b/Hw1-23/Hw1-23/main.swift new file mode 100644 index 0000000..9e6a7a7 --- /dev/null +++ b/Hw1-23/Hw1-23/main.swift @@ -0,0 +1,33 @@ +// +// main.swift +// Hw1-23 +// +// Created by Varindra Hart on 1/25/16. +// Copyright © 2016 Varindra Hart. All rights reserved. +// + +import Foundation + +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) { + // YOUR CODE HERE + print("DIR: " + fileOrDir) + } else if exists { + // YOUR CODE HERE + print("FILE: " + fileOrDir) + } else { + print("NEITHER: " + fileOrDir) + } + } + return "NOT FOUND" +} + +print(findFile("awesome-idea.txt", atPath: "/Users/varindra/Documents")) + From 8ff482b7252d37e311e792cbeb491d29b9c320c5 Mon Sep 17 00:00:00 2001 From: Varindra Date: Tue, 2 Feb 2016 00:26:52 -0500 Subject: [PATCH 7/9] recursive sort --- .../Contents.swift | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/HWFrom1-28-16(Merge Sort).playground/Contents.swift b/HWFrom1-28-16(Merge Sort).playground/Contents.swift index afdc1b6..e7bd139 100644 --- a/HWFrom1-28-16(Merge Sort).playground/Contents.swift +++ b/HWFrom1-28-16(Merge Sort).playground/Contents.swift @@ -4,3 +4,57 @@ //Insert code here: + +func recursiveInsertionSort(inout arr: [T], index: Int){ + + if index == arr.count { + return + } + + findAndInsert(&arr, fromIndex: index, compared: 0) + recursiveInsertionSort(&arr, index: index+1) +} + +func findAndInsert(inout arr: [T], fromIndex: Int, compared: Int) { + + if fromIndex == 0 || compared == fromIndex { + return + } + + if arr[compared] > arr[fromIndex] { + let temp = arr[fromIndex] + arr.removeAtIndex(fromIndex) + arr.insert(temp, atIndex: compared) + return + } + + findAndInsert(&arr, fromIndex: fromIndex, compared: compared+1) +} + +var array = [8,2,33,1,13,66] +recursiveInsertionSort(&array, index: 0) + +func recursiveSelectionSort(inout arr: [T], index: Int) { + + if index == arr.count {return} + + let indexOfMin = indexOfNextMin(arr, from: index, lastMin: index) + swap(&arr[index], &arr[indexOfMin]) + recursiveInsertionSort(&arr, index: index+1) + +} + +func indexOfNextMin(arr: [T], from: Int, var lastMin: Int) -> Int { + + if from == arr.count {return lastMin} + + if arr[from] < arr[lastMin] { + lastMin = from + } + return indexOfNextMin(arr, from: from+1, lastMin: lastMin) +} + +var array2 = [2,41,13,63,12,6,1] +recursiveSelectionSort(&array2, index: 0) + + From 04b723e98cfdf9f00471a36afa8dcc98b591a468 Mon Sep 17 00:00:00 2001 From: Varindra Date: Sat, 6 Feb 2016 12:42:44 -0500 Subject: [PATCH 8/9] stack/queue --- .../Contents.swift | 121 +++++++++++++++++- 1 file changed, 118 insertions(+), 3 deletions(-) diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift index bcf8eda..a3d5d8f 100644 --- a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -3,14 +3,129 @@ //Answer the questions in the homework below //https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit# -//1) +//1)bubble/Selection/insertion: O(n^2) +// merge/quicksort: O(nlogn) -//2) +//2) less memory usage //3) +func quicksort(inout arr: [T], left: Int, right: Int){ + + if left >= right { + return + } + + let split = partition(&arr, left: left, right: right) + quicksort(&arr, left: left, right: split-1) + quicksort(&arr, left: split+1, right: right) + +} + +func partition(inout arr: [T],var left: Int,var right: Int) -> Int { + + var isLeft = true + + while left != right { + if isLeft{ + if arr[right] < arr[left] { + swap(&arr[right], &arr[left]) + isLeft = false + }else{ + right-- + } + } + else{ + if arr[left] > arr[right]{ + swap(&arr[left], &arr[right]) + isLeft = true + }else{ + left++ + } + } + + } + + return left +} + +var arr = [3,67,1,13,91,54,34,6,3,11] +quicksort(&arr, left: 0, right: arr.count-1) + //4) + //5) -//6) \ No newline at end of file + +//6) + +struct Stack { + var arrayStack = [T]() + + mutating func push(item: T) { + arrayStack.append(item) + } + + mutating func pop() -> T? { + if !isEmpty() { + return arrayStack.removeLast() + } + + return nil + } + + func isEmpty() -> Bool { + return arrayStack.count == 0 + } + + func peek() -> T? { + if !isEmpty() { + return arrayStack.last + } + return nil + } + +} + +let matches = ["(":")", "{":"}", "[":"]"] + +func isCompletelyMatched(str: String) -> Bool { + + var strStack = Stack() + + var index = 0 + + while index < str.characters.count { + + let currentPar = String(Array(str.characters)[index]) + + if strStack.isEmpty() { + + if matches[currentPar] == nil { + return false + } + else{ + strStack.push(currentPar) + } + } + else { + if matches[strStack.peek()!] == currentPar { + strStack.pop() + } + else if matches[currentPar] != nil { + strStack.push(currentPar) + } + else{ + return false + } + } + + index++ + } + + return strStack.isEmpty() +} + +isCompletelyMatched("{[()]}[][()]") +isCompletelyMatched("{))(}") From aafbfbe68b75b4babc02c36969e845b2dbe8f2ca Mon Sep 17 00:00:00 2001 From: Varindra Date: Tue, 9 Feb 2016 21:13:40 -0500 Subject: [PATCH 9/9] merge hw --- .../Contents.swift | 17 +++++++++- .../Contents.swift | 34 +++++++++++++++---- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/HWFrom1-24(Recursion).playground/Contents.swift b/HWFrom1-24(Recursion).playground/Contents.swift index 1c44504..070f08e 100644 --- a/HWFrom1-24(Recursion).playground/Contents.swift +++ b/HWFrom1-24(Recursion).playground/Contents.swift @@ -10,11 +10,26 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l //Question 1 +func fibIter(n: Int) -> Int{ + if n <= 1 { + return 1 + } + var oneBack = 1 + var twoBack = 1 + for _ in 2..(inout arr: [T], index: Int){ return } - findAndInsert(&arr, fromIndex: index, compared: 0) + findAndInsert(&arr, toIndex: index, compared: 0) recursiveInsertionSort(&arr, index: index+1) } -func findAndInsert(inout arr: [T], fromIndex: Int, compared: Int) { +func findAndInsert(inout arr: [T], toIndex: Int, compared: Int) { - if fromIndex == 0 || compared == fromIndex { + if toIndex == 0 || compared == toIndex { return } - if arr[compared] > arr[fromIndex] { - let temp = arr[fromIndex] - arr.removeAtIndex(fromIndex) + if arr[toIndex] < arr[compared] { + let temp = arr[toIndex] + arr.removeAtIndex(toIndex) arr.insert(temp, atIndex: compared) return } - findAndInsert(&arr, fromIndex: fromIndex, compared: compared+1) + findAndInsert(&arr, toIndex: toIndex, compared: compared+1) } var array = [8,2,33,1,13,66] recursiveInsertionSort(&array, index: 0) +//Find next min and swap with current index func recursiveSelectionSort(inout arr: [T], index: Int) { if index == arr.count {return} @@ -58,3 +59,22 @@ var array2 = [2,41,13,63,12,6,1] recursiveSelectionSort(&array2, index: 0) +func mult(a: Int,_ b: Int) -> Int{ + + if a == 0 || b == 0{ + return 0 + } + + if b == 1 { + return a + } + + if b < 0 { + return 0 - (a + mult(a, (0 - b) - 1)) + } + else{ + return a + mult(a, b - 1) + } +} + +mult(-3, -6) \ No newline at end of file