-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.swift
More file actions
188 lines (159 loc) · 4.11 KB
/
Copy pathList.swift
File metadata and controls
188 lines (159 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// List.swift
// NinetyNineProblems
//
// Created by James Birtwell on 24/07/2017.
// Copyright © 2017 JimmyBee. All rights reserved.
//
import Foundation
class List<T>: CustomStringConvertible {
var value: T!
var next: List<T>?
public convenience init!(_ values: T...) {
self.init(Array(values))
}
init?(_ values: Array<T>) {
if values.count == 0 {
return nil
}
var nextValues = values
value = nextValues.removeFirst()
next = List(nextValues)
}
init() {
}
}
// P01
extension List {
var last: T? {
if next == nil {
return value
} else {
return next?.last
}
}
}
// P02
extension List {
var pennultimate: T? {
if next?.next == nil {
return value
} else {
return next?.pennultimate
}
}
}
// P03
extension List {
subscript(index: Int) -> T? {
return index == 0 ? value : next?[index - 1]
}
}
// P04
extension List {
var length: Int {
return next == nil ? 1 : 1 + next!.length
}
}
// P05
extension List {
func reversed() -> List<T>? {
let newHead = recurseiveReverse(self, rest: next)
self.next = nil
return newHead
}
private func recurseiveReverse(_ last: List<T>, rest: List<T>?) -> List<T>? {
if next == nil {
next = last
return self
} else {
let newHead = next?.recurseiveReverse(self, rest: next)
next = last
return newHead
}
}
}
// P06
extension List where T:Equatable {
func isPalindrome() -> Bool {
let subscriptLength = self.length - 1
return checkPair(head: self, length: subscriptLength, valid: true)
}
func checkPair(head: List<T>?, length: Int, valid: Bool) -> Bool {
if head?[length] == nil {
return valid
} else {
return checkPair(head: head?.next, length: length - 2, valid: valid) && head!.value == head![length]! && valid
}
}
}
// P07
extension List {
func flatten() -> List {
_ = self.recursiveFlatten()
return self
}
private func recursiveFlatten() -> List<T>? {
if let subList = value as? List<T> {
let lastItemInSublist = subList.recursiveFlatten()
self.value = subList[0]
lastItemInSublist?.next = self.next
next = subList.next
}
if next != nil {
return next!.recursiveFlatten()
} else {
return self
}
}
}
// P08
extension List where T: Equatable {
func compress() {
next = removedDuplicate(value)
next?.compress()
}
private func removedDuplicate(_ ofValue: T) -> List<T>? {
if value == ofValue {
return next?.removedDuplicate(ofValue)
} else {
return self
}
}
}
// P09
extension List where T: Equatable {
func pack() -> List<List<T>> {
let duplicates = repeatedCountAndLast(value, count: 0)
let count = duplicates.0
let sublistLast = duplicates.1
let array = Array(repeating: value!, count: count)
let valueList: List<T>? = List.init(array)
let list = List<List<T>>()
list.value = valueList
let nextPacked = sublistLast?.pack()
list.next = nextPacked
return list
}
private func repeatedCountAndLast(_ ofValue: T, count: Int) -> (Int, List<T>?) {
if value == ofValue {
if next != nil {
return next!.repeatedCountAndLast(ofValue, count: count + 1)
} else {
return (count + 1, nil)
}
} else {
return (count, self)
}
}
}
extension List where T: Equatable {
func encode() -> List<(Int, T)> {
let packed = self.pack()
let count = packed.value.length
let list = List<(Int, T)>()
list.value = (count, self.value)
list.next = packed.next?.value.encode()
return list
}
}