Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Сложение ступенчатых графиков
- struct Point {
- var time: Int
- var value: Int
- }
- func sumSeries(a: [Point], b: [Point]) -> [Point] {
- var result: [Point] = []
- var pntA = 0
- var pntB = 0
- var last_a = 0
- var last_b = 0
- while pntA < a.count && pntB < b.count {
- if a[pntA].time < b[pntB].time {
- result.append(Point(time: a[pntA].time, value: a[pntA].value + last_b))
- last_a = a[pntA].value
- pntA += 1
- } else if a[pntA].time == b[pntB].time {
- result.append(Point(time: a[pntA].time, value: a[pntA].value + b[pntB].value))
- last_a = a[pntA].value
- last_b = b[pntB].value
- pntA += 1
- pntB += 1
- } else {
- result.append(Point(time: b[pntB].time, value: b[pntB].value + last_a))
- last_b = b[pntB].value
- pntB += 1
- }
- }
- while pntA < a.count {
- result.append(Point(time: a[pntA].time, value: a[pntA].value + last_b))
- pntB += 1
- }
- while pntB < b.count {
- result.append(Point(time: b[pntB].time, value: b[pntB].value + last_a))
- pntB += 1
- }
- return result
- }
- var result = sumSeries(a: [Point(time: 1, value: 2), Point(time: 5, value: 1)], b: [Point(time: 2, value: 4), Point(time: 3, value: 6), Point(time: 9, value: 7)])
- for point in result {
- print("{\(point.time), \(point.value)}", terminator: " ")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement