题目:读入一个文本文件,确定所有单词的使用频率并从高到低排序,打印出所有单词及其频率的排序列表。


题目:找到一个字符串里面某个字符数组里面第一个出现的字符的位置。比如“Hello, World”,[“a”, “e”, “i”, “o”, “u”],那 e 是在字符串第一个出现的字符,位置是 1, 返回 1
解题代码:
let words = "h e l l o w o r l d"
let compare = ["a", "e", "i", "o", "u"]
let wordsList = words.split(separator: " ")
var index = 0
zip(wordsList, compare).filter { $0 == $1}.map { (sub, str) in
index = wordsList.firstIndex(of: sub) ?? 0
}
print(index)
end