From 433794b7db354964252388eb8332d7f891549273 Mon Sep 17 00:00:00 2001 From: Derek316x Date: Fri, 15 Jan 2016 23:49:19 -0500 Subject: [PATCH 1/7] e --- .../Contents.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..c3bbee1 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -11,8 +11,20 @@ Question 1: https://www.hackerrank.com/challenges/minimum-draws Copy and paste your code: + + + + + + What is the big O runtime of your code?: + + + + + + Question 2: https://www.hackerrank.com/challenges/handshake Copy and paste your code: From ffcc820a96d6c3ae75b065fc72703ba8a4da8b06 Mon Sep 17 00:00:00 2001 From: Derek316x Date: Sat, 16 Jan 2016 02:05:18 -0500 Subject: [PATCH 2/7] submit --- .../Contents.swift | 129 +++++++++++++----- 1 file changed, 95 insertions(+), 34 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index c3bbee1..4320efb 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -2,40 +2,101 @@ import UIKit -var str = "Hello, playground" +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//Question 1: https://www.hackerrank.com/challenges/minimum-draws + +//Jim is off to a party and is searching for a matching pair of socks. His drawer is filled with socks, each pair of a different color. In its worst case scenario, how many socks (x) should Jim remove from his drawer until he finds a matching pair? + +func howManySocks(pairsOfSocks: Int) -> Int { + + if pairsOfSocks <= 0 || pairsOfSocks == 1 { + return 0 + } + let pairsOfSocksDouble = Double(pairsOfSocks) + let worstCaseSocksToRemoveDouble = ceil(pairsOfSocksDouble/2) + 1 + + return Int(worstCaseSocksToRemoveDouble) +} + +func worstCaseScenarioDrawsFor(sockArray:[Int]) -> [Int] { + var answer = [Int]() + for pairCount in sockArray{ + answer.append(howManySocks(pairCount)) + } + return answer +} + +worstCaseScenarioDrawsFor([2,3,4,5,6,7,8]) + +//What is the big O runtime of your code?: +// O(n) + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//Question 2: https://www.hackerrank.com/challenges/handshake + +//At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes? + +func factorial(number:Int) -> Int{ + if number == 0 || number == 1 { + return 1 + } + return number * factorial(number - 1) +} + +func handshakeCountFor(boardMembers: Int) -> Int{ + + if (boardMembers <= 1) { + return 0 + } + + let r = 2 + let handshakeCount = (factorial(boardMembers)) / (factorial(r) * factorial(boardMembers - r)) + + return handshakeCount +} + +handshakeCountFor(5) + +func handshakeCountsForArray(arrayOfBoardMembers: [Int]) -> [Int]{ + var handShakeCounts = [Int]() + for boardMemberCount in arrayOfBoardMembers{ + handShakeCounts.append(handshakeCountFor(boardMemberCount)) + } + return handShakeCounts +} + +handshakeCountsForArray([1,2,3,4,5]) + +//What is the big O runtime of your code?: +// O(n^2) + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//Question 3: https://www.hackerrank.com/challenges/connecting-towns +//Gandalf is travelling from Rohan to Rivendell to meet Frodo but there is no direct route from Rohan (T1) to Rivendell (Tn). +//But there are towns T2,T3,T4...Tn-1 such that there are N1 routes from Town T1 to T2, and in general, Ni routes from Ti to Ti+1 for i=1 to n-1 and 0 routes for any other Ti to Tj for j ≠ i+1 +//Find the total number of routes Gandalf can take to reach Rivendell from Rohan. + +//Note +//Gandalf has to pass all the towns Ti for i=1 to n-1 in numerical order to reach Tn. +//For each Ti , Ti+1 there are only Ni distinct routes Gandalf can take. + +//Copy and paste your code: + +func possibleRoutesBetweenTowns(townRoutes:[Int]) -> Int{ //number of towns is townRoutes.count + 1 + + var possibleRoutes = 1 + for routes in townRoutes{ + possibleRoutes = possibleRoutes * routes + } + + return possibleRoutes +} + +possibleRoutesBetweenTowns([2,3,5,6]) + +//What is the big O runtime of your code?: +//O(n) -/* - -Question 1: https://www.hackerrank.com/challenges/minimum-draws - -Copy and paste your code: - - - - - - - -What is the big O runtime of your code?: - - - - - - - -Question 2: https://www.hackerrank.com/challenges/handshake - -Copy and paste your code: - -What is the big O runtime of your code?: - -Question 3: https://www.hackerrank.com/challenges/connecting-towns - -Copy and paste your code: - -What is the big O runtime of your code?: - -*/ From cde78394103ad73b556b455ae51bbdb81151cc7c Mon Sep 17 00:00:00 2001 From: benstone1 Date: Mon, 18 Jan 2016 12:01:44 -0500 Subject: [PATCH 3/7] added 1-17 hw --- .../Contents.swift | 26 +++++++++++++++++++ .../contents.xcplayground | 4 +++ .../contents.xcworkspacedata | 7 +++++ .../timeline.xctimeline | 6 +++++ 4 files changed, 43 insertions(+) create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/Contents.swift create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift new file mode 100644 index 0000000..5d51051 --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -0,0 +1,26 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + +/* + + +Work on your solutions here. + +Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw + +1) + + + +2) + + + +3) + + + +*/ \ No newline at end of file diff --git a/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground b/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline b/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 1708efad0fbfcf36e37feb29840e40c59a8442e5 Mon Sep 17 00:00:00 2001 From: Derek316x Date: Thu, 21 Jan 2016 18:54:02 -0500 Subject: [PATCH 4/7] d --- .../Contents.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 4320efb..2c00b1d 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -59,17 +59,19 @@ func handshakeCountFor(boardMembers: Int) -> Int{ handshakeCountFor(5) func handshakeCountsForArray(arrayOfBoardMembers: [Int]) -> [Int]{ + var handShakeCounts = [Int]() for boardMemberCount in arrayOfBoardMembers{ handShakeCounts.append(handshakeCountFor(boardMemberCount)) } return handShakeCounts + } -handshakeCountsForArray([1,2,3,4,5]) +handshakeCountsForArray([1,2,3,4,5,20]) //What is the big O runtime of your code?: -// O(n^2) +// O( Σ ai) //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //Question 3: https://www.hackerrank.com/challenges/connecting-towns From 77034156e504846789dbe16bf2d73cd0335ad956 Mon Sep 17 00:00:00 2001 From: Derek316x Date: Fri, 15 Jan 2016 23:49:19 -0500 Subject: [PATCH 5/7] e --- .../Contents.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..c3bbee1 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -11,8 +11,20 @@ Question 1: https://www.hackerrank.com/challenges/minimum-draws Copy and paste your code: + + + + + + What is the big O runtime of your code?: + + + + + + Question 2: https://www.hackerrank.com/challenges/handshake Copy and paste your code: From 19e878a0dd6de97e3f3318af1995db7dd31bcd06 Mon Sep 17 00:00:00 2001 From: Derek316x Date: Sat, 16 Jan 2016 02:05:18 -0500 Subject: [PATCH 6/7] submit --- .../Contents.swift | 129 +++++++++++++----- 1 file changed, 95 insertions(+), 34 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index c3bbee1..4320efb 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -2,40 +2,101 @@ import UIKit -var str = "Hello, playground" +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//Question 1: https://www.hackerrank.com/challenges/minimum-draws + +//Jim is off to a party and is searching for a matching pair of socks. His drawer is filled with socks, each pair of a different color. In its worst case scenario, how many socks (x) should Jim remove from his drawer until he finds a matching pair? + +func howManySocks(pairsOfSocks: Int) -> Int { + + if pairsOfSocks <= 0 || pairsOfSocks == 1 { + return 0 + } + let pairsOfSocksDouble = Double(pairsOfSocks) + let worstCaseSocksToRemoveDouble = ceil(pairsOfSocksDouble/2) + 1 + + return Int(worstCaseSocksToRemoveDouble) +} + +func worstCaseScenarioDrawsFor(sockArray:[Int]) -> [Int] { + var answer = [Int]() + for pairCount in sockArray{ + answer.append(howManySocks(pairCount)) + } + return answer +} + +worstCaseScenarioDrawsFor([2,3,4,5,6,7,8]) + +//What is the big O runtime of your code?: +// O(n) + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//Question 2: https://www.hackerrank.com/challenges/handshake + +//At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes? + +func factorial(number:Int) -> Int{ + if number == 0 || number == 1 { + return 1 + } + return number * factorial(number - 1) +} + +func handshakeCountFor(boardMembers: Int) -> Int{ + + if (boardMembers <= 1) { + return 0 + } + + let r = 2 + let handshakeCount = (factorial(boardMembers)) / (factorial(r) * factorial(boardMembers - r)) + + return handshakeCount +} + +handshakeCountFor(5) + +func handshakeCountsForArray(arrayOfBoardMembers: [Int]) -> [Int]{ + var handShakeCounts = [Int]() + for boardMemberCount in arrayOfBoardMembers{ + handShakeCounts.append(handshakeCountFor(boardMemberCount)) + } + return handShakeCounts +} + +handshakeCountsForArray([1,2,3,4,5]) + +//What is the big O runtime of your code?: +// O(n^2) + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//Question 3: https://www.hackerrank.com/challenges/connecting-towns +//Gandalf is travelling from Rohan to Rivendell to meet Frodo but there is no direct route from Rohan (T1) to Rivendell (Tn). +//But there are towns T2,T3,T4...Tn-1 such that there are N1 routes from Town T1 to T2, and in general, Ni routes from Ti to Ti+1 for i=1 to n-1 and 0 routes for any other Ti to Tj for j ≠ i+1 +//Find the total number of routes Gandalf can take to reach Rivendell from Rohan. + +//Note +//Gandalf has to pass all the towns Ti for i=1 to n-1 in numerical order to reach Tn. +//For each Ti , Ti+1 there are only Ni distinct routes Gandalf can take. + +//Copy and paste your code: + +func possibleRoutesBetweenTowns(townRoutes:[Int]) -> Int{ //number of towns is townRoutes.count + 1 + + var possibleRoutes = 1 + for routes in townRoutes{ + possibleRoutes = possibleRoutes * routes + } + + return possibleRoutes +} + +possibleRoutesBetweenTowns([2,3,5,6]) + +//What is the big O runtime of your code?: +//O(n) -/* - -Question 1: https://www.hackerrank.com/challenges/minimum-draws - -Copy and paste your code: - - - - - - - -What is the big O runtime of your code?: - - - - - - - -Question 2: https://www.hackerrank.com/challenges/handshake - -Copy and paste your code: - -What is the big O runtime of your code?: - -Question 3: https://www.hackerrank.com/challenges/connecting-towns - -Copy and paste your code: - -What is the big O runtime of your code?: - -*/ From 697929bf0cf43a22c6d35690be6b1ba582bbe1c7 Mon Sep 17 00:00:00 2001 From: Derek316x Date: Thu, 21 Jan 2016 18:54:02 -0500 Subject: [PATCH 7/7] d --- .../Contents.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 4320efb..2c00b1d 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -59,17 +59,19 @@ func handshakeCountFor(boardMembers: Int) -> Int{ handshakeCountFor(5) func handshakeCountsForArray(arrayOfBoardMembers: [Int]) -> [Int]{ + var handShakeCounts = [Int]() for boardMemberCount in arrayOfBoardMembers{ handShakeCounts.append(handshakeCountFor(boardMemberCount)) } return handShakeCounts + } -handshakeCountsForArray([1,2,3,4,5]) +handshakeCountsForArray([1,2,3,4,5,20]) //What is the big O runtime of your code?: -// O(n^2) +// O( Σ ai) //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //Question 3: https://www.hackerrank.com/challenges/connecting-towns