Sha256: 90bcd9a105f3840cf0c6cba07a1faa50946eff40fa4384d4b21e58762a005cb9

Contents?: true

Size: 1.37 KB

Versions: 325

Compression:

Stored size: 1.37 KB

Contents

import Foundation

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

325 entries across 325 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.158 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.157 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.156 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.155 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.154 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.153 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.152 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.151 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.150 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.149 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.148 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.147 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.146 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.145 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.144 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.143 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.142 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.141 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift
trackler-2.2.1.140 tracks/swift/exercises/scrabble-score/Sources/ScrabbleScoreExample.swift