String 关于在字符串scala中查找子字符串的代码的小问题
我目前正在编写一个小代码,该代码应该允许判断给定的子字符串是否在字符串中。我检查了所有其他类似的问题,但每个人都在使用预定义的函数。我需要从头开始建造…你能告诉我我做错了什么吗String 关于在字符串scala中查找子字符串的代码的小问题,string,scala,substring,String,Scala,Substring,我目前正在编写一个小代码,该代码应该允许判断给定的子字符串是否在字符串中。我检查了所有其他类似的问题,但每个人都在使用预定义的函数。我需要从头开始建造…你能告诉我我做错了什么吗 def substring(s: String, t: String): Boolean ={ var i = 0 // position on substring var j = 0 // position on string var result = false var isSim = true var n
def substring(s: String, t: String): Boolean ={
var i = 0 // position on substring
var j = 0 // position on string
var result = false
var isSim = true
var n = s.length // small string size
var m = t.length // BIG string size
// m must always be bigger than n + j
while (m>n+j && isSim == true){
// j grows with i
// stopping the loop at i<n
while (i<n && isSim == true){
// if characters are similar
if (s(i)==t(j)){
// add 1 to i. So j will increase by one as well
// this will run the loop looking for similarities. If not, exit the loop.
i += 1
j = i+1
// exciting the loop if no similarity is found
}
// answer given if no similarity is found
isSim = false
}
}
// printing the output
isSim
}
substring("moth", "ramathaaaaaaa")
def子字符串(s:String,t:String):布尔值={
变量i=0//子串上的位置
var j=0//字符串上的位置
var结果=错误
var isSim=真
var n=s.length//小字符串大小
var m=t.length//大字符串大小
//m必须始终大于n+j
而(m>n+j&&isSim==true){
//j和i一起成长
//在i停止循环问题由两个相同类型的子问题组成。您必须检查
- 存在一个起始索引
j
- 对于所有
i问题由两个相同类型的子问题组成。您必须检查
- 存在一个起始索引
j
- 总之,
i我提供了以下更为惯用的Scala代码,不是因为我认为它比Andrey的代码性能更好——我不这么认为——只是因为它使用递归,而且可能更易于阅读:
/**
* Method to determine if "sub" is a substring of "string".
*
* @param sub the candidate substring.
* @param string the full string.
* @return true if "sub" is a substring of "string".
*/
def substring(sub: String, string: String): Boolean = {
val p = sub.toList
/**
* Tail-recursive method to determine if "p" is a subsequence of "s"
*
* @param s the super-sequence to be tested (part of the original "string").
* @return as follows:
* (1) "p" longer than "s" => false;
* (2) "p" elements match the corresponding "s" elements (starting at the start of "s") => true
* (3) recursively invoke substring on "p" and the tail of "s".
*/
@tailrec def substring(s: Seq[Char]): Boolean = p.length <= s.length && (
s.startsWith(p) || (
s match {
case Nil => false
case _ :: z => substring(z)
}
)
)
p.isEmpty || substring(string.toList)
}
但是我们必须在从头开始创建所有东西和使用内置函数之间划清界限。我提供了以下更为惯用的Scala代码,不是因为我认为它比Andrey的代码性能更好——我不这么认为——只是因为它使用递归,而且可能更易于阅读:
/**
* Method to determine if "sub" is a substring of "string".
*
* @param sub the candidate substring.
* @param string the full string.
* @return true if "sub" is a substring of "string".
*/
def substring(sub: String, string: String): Boolean = {
val p = sub.toList
/**
* Tail-recursive method to determine if "p" is a subsequence of "s"
*
* @param s the super-sequence to be tested (part of the original "string").
* @return as follows:
* (1) "p" longer than "s" => false;
* (2) "p" elements match the corresponding "s" elements (starting at the start of "s") => true
* (3) recursively invoke substring on "p" and the tail of "s".
*/
@tailrec def substring(s: Seq[Char]): Boolean = p.length <= s.length && (
s.startsWith(p) || (
s match {
case Nil => false
case _ :: z => substring(z)
}
)
)
p.isEmpty || substring(string.toList)
}
但是我们必须在从头开始创建所有内容和使用内置函数之间划清界限。子字符串(“moth”,“ramathaaaaa”)
返回false
。有什么问题吗?是的,但是子字符串(“math”,“ramathaaaaa”)
也返回false…对于这类问题,我的建议是使用调试器。例如,Intellij提供了一个非常易于使用(且功能强大)的调试器。然后你应该按照你的算法一步一步地进行,直到你看到它哪里做错了。好的,我会的,谢谢你!不,我没有使用任何算法。顺便说一下,我清除了我所有的错误,谢谢你对Intellij的建议。子字符串(“moth”,“Ramathaaaaa”)
返回false
。有什么问题吗?是的,但是子字符串(“math”,“ramathaaaaaaa”)
也返回false…我对这类问题的建议是使用调试器。例如,Intellij提供了一个非常易于使用(且功能强大)的调试器。然后你应该按照你的算法一步一步地去做,直到你看到它哪里做错了。好的,我会的,谢谢你!不,我没有使用任何算法。顺便说一下,我清除了所有错误,谢谢你关于Intellij的建议。非常感谢你,这真的很有帮助!我以后会尽量避免while循环。@Data\u Gengar No,p租赁,不要“试图避免”。尝试了解语言功能何时有用,何时不太有用,以及原因,然后在适当的时候尝试应用它,而不要在不适当的地方应用它。在这种情况下,如果您真的关心性能,用while
-循环替换函数调用可能不会有什么坏处,但这将是一种错误索引和布尔标志很麻烦。非常感谢,这真的很有帮助!我以后会尝试避免while循环。@Data\u Gengar否,请不要“尝试避免”。尝试了解语言功能何时有用,何时不太有用,以及原因,然后在适当的时候尝试应用它,而不要在不适当的地方应用它。在这种情况下,如果您真的关心性能,用while
-循环替换函数调用可能不会有什么坏处,但这将是一种错误索引和布尔标志很麻烦。非常感谢你的帮助,我刚刚开始关于递归算法的一章,我很快就会更好地理解这段代码!非常感谢你的帮助,我刚刚开始关于递归算法的一章,我很快就会更好地理解这段代码!
moth ramathaaaaaaa: false
moth ramothaaaaaaa: true
moth mothraaaaaaaa: true
moth raaaaaaaamoth: true
moth mmoth: true
moth moth: true
def substring(s: String, t: String): Boolean = {
val n = s.size
val m = t.size
(0 to m-n).exists(j => (0 until n).forall(i => s(i) == t(j + i)))
}
/**
* Method to determine if "sub" is a substring of "string".
*
* @param sub the candidate substring.
* @param string the full string.
* @return true if "sub" is a substring of "string".
*/
def substring(sub: String, string: String): Boolean = {
val p = sub.toList
/**
* Tail-recursive method to determine if "p" is a subsequence of "s"
*
* @param s the super-sequence to be tested (part of the original "string").
* @return as follows:
* (1) "p" longer than "s" => false;
* (2) "p" elements match the corresponding "s" elements (starting at the start of "s") => true
* (3) recursively invoke substring on "p" and the tail of "s".
*/
@tailrec def substring(s: Seq[Char]): Boolean = p.length <= s.length && (
s.startsWith(p) || (
s match {
case Nil => false
case _ :: z => substring(z)
}
)
)
p.isEmpty || substring(string.toList)
}
(p zip s forall (t => t._1==t._2))