1
첫 번째 코드는 웹에서 연습 한 첫 번째 초안 솔루션으로 모든 테스트를 통과하지만 많은 코드가 있습니다. 그래서 나는 함수를 추상화하고 두 번째 항목을 얻었습니다. 이것은 똑같이 작동해야한다고 생각하지만, (단지 몇 가지를 통과합니다), 내가 잘못하고있는 것은 무엇입니까?스칼라의 코드 추상화
1)
class Anagram(strg: String) {
def matches(words: Seq[String]): Seq[String] = {
def stylisticDifference(things: Seq[String], text: String): Seq[String] = {
things.filter(w => w.toLowerCase == text.toLowerCase)
}
def essensialSimlitude(things: Seq[String], text: String):Seq[String] = {
things.filter(w => w.toLowerCase.sorted == text.toLowerCase.sorted)
}
essensialSimlitude(words,strg) diff stylisticDifference(words,strg)
}
}
2)
class Anagram(strg: String) {
def matches(words: Seq[String]):Seq[String] = {
def answer(things: Seq[String], text: String, fn: String => String): Seq[String] = {
things.filter(w => fn(w.toLowerCase) == fn(text.toLowerCase))
}
val stringSort = (s: String) => s.sorted
answer(words,strg, identity) diff answer(words, strg, stringSort)
}
}
두 번째 실패는 무엇입니까? 무엇을해야하며 대신 무엇을해야합니까? – puhlen
은'w => fn (w.toLowerCase) == fn (text.toLowerCase)'에서'w => fn (w) .'LowerCase == fn (텍스트) .toLowerCase'로 변경합니다. 'sorting then lowercase' 결과는'lowercase then sorting'과 다를 것입니다. –