Challenge
Write a function that will take a key of X and place it in the middle of Y repeated N times.
Rules:
If X cannot be placed in the middle, return X.
N will always be > 0.
Example:
X = 'A'; Y = '*'; N = 10; Y repeated N times = '**********'; X in the middle of Y repeated N times = '*****A*****';
Good luck!
This challenge comes from Capocaccia on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (9)
My solution in Python:
Which can be abbreviated to:
Hoped you liked this solution, definitely not the most efficient or clean, but I have been working on my Python skills as I am mainly a web developer working with JS and ES6.
— Gabriel
Nice! just a hint, if you have a
return
inside the firstif
you don't need theelse
;)Here's my solution:
I interpret that this means if it can't be placed in the exact middle position, i.e.,
N
is odd. Otherwise, I don't know how to understand this statement.TypeScript
Assuming we only accept one character for the middle and the surrounding.
Playground
Repl.it.
I'm currently learning Clojure - I am sure there is a better solution, but this seems to work.
Swift solution :
I was looking for a even better solutions and look what i found;
Haskell solution!
In JavaScript