Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Look-and-say-sequence/Swift/look-and-say-sequence.swift
Normal file
28
Task/Look-and-say-sequence/Swift/look-and-say-sequence.swift
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
func lookAndSay(_ seq: [Int]) -> [Int] {
|
||||
var result = [Int]()
|
||||
var cur = seq[0]
|
||||
var curRunLength = 1
|
||||
|
||||
for i in seq.dropFirst() {
|
||||
if cur == i {
|
||||
curRunLength += 1
|
||||
} else {
|
||||
result.append(curRunLength)
|
||||
result.append(cur)
|
||||
curRunLength = 1
|
||||
cur = i
|
||||
}
|
||||
}
|
||||
|
||||
result.append(curRunLength)
|
||||
result.append(cur)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
var seq = [1]
|
||||
|
||||
for i in 0..<10 {
|
||||
print("Seq \(i): \(seq)")
|
||||
seq = lookAndSay(seq)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue