Determine if a string is collapsible: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 496:
=={{header|Scala}}==
<lang Scala> object CollapsibleString {
/**Collapse a string (if possible)*/▼
def collapseString (s : String) : String = {▼
▲/**Collapse a string (if possible)*/
var res = s
▲def collapseString (s : String) : String = {
var
var i = 0
for(i <- 0 to s.toList.length-2 by 1) if(s(i) == s(i+1)) res = res.take(i) ++ res.drop(i + 1)▼
if(res.size == 0) res
res▼
else while(!isOver){
}▼
if(res(i) == res(i+1)){
i-=1
}
i+=1
if(i==res.size-1) isOver = true
}
▲ res
}
/**Check if a string is collapsible*/
def isCollapsible (s : String) : Boolean = if(collapseString(s).length == s.length) false else true
/**Display results as asked in the task*/
def reportResults (s : String) : String = {
val sCollapsed = collapseString(s)
val originalRes = "ORIGINAL : length = " + s.length() + ", string = «««" + s + "»»»"
val collapsedRes = "COLLAPSED : length = " + sCollapsed.length() + ", string = «««" + sCollapsed + "»»»"
//In order to avoid useless computations, the function isCollapsible isn't called
if(s.length != sCollapsed.length) originalRes + "\n" + collapsedRes + "\n" + "This string IS collapsible !"▼
▲
}
println(reportResults("The better the 4-whel drive, the further you'll be from help when ya get stuck!"))▼
println("------------")▼
def main(args: Array[String]): Unit = {
println(reportResults("headmistressship"))▼
println(reportResults("
println
println(reportResults("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "))
▲ println("------------")
println(reportResults("..1111111111111111111111111111111111111111111111111111111111111117777888"))
println("------------")
println(reportResults("I never give 'em hell, I just tell the truth, and they think it's hell. "))
println("------------")
println(reportResults(" --- Harry S Truman "))
println("------------")
▲ println(reportResults("The better the 4-
println("------------")
▲ println(reportResults("headmistressship"))
println("------------")
println(reportResults("aardvark"))
}
▲}
</lang>
{{out}}
<pre>
ORIGINAL : length =
COLLAPSED : length = 0, string = «««»»»
This string is NOT collapsible !
------------
ORIGINAL : length = 72, string = «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
COLLAPSED : length = 70, string = «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
This string IS collapsible !
------------
ORIGINAL : length = 72, string = «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
COLLAPSED : length = 4, string = «««.178»»»
This string IS collapsible !
------------
ORIGINAL : length = 72, string = «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
COLLAPSED : length = 69, string = «««I never give 'em hel, I just tel the truth, and they think it's hel. »»»
This string IS collapsible !
------------
ORIGINAL : length = 72, string = ««« --- Harry S Truman »»»
COLLAPSED : length = 17, string = ««« - Hary S Truman »»»
This string IS collapsible !
------------
ORIGINAL : length = 80, string = «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
COLLAPSED : length = 77, string = «««The beter the 4-whel drive, the further you'l be from help when ya get stuck!»»»
This string IS collapsible !
Line 535 ⟶ 582:
This string IS collapsible !
------------
ORIGINAL : length = 8, string = «««
COLLAPSED : length =
This string
</pre>
|