DEV Community

loading...

Discussion on: Daily Challenge #124 - Middle Me

Collapse
rafaacioly profile image
Rafael Acioly

Nice! just a hint, if you have a return inside the first if you don't need the else ;)

Here's my solution:

def middle_me(middle: str, repeat: str, repeat_quantity: int) -> str:
  if repeat_quantity % 2 != 0:
    return middle

  half = repeat_quantity // 2
  content = repeat * repeat_quantity
  return content[:half] + middle + content[half:]
Collapse
citizen428 profile image
Michael Kohl

The string slicing seems to be extra effort, how about something like this?

padding = repeat * (repeat_quantity // 2)
return padding + middle + padding
Thread Thread
rafaacioly profile image
Rafael Acioly • Edited

I was looking for a even better solutions and look what i found;

fill_char = (repeat_quantity // 2) + 1
return middle.center(repeat, fill_char)