Skip to content

Instantly share code, notes, and snippets.

@spullara
Created March 24, 2018 22:08
Show Gist options
  • Save spullara/95e46efe9ec48c520ce40101c4b85fda to your computer and use it in GitHub Desktop.
Save spullara/95e46efe9ec48c520ce40101c4b85fda to your computer and use it in GitHub Desktop.
Swift implementation of my interview code
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