Determine if a string is collapsible: Difference between revisions

m (→‎{{header|Modula-2}}: added NetLogo header)
(→‎{{header|NetLogo}}: complete item)
Line 2,186:
 
=={{header|NetLogo}}==
 
==== To run the task ====
 
* Enter the code into the Code Pane of NetLogo
* On the Interface Pane, in the Command Center, type "demo" and hit enter.
 
<lang NetLogo>
to-report split [ string ]
;; utility reporter to split a string into a list
report n-values length string [ [ n ] -> item n string ]
end
 
to-report collapse [ string ]
;; reporter that actually does the collapse function
ifelse ( string = "" )
[ report "" ]
[ report reduce [ [ a b ] -> (word a ifelse-value last a != b [ b ] [ "" ] ) ] split string ]
end
 
to-report format [ string ]
;; reporter to format the output as required
report ( word "<<<" string ">>> " length string )
end
 
to demo-collapse [ string ]
;; procedure to display the required output
output-print format string
output-print format collapse string
output-print ""
end
 
to demo
;; procedure to perform the test cases
foreach
[ ""
"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
"..1111111111111111111111111111111111111111111111111111111111111117777888"
"I never give 'em hell, I just tell the truth, and they think it's hell. "
" --- Harry S Truman "
]
demo-collapse
end
</lang>
 
{{out}}
 
<pre>
observer> demo
<<<>>> 0
<<<>>> 0
<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> 72
<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> 70
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> 72
<<<.178>>> 4
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> 72
<<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> 69
<<< --- Harry S Truman >>> 72
<<< - Hary S Truman >>> 17
</pre>
 
=={{header|Nim}}==