java - 将伪代码转换为 Scala 时遇到问题
问题描述
我试图让我的 Scala 代码像我的伪代码一样。我可以轻松地将我的伪代码转换为 Python 代码并获得我想要的值。但是,我的 Scala 代码给出了各种错误。如何让我的 Scala 代码以与伪代码相同的方式执行?
伪代码:
#def word(file, first_word, second_word):
# -open file and read the file
# -create a hash table called table_1
# -create a hash table called table_2
# -create a hash table called table_3
# -create a empty string called string_1
# -create a empty string called string_2
# -loop through file line by line
# -take every first word of each line as key and concatenate the
# pronunciations of each line as value and store this in
# table_1
# -loop through the keys and values in table_1
# -loop through the values of table_1 and add only the values
# to string_1 that are not numeric values; this new string
# will now be the value of table_2 with the key still remaining
# as the word
# -loop through the keys and values in table_2
# -now take the string values of table_2 and reverse that
# backwards
# -loop through the reversed string and keep adding the
# values to string_2 until you encounter a vowel.
# -now you add this new string to table_3 as values with
# the same words as before as keys
# -if first_word is not in table_3 or second_word is not in
# table_3, then return an empty list
# -if the values of first_word and the values of second_word
# are equal; then return True, meaning that the words rhyme
# -if the values of first_word and the values of second_word
# are not equal; then return False, meaning that the words
# don't rhyme
解决方案
这是伪代码的scala版本。它与伪代码不完全匹配,但也许它提供了一些有用的提示
val source = Source.fromFile(filename).getLines
// take every first word of each line as key and concatenate the
// pronunciations of each line as value and store this in table_1
val table1: Map[String, String] = source.map { line =>
val words = line.split(" ")
(words.head, words.drop(1).mkString)
}
.toMap
// loop through the values of table_1 and add only the values
// to string_1 that are not numeric values; this new string
// will now be the value of table_2 with the key still remaining
// as the word
val table2 = table1.map { case (key, value) =>
val string_1 = value.toCharArray
.filter(!_.isDigit)
.mkString
(key, string_1)
}
.toMap
// -loop through the reversed string and keep adding the
// values to string_2 until you encounter a vowel.
// -now you add this new string to table_3 as values with
//the same words as before as keys
val table3 = table2.map { case (key, value) =>
val string_2 = value
.reverse
.takeWhile("AEIOU".indexOf(_) == -1)
(key, string_2 + value.charAt(value.length - 1 - string_2.length))
}
.toMap
val first_word = "CAT"
val second_word = "xx"
if (table3.get(first_word).isDefined && table3.get(second_word).isDefined) {
Left(table3.get(first_word).get == table3.get(second_word).get)
} else {
Right(List())
}
推荐阅读
- javascript - 如何从对象数组中创建数量最多的项目的对象映射?
- c# - json文件修改时如何实时更改listview值
- python-3.x - Python 线程脚本获取无效状态
- python - 如何使用 python strptime 和 strftime 将日期时间格式如 2017-02-09T09:43:04.216+1000 转换为 09-Feb-2017?
- kubernetes - openshift 3.11 安装失败 - 无法更新 cni 配置:在 /etc/cni/net.d 中找不到网络”,
- mongodb - 您只能使用弹簧数据获取井中集合的某些属性吗?
- python - 如何在numpy中使用order ='F'更改数组条目
- c - 压缩整数列表以进行搜索访问
- ios - iOS 13.0 - 支持深色模式并支持 iOS 11 和 12 的最佳方法
- python - 为 pandas 中的不同列以不同方式填充缺失值