diff --git a/FindAFile/FindAFile.xcodeproj/project.pbxproj b/FindAFile/FindAFile.xcodeproj/project.pbxproj new file mode 100644 index 0000000..ea6d34b --- /dev/null +++ b/FindAFile/FindAFile.xcodeproj/project.pbxproj @@ -0,0 +1,246 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 290DA3C21C59AF590048F9FE /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290DA3C11C59AF590048F9FE /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 290DA3BC1C59AF590048F9FE /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 290DA3BE1C59AF590048F9FE /* FindAFile */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = FindAFile; sourceTree = BUILT_PRODUCTS_DIR; }; + 290DA3C11C59AF590048F9FE /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 290DA3BB1C59AF590048F9FE /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 290DA3B51C59AF590048F9FE = { + isa = PBXGroup; + children = ( + 290DA3C01C59AF590048F9FE /* FindAFile */, + 290DA3BF1C59AF590048F9FE /* Products */, + ); + sourceTree = ""; + }; + 290DA3BF1C59AF590048F9FE /* Products */ = { + isa = PBXGroup; + children = ( + 290DA3BE1C59AF590048F9FE /* FindAFile */, + ); + name = Products; + sourceTree = ""; + }; + 290DA3C01C59AF590048F9FE /* FindAFile */ = { + isa = PBXGroup; + children = ( + 290DA3C11C59AF590048F9FE /* main.swift */, + ); + path = FindAFile; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 290DA3BD1C59AF590048F9FE /* FindAFile */ = { + isa = PBXNativeTarget; + buildConfigurationList = 290DA3C51C59AF590048F9FE /* Build configuration list for PBXNativeTarget "FindAFile" */; + buildPhases = ( + 290DA3BA1C59AF590048F9FE /* Sources */, + 290DA3BB1C59AF590048F9FE /* Frameworks */, + 290DA3BC1C59AF590048F9FE /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = FindAFile; + productName = FindAFile; + productReference = 290DA3BE1C59AF590048F9FE /* FindAFile */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 290DA3B61C59AF590048F9FE /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0720; + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = "Ayuna Vogel"; + TargetAttributes = { + 290DA3BD1C59AF590048F9FE = { + CreatedOnToolsVersion = 7.2; + }; + }; + }; + buildConfigurationList = 290DA3B91C59AF590048F9FE /* Build configuration list for PBXProject "FindAFile" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 290DA3B51C59AF590048F9FE; + productRefGroup = 290DA3BF1C59AF590048F9FE /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 290DA3BD1C59AF590048F9FE /* FindAFile */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 290DA3BA1C59AF590048F9FE /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 290DA3C21C59AF590048F9FE /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 290DA3C31C59AF590048F9FE /* 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.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 290DA3C41C59AF590048F9FE /* 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.11; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 290DA3C61C59AF590048F9FE /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 290DA3C71C59AF590048F9FE /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 290DA3B91C59AF590048F9FE /* Build configuration list for PBXProject "FindAFile" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 290DA3C31C59AF590048F9FE /* Debug */, + 290DA3C41C59AF590048F9FE /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 290DA3C51C59AF590048F9FE /* Build configuration list for PBXNativeTarget "FindAFile" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 290DA3C61C59AF590048F9FE /* Debug */, + 290DA3C71C59AF590048F9FE /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = 290DA3B61C59AF590048F9FE /* Project object */; +} diff --git a/FindAFile/FindAFile.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/FindAFile/FindAFile.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..3c31257 --- /dev/null +++ b/FindAFile/FindAFile.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/FindAFile/FindAFile/main.swift b/FindAFile/FindAFile/main.swift new file mode 100644 index 0000000..7d00a5b --- /dev/null +++ b/FindAFile/FindAFile/main.swift @@ -0,0 +1,49 @@ +// +// main.swift +// FindAFile +// +// Created by Ayuna Vogel on 1/27/16. +// Copyright © 2016 Ayuna Vogel. All rights reserved. +// + +// 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. + + + +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) { + //print("DIR: " + fileOrDir) + // YOUR CODE HERE + let result = findFile(name, atPath: fullPath) + if result != "NOT FOUND" { + return result + } + } else if exists { + //print("FILE: " + fileOrDir) + // YOUR CODE HERE + if fileOrDir == name { + return fullPath + } + } else { + print("NEITHER: " + fileOrDir) + } + } + return "NOT FOUND" +} + +print(findFile("awesome-idea.txt", atPath: "/Users/ayunavogel/Documents")) + +print(findFile("4f336c1ad70f0aa37b21fa9db1a66cc9a3068a7aaad85be3430969dd85639cea.jpg", atPath: "/Users/ayunavogel/Documents")) + diff --git a/HWFrom1-24(Recursion).playground/Contents.swift b/HWFrom1-24(Recursion).playground/Contents.swift index 1c44504..6981d0d 100644 --- a/HWFrom1-24(Recursion).playground/Contents.swift +++ b/HWFrom1-24(Recursion).playground/Contents.swift @@ -1,3 +1,5 @@ +import Foundation + /* @@ -8,18 +10,106 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l */ - //Question 1 +/* + +Write an iterative (not recursive) fibonacci function that calculates the nth fibonacci number. How does its performance compare with the non-memorized recursive one (see Appendix A below), based on the number of iterations that Swift says it takes in the right margin? + +*/ + +//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) +} +print(fib(18)) +(0...5).map { i in fib(i) } +// iterative fibonacci function + +func fibonacciIterative(n: Int) -> Int { + var a = 0 + var b = 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 +} + + +func stepUp() { + switch tryStep() { + case 1: + return + case -1: + stepUp() + stepUp() + default: + stepUp() + } +} + +stepUp() + +//func stepUp() { +// tryStep() { +// switch(stepCount) { +// case -1: stepUp() +// stepUp() +// case 1: return +// default: stepUp() +// } +// } +//} + + + +//Question 3 + + + + + + + + -//Question 3 \ No newline at end of file diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..c33592e 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -8,16 +8,94 @@ 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 +https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit */ +// 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. -1) +/* A series of google search results led to this, and I decided to go with this strategy: https://www.glassdoor.com/Interview/We-have-numbers-from-1-to-52-that-are-put-into-a-51-number-array-what-s-the-best-way-to-find-out-which-number-is-missing-QTN_79443.htm +What they are looking for is probably how good you are at math and algorithms. My answer would be: 1) Calculate the sum of all numbers stored in the array of size 51. 2) Subtract the sum from (52 * 53)/2 ---- Formula : n (n+1)/2. The result of subtraction is the answer for this question. */ -2) +func findMissingNumber(n : Int, numbers : [Int]) -> Int { + let totalSum = n * (n + 1) / 2 + var totalSumOfAllNumbers = 0 + for i in numbers { + totalSumOfAllNumbers += i + } + return totalSum - totalSumOfAllNumbers +} + findMissingNumber(5, numbers: [1, 4, 3, 2]) + -3) +// 2) Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not + // this solution is from in-class exercise + + func hasDuplicates(numbers : [Int]) -> Bool { + for var i = 0; i < numbers.count; i++ { + for var j = 0; j < numbers.count; j++ { + if j != i { + if numbers[i] == numbers[j]{ + return false + } + } + } + } + + return true +} + hasDuplicates([1, 2, 3]) + hasDuplicates([1, 2, 2, 3, 4]) + +// 3) Given two lists, find the smallest value that exists in both lists. +//L1 = [1,2,5,9] +//L2 = [9, 20 , 5] -4) + + // this was the second result in google search, and I didn't find anything else: https://gist.github.com/xhenna92/ea27fb0496a3ae0ee334 + + func findMin(arr1 : [Int], arr2 : [Int]) -> 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] < minArr1 { + minArr1 = arr1[i] + } + + } + + for var j = 0; j < arr2.count; j++ { + + if arr2[j] < minArr2 { + minArr2 = arr2[j] + } + + } + + return (minArr1! > minArr2! ? minArr2 : minArr1)! +} +findMin([1, 2, 2, 3, 4], arr2: [-1, 2, 2, 3, 4]) -*/ +findMin([1,2,5,9],arr2: [9, 20 , 5]) // the function doesn't work + + +// 4) Check to see if an integer is a palindrome don’t use casting + // http://beginnersbook.com/2015/02/c-program-to-check-if-a-number-is-palindrome-or-not/ using recursion + // divide each digit and rebuild number again then compare with original: + func isPalindrome(var num: Int) -> Bool { + let originalNum = num + var finalNum = 0 + + while(num > 0) { + finalNum *= 10 + finalNum += num % 10 + num /= 10 + } + return finalNum == originalNum // return true if is palindrome + } +isPalindrome(134543) // test it +isPalindrome(232) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..e3c118f 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -8,22 +8,129 @@ 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/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/edit#heading=h.za36ai6n5fth +https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/edit#heading=h.za36ai6n5fth */ +/*1) a) 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? -1) -2) +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]; // 200 psec + } + } + return image; +} -3) -4) +O(nm) ? -5) -6) -7) + +/* + +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’. + +O(n^3) ? + + +1) 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? + + + +2) 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 n2, what is the time complexity of each of the following snippets of code or algorithms? + +2a) O(n^2) ? + +for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + foo(xs); + } +} // O(n^2) +for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + bar(xs); + } +} // O(n^2) +for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + // do cool stuff + } +} // O(n^2) + + +2b) O(n) + +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. + + +2c) 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. + + + +2d) An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). + +O(log(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): + + +3a) 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. + +Array + + +3b) 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. + +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). + +set, array, skip list + + + +3c) 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? + + +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? + + +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). + +func multiplyByAddition(let numA: Int, let numB: Int) -> Int{ + +var sum = 0 + +for _ in 0...numB { + +sum += numA +} + +return sum +} + +O(m) + +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. + + +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-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..6bb8d19 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -9,21 +9,92 @@ var str = "Hello, playground" Question 1: https://www.hackerrank.com/challenges/minimum-draws -Copy and paste your code: +Copy and paste your code: */ +// Maximum number of draws equals the number of pairs of socks + 1. -What is the big O runtime of your code?: +func maxDraws(let n: Int) -> Int { + return n + 1 +} -Question 2: https://www.hackerrank.com/challenges/handshake +let numOfPairs = 3 +maxDraws(numOfPairs) -Copy and paste your code: -What is the big O runtime of your code?: +// What is the big O runtime of your code?: O(1) -Question 3: https://www.hackerrank.com/challenges/connecting-towns +// code from the study group from Kaira for the Hackerrank, will need to go over this again -Copy and paste your code: +//let testNum : Int = userInput() // asks for how many tests do you want done +//for i in 0.. Int { // doesn't work in playgrounds, needs to run in command line app +// let input = readLine() +// let num = Int(input!) +// return num! +//} -What is the big O runtime of your code?: -*/ + +//Question 2: https://www.hackerrank.com/challenges/handshake +// +//Copy and paste your code: + +// Total number of Handshakes solved with Combinations +// C(n,2) = n! / 2! * (n-2)! +// C(n,2) = n*(n-1)/2 // simplified by Xiulan, from the study group on Fri + + +// Justine's solution from the study group on Fri + +func factorial(var n: Int) -> Int +{ + var num = n - 1 + + for _ in 1.. Int +{ + + return factorial(n) / (factorial(2) * factorial(n - 2)) +} + +handshakes(3) +handshakes(5) + + + +func handshakesSimplified(let n: Int) -> Int +{ + + return n * (n - 1) / 2 +} + +handshakesSimplified(3) +handshakesSimplified(5) + +// +//What is the big O runtime of your code?: 0(1) +// +//Question 3: https://www.hackerrank.com/challenges/connecting-towns +// +//Copy and paste your code: +// +//What is the big O runtime of your code?: +