One day, I saw the challenge to multiply two strings and I thought I might be able to do one better.
That challenge was fake. It was elementwise maximum. It was not real multiplication. So I set out to make something real. Real division between two strings.
I quickly realized that this would make an amazing challenge, as the algorithm was surprisingly complex and interesting to implement.
I then realized that it was actually easily reduced into a mere few operations. I'm still doing the challenge, though.
Enough with the backstory. Let's go.
Method
To divide two strings, do the following, where x
is the first string and y
the second:
- If
x
does not containy
, return a space and a period concatenated tox
.- For example,
testx
andblah
would become.testx
, with a space at the beginning.
- For example,
- Otherwise, return every occurrence of
y
inx
, a period, theny
divided byx
with every occurrence ofy
removed, with all the periods removed.- For example,
eestestst
andest
would becomeestest.est
.
- For example,
Challenge
Write a program or function that, given two strings via standard input, returns the first string divided by the second.
You may assume that both input strings will only contain letters between ASCII codepoints 33 and 45, and between codepoints 47 and 126 (yes, space and period are intended to be excluded), and that the operation does not require more than 10 layers of recursion.
You are not required to divide the empty string by itself.
Test cases
test, es => es. es
test, blah => .test
okayye, y => yy. y
testes, es => eses. es
battat, at => atat. at
see, es => .see
see, e => ee. e
same, same => same.
aabb, ab => ab.ab
eestestst, est => estest.est
aheahahe, aheah => aheah.aheah ah
-={}[];:"'!@#$%^&*()~`\|/.<>_+?, ^&*()~` => ^&*()~`. ^&*()~`
best, => .
Scoring
As this is code-golf, the submission with the least amount of bytes wins.
a
toz
? 2. Why isn't the output of the first onees.tt
? 3. Why is there a space after the periods in your testcases? \$\endgroup\$a
toz
? What exactly are the characters that will appear? Can you phrase it in a positive instead of a negative? \$\endgroup\$same.
the output ofsame, same
? \$\endgroup\$.testx
string reads.textx
instead. \$\endgroup\$