Skip to content

Instantly share code, notes, and snippets.

@spullara spullara/main.swift
Created Mar 24, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.