Determine if a string is collapsible: Difference between revisions
Content deleted Content added
m →{{header|Python}}: Added predicate test output. |
|||
Line 576: | Line 576: | ||
<lang fsharp> |
<lang fsharp> |
||
// Collapse a String. Nigel Galloway: June 9th., 2020 |
// Collapse a String. Nigel Galloway: June 9th., 2020 |
||
//As per the task description a function which 'determines if a character string is collapsible' by testing if any consecutive characters are thesame. |
|||
let collapse n=if String.length n=0 then None else |
|||
let isCollapsible n=n|>Seq.pairwise|>Seq.tryFind(fun(n,g)->n=g) |
|||
⚫ | |||
//As per the task description a function which 'if the string is collapsable And if so, collapses the string (by removing immediately repeated characters). |
|||
⚫ | |||
⚫ | |||
if fG.Length=n.Length then None else Some fG |
|||
Some _->let i=Seq.head n |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
collapse "" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
isCollapsible "" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
Line 601: | Line 602: | ||
<<<withoutConsecutivelyRepeatedCharacters>>> (length 38) does not collapse |
<<<withoutConsecutivelyRepeatedCharacters>>> (length 38) does not collapse |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang factor>USING: formatting io kernel sbufs sequences strings ; |
<lang factor>USING: formatting io kernel sbufs sequences strings ; |