Advertisement
Larme

Untitled

Jul 21st, 2023
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.81 KB | None | 0 0
  1. func timingDicts() {
  2.     let keyTimeValues: [TimeInterval: Float] = [
  3.         0.4: 0.0,
  4.         0.45: 1.0,
  5.         0.475: 1.0,
  6.         0.5: 1.0,
  7.         0.525: 1.0,
  8.         0.55: 0.0,
  9.         0.575: 1.0,
  10.         0.6: 1.0,
  11.         0.65: 1.0,
  12.         0.7: 0.0
  13.     ]
  14.  
  15.     /// The challenge:
  16.     /// The keys represent animation key times, the values represent values. Some key times repeat the same value of the previous keytime.
  17.     /// How would you reduce the above dictionary in such way that the values do not repeat themselves
  18.     /// more than once?
  19.     /// For example, this group:
  20.     ///     0.45: 1.0,
  21.     ///     0.475: 1.0,
  22.     ///     0.5: 1.0,
  23.     ///     0.525: 1.0,
  24.     ///
  25.     /// The keytime 0.475 and 0.5 don't change the value and are, therefore, redundant in key frame animations.
  26.     ///
  27.     /// Expected outcome:
  28.     let expectedOutcome: [TimeInterval: Float] = [
  29.         0.4: 0.0,
  30.         0.45: 1.0,
  31.         0.525: 1.0,
  32.         0.55: 0.0,
  33.         0.575: 1.0,
  34.         0.65: 1.0,
  35.         0.7: 0.0
  36.     ]
  37.    
  38.     //I don't like the triple "iterations":
  39.     //          - sorted on keys
  40.     //          - `reduce(into:_:)`
  41.     //          - reconstruct the Dictionary, with `reduce(into:_:)` or `Dictionary(uniqueKeysWithValues:)`
  42.     // But I guess it's better for comparison than retrieve each time what is the "previous value", and before that one at each iteration
  43.    
  44.     let outputTupled = keyTimeValues.sorted { $0.key < $1.key }
  45.                         .reduce(into: [(TimeInterval, Float)]()) { partialResult, current in
  46.         guard partialResult.count > 1 else {
  47.             partialResult.append(current)
  48.             return
  49.         }
  50.         let previous = partialResult[partialResult.count - 1]
  51.         let beforeLast = partialResult[partialResult.count - 2]
  52.         if current.value == previous.1, previous.1 == beforeLast.1 {
  53.             partialResult[partialResult.count - 1] = current
  54.         } else {
  55.             partialResult.append(current)
  56.         }
  57.     }
  58.    
  59.     let output = outputTupled.reduce(into: [TimeInterval: Float]()) { $0[$1.0] = $1.1 }
  60.    
  61.     printDict(dict: output)
  62.     printDict(dict: expectedOutcome)
  63.     print(output == expectedOutcome)
  64.    
  65.     let output2 = Dictionary(uniqueKeysWithValues: outputTupled) //Is there some optimization here, might be better than `reduce(into:_:)`
  66.     printDict(dict: output2)
  67.     printDict(dict: expectedOutcome)
  68.     print(output2 == expectedOutcome)
  69.    
  70.     //To print in "order", easier to debug what went wrong, since the print doesn't guarantee the order if we want to compare
  71.     func printDict(dict: [TimeInterval: Float]) {
  72.         var printContent = Array(dict.keys).sorted { $0 < $1 }.map { "\t\($0): \(dict[$0]!)" }.joined(separator: ",\n")
  73.         print("[\n\(printContent)\n]")
  74.     }
  75. }
  76.  
  77. timingDicts()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement