Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func comparingSpeedTyping() {
- func taskOne(input: String) -> String {
- var s: String
- var prevTime = 0
- var a: [Int]
- let string = input
- let stringArr = string.components(separatedBy: [" ", "\n"])
- if Int(stringArr[0]) ?? 0 == 0 {
- return ""
- }
- s = String(stringArr[1])
- a = stringArr.compactMap{ Int($0) }
- a.remove(at: 0)
- a = a.reduce([Int]()) { (newA: [Int], time: Int) -> [Int] in
- let newTime = time - prevTime
- prevTime = time
- return newA + [newTime]
- }
- let maxIndex = a.max()
- let indexOfMax = Int(a.lastIndex(of: maxIndex ?? 0)!)
- return String(s[s.index(s.startIndex, offsetBy: indexOfMax)])
- }
- func taskOne2(input: String) -> String {
- let lines = input.components(separatedBy: .newlines)
- guard lines.count == 3 else { return "" }
- guard let length = Int(lines[0]) else { return "" }
- let letters = lines[1]
- guard letters.count == length else { return "" }
- let timing = lines[2].components(separatedBy: .whitespaces)
- guard timing.count == length else { return "" }
- var timer = 0
- var longestTime = 0
- var longestIndex = 0
- for anIndex in 0..<length {
- let date = Int(timing[anIndex])!
- let time = date - timer
- timer = date
- if time >= longestTime { //"First longest", time > longestTime, if "last longest"
- longestTime = time
- longestIndex = anIndex
- }
- }
- let index = letters.index(letters.startIndex, offsetBy: longestIndex)
- return String(letters[index])
- }
- func constructInput(lenght: Int) -> String {
- let possibleLetters = "abcdefghijklmnopqrstuvwxyz"
- var timer = 0
- let letters = (0..<lenght).map { _ in String(possibleLetters.randomElement()!) }.joined()
- let durations = (0..<lenght).reduce(into: [String](), { partialResult, _ in
- let newDuration = Int.random(in: 0..<100)
- timer += newDuration
- partialResult.append(String(timer))
- }).joined(separator: " ")
- return ["\(lenght)", letters, durations].joined(separator: "\n")
- }
- func compareMethods(input: String) {
- print("----")
- // print("Analyzing:\n\(input)")
- var date = Date()
- let result = taskOne(input: input)
- let duration1 = Date().timeIntervalSince(date)
- date = Date()
- let result2 = taskOne2(input: input)
- let duration2 = Date().timeIntervalSince(date)
- print("Initial Method output: \(result) duration: \(duration1)")
- print("Other Method output: \(result2) duration: \(duration2)")
- print("Coeff: \(duration1/duration2)") //If > 1, it means that initial method is longer
- print("----")
- }
- let initialInput = """
- 5
- aabbc
- 1 3 5 7 8
- """
- compareMethods(input: initialInput) //On my test: coeff: 4.77
- compareMethods(input: constructInput(lenght: 10)) //On my test: coeff: 1.19
- compareMethods(input: constructInput(lenght: 20)) //On my test: coeff: 1.66
- compareMethods(input: constructInput(lenght: 30)) //On my test: coeff: 1.42
- compareMethods(input: constructInput(lenght: 100)) //On my test: coeff: 1.30
- compareMethods(input: constructInput(lenght: 1000)) //On my test: coeff: 1.04
- compareMethods(input: constructInput(lenght: 100_000)) //On my test: coeff: 10.5
- }
- comparingSpeedTyping()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement