|
| 1 | +""" Repeat Character |
| 2 | +
|
| 3 | +Write a function that takes in a string, a single character, and a number. |
| 4 | +The function returns a string with each occurrence of the character repeated n times. |
| 5 | +
|
| 6 | +""" |
| 7 | + |
| 8 | +def repeat_character(text: str, char_to_repeat: str, repetitions: int) -> str: |
| 9 | + """The function returns a string with each occurrence of the character repeated n times. |
| 10 | +
|
| 11 | + Parameters: |
| 12 | + text (str): we will repeat the character in this string |
| 13 | + char_to_repeat (str): this is the character we want to repeat |
| 14 | + repetitions (int): how many times to repeat the character |
| 15 | +
|
| 16 | + Returns: |
| 17 | + string: the string with a single character repeated |
| 18 | +
|
| 19 | + Raises: |
| 20 | + AssertionError: if the first argument is not a string |
| 21 | + AssertionError: if the second argument is not a single character |
| 22 | + AssertionError: if the third argument is not an integer |
| 23 | + AssertionError: if the third argument is less than 0 |
| 24 | +
|
| 25 | + >>> repeat_character('Omnia', 'm', 7) |
| 26 | + 'Ommmmmmmnia' |
| 27 | +
|
| 28 | + >>> repeat_character('Jola-Moses', 's', 2) |
| 29 | + 'Jola-Mossess' |
| 30 | +
|
| 31 | + >>> repeat_character('Hasan', 'e', 999999999999) |
| 32 | + 'Hasan' |
| 33 | +
|
| 34 | + >>> repeat_character('Rafaa', 'a', 3) |
| 35 | + 'Raaafaaaaaa' |
| 36 | + """ |
| 37 | + |
| 38 | + assert isinstance(repetitions, int), 'third argument must be an integer' |
| 39 | + assert repetitions >= 0, 'third argument cannot be less than 0' |
| 40 | + |
| 41 | + repeated_text = '' |
| 42 | + |
| 43 | + for char in text: |
| 44 | + if char.lower() == char_to_repeat.lower(): |
| 45 | + # repeated_text += char_to_repeat * repetitions |
| 46 | + repeated_text += char * repetitions |
| 47 | + else: |
| 48 | + repeated_text += char |
| 49 | + |
| 50 | + return repeated_text |
0 commit comments