Advertisement
Larme

Untitled

Jun 20th, 2022
1,636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.65 KB | None | 0 0
  1. func comparingSpeedTyping() {
  2.  
  3.  
  4.  
  5.     func taskOne(input: String) -> String {
  6.  
  7.         var s: String
  8.         var prevTime = 0
  9.         var a: [Int]
  10.  
  11.         let string = input
  12.         let stringArr = string.components(separatedBy: [" ", "\n"])
  13.  
  14.         if Int(stringArr[0]) ?? 0 == 0 {
  15.             return ""
  16.         }
  17.  
  18.         s = String(stringArr[1])
  19.  
  20.         a = stringArr.compactMap{ Int($0) }
  21.         a.remove(at: 0)
  22.         a = a.reduce([Int]()) { (newA: [Int], time: Int) -> [Int] in
  23.             let newTime = time - prevTime
  24.             prevTime = time
  25.             return newA + [newTime]
  26.         }
  27.  
  28.         let maxIndex = a.max()
  29.         let indexOfMax = Int(a.lastIndex(of: maxIndex ?? 0)!)
  30.  
  31.         return String(s[s.index(s.startIndex, offsetBy: indexOfMax)])
  32.     }
  33.  
  34.  
  35.  
  36.     func taskOne2(input: String) -> String {
  37.         let lines = input.components(separatedBy: .newlines)
  38.         guard lines.count == 3 else { return "" }
  39.         guard let length = Int(lines[0]) else { return "" }
  40.         let letters = lines[1]
  41.         guard letters.count == length else { return "" }
  42.         let timing = lines[2].components(separatedBy: .whitespaces)
  43.         guard timing.count == length else { return "" }
  44.  
  45.         var timer = 0
  46.         var longestTime = 0
  47.         var longestIndex = 0
  48.         for anIndex in 0..<length {
  49.             let date = Int(timing[anIndex])!
  50.             let time = date - timer
  51.             timer = date
  52.             if time >= longestTime {  //"First longest", time > longestTime, if "last longest"
  53.                 longestTime = time
  54.                 longestIndex = anIndex
  55.             }
  56.         }
  57.         let index = letters.index(letters.startIndex, offsetBy: longestIndex)
  58.         return String(letters[index])
  59.     }
  60.  
  61.  
  62.  
  63.     func constructInput(lenght: Int) -> String {
  64.         let possibleLetters = "abcdefghijklmnopqrstuvwxyz"
  65.         var timer = 0
  66.         let letters = (0..<lenght).map { _ in String(possibleLetters.randomElement()!) }.joined()
  67.         let durations = (0..<lenght).reduce(into: [String](), { partialResult, _ in
  68.             let newDuration = Int.random(in: 0..<100)
  69.             timer += newDuration
  70.             partialResult.append(String(timer))
  71.         }).joined(separator: " ")
  72.         return ["\(lenght)", letters, durations].joined(separator: "\n")
  73.     }
  74.  
  75.     func compareMethods(input: String) {
  76.         print("----")
  77. //        print("Analyzing:\n\(input)")
  78.         var date = Date()
  79.         let result = taskOne(input: input)
  80.         let duration1 = Date().timeIntervalSince(date)
  81.         date = Date()
  82.         let result2 = taskOne2(input: input)
  83.         let duration2 = Date().timeIntervalSince(date)
  84.         print("Initial Method output: \(result) duration: \(duration1)")
  85.         print("Other Method output: \(result2) duration: \(duration2)")
  86.         print("Coeff: \(duration1/duration2)") //If > 1, it means that initial method is longer
  87.         print("----")
  88.     }
  89.  
  90.     let initialInput = """
  91.        5
  92.        aabbc
  93.        1 3 5 7 8
  94.        """
  95.     compareMethods(input: initialInput)                     //On my test: coeff: 4.77
  96.     compareMethods(input: constructInput(lenght: 10))       //On my test: coeff: 1.19
  97.     compareMethods(input: constructInput(lenght: 20))       //On my test: coeff: 1.66
  98.     compareMethods(input: constructInput(lenght: 30))       //On my test: coeff: 1.42
  99.     compareMethods(input: constructInput(lenght: 100))      //On my test: coeff: 1.30
  100.     compareMethods(input: constructInput(lenght: 1000))     //On my test: coeff: 1.04
  101.     compareMethods(input: constructInput(lenght: 100_000))  //On my test: coeff: 10.5
  102.  
  103. }
  104.  
  105. comparingSpeedTyping()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement