Created
March 24, 2018 22:08
-
-
Save spullara/95e46efe9ec48c520ce40101c4b85fda to your computer and use it in GitHub Desktop.
Swift implementation of my interview code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func render(text: String, entities: Set<Entity>) -> String { | |
let entityArray = Array(entities).sorted() | |
var sb = String() | |
var pos = 0 | |
var posIndex = text.startIndex | |
for entity in entityArray { | |
let startIndex = text.index(posIndex, offsetBy: entity.start - pos) | |
sb.append(contentsOf: text[posIndex ..< startIndex]) | |
sb += entity.html | |
posIndex = text.index(startIndex, offsetBy: entity.end - entity.start) | |
pos = entity.end | |
} | |
sb.append(contentsOf: text[posIndex...]) | |
return sb | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment