Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- let id: Int
- let x: Double
- let y: Double
- var neighbors: [Node] = []
- init(id: Int, x: Double, y: Double) {
- self.id = id
- self.x = x
- self.y = y
- }
- }
- /** Phase 1: Create a node for each entry in rawNodes.
- * Use a for-loop to create a node for each raw node.
- * In the for-loop, invoke the node constructor,
- * and add the node to the list.
- *
- Example input:
- [
- [1, 1.0, 1.0],
- [2, 1.0, 0.5],
- [3, 1.4, 0.8],
- [4, 1.9, 1.1],
- [5, 1.3, 1.0],
- [6, 1.1, 1.8]]
- Expected output:
- a list of 6 Node instances, one for each entry above.
- */
- func parseNodesPhase1(_ rawNodes: [[Double]]) -> [Node] {
- //
- // YOUR CODE GOES HERE.
- //
- }
- /** Test code, you don't need to modify this! Just run the file. */
- // Given these test rawnodes.
- let testinput = [
- [1, 1.0, 1.0],
- [2, 1.0, 0.5],
- [3, 1.4, 0.8],
- [4, 1.9, 1.1],
- [5, 1.3, 1.0],
- [6, 1.1, 1.8]]
- // When you call parseNodesPhase1.
- let testoutput = parseNodesPhase1(testinput)
- // Expect it create a node for each rawnode.
- if (testoutput.count != testinput.count) {
- print("Expected \(testinput.count) nodes but only got \(testoutput.count)")
- } else {
- var hadError = false
- for i in 0...testinput.count-1 {
- let rn = testinput[i]
- let n = testoutput[i]
- if (Int(rn[0]) != n.id) {
- print("Node and index \(i) had id=\(n.id) but expected \(Int(rn[0]))")
- hadError = true
- }
- }
- if (!hadError) {
- print("Passed.")
- }
- }
Add Comment
Please, Sign In to add comment