Doubled Letters
July 9, 2019
We begin with a function that detects words with doubled letters by scanning through the letters in the word:
(define (double? str) (let loop ((cs (string->list str))) (cond ((or (null? cs) (null? (cdr cs))) #f) ((char=? (car cs) (cadr cs)) #t) (else (loop (cdr cs))))))
Then we apply the filter to the input list:
(define (remove-doubled words) (filter (complement double?) words))
Here’s the output:
> (map string->symbol (remove-doubled (map symbol->string '(Now is the time for all good men to come to the aid of their country)))) (Now is the time for men to come to the aid of their country)
You can run the program at https://ideone.com/QPL3Kr.
Perl to the rescue again – just a simple oneliner – that splits the words apart and joins them back together again after stripping double letters.
Here’s a case-sensitive solution in Python.
[sourceccode lang=”python”]
def remove(s):
return ‘ ‘.join(w for w in s.split() if all(c1 != c2 for c1, c2 in zip(w, w[1:])))
s = ‘Now is the time for all good men to come to the aid of their country’
print(remove(s))
[/sourcecode]
Output:
My spelling was wrong in the markup. Here it is again, hopefully formatted correctly this time.
Output:
Simple one-liner in perl5:
I need some C++ practice:
Rust version
Playground https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4ec2d8d1c446e288febe489ed208e0d7
A Haskell version.
Klong version 20190209
Klong version 20190209
[sourcecode lang="css"]
Code:
:” Doubled Letter”
:” Find double letter for a word”
dblw::{[l word]; l::[]; word::x; {x}'{:[1<#x; l::l,,x; 0]}’=word; :[0=#l; 0; 0<+/,/{{1=#x}’-:’x}’l]}
:”Print non-double words in string”
go::{[n s w]; s::””; {~@n::x?” “}{s:::[dblw(w::(n::n@0)#x); s; s,w,” “]; (n+1)_x}:~x,” “; (-1)_s}
Run:
Logic:
Klong is 0-based. The = operator identifies the unique matches and puts each identical match in a list
=”googgle”
[[0 3 4] [1 2] [5] [6]] :” g occurs in positions 0/3/4;
o occurs in positions 1/2;
l occurs in position 5; and
e occurs in position 6
[[0 3 4] [1 2]] :”this gathers matches for each letter where there is more than 1 match”
[[-3 -1] [-1]] :”this finds the difference in each pair”
[[0 1] [1]] :”this identifies those differences where the abs() = 1 – the matches are right next to each other”
[0 1 1] :”this flattens the list”
2
1
{~@n::x?” “}{s:::[dblw(w::(n::n@0)#x); s; s,w,” “]; (n+1)_x}:~x,” ”
x?” ” – creates list of matches to a space in string
n::x?” ” – sets n equal to that list
@n – Check that it is an atom (empty list)
~@n – Checks that it is NOT an atom
{~@n::x?” “} – while the string is not empty
n@0 – get first position of space in string
n=n@0 – set n equal to first position of space in string
w::(n::n@0)#x – get word up to first spaces in atom and set w to it
dblw(w…) – if w has spaces, return 0; otherwise return 1
:[dblw(…; s; s,w,” “] – if w has spaces, set s = s; otherwise append w and a space to the end of string
(n+1)_x – set string for the next iteration equal to the string without the leading word and space
{~@…}{s::…}:~x” ” – while the string is not empty, identify the next word, append it to new string if no double letters and get next string. First string = input appended to space
Klong version 20190209