Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
Created March 28, 2021 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodycatliu/7e251d0c8b8d3cbf856232a754c5ebd6 to your computer and use it in GitHub Desktop.
Save woodycatliu/7e251d0c8b8d3cbf856232a754c5ebd6 to your computer and use it in GitHub Desktop.
import Foundation
let input = """
3
4
4 2 1 3
2
1 2
7
7 1 2 3 4 5 6
"""
var lines = input.components(separatedBy: "\n")
func readLine() -> String? {
let firstStr = lines.first
lines.remove(at: 0)
return firstStr!
}
func getIntegersOf(line: String, separatedBy separator: String) -> [Int] {
let stringArray = line.components(separatedBy: separator)
return stringArray.map { Int($0)! }
}
var c: Int = 0
for line in lines {
if line.count > 1 {
// dosomething
c += 1
let input = getIntegersOf(line: line, separatedBy: " ")
let output = doSomething(input)
print("input:", input)
print(output)
}
}
func doSomething(_ input: [Int])-> String {
var result: Int = 0
var input = input
// (i, exchange)
for i in 0..<input.count - 1 {
var exchange = i
var min: Int = input[i]
for j in i + 1..<input.count {
if min > input[j] {
min = input[j]
exchange = j
}
}
let reversed = Array(input[i...exchange].reversed())
for k in i...exchange {
input[k] = reversed[k - i]
}
result += ( exchange - i + 1)
}
return "Case #\(c): \(result)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment