From 7607d3fb6664e2bded25823cc6b125727ce31758 Mon Sep 17 00:00:00 2001 From: zoufishanmehdi Date: Wed, 13 Jan 2016 20:58:09 -0500 Subject: [PATCH 01/11] bam --- .../Contents.swift | 108 +++++++++++++++++- 1 file changed, 103 insertions(+), 5 deletions(-) diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..3ed0762 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -10,14 +10,112 @@ 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) -2) +//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. -3) +func findMissingNumber(N: Int, list: [Int]) -> Int { + var totalSum = N*(N+1)/2 //Gaussian sum-sum everything up from 1 to 10 for example + var totalSumOfList = 0 + for i in list { + totalSumOfList += i + } + + return totalSum - totalSumOfList +} + + + + + +//2)Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not + +var list3 = [Int]() +list3.sort() + +func hasDuplicates(list3: [Int]) -> Bool { + for i in 0.. [Int] { + var com = [Int]() + for i in 0.. Int { + var min = Int.max + + for i in 0.. Int? { + var commonElements = getCommonValues(list1, list2: list2) + if (commonElements.count == 0) { + return nil + } + var smallestVal = getSmallestValue(commonElements) + + return smallestVal +} + +var answer = smallestCommonVal(list1, list2: list2) +print(answer) + + + + + +//4)Check to see if an integer is a palindrome don’t use casting + +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 +} + +let num = 21012 +isPalindrome(num) -4) -*/ From b7a2b4c88640bc12502e0fba9193288c11744d12 Mon Sep 17 00:00:00 2001 From: zoufishanmehdi Date: Wed, 13 Jan 2016 23:51:47 -0500 Subject: [PATCH 02/11] BAM --- .../Contents.swift | 97 ++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..992182c 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -12,10 +12,105 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) +//// 1a) + [math] 1000 * 3 + [math] 1000 * 2000 * 3 + [memory] 1000 * 2000 * 2 + [storage] 1000 * 2000 * 10 + [filter] 1000 * 2000 * 200 + 430,003,000 picoseconds or 4.3 * 10^7 + +//// 1b) + [math] n * 3 + [math] n * m * 3 + [memory] n * m * 2 + [storage] n * m * 10 + [filter] n * m * 200 + (3n)+(3nm)+(2nm)+(10nm)+(200nm) + (3n)+(215nm) + +//// 1c) O(n^2) + +//// 1d) O(n^4) +*/ + + + + +/* +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) +for (int i = 0; i < n; i++) { //n + for (int j = 0; j < n; j++) { // n + foo(xs); // n + } +} +n * n * n = n^3 +for (int i = 0; i < n; i++) { // n + for (int j = 0; j < n; j++) { // n + bar(xs); // n^2 + } +} +n * n * n^2 = n^4 +for (int i = 0; i < n; i++) { // n + for (int j = 0; j < n; j++) { // n + // do cool stuff + } +} + + n * n = n^2 + +///// 2b) +int frobnicate(ys, m) { + if (m == 0) { + return 0; + } + return ys[m] + frobnicate(ys, m - 1); +} +frobnicate(xs, n); + + +Big O notation -> 0(n); + +*/ + + -2) +/* 3) +BOOL betterHasDuplicates(int xs[], int n) { + for (int i = 0; i < n; i++) { + for (int j = i; j < n; j++) { + if (i != j && xs[i] == xs[j]) { + return YES; + } + } + } + return NO; +} + + + +//MERGE SORT +list1 = [Int]() +func mergeSort (list1, [Int]) -> [Int]{ + +// split array in half +var middle = list1.count/ 2 +var right = [Int](); +var left = [Int](); + +for i in 0.. Date: Thu, 14 Jan 2016 10:33:58 -0500 Subject: [PATCH 03/11] submit --- .../Contents.swift | 64 ++++++++++++++----- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 992182c..6942383 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -72,24 +72,18 @@ frobnicate(xs, n); Big O notation -> 0(n); -*/ - - - -/* -3) +/////2c) BOOL betterHasDuplicates(int xs[], int n) { - for (int i = 0; i < n; i++) { - for (int j = i; j < n; j++) { - if (i != j && xs[i] == xs[j]) { - return YES; - } - } - } - return NO; +for (int i = 0; i < n; i++) { +for (int j = i; j < n; j++) { +if (i != j && xs[i] == xs[j]) { +return YES; +} +} +} +return NO; } - //MERGE SORT @@ -112,10 +106,50 @@ right.append(list2[j]) */ + + +/* + + +3)3)a. Tree +b. Stack +c. Tree + + + + 4) +func factorial(num: Int) -> Int{ + + if (num == 0){ + return 1; + } + + else { + return (num * factorial(num - 1)) + } +} + + + + 5) +func multiplyTheseTwo(num1: Int, num2: Int) -> Int { + + let baseNum = num1 + let multiplierNum = num2 + var product = 0 + + for (var i = 0; i < multiplierNum; i++){ + product += baseNum + print(product) + } + + return product +} + 6) 7) From 624c3d846a10c85ecc1ff8101bece0cc7f134bd1 Mon Sep 17 00:00:00 2001 From: zoufishanmehdi Date: Thu, 14 Jan 2016 17:13:57 -0500 Subject: [PATCH 04/11] more stuff? --- .../Contents.swift | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 6942383..f2fc184 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -75,9 +75,9 @@ Big O notation -> 0(n); /////2c) BOOL betterHasDuplicates(int xs[], int n) { -for (int i = 0; i < n; i++) { -for (int j = i; j < n; j++) { -if (i != j && xs[i] == xs[j]) { +for (int i = 0; i < n; i++) { //n +for (int j = i; j < n; j++) { //n * n +if (i != j && xs[i] == xs[j]) { //n return YES; } } @@ -85,6 +85,7 @@ return YES; return NO; } + n * n^2 * n = n ^4 //MERGE SORT list1 = [Int]() @@ -104,6 +105,9 @@ right.append(list2[j]) } } + + O(n log(n)) + */ @@ -111,7 +115,7 @@ right.append(list2[j]) /* -3)3)a. Tree +3)3)a. Tree or Hash Table b. Stack c. Tree @@ -152,7 +156,26 @@ func multiplyTheseTwo(num1: Int, num2: Int) -> Int { 6) -7) +func russianPeasant(var left_num : Int, var right_num : Int) -> Int { +var product = 0 +if(left_num % 2 != 0) +{ +product += right_num +} +while(left_num > 1) +{ +left_num /= 2 +right_num *= 2 +if( left_num % 2 != 0) +{ +product += right_num +} +} +return product +} + + +7)Don't really know how to do this. Will continue working on this */ From 7be56f5b60edca93bb0e25159ff1b2e728529a89 Mon Sep 17 00:00:00 2001 From: zoufishanmehdi Date: Fri, 15 Jan 2016 20:09:40 -0500 Subject: [PATCH 05/11] commit --- HWfrom1-10-016(BigO).playground/Contents.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index f2fc184..c20a487 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -60,6 +60,10 @@ for (int i = 0; i < n; i++) { // n n * n = n^2 + + + + ///// 2b) int frobnicate(ys, m) { if (m == 0) { From f01a0de28635b44838aa66ceb47bc83288b189ba Mon Sep 17 00:00:00 2001 From: zoufishanmehdi Date: Sat, 16 Jan 2016 00:24:06 -0500 Subject: [PATCH 06/11] Time to ZZzzz --- .../Contents.swift | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 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..71bf946 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -10,20 +10,59 @@ var str = "Hello, playground" Question 1: https://www.hackerrank.com/challenges/minimum-draws Copy and paste your code: +let testCases = Int(readLine(stripNewline: true)!)! +for i in 0...n{ +//new line +let n = Int(readLine(stripNewline: true)!)! +print(n+1) +} -What is the big O runtime of your code?: +What is the big O runtime of your code?: O(n) Question 2: https://www.hackerrank.com/challenges/handshake Copy and paste your code: +let testCases = Int(readLine(stripNewline: true)!)! +for i in 0...n{ +//n represents number of people +let n = Int(readLine(stripNewline: true)!)! +print((n *(n-1))/2) +} -What is the big O runtime of your code?: +What is the big O runtime of your code?: O(n) Question 3: https://www.hackerrank.com/challenges/connecting-towns Copy and paste your code: -What is the big O runtime of your code?: +let testCases = Int(readLine(stripNewline: true)!)! +for i in 0...testCases { +let towns = Int(readLine(stripNewline: true)!)! +var routes = [Int]() +var result = 0; + +for i in 0.. Date: Thu, 21 Jan 2016 16:50:18 -0500 Subject: [PATCH 07/11] update --- .../Contents.swift | 109 +++++++++++++++++- .../Contents.swift | 5 +- .../Contents.swift | 11 ++ 3 files changed, 121 insertions(+), 4 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..ba1a11f 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -2,7 +2,6 @@ import UIKit -var str = "Hello, playground" /* @@ -12,15 +11,119 @@ Work on your solutions here. Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw 1) +//Pseudocode + +Boolean sudokuBoard(Grid grid){ +int row, col; + +if(!FindUnassignedField(grid, row, col)) +return true; + +for(int num = 1; num <= 9; num++) { +if(isValidChoice(grid, row, col, num)) { / +grid(row, col) = num; +if(solveSudoku(grid)) return true; +grid(row, col) = UNASSIGNED; +} +} +return false; // no valid choice found, trigger backtracking +} + + + + +2) func rotateNinety (arr: [Int]) -> [Int] { + + for i in 0.. [Int] { + + for i in 0.. fourElements[i + 1] { + temp = fourElements[i + 1] + fourElements[i+1] = fourElements[i] + fourElements[i] = temp + + } + + if fourElements[fourElements.count - 2] > fourElements[fourElements.count - 1] { + temp = fourElements[fourElements.count - 2] + fourElements[fourElements.count - 2] = fourElements[fourElements.count - 1] + fourElements[fourElements.count - 1] = temp + + } + + return fourElements +} + + +var max = 0; +var min = 0; + +func findMinAndMax(fourElements) -> Int { + + for i in 0.. fourElements.count[i-1] { + var max = fourElements.count[i+1] + } else { + var max = fourElements.count[i-1] + } + } + + return min + return max +} + +*/ + + + + + + + + + + + + + + + + + -*/ \ 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 c20a487..8b137ca 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -115,7 +115,7 @@ right.append(list2[j]) */ - +//2d) nlogn /* @@ -179,6 +179,9 @@ return product } +Big O: logn <--- correct answer + ///when you double something and then add something, it's logn + 7)Don't really know how to do this. Will continue working on this diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 71bf946..fa2b75f 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -66,3 +66,14 @@ What is the big O runtime of your code?: O(n^2) */ + + + +//************************************************// +/* Unwrap optional +func user() -> Int { + let input = readLine() + let num = Int(input!) + return num! + +*/ From bff0a9ad497dbade0a03dc7b3c1feda73fc4a187 Mon Sep 17 00:00:00 2001 From: zoufishanmehdi Date: Thu, 28 Jan 2016 11:12:06 -0500 Subject: [PATCH 08/11] changes --- .../Contents.swift | 29 +++++++++++++++++-- 1 file changed, 27 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 ba1a11f..566fcbe 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -32,7 +32,7 @@ return false; // no valid choice found, trigger backtracking -2) func rotateNinety (arr: [Int]) -> [Int] { +2) func rotateNinety (arr: [[Int]]) -> [[Int]] { for i in 0.. Int { +//Simplest implementation of recursion +func fib(n: Int) -> Int { + if n < 2 { + return 1 + } + let a = n - 2 + let b = n - 1 + return fib(a) + fib(b) +} + +let values = (0..<10).map {i in fib(i)} +values + + + + + + + + From e7c20d0793485dfc0f4951f639c00149f1b9d6e3 Mon Sep 17 00:00:00 2001 From: zoufishanmehdi Date: Thu, 28 Jan 2016 16:22:33 -0500 Subject: [PATCH 09/11] still a work in progress --- .../Contents.swift | 43 +++- .../contents.xcplayground | 2 +- .../homeworkJan24.xcodeproj/project.pbxproj | 243 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + homeworkJan24/homeworkJan24/main.swift | 38 +++ 5 files changed, 331 insertions(+), 2 deletions(-) create mode 100644 homeworkJan24/homeworkJan24.xcodeproj/project.pbxproj create mode 100644 homeworkJan24/homeworkJan24.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 homeworkJan24/homeworkJan24/main.swift diff --git a/HWFrom1-24(Recursion).playground/Contents.swift b/HWFrom1-24(Recursion).playground/Contents.swift index 1c44504..6cf2bbb 100644 --- a/HWFrom1-24(Recursion).playground/Contents.swift +++ b/HWFrom1-24(Recursion).playground/Contents.swift @@ -12,14 +12,55 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l //Question 1 +var i = 0 +var j = 1 +var n = Int () +var k = Int () +var temp = 0 +func fibIteration (var i: Int) -> Int { + 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() { +if tryStep() { +// We’re done! +return +} +// Now we’re two steps below where we want to be :-( +stepUp() + +//Question 3 -//Question 3 \ No newline at end of file diff --git a/HWFrom1-24(Recursion).playground/contents.xcplayground b/HWFrom1-24(Recursion).playground/contents.xcplayground index 5da2641..ee7c14f 100644 --- a/HWFrom1-24(Recursion).playground/contents.xcplayground +++ b/HWFrom1-24(Recursion).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/homeworkJan24/homeworkJan24.xcodeproj/project.pbxproj b/homeworkJan24/homeworkJan24.xcodeproj/project.pbxproj new file mode 100644 index 0000000..a4d1081 --- /dev/null +++ b/homeworkJan24/homeworkJan24.xcodeproj/project.pbxproj @@ -0,0 +1,243 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 5F0F4D531C5A84C900BB2D9A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F0F4D521C5A84C900BB2D9A /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 5F0F4D4D1C5A84C900BB2D9A /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 5F0F4D4F1C5A84C900BB2D9A /* homeworkJan24 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = homeworkJan24; sourceTree = BUILT_PRODUCTS_DIR; }; + 5F0F4D521C5A84C900BB2D9A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 5F0F4D4C1C5A84C900BB2D9A /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 5F0F4D461C5A84C900BB2D9A = { + isa = PBXGroup; + children = ( + 5F0F4D511C5A84C900BB2D9A /* homeworkJan24 */, + 5F0F4D501C5A84C900BB2D9A /* Products */, + ); + sourceTree = ""; + }; + 5F0F4D501C5A84C900BB2D9A /* Products */ = { + isa = PBXGroup; + children = ( + 5F0F4D4F1C5A84C900BB2D9A /* homeworkJan24 */, + ); + name = Products; + sourceTree = ""; + }; + 5F0F4D511C5A84C900BB2D9A /* homeworkJan24 */ = { + isa = PBXGroup; + children = ( + 5F0F4D521C5A84C900BB2D9A /* main.swift */, + ); + path = homeworkJan24; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 5F0F4D4E1C5A84C900BB2D9A /* homeworkJan24 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 5F0F4D561C5A84C900BB2D9A /* Build configuration list for PBXNativeTarget "homeworkJan24" */; + buildPhases = ( + 5F0F4D4B1C5A84C900BB2D9A /* Sources */, + 5F0F4D4C1C5A84C900BB2D9A /* Frameworks */, + 5F0F4D4D1C5A84C900BB2D9A /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = homeworkJan24; + productName = homeworkJan24; + productReference = 5F0F4D4F1C5A84C900BB2D9A /* homeworkJan24 */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 5F0F4D471C5A84C900BB2D9A /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0700; + ORGANIZATIONNAME = c4q; + TargetAttributes = { + 5F0F4D4E1C5A84C900BB2D9A = { + CreatedOnToolsVersion = 7.0; + }; + }; + }; + buildConfigurationList = 5F0F4D4A1C5A84C900BB2D9A /* Build configuration list for PBXProject "homeworkJan24" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 5F0F4D461C5A84C900BB2D9A; + productRefGroup = 5F0F4D501C5A84C900BB2D9A /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 5F0F4D4E1C5A84C900BB2D9A /* homeworkJan24 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 5F0F4D4B1C5A84C900BB2D9A /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 5F0F4D531C5A84C900BB2D9A /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 5F0F4D541C5A84C900BB2D9A /* 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; + 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; + }; + 5F0F4D551C5A84C900BB2D9A /* 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; + 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; + }; + 5F0F4D571C5A84C900BB2D9A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 5F0F4D581C5A84C900BB2D9A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 5F0F4D4A1C5A84C900BB2D9A /* Build configuration list for PBXProject "homeworkJan24" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5F0F4D541C5A84C900BB2D9A /* Debug */, + 5F0F4D551C5A84C900BB2D9A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 5F0F4D561C5A84C900BB2D9A /* Build configuration list for PBXNativeTarget "homeworkJan24" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5F0F4D571C5A84C900BB2D9A /* Debug */, + 5F0F4D581C5A84C900BB2D9A /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = 5F0F4D471C5A84C900BB2D9A /* Project object */; +} diff --git a/homeworkJan24/homeworkJan24.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/homeworkJan24/homeworkJan24.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..2944ca5 --- /dev/null +++ b/homeworkJan24/homeworkJan24.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/homeworkJan24/homeworkJan24/main.swift b/homeworkJan24/homeworkJan24/main.swift new file mode 100644 index 0000000..552492f --- /dev/null +++ b/homeworkJan24/homeworkJan24/main.swift @@ -0,0 +1,38 @@ +// +// main.swift +// homeworkJan24 +// +// Created by Zoufishan Mehdi on 1/28/16. +// Copyright © 2016 c4q. All rights reserved. +// + +import Foundation + +print("Hello, World!") + +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) { + // fileOrDir = fileManager.contentsOfDirectoryAtPath(atPath) + contentsOfDirectoryAtPath(atPath) + print("DIR: " + fileOrDir) + } else if exists { + //file.append(atPath) + fileExistsAtPath(atPath, + isDirectory) + print("FILE: " + fileOrDir) + } else { + print("NEITHER: " + fileOrDir) + } + } + return "NOT FOUND" +} + +print(findFile("awesome-idea.txt", atPath: "/Users/zinfandel/Documents")) + From 8948e72b2731c308a7fdc0532de824f5cd9df5a3 Mon Sep 17 00:00:00 2001 From: zoufishanmehdi Date: Sat, 30 Jan 2016 08:54:15 -0500 Subject: [PATCH 10/11] need to do it without loops --- .../Contents.swift | 237 +++++++++++++++++- .../Contents.swift | 25 ++ 2 files changed, 261 insertions(+), 1 deletion(-) diff --git a/HWFrom1-24(Recursion).playground/Contents.swift b/HWFrom1-24(Recursion).playground/Contents.swift index 6cf2bbb..7c582cd 100644 --- a/HWFrom1-24(Recursion).playground/Contents.swift +++ b/HWFrom1-24(Recursion).playground/Contents.swift @@ -51,6 +51,19 @@ return stepCount } + +func stepUp() { + switch tryStep() { + case1: + 0 + default: + stepUp() + case -1: + stepUp() + stepUp() + +} + func stepUp() { if tryStep() { // We’re done! @@ -62,5 +75,227 @@ stepUp() -//Question 3 + + +3) + +let result = findFile(name, atPath: fullPath) + +if result != "NOT FOUND" { + return result +} + +if fileOrDir === name +{ + return fullPath +} + + + + +///////////////////////Merge sort///////////////////////// + +func mergeSort(values: [Int]) -> [Int] { + +//check length of values, return values if count is less than 2 +// Split values into left and right (roughly equal size) + +let array = [1,2,3,4] +let n = values.count + + +n + +n / 2 + +if n < 2 { + return values + +} + + +//slice in swift +var left = Array(values[0.. [Int] { +// Iterate over both left and right at the same time + + var i = 0 + var j = 0 + while i < left.count || j < right.count { + if j >= right.count || left[i] < right[j] } + result.append(left[i]) + i += 1 + } else if j < right.count { + result.append(right[j]) + j += 1 + + } + } + + return result + +} + +mergeSort([10,2,6,5]) + +merge(withLeftArray: [3,6], andRightArray: [1,2]) + + + + + + +/////////////MERGE SORT 2////////////////// + + + + + + + + + + + + + +////////////example of recursion///////////////// + +func printAllElements(values: [Int]) { + for value in values { + print(value) + } +} + + +func printAllElementsRecursive(values: [Int]) { + printElementsHelper(values, index: 0) +} + + +func printElementsHelper(values: [Int], index: Int) { + print(values[index]) + printElementsHelper(values, index: index + 1) +} + + + +let values = [10,20,30] +printElementsHelper(values, index: 0) + + + + + + + + + + + + + + +for i in 0.. [Int] { +// Iterate over both left and right at the same time + +// Compare pairs of values in left and right + +// Take the minimum of the two and add it to the result + +return [] // <-- change this +} + + + + + + + + + + + + + + + + + + + + + diff --git a/HWFrom1-28-16(Merge Sort).playground/Contents.swift b/HWFrom1-28-16(Merge Sort).playground/Contents.swift index afdc1b6..aa41087 100644 --- a/HWFrom1-28-16(Merge Sort).playground/Contents.swift +++ b/HWFrom1-28-16(Merge Sort).playground/Contents.swift @@ -4,3 +4,28 @@ //Insert code here: + +var array = [1, 2, 6, 4, 7] +swap(&array[0], &array[1]) +array // [2, 1] + + +func selectionSort(var arr: [Int]) -> [Int] +{ + var minIndex = 0 + + for i in 0.. Date: Thu, 4 Feb 2016 16:08:17 -0500 Subject: [PATCH 11/11] work thus far --- .../Contents.swift | 106 +++++++++++++++++- .../Contents.swift | 11 +- 2 files changed, 109 insertions(+), 8 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..9d11129 100644 --- a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -5,12 +5,108 @@ //1) -//2) +//bubble sort- O(n) +//insertion sort- O(n^2) +//selection sort- O(n^2) +//mergesort- O(nlogn) +//quicksort- O(nlogn) -//3) +//2)advantage of partitioning quicksort in place- you can loop thru both right and left side simultaneously which cuts down the run time. + +//3)Without looking, implement quicksort +func quickSort(inout arr: [Int], firstIdx: Int, lastIdx: Int) { + // base case + if firstIdx >= lastIdx { return } + // partition + let splitPoint = partition(&arr, firstIdx: firstIdx, lastIdx: lastIdx) + // quickSort on leftHalf + quickSort(&arr, firstIdx: firstIdx, lastIdx: splitPoint - 1) + // quickSort on rightHalf + quickSort(&arr, firstIdx: splitPoint + 1, lastIdx: lastIdx) +} + +func quickSort(inout arr: [Int]) { + quickSort(&arr, firstIdx: 0, lastIdx: arr.count-1) +} + +func partition(inout arr: [Int], firstIdx: Int, lastIdx: Int) -> Int { + // set pivotValue to firstElement + let 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 +} + +var numbers = [22, 59, 38, 93,95, 0, 34, 58, 72, 15] + +quickSort(&numbers) + + + +//4) Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000. + +// create an array of 0 through 1000 +//var nums = Array(0...10000) + +// remove the blacklist number +//nums.removeAtIndex(nums.indexOf(8)!) +// +//var randoms = [Int]() +//for _ in 1...10000 { +// let index = Int(arc4random_uniform(UInt32(10000))) +// randoms.append(nums[index]) +// nums.removeAtIndex(index) +//} + + + +//5)The Merge Sort is usually required while sorting a too large set to hold or handle in internal memory. It divides the set into a number of subsets of one element and then repeatedly merge the subsets into increasingly larger subsets with the elements sorted correctly until one set is left. Sorting happens as elements are being popped off from the large sets. + +//The Quick Sort operates by selecting a single element from the set and labelling it the pivot. The set is then reorder to ensure that all elements of lesser value than the pivot come before it and all elements of greater value come after it. This operation is recursively applied to the subsets on both sides of pivot until the entire set is sorted. Sorting happens as the recursive calls are being pushed onto the stack. + + +//6) +// +//var paren = [ “[“,"]”,”{“,”}”,”(“,”)” ] +// +// func isBalanced(paren: [String]) -> Bool { +// for i in 0..