Guest User

Untitled

a guest
Mar 13th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. class Node {
  2. let id: Int
  3. let x: Double
  4. let y: Double
  5. var neighbors: [Node] = []
  6.  
  7. init(id: Int, x: Double, y: Double) {
  8. self.id = id
  9. self.x = x
  10. self.y = y
  11. }
  12. }
  13.  
  14. /** Phase 1: Create a node for each entry in rawNodes.
  15. * Use a for-loop to create a node for each raw node.
  16. * In the for-loop, invoke the node constructor,
  17. * and add the node to the list.
  18. *
  19. Example input:
  20. [
  21. [1, 1.0, 1.0],
  22. [2, 1.0, 0.5],
  23. [3, 1.4, 0.8],
  24. [4, 1.9, 1.1],
  25. [5, 1.3, 1.0],
  26. [6, 1.1, 1.8]]
  27. Expected output:
  28. a list of 6 Node instances, one for each entry above.
  29. */
  30. func parseNodesPhase1(_ rawNodes: [[Double]]) -> [Node] {
  31. //
  32. // YOUR CODE GOES HERE.
  33. //
  34. }
  35.  
  36.  
  37. /** Test code, you don't need to modify this! Just run the file. */
  38.  
  39. // Given these test rawnodes.
  40. let testinput = [
  41. [1, 1.0, 1.0],
  42. [2, 1.0, 0.5],
  43. [3, 1.4, 0.8],
  44. [4, 1.9, 1.1],
  45. [5, 1.3, 1.0],
  46. [6, 1.1, 1.8]]
  47.  
  48. // When you call parseNodesPhase1.
  49. let testoutput = parseNodesPhase1(testinput)
  50.  
  51. // Expect it create a node for each rawnode.
  52. if (testoutput.count != testinput.count) {
  53. print("Expected \(testinput.count) nodes but only got \(testoutput.count)")
  54. } else {
  55. var hadError = false
  56. for i in 0...testinput.count-1 {
  57. let rn = testinput[i]
  58. let n = testoutput[i]
  59. if (Int(rn[0]) != n.id) {
  60. print("Node and index \(i) had id=\(n.id) but expected \(Int(rn[0]))")
  61. hadError = true
  62. }
  63. }
  64. if (!hadError) {
  65. print("Passed.")
  66. }
  67. }
Add Comment
Please, Sign In to add comment