在和数组中找到一个字母(来自一个数组),然后使用索引从另一个数组中获取一个值
Find a letter (from an Array) in and Array and then use the index to get a value from another Array
我正在尝试使用下面的 calc 函数计算一个词
让我们假设 showLetters = ["S","W","I","F","T"]
所以 S = 1,W = 4,I = 1,F = 4,T = 1,给我 wordScore 11....
我使用数组 alphaLetter 来查找字母在字母表中的位置。
我使用找到的字母的索引从 alphaScore 中获取它的值。
func calcButton() {
var testWord = ""
var wordScore = 0
let alphaLetter = Array("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")
let alphaScore = Array ("1,3,2,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10")
for k in (0...6)
{
testWord = testWord + showLetters[k]
let letterFound = String(showLetters[k])
*if let index = alphaLetter.firstIndex(of: letterFound) {*
}
}
我正在努力获取索引
我尝试将 letterFound 指定为找到的字母的字符串。然后尝试使用
如果让 index = alphaLetter.firstIndex(of: letterFound)
但这给了我一个错误
无法将类型 'String' 的值转换为预期的参数类型 'String.Element'(又名 'Character')
所以我有点卡住了...
建议或指导将不胜感激
谢谢
alphaLetter
和 alphaScore
是字符串。从一个字符串创建数组的结果是 [Character]
包括逗号!
可能是这个意思(在 Playground 中测试过)
let showLetters = ["S","W","I","F","T"]
func calcButton() {
var testWord = ""
var wordScore = 0
let alphaLetter = Array(["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"])
let alphaScore = Array ([1,3,2,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10])
for k in 0..<showLetters.count
{
testWord = testWord + showLetters[k]
let letterFound = showLetters[k]
if let index = alphaLetter.firstIndex(of: letterFound) {
wordScore += alphaScore[index]
}
}
print(wordScore)
}
calcButton()
尽管如此,使用字典效率更高 ["A" : 1, "B" : 3 ...]
func calcButton() {
var testWord = ""
var wordScore = 0
let alphaDict = ["P": 3, "U": 1, "B": 3, "M": 3, "A": 1, "C": 2, "V": 4, "L": 1, "Q": 10, "D": 2, "H": 4, "K": 5, "N": 1, "J": 8, "T": 1, "E": 1, "X": 8, "R": 1, "O": 1, "I": 1, "G": 2, "F": 4, "Y": 4, "Z": 10, "W": 4, "S": 1]
for character in showLetters {
if let value = alphaDict[character] {
wordScore += value
}
}
print(wordScore)
}
我正在尝试使用下面的 calc 函数计算一个词 让我们假设 showLetters = ["S","W","I","F","T"]
所以 S = 1,W = 4,I = 1,F = 4,T = 1,给我 wordScore 11....
我使用数组 alphaLetter 来查找字母在字母表中的位置。 我使用找到的字母的索引从 alphaScore 中获取它的值。
func calcButton() {
var testWord = ""
var wordScore = 0
let alphaLetter = Array("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")
let alphaScore = Array ("1,3,2,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10")
for k in (0...6)
{
testWord = testWord + showLetters[k]
let letterFound = String(showLetters[k])
*if let index = alphaLetter.firstIndex(of: letterFound) {*
}
}
我正在努力获取索引
我尝试将 letterFound 指定为找到的字母的字符串。然后尝试使用 如果让 index = alphaLetter.firstIndex(of: letterFound)
但这给了我一个错误 无法将类型 'String' 的值转换为预期的参数类型 'String.Element'(又名 'Character')
所以我有点卡住了...
建议或指导将不胜感激
谢谢
alphaLetter
和 alphaScore
是字符串。从一个字符串创建数组的结果是 [Character]
包括逗号!
可能是这个意思(在 Playground 中测试过)
let showLetters = ["S","W","I","F","T"]
func calcButton() {
var testWord = ""
var wordScore = 0
let alphaLetter = Array(["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"])
let alphaScore = Array ([1,3,2,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10])
for k in 0..<showLetters.count
{
testWord = testWord + showLetters[k]
let letterFound = showLetters[k]
if let index = alphaLetter.firstIndex(of: letterFound) {
wordScore += alphaScore[index]
}
}
print(wordScore)
}
calcButton()
尽管如此,使用字典效率更高 ["A" : 1, "B" : 3 ...]
func calcButton() {
var testWord = ""
var wordScore = 0
let alphaDict = ["P": 3, "U": 1, "B": 3, "M": 3, "A": 1, "C": 2, "V": 4, "L": 1, "Q": 10, "D": 2, "H": 4, "K": 5, "N": 1, "J": 8, "T": 1, "E": 1, "X": 8, "R": 1, "O": 1, "I": 1, "G": 2, "F": 4, "Y": 4, "Z": 10, "W": 4, "S": 1]
for character in showLetters {
if let value = alphaDict[character] {
wordScore += value
}
}
print(wordScore)
}