Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import UIKit
  2. import XCTest
  3.  
  4. var str = "Hello, playground"
  5. //var original = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"]
  6. var original = ["1", "2", "3", "4", "5", "6"]// "7", "8", "9"]
  7.  
  8. func arrange(original: [String], columnCount: Int) -> [String] {
  9. if original.count <= columnCount {
  10. return original
  11. }
  12. var arranged: [String] = Array(repeating: "", count: original.count)
  13. let columnCount = columnCount
  14.  
  15.  
  16. print("original.count / \(columnCount)", original.count / columnCount)
  17. print("original.count % \(columnCount)", original.count % columnCount)
  18. let height = (original.count / columnCount) + (original.count % columnCount)
  19. print("height", height)
  20.  
  21.  
  22. for position in 0..<original.count {
  23.  
  24. let newPos = (position % height) * columnCount + position / height
  25. print("newPos=", newPos, "position=", position, "height=", height)
  26. if newPos < original.count {
  27. arranged[newPos] = original[position]
  28. }
  29. }
  30. return arranged;
  31. }
  32.  
  33. //var arranged = arrange(original: original, columnCount: 3)
  34.  
  35.  
  36.  
  37.  
  38. class MyTestCase: XCTestCase {
  39.  
  40. func test1() {
  41. let input1 = ["1", "2", "3", "4", "5", "6"]
  42. let result1 = arrange(original: input1, columnCount: 2)
  43. XCTAssertEqual(result1, ["1", "4", "2", "5", "3", "6"])
  44. }
  45.  
  46. func test2() {
  47. let input2 = ["1", "2", "3", "4", "5", "6"]
  48. let result2 = arrange(original: input2, columnCount: 3)
  49. XCTAssertEqual(result2, ["1", "3", "5", "2", "4", "6"])
  50. }
  51.  
  52. func test3() {
  53. let input = ["1", "2"]
  54. let result = arrange(original: input, columnCount: 3)
  55. XCTAssertEqual(result, ["1", "2"])
  56. }
  57.  
  58. func test4() {
  59. let input = ["1", "2", "3"]
  60. let result = arrange(original: input, columnCount: 3)
  61. XCTAssertEqual(result, ["1", "2", "3"])
  62. }
  63.  
  64. func test5() {
  65. let input = ["1", "2", "3", "4", "5"]
  66. let result = arrange(original: input, columnCount: 3)
  67. XCTAssertEqual(result, ["1", "3", "5", "2", "4"])
  68. }
  69.  
  70. }
  71.  
  72.  
  73. MyTestCase.defaultTestSuite.run() // Swift 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement