Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I solve this problem in Swift. However, some cases I got terminated due to timeout because the solution isn't efficient enough. I did check the solutions and they are correct. Any tips on making it more efficient and code review?

enter image description here

import Foundation

func readInteger() -> Int {
    guard let line = readLine() else { fatalError("cannot read line") }
    guard let i = Int(line) else { fatalError("cannot convert line to int") }
    return i
}

func readStrings() -> [String] {
    guard let line = readLine() else { fatalError("cannot read line") }
    let strings = line.componentsSeparatedByString(" ").map { $0 }
    return strings
}

var stack = [String]()

var string = String()

enum Type: Int {
    case append = 1, delete, print, undo
}

func textEditor(operation: [String]) {
    guard let type = Type(rawValue: Int(operation[0])!) else { fatalError("unknown type") }
    switch type {
    case .append:
        stack.append(string)
        string += operation[1]
    case .delete:
        guard string.characters.count > Int(operation[1]) else { stack.append(string); string = ""; return }
        stack.append(string)
        string = string.substringToIndex(string.endIndex.advancedBy(-Int(operation[1])!))
    case .print:
        print(string[string.startIndex.advancedBy(Int(operation[1])!-1)])
    case .undo:
        if !stack.isEmpty {
            string = stack.last!
            stack.popLast()
        }
    }
}

func inputs() {
    let operations = readInteger()
    for _ in 0..<operations {
        let operation = readStrings()
        textEditor(operation)
    }
}

inputs()
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.