Determine if a string is squeezable: Difference between revisions

(Moved NetLogo Header to correct location)
Line 2,601:
 
=={{header|NetLogo}}==
 
1. Paste this code into the Code Pane
2. In 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 squeeze [ character string ]
;; reporter that actually does the squeeze function
;; remote immeadiately repeating instances of character from string
ifelse ( string = "" )
[ report "" ] ;; empty input, report empty output
[ report
reduce
[ [ a b ] ->
( word a
ifelse-value b = character and b = last a
[ "" ]
[ b ]
)
]
split string
]
end
 
to-report bracket [ string ]
;; utility reporter to enclose a string in brackets
report ( word "<<<" string ">>>" )
end
 
to-report format [ string ]
;; utility reporter to format the output as required
report ( word bracket string " " length string )
end
 
to demo-squeeze [ character string ]
;; procedure to display the required output
output-print bracket character
output-print format string
output-print format squeeze character string
end
 
to demo
;; procedure to perform the test cases
demo-squeeze " " ""
demo-squeeze "-" "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
demo-squeeze "7" "..1111111111111111111111111111111111111111111111111111111111111117777888"
demo-squeeze "." "I never give 'em hell, I just tell the truth, and they think it's hell. "
demo-squeeze " " " --- Harry S Truman "
demo-squeeze "-" " --- Harry S Truman "
demo-squeeze "r" " --- Harry S Truman "
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
<<<7>>>
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> 72
<<<..1111111111111111111111111111111111111111111111111111111111111117888>>> 69
<<<.>>>
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> 72
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> 72
<<< >>>
<<< --- Harry S Truman >>> 72
<<< --- Harry S Truman >>> 20
<<<->>>
<<< --- Harry S Truman >>> 72
<<< - Harry S Truman >>> 70
<<<r>>>
<<< --- Harry S Truman >>> 72
<<< --- Hary S Truman >>> 71</pre>
 
=={{header|Nim}}==