Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
19
Task/Repeat-a-string/Swift/repeat-a-string-1.swift
Normal file
19
Task/Repeat-a-string/Swift/repeat-a-string-1.swift
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
extension String {
|
||||
// Slower version
|
||||
func repeatString(n: Int) -> String {
|
||||
return Array(count: n, repeatedValue: self).joinWithSeparator("")
|
||||
}
|
||||
|
||||
// Faster version
|
||||
// benchmarked with a 1000 characters and 100 repeats the fast version is approx 500 000 times faster :-)
|
||||
func repeatString2(n:Int) -> String {
|
||||
var result = self
|
||||
for _ in 1 ..< n {
|
||||
result.appendContentsOf(self) // Note that String.appendContentsOf is up to 10 times faster than "result += self"
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
print( "ha".repeatString(5) )
|
||||
print( "he".repeatString2(5) )
|
||||
1
Task/Repeat-a-string/Swift/repeat-a-string-2.swift
Normal file
1
Task/Repeat-a-string/Swift/repeat-a-string-2.swift
Normal file
|
|
@ -0,0 +1 @@
|
|||
String(count:5, repeatedValue:"*" as Character)
|
||||
19
Task/Repeat-a-string/Swift/repeat-a-string-3.swift
Normal file
19
Task/Repeat-a-string/Swift/repeat-a-string-3.swift
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
extension String {
|
||||
func repeatBiterative(count: Int) -> String {
|
||||
var reduceCount = count
|
||||
var result = ""
|
||||
var doubled = self
|
||||
while reduceCount != 0 {
|
||||
if reduceCount & 1 == 1 {
|
||||
result.appendContentsOf(doubled)
|
||||
}
|
||||
reduceCount >>= 1
|
||||
if reduceCount != 0 {
|
||||
doubled.appendContentsOf(doubled)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
"He".repeatBiterative(5)
|
||||
Loading…
Add table
Add a link
Reference in a new issue