无法将伪代码转换为Scala [英] Trouble translating pseudo-code into Scala

查看:122
本文介绍了无法将伪代码转换为Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的Scala代码像伪代码一样。我可以轻松地将伪代码转换为Python代码,并获取所需的值。但是,我的Scala代码给出了各种错误。如何使我的Scala代码与伪代码执行相同的操作?

I am trying to make my Scala code like my pseudo-code. I can easily convert my pseudo-code to Python code and get the values I want. But, my Scala code is giving all kinds of errors. How can I make my Scala code perform the same way as my pseudo-code?

伪代码:

#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版本。它与伪代码不完全匹配,但可能会给出一些有用的提示

Here is a scala version of the pseudo code. It doesn't match the pseudo code exactly but maybe it gives some useful hints

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())
}

这篇关于无法将伪代码转换为Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆