Guest User

Untitled

a guest
Jan 24th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. CHALLENGE
  2.  
  3. Today your challenge is to write a function that takes in an Array of Arrays, and returns a String which is the markdown representation of a table of that data. Assume the first tuple is the headers for the table.
  4.  
  5. Examples
  6.  
  7. markdownTable([["name", "email"], ["emily", "emily@email.com"] , ["mary", "maryberry@gbbs.co.uk"]])
  8. // returns the String below
  9.  
  10. Example output (raw):
  11.  
  12. |name|email|
  13. |---|---|
  14. |emily|emily@email.com|
  15. |mary|maryberry@gbbs.co.uk|
  16.  
  17.  
  18. ASSUMPTIONS
  19. 1. The first array entered will be the header information
  20. 2. Only arrays consisting of two strings will be entered
  21.  
  22.  
  23. TEST CASES
  24.  
  25.  
  26.  
  27. APPROACH
  28.  
  29. First deal with the headers. They need to be printed followed by a "blank line": | --- | --- |. >> Actually I think this needs to be done inside the for loop. So a for loop can be used to loop through the arrays. For each array, print each string separated by piping and a space. Write an if statement to see if it is the first array in the array - if it is, print the blankLine. If not, don't print one.
  30.  
  31. CODE
  32.  
  33. func markdownTable(groups: [[String]]) -> String {
  34.  
  35. let header = groups[0]
  36.  
  37. let headerToPrint = "| \(header.joined(separator: " | ")) |"
  38.  
  39. //print(headerToPrint)
  40.  
  41. let blankLine = "| ---- | ---- |"
  42.  
  43. for eachArray in groups {
  44.  
  45. if eachArray == groups[0] {
  46. print("| \(eachArray.joined(separator: " | ")) |\n\(blankLine)")
  47. } else {
  48. print("| \(eachArray.joined(separator: " | ")) |")
  49. }
  50.  
  51. }
  52.  
  53. print("\(headerToPrint)\n\(blankLine)")
  54.  
  55. return "\(headerToPrint)\n\(blankLine)"
  56. }
  57.  
  58. markdownTable(groups: [["name", "email"], ["emily", "emily@email.com"], ["mary", "maryberry@gbbs.co.uk"]])
Add Comment
Please, Sign In to add comment