diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..e47f2ff Binary files /dev/null and b/.DS_Store differ diff --git a/ArraysAndListHW.playground/Contents.swift b/ArraysAndListHW.playground/Contents.swift new file mode 100644 index 0000000..4b41682 --- /dev/null +++ b/ArraysAndListHW.playground/Contents.swift @@ -0,0 +1,158 @@ +//: Playground - noun: a place where people can play + +import Cocoa + +//1. +var sudokuBoard = [ + [5, 0, 8, 0, 7, 3, 1, 9, 0], + [9, 0, 0, 6, 0, 0, 4, 0, 8], + [0, 0, 0, 9, 0, 8, 0, 3, 5], + [0, 7, 0, 0, 0, 0, 0, 6, 0], + [0, 0, 2, 0, 0, 0, 9, 0, 0], + [0, 1, 0, 0, 0, 0, 0, 8, 0], + [1, 9, 0, 3, 0, 6, 0, 0, 0], + [2, 0, 3, 0, 0, 7, 0, 0, 9], + [0, 8, 7, 1, 9, 0, 3, 0, 4]] + +func getValidOutputs(arr:[[Int]], r:Int, c:Int) -> [Int] { + //checking row + var validNumbers = Array(count: 10, repeatedValue: true) + for i in 0.. [[Int]] { + var matrixBoard = Array(count: sampleInput[0].count, repeatedValue: Array(count: sampleInput.count, repeatedValue: 0)) + + //horizontal + for i in 0.. sampleInput.count - 1 - i][revers + matrixBoard[j][sampleInput.count - 1 - i] = sampleInput[i][j] + } + } + return matrixBoard +} +print(rotateNinetyDegrees(sampleInput)) + + +//let number of rows == number of columns and number of columns == number of rows +//output column(s) in a row(s) in reverse order + + +//3. +//var array = [4,3,2,1] +// +//func sort(values:[Int]) -> [Int] { +// let left = values[0...1] +// return values +// 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 +// print(right) +// } +//} + +//print(sort(array)) + +var data = [4, 3, 2, 1] + +func merge_sort(inout array: [T]){ + if array.count <= 1{ + return + } + + // Split lists into equal sized sublists + var middle = array.count + middle /= 2 + var left = [T]() + var right = [T]() + + for x in 0..(inout left: [T], inout right:[T])-> [T]{ + + var result = [T]() + + // Merge taking lowest value first seen + while (!left.isEmpty && !right.isEmpty){ + if left[0] <= right[0]{ + result.append(left[0]) + left.removeAtIndex(0) + }else{ + result.append(right[0]) + right.removeAtIndex(0) + } + } + + // Handle remaining elements + while !left.isEmpty{ + result.append(left[0]) + left.removeAtIndex(0) + } + while !right.isEmpty{ + result.append(right[0]) + right.removeAtIndex(0) + } + + return result +} + +merge_sort(&data) + diff --git a/ArraysAndListHW.playground/contents.xcplayground b/ArraysAndListHW.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/ArraysAndListHW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ArraysAndListHW.playground/playground.xcworkspace/contents.xcworkspacedata b/ArraysAndListHW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/ArraysAndListHW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ArraysAndListHW.playground/timeline.xctimeline b/ArraysAndListHW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/ArraysAndListHW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/FindFileHW/.DS_Store b/FindFileHW/.DS_Store new file mode 100644 index 0000000..40570df Binary files /dev/null and b/FindFileHW/.DS_Store differ diff --git a/FindFileHW/FindFileHW.xcodeproj/project.pbxproj b/FindFileHW/FindFileHW.xcodeproj/project.pbxproj new file mode 100644 index 0000000..7da2014 --- /dev/null +++ b/FindFileHW/FindFileHW.xcodeproj/project.pbxproj @@ -0,0 +1,246 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 00A7754C1C5AB2710081B579 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00A7754B1C5AB2710081B579 /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 00A775461C5AB2710081B579 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 00A775481C5AB2710081B579 /* FindFileHW */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = FindFileHW; sourceTree = BUILT_PRODUCTS_DIR; }; + 00A7754B1C5AB2710081B579 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 00A775451C5AB2710081B579 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 00A7753F1C5AB2710081B579 = { + isa = PBXGroup; + children = ( + 00A7754A1C5AB2710081B579 /* FindFileHW */, + 00A775491C5AB2710081B579 /* Products */, + ); + sourceTree = ""; + }; + 00A775491C5AB2710081B579 /* Products */ = { + isa = PBXGroup; + children = ( + 00A775481C5AB2710081B579 /* FindFileHW */, + ); + name = Products; + sourceTree = ""; + }; + 00A7754A1C5AB2710081B579 /* FindFileHW */ = { + isa = PBXGroup; + children = ( + 00A7754B1C5AB2710081B579 /* main.swift */, + ); + path = FindFileHW; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 00A775471C5AB2710081B579 /* FindFileHW */ = { + isa = PBXNativeTarget; + buildConfigurationList = 00A7754F1C5AB2710081B579 /* Build configuration list for PBXNativeTarget "FindFileHW" */; + buildPhases = ( + 00A775441C5AB2710081B579 /* Sources */, + 00A775451C5AB2710081B579 /* Frameworks */, + 00A775461C5AB2710081B579 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = FindFileHW; + productName = FindFileHW; + productReference = 00A775481C5AB2710081B579 /* FindFileHW */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 00A775401C5AB2710081B579 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0710; + LastUpgradeCheck = 0710; + ORGANIZATIONNAME = "Felicia Weathers"; + TargetAttributes = { + 00A775471C5AB2710081B579 = { + CreatedOnToolsVersion = 7.1.1; + }; + }; + }; + buildConfigurationList = 00A775431C5AB2710081B579 /* Build configuration list for PBXProject "FindFileHW" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 00A7753F1C5AB2710081B579; + productRefGroup = 00A775491C5AB2710081B579 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 00A775471C5AB2710081B579 /* FindFileHW */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 00A775441C5AB2710081B579 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 00A7754C1C5AB2710081B579 /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 00A7754D1C5AB2710081B579 /* 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; + }; + 00A7754E1C5AB2710081B579 /* 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; + }; + 00A775501C5AB2710081B579 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 00A775511C5AB2710081B579 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 00A775431C5AB2710081B579 /* Build configuration list for PBXProject "FindFileHW" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 00A7754D1C5AB2710081B579 /* Debug */, + 00A7754E1C5AB2710081B579 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 00A7754F1C5AB2710081B579 /* Build configuration list for PBXNativeTarget "FindFileHW" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 00A775501C5AB2710081B579 /* Debug */, + 00A775511C5AB2710081B579 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = 00A775401C5AB2710081B579 /* Project object */; +} diff --git a/FindFileHW/FindFileHW.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/FindFileHW/FindFileHW.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..d55eff3 --- /dev/null +++ b/FindFileHW/FindFileHW.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/FindFileHW/FindFileHW/main.swift b/FindFileHW/FindFileHW/main.swift new file mode 100644 index 0000000..bb6579f --- /dev/null +++ b/FindFileHW/FindFileHW/main.swift @@ -0,0 +1,37 @@ +// +// main.swift +// FindFileHW +// +// Created by Felicia Weathers on 1/28/16. +// Copyright © 2016 Felicia Weathers. 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) + let result = findFile(name, atPath: fullPath) + if result != "NOT FOUND" { + return result + } + } else if exists { + // YOUR CODE HERE +// print("FILE: " + fileOrDir) + return fullPath + } else { + print("NEITHER: " + fileOrDir) + } + } + return "NOT FOUND" +} + +print(findFile("awesome-idea.txt", atPath: "/Users/feliciaweathers/Documents")) \ 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..4f8ac6f 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -11,13 +11,55 @@ 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) */ +func findMissingNumber(N:Int, list:[Int]) -> Int { +var totalSumOfNArray = 0 +var totalSumOfList = 0 -2) +for i in 1...N { +totalSumOfNArray += i +} +for i in list { +totalSumOfList += i +} -3) +return totalSumOfNArray - totalSumOfList + +} + + +//2) +//Given a list of size N containing numbers 1 - N(inclusive) return true if there are duplicates, false if not +func hasDuplicates(arr: [Int]) ->Bool { + + return true +} + + +//3) +//Given two lists, find the smallest value that exists in both lists +func getSmallestCommonValue(list1:[Int], list2: [Int]) ->Int? { + let list1 = [1,2,5,9] + let list2 = [9, 20, 5] + + for i in (0..Bool { + return String(word.characters.reverse()) == word +} -4) -*/ diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..76c0d84 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,17 +11,120 @@ 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) -2) +Big O Homework +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? -3) -4) +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; +} -5) -6) +What is the time complexity of this method, expressed in big O notation? Assume the image is square, and both dimensions are ‘n’. +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? + + +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? + + +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 +} +} + + +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. + + +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. + + +An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). + + +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): + + +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. + + +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). + + +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? + + +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? + + +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). + + +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. + + +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). + + + + +1) a. 2,000,000 + b. O(n^2) + c. O(n^4) + +2) a. cubic O(n^3) + quadratic O(n^4) + quadratic O(n^2) + O(n^4) answer + b. O(n) + c. O(n^2) + d. O(log n) + +3) a. tree + b. hash table + c. tree + +*4) what is the number? n -> list all the numbers from n counting down to 1 -> multiply each number, n * (n-1) * (n-2) *... * 1 + +5) func multiplication(x: Int, y: Int) -> Int { + + guard x != 0 || y != 0 else { + return 0 + } + + return x + multiplication(x, y: y-1) + +*6) O(log n) 7) diff --git a/LogicDiscreteMathHW.playground/Contents.swift b/LogicDiscreteMathHW.playground/Contents.swift new file mode 100644 index 0000000..6a26073 --- /dev/null +++ b/LogicDiscreteMathHW.playground/Contents.swift @@ -0,0 +1,62 @@ +//: Playground - noun: a place where people can play + +import UIKit + +/* +1. +let n = number of socks +max number of socks drawn for a pair = (n/2) + 1 +O(n) bc only need one for loop + +2. + +C(n,2) + +n! / (2! * (n-2)!) + +3. use product rule + + + +*/ + + +//1. (n-1) + (n-2) + (n-3) ....1 + +//let numberOfPeople = Int() +//for _ in 1...numberOfPeople { +// +//// n * (n-1)/2 +// +//} + + +//func parseInputAsInteger() -> Int { +// let input = readLine() +// let num = Int(input!) +// return num! +//} + +let testCases = Int(readLine(stripNewline: true)!)! +for var x = 0; x < testCases; x++ { + //BOOM + + // var firstLineInTheCase = Int(readLine(stripNewline: true)!)! + print (readLine(stripNewline: true)!)! +} + + + + +//func maxSocksDrawn(N:Int) -> Int { +// let n = Int?() +// +// for _ in 0...N { +// return (n!/2) + 1 +// } +// return 0 +//} +// +//let n = Int.self; +//maxSocksDrawn() + \ No newline at end of file diff --git a/LogicDiscreteMathHW.playground/contents.xcplayground b/LogicDiscreteMathHW.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/LogicDiscreteMathHW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/LogicDiscreteMathHW.playground/playground.xcworkspace/contents.xcworkspacedata b/LogicDiscreteMathHW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/LogicDiscreteMathHW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/LogicDiscreteMathHW.playground/timeline.xctimeline b/LogicDiscreteMathHW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/LogicDiscreteMathHW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/QuickSort_Stacks_Queues_HW.playground/Contents.swift b/QuickSort_Stacks_Queues_HW.playground/Contents.swift new file mode 100644 index 0000000..b3cc342 --- /dev/null +++ b/QuickSort_Stacks_Queues_HW.playground/Contents.swift @@ -0,0 +1,116 @@ +//: Playground - noun: a place where people can play + +import Cocoa + +var arr = "Hello, Felicia" + +/* 1. +Without looking at the Big O Cheatsheet, write down the average time and space complexity for bubble sort, insertion sort, selection sort, mergesort, and quicksort. + + Sort time complexity space complexity +-Bubble Sort: O(n^2) O(n) +-Insertion Sort: O(n^2) O(1) +-Selection Sort: O(n^2) O(1) +-Merge Sort: O(nlog(n)) O(n) +-Quick Sort: O(nlogn) log(n) +*/ + +/* 2. +What is the advantage of partitioning quicksort in place? + +The partitioning takes place as the quicksort moves downward, allowing for a faster sort because the elements are being sorted as it iterates through the elements/array. +*/ + +/* 3. +Without looking, implement quicksort. + +*/ + +func partition(inout arr: [Int], firstIdx: Int, lastIdx: Int) -> Int { + let pivotValue = arr[firstIdx] + + var leftMark = firstIdx + 1 + + var rightMark = lastIdx + + while leftMark <= rightMark { + while arr[leftMark] < pivotValue { + leftMark += 1 + } + + while arr[rightMark] > pivotValue { + rightMark -= 1 + } + + if leftMark < rightMark { + swap(&arr[leftMark], &arr[rightMark]) + } + } + + if firstIdx != rightMark { + swap(&arr[firstIdx], &arr[rightMark]) + } + + return rightMark +} + +func quickSort(inout arr: [Int], firstIdx: Int, lastIdx: Int) { + // base case + if firstIdx >= lastIdx { + return + } + + let splitPoint = partition(&arr, firstIdx: firstIdx, lastIdx: lastIdx) + + //left half + quickSort(&arr, firstIdx: firstIdx, lastIdx: splitPoint - 1) + + //right half + quickSort(&arr, firstIdx: splitPoint + 1, lastIdx: lastIdx) +} + +func quickSort(inout arr: [Int]) { + quickSort(&arr, firstIdx: 0, lastIdx: arr.count - 1) +} +1 +var array = [22, 15, 38, 93, 95, 0, 34, 58, 72, 59] +quickSort(&array) + + +/* 4. +Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000. + +Int(arc4random_uniform(UInt32(10000))) +*/ + +func uniqueRandoms(numberOfRandoms: Int, minNum: Int, maxNum: UInt32) -> [Int] { + var uniqueNumbers = Set() + while uniqueNumbers.count < numberOfRandoms { + uniqueNumbers.insert(Int(arc4random_uniform(maxNum + 1)) + minNum) + } + return Array(uniqueNumbers) +} +print(uniqueRandoms(10000, minNum: 0, maxNum: 10000)) + + +/* 5. +Describe the algorithmic difference between mergesort and quicksort. Where does the sorting happen? As the recursive calls are being pushed onto the stack or as they are being popped off? + +- MergeSort breaks into smaller subarrays and sorts the elements, merging and sorting as the iteration goes back upward, whereas, QuickSort sort is in place and sorts as it goes down. +*/ + +/* 6. +Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced. +Good examples: () [] () ([]()[]) +Bad examples: ( ( ] ([)] + + +*/ +//var arrParen = ["][“,”]”,”{“,”}”,”(“,”)"] +// +//func isBalanced(paren: [String]) -> Bool { +// +//} +//var arr1 = ["(,),[,]"] +//isBalanced(arr1) +//isBalanced(arrParen) diff --git a/QuickSort_Stacks_Queues_HW.playground/contents.xcplayground b/QuickSort_Stacks_Queues_HW.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/QuickSort_Stacks_Queues_HW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/QuickSort_Stacks_Queues_HW.playground/playground.xcworkspace/contents.xcworkspacedata b/QuickSort_Stacks_Queues_HW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/QuickSort_Stacks_Queues_HW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/QuickSort_Stacks_Queues_HW.playground/timeline.xctimeline b/QuickSort_Stacks_Queues_HW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/QuickSort_Stacks_Queues_HW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/RecursionHW.playground/Contents.swift b/RecursionHW.playground/Contents.swift new file mode 100644 index 0000000..dc8cb8c --- /dev/null +++ b/RecursionHW.playground/Contents.swift @@ -0,0 +1,75 @@ +////: Playground - noun: a place where people can play +// +import Cocoa +// +////1. +func fibonacci(n: Int) -> Int { + // Some temporary variables. + var a = 0 + var b = 1 + // Add up numbers to the desired iteration. + for _ in 0.. Int { + print("X") + if (n == 0 || n == 1) { + return 1 + } + return fib(n - 1) + fib(n - 2) + } +} + +// Apendix A +func fib(n: Int) -> Int { + print("X") + if (n == 0 || n == 1) { + return 1 + } + return fib(n - 1) + fib(n - 2) +} +(0...5).map { i in fib(i) } + + +//2. +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(_: Int) -> Int { + let steps = 0 + if stepNum > 0 { + stepNum += steps + print("yay, made it up a stair") + } else if stepNum == 0 { + stepNum == 0 + print("try again") + tryStep() + } else { + stepNum -= steps + print("gotta make up that step") + tryStep() + tryStep() + } + return stepNum +} + +print(stepUp(7)) \ No newline at end of file diff --git a/RecursionHW.playground/contents.xcplayground b/RecursionHW.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/RecursionHW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/RecursionHW.playground/playground.xcworkspace/contents.xcworkspacedata b/RecursionHW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/RecursionHW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/RecursionHW.playground/timeline.xctimeline b/RecursionHW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/RecursionHW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/SortingHW.playground/Contents.swift b/SortingHW.playground/Contents.swift new file mode 100644 index 0000000..de4e87d --- /dev/null +++ b/SortingHW.playground/Contents.swift @@ -0,0 +1,26 @@ +//: Playground - noun: a place where people can play + +import Cocoa + +func exchange(inout data: [T], i:Int, j:Int) { + let temp:T = data[i] + data[i] = data[j] + data[j] = temp +} + +func insertionSort(var unsortedArray:Array)->Array{ + if(unsortedArray.count<2) { + return unsortedArray + } + for var j = 1; j < unsortedArray.count; j++ { + var i = j + while i>0 && unsortedArray[i-1]>unsortedArray[i] { + exchange(&unsortedArray, i: i-1, j: i) + i--; + } + } + + return unsortedArray; +} + + diff --git a/SortingHW.playground/contents.xcplayground b/SortingHW.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/SortingHW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/SortingHW.playground/playground.xcworkspace/contents.xcworkspacedata b/SortingHW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/SortingHW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/SortingHW.playground/timeline.xctimeline b/SortingHW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/SortingHW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + +