Sha256: 0fbda34d2a1647090cd5575c24a8e8ee43eb384c63a3215424f46335b591c0c7

Contents?: true

Size: 2 KB

Versions: 262

Compression:

Stored size: 2 KB

Contents

import Foundation

private extension String {

    func trimWhiteSpace() -> String {
        let removeSpaces = trimCharacters(" ", sourceText: self)
        if removeSpaces.hasSuffix("\n") {
            return String(removeSpaces.characters.dropLast())
        }
        return  removeSpaces
    }

    func trimCharacters(_ charToTrim: Character, sourceText: String) -> String {
        var editCharacterView = sourceText.characters
        var editString = String(editCharacterView)

        let trimFirst  = sourceText.characters.first == charToTrim
        let trimLast   = sourceText.characters.last == charToTrim

        if trimFirst { editCharacterView  = editCharacterView.dropFirst() }
        if trimLast { editCharacterView  = editCharacterView.dropLast() }

        if trimFirst || trimLast == true {
            editString = trimCharacters(charToTrim, sourceText: String(editCharacterView))
        }
        return editString
    }
    var isQuestion: Bool {
        return hasSuffix("?")
    }

    var hasLetters: Bool {
        return containsLetters(self)
    }

    var isShouting: Bool {
        return (self == uppercased() && hasLetters)

    }

    private func containsLetters(_ input: String) -> Bool {
        let abc = "abcdefghijklmnopqrstuvwxyz"
        var contains = false
        let inputStringCollection = input.characters.map({ String($0) })
        let abcStringCollection = abc.characters.map({ String($0) })

        for each in inputStringCollection {
            abcStringCollection.forEach({
                if each == $0 || each == $0.uppercased() {
                    contains = true }

            })
        }
        return contains
    }

}

struct Bob {
    static func hey(_ input: String) -> String {
        if input.trimWhiteSpace().isEmpty {
            return "Fine. Be that way!"
        } else if input.isShouting {
            return "Whoa, chill out!"
        } else if input.isQuestion {
            return "Sure."
        } else {
            return "Whatever."
        }
    }

}

Version data entries

262 entries across 262 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.179 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.178 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.177 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.176 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.175 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.174 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.173 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.172 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.171 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.170 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.169 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.167 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.166 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.165 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.164 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.163 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.162 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.161 tracks/swift/exercises/bob/Sources/BobExample.swift
trackler-2.2.1.160 tracks/swift/exercises/bob/Sources/BobExample.swift