Double Space
April 4, 2017
Today’s exercise is simple, but some languages make it hard:
Write a program that returns an input string with each space character in the string doubled. For instance, the string “hello hello” (with one space between the two words) is transformed to “hello hello” (with two spaces between the two words).
That may not come out right in all browsers; here is the transformation again, with space characters replaced by plus signs for visibility, using a constant-space font:
"hello+hello" => "hello++hello"
Depending on your language, that might be an easy task, or a hard one. You may have to deal with memory allocation, or non-mutable strings, or appends that blow up to quadratic time.
Your task is to write a program that doubles the space characters in its input. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
As a perl programmer this one is trivial:
sub ds{shift=~s{ }{ }rg}if you don’t want to use s{} could do
sub ds{join'',map{$_,' 'eq$_?' ':''}split'',shift}Clojure:
(defn dup_space [input] (apply str (map (fn [c] (if(= \space c) " " (str c))) input)))Also not too difficult in Python.
def double_space(txt): return txt.replace(" ", " ")javascript:
function double_space(txt){
return txt.replace(/ /g,’ ‘);
}
https://pastebin.com/embed_js/MQchRuyU
Read-write Scheme.
(setlocale LC_ALL "") ;sigh (this is in guile 2.0.9) (define (double-space-port in out) (let ((o (read-char in))) (or (eof-object? o) (begin (write-char o out) (case o ((#\space) (write-char o out))) (double-space-port in out))))) (define (double-spaced-string s) (let ((out (open-output-string))) (double-space-port (open-input-string s) out) (get-output-string out))) (write (double-spaced-string "Taas mennään. Ja äätkin näkyy!")) (newline)Read-write Python.
import io def double_space_io(inio, outio): while True: o = inio.read(1) if not o: return outio.write(o) if o in ' ': outio.write(o) def double_space_str(s): outio = io.StringIO() double_space_io(io.StringIO(s), outio) return outio.getvalue() print(repr(double_space_str("Taas mennään. Ja äätkin näkyy!")))Took the Python generator approach. We can feed the double_space generator with characters and they get doubled up.
MUMPS:
r !,word,! f i=1:1:$l(word,” “) w:i>1 ” ” w $p(word,” “,i)
hello, world!
hello, world!
A Haskell version. (Note: cat -e prints a ‘$’ at the end of a line.)
Nothing difficult for Ruby
`def double_space(string)
return string.gsub(/\s/, ‘ ‘)
end
puts double_space(“hello word”)`
dup (' ':xs) = ' ':' ':dup xs dup ( x :xs) = x :dup xs dup [] = []for non streaming algorithms the former maybe slow and memory expensive then, we can use ByteStrings (lazy or strict, depends)
{-# LANGUAGE OverloadedStrings #-} import qualified Data.ByteString.Lazy.Char8 as BS dup' = BS.concatMap d where d ' ' = " " d x = BS.singleton x[…] how easy your favorite programming language makes it to work with strings. The problem is to double every blank in a string. That’s pretty simple but some languages make you deal with string allocation or perhaps, in […]
Powershell makes it easy:
Function Set-DoubleSpace ($a) {$a-replace” “,” “}
or from STDIN
(read-host)-replace” “,” “
in Java:
public String doubleSpaces(String in) {
return in.replace(” “, ” “);
}