Created
March 15, 2018 01:35
-
-
Save marsikeda/366c70942392f399dad0278301c989df to your computer and use it in GitHub Desktop.
Midterm DSA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Closure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func first5 (input: [Int]) -> Int { | |
var evenArray: [Int] = [] | |
for element in input { | |
if element % 2 == 0 { | |
evenArray.append(element) | |
} | |
} | |
if evenArray.count < 5 { | |
return input.reduce( 0, {$0 + $1}) | |
} | |
var answerArray: [Int] = [] | |
for index in 1...5 { | |
answerArray.append(evenArray[index]) | |
} | |
return answerArray.reduce(0 , {$0 + $1}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//not sure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
var key: Int | |
var next: Node? | |
init(key: Int) { | |
self.key = key | |
} | |
} | |
func sumNode (input: Node) -> Int { | |
return 0 | |
} | |
//not sure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//not sure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Queue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Queues are structured such that the first job in is the first job out (or done/printed in this case). A stack would print the last job entered into it first. And a hashmap is a bit too elaborate for this job. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hashmap |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//A hashmap would be ideal as it pairs values and keys together, so the artist name could be the key and their albums could be the songs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//An array, or more specifically, a Set would be the best for picking a name at random as the raffle numbers would be stored without an order and it would be easier to pick one at random (i.e., print out the set and pick the first one). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
O(n^2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
O(n^2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment