Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import XCTest
- var str = "Hello, playground"
- //var original = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"]
- var original = ["1", "2", "3", "4", "5", "6"]// "7", "8", "9"]
- func arrange(original: [String], columnCount: Int) -> [String] {
- if original.count <= columnCount {
- return original
- }
- var arranged: [String] = Array(repeating: "", count: original.count)
- let columnCount = columnCount
- print("original.count / \(columnCount)", original.count / columnCount)
- print("original.count % \(columnCount)", original.count % columnCount)
- let height = (original.count / columnCount) + (original.count % columnCount)
- print("height", height)
- for position in 0..<original.count {
- let newPos = (position % height) * columnCount + position / height
- print("newPos=", newPos, "position=", position, "height=", height)
- if newPos < original.count {
- arranged[newPos] = original[position]
- }
- }
- return arranged;
- }
- //var arranged = arrange(original: original, columnCount: 3)
- class MyTestCase: XCTestCase {
- func test1() {
- let input1 = ["1", "2", "3", "4", "5", "6"]
- let result1 = arrange(original: input1, columnCount: 2)
- XCTAssertEqual(result1, ["1", "4", "2", "5", "3", "6"])
- }
- func test2() {
- let input2 = ["1", "2", "3", "4", "5", "6"]
- let result2 = arrange(original: input2, columnCount: 3)
- XCTAssertEqual(result2, ["1", "3", "5", "2", "4", "6"])
- }
- func test3() {
- let input = ["1", "2"]
- let result = arrange(original: input, columnCount: 3)
- XCTAssertEqual(result, ["1", "2"])
- }
- func test4() {
- let input = ["1", "2", "3"]
- let result = arrange(original: input, columnCount: 3)
- XCTAssertEqual(result, ["1", "2", "3"])
- }
- func test5() {
- let input = ["1", "2", "3", "4", "5"]
- let result = arrange(original: input, columnCount: 3)
- XCTAssertEqual(result, ["1", "3", "5", "2", "4"])
- }
- }
- MyTestCase.defaultTestSuite.run() // Swift 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement