Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,31 @@
func list(s: Any...) -> [Any] {
return s
}
func flatten<T>(s: [Any]) -> [T] {
var r = [T]()
for e in s {
switch e {
case let a as [Any]:
r += flatten(a)
case let x as T:
r.append(x)
default:
assert(false, "value of wrong type")
}
}
return r
}
let s = list(list(1),
2,
list(list(3, 4), 5),
list(list(list())),
list(list(list(6))),
7,
8,
list()
)
println(s)
let result : [Int] = flatten(s)
println(result)

View file

@ -0,0 +1,29 @@
func list(s: Any...) -> [Any] {
return s
}
func flatten<T>(s: [Any]) -> [T] {
return s.flatMap {
switch $0 {
case let a as [Any]:
return flatten(a)
case let x as T:
return [x]
default:
assert(false, "value of wrong type")
}
}
}
let s = list(list(1),
2,
list(list(3, 4), 5),
list(list(list())),
list(list(list(6))),
7,
8,
list()
)
println(s)
let result : [Int] = flatten(s)
println(result)

View file

@ -0,0 +1,47 @@
func list(s: Any...) -> [Any]
{
return s
}
func flatten<T>(array: [Any]) -> [T]
{
var result: [T] = []
var workstack: [(array: [Any], lastIndex: Int)] = [(array, 0)]
workstackLoop: while !workstack.isEmpty
{
for element in workstack.last!.array.suffixFrom(workstack.last!.lastIndex)
{
workstack[workstack.endIndex - 1].lastIndex++
if let element = element as? [Any]
{
workstack.append((element, 0))
continue workstackLoop
}
result.append(element as! T)
}
workstack.removeLast()
}
return result
}
let input = list(list(1),
2,
list(list(3, 4), 5),
list(list(list())),
list(list(list(6))),
7,
8,
list()
)
print(input)
let result: [Int] = flatten(input)
print(result)