Sha256: 9ffa2c121e80754c746130da465e0ed4fc57bbd80c929dfb6b5a4a93e70581b1

Contents?: true

Size: 1.35 KB

Versions: 41

Compression:

Stored size: 1.35 KB

Contents

private extension String {

    func containsCustom(_ input: Character) -> Bool {
        return contains(String(input))

    }

    func lowercasedCustom() -> String {
        return lowercased()

    }

    private func stripCharacters(_ charsToRemove: String) -> String {
        var returnString = ""
        self.characters.forEach {
            if !charsToRemove.containsCustom($0) {
                returnString.append($0)
            }}
        return returnString
    }
    var stripWhiteSpace: String {
        return stripCharacters(" ")
    }

    var isEmptyOrWhiteSpace: Bool {
        return self.stripWhiteSpace.isEmpty
    }
}

struct Scrabble {

    static var letterScores =
        [ "a": 1, "e": 1, "i": 1, "o": 1, "u": 1, "l": 1, "n": 1, "r": 1, "s": 1, "t": 1, "d": 2, "g": 2, "b": 3, "c": 3, "m": 3, "p": 3, "f": 4, "h": 4, "v": 4, "w": 4, "y": 4, "k": 5, "j": 8, "x": 8, "q": 10, "z": 10 ]

    static func score(_ input: String) -> Int {
        if input.isEmptyOrWhiteSpace {
            return 0
        }

        var count: Int = 0
        for each in input.lowercasedCustom().characters {
            count += letterScores[String(each)] ?? 0
        }
        return count
    }

    var word: String = ""

    var score: Int = 0

    init(_ word: String?) {
        self.word = word ?? ""
        self.score = Scrabble.score(word ?? "")

    }

}

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
trackler-2.0.6.15 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.14 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.13 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.12 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.11 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.10 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.9 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.8 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.7 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.6 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.5 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.4 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.3 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.2 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.1 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.6.0 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.5.18 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.5.17 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.5.16 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift
trackler-2.0.5.15 tracks/swift/exercises/scrabble-score/ScrabbleScoreExample.swift