Jump to content

Determine if a string is collapsible: Difference between revisions

no edit summary
No edit summary
Line 496:
=={{header|Scala}}==
 
<lang Scala> object CollapsibleString {
{{incorrect|Scale| <br><br> The five examples stated in the task's preamble weren't used. <br><br> Please use those examples as directed. <br><br>}}
 
/**Collapse a string (if possible)*/
<lang Scala>
def collapseString (s : String) : String = {
/**Collapse a string (if possible)*/
var res = s
def collapseString (s : String) : String = {
var resisOver = sfalse
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)){
for(i <- 0 to s.toList.length-2 by 1) if(s(i) == s(i+1)) res = res.take(i) ++ res.drop(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 !"
else if(s.length != sCollapsed.length) originalRes + "\n" + collapsedRes + "\n" + "This string is NOTIS collapsible !"
if(s.length != sCollapsed.length)else originalRes + "\n" + collapsedRes + "\n" + "This string ISis NOT 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(reportResults("ab cd ef------------"))
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-whelwheel drive, the further you'll be from help when ya get stuck!"))
println("------------")
println(reportResults("headmistressship"))
println("------------")
println(reportResults("aardvark"))
}
 
 
</lang>
 
{{out}}
<pre>
ORIGINAL : length = 790, string = «««The better the 4-whel drive, the further you'll be from help when ya get stuck!»»»
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 = «««ab cd efaardvark»»»
COLLAPSED : length = 87, string = «««ab cd efardvark»»»
This string is NOTIS collapsible !
</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.