我在课程上做scala课程。我将完成第6周的任务。我创建了一个函数,用于查找单词中char的出现。测试方法相对于预期的结果序列失败。
以下是问题描述:
/** A word is simply a `String`. */
type Word = String
/** Converts the word into its character occurrence list.
*
* Note: the uppercase and lowercase version of the character are treated as the
* same character, and are represented as a lowercase character in the occurrence list.
*
* Note: you must use `groupBy` to implement this method!
*/
def wordOccurrences(w: Word): Occurrences = {
def breakWord(s: List[Char], xs: List[(Char, Int)]): List[(Char, Int)] = s match {
case Nil => xs
case char :: rest => breakWord(s.tail, matchTuple(xs, char))
}
breakWord(w.toList, List[(Char, Int)]())
}
def matchTuple(tupleList: List[(Char, Int)], char: Char): List[(Char, Int)] = tupleList match {
case Nil => tupleList:+ (char.toLower, 1)
case pair :: restOfList => {
if(pair._1.toLower == char.toLower)
restOfList :+(pair._1, pair._2+1)
else
tupleList:+ (char.toLower, 1)
}
}
有人可以指出我做错了什么。我不需要直接回答,只是对我的序列有什么问题的逻辑提示。这是测试及其结果:
assert(wordOccurrences("Robert") === List(('b', 1), ('e', 1), ('o', 1), ('r', 2), ('t', 1)))
这是输出:
[info] - wordOccurrences: Robert *** FAILED ***
[info] List((o,1), (b,1), (e,1), (r,2), (t,1)) did not equal List((b,1), (e,1), (o,1), (r,2), (t,1)) (AnagramsSuite.scala:20)
更新: 重构了我的功能:
def wordOccurrences(w: Word): Occurrences = {
def breakWord(s: List[Char], xs: List[(Char, Int)]): List[(Char, Int)] = s match {
case Nil => xs
case char :: rest => breakWord(s.tail, xs:+ (char, 1))
}
breakWord(w.toList, List[(Char, Int)]()).groupBy( pair => pair._1.toLower)
.map(entry => (entry._1.toLower, (entry._1.toLower, entry._2.size)) )
.values.toList.sorted
}
看起来太糟糕了,但我会继续努力改进这种方法。
答案 0 :(得分:2)
你得到的结果很好,它只是按照不同的顺序,但是任务似乎没有说明需要特定订单。因此,您需要在之后对其进行排序(在修改解决方案之前或之后使用groupBy
)。您可以调用sorted
方法来执行此操作(或查找sortBy
/ sortWith
)。
答案 1 :(得分:1)
您可以通过此方法计算出现次数 -
REM Used to keep variable for duration of script
setlocal enabledelayedexpansion
cls
@echo off
echo Math Experts Commands
set /p loop=Loop how many times:
for /L %%A IN (1,1,!loop!) DO (
REM Request the first number
echo Enter your first digit you wish to add
REM Set first user input to "num1"
set /p num1=
REM Request the second number
echo Enter the second digit you wish to add the first by
REM Set second usr input to "num2"
set /p num2=
REM Add the 2 values then set the outcome to "total"
set /a total=num1+num2
REM Clear the screen
cls
if !total! leq 4 (
echo ####################################################################################
echo # Hosts Req: # Mask: # Netmask: # Max Supported: #
echo # !total! /30 255.255.255.252 4 #
echo ####################################################################################
echo.
) else if !total! gtr 4 and leq 8 ( REM <----WHERE THE PROBLEM IS.
echo ####################################################################################
echo # Hosts Req: # Mask: # Netmask: # Max Supported: #
echo # !total! /29 255.255.255.248 8 #
echo ####################################################################################
echo.
)
)
REM Pause the console
pause
echo.
)
此方法在第4周分配中使用:)