The challenge
Remove a exclamation mark from the end of string. You can assume that the input data is always a string, no need to verify it.
Examples
1
2
3
4
5
6
|
remove("Hi!") === "Hi"
remove("Hi!!!") === "Hi!!"
remove("!Hi") === "!Hi"
remove("!Hi!") === "!Hi"
remove("Hi! Hi!") === "Hi! Hi"
remove("Hi") === "Hi"
|
Test cases
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
test.describe("Basic Tests")
tests = [
#[input, [expected]],
["Hi!", "Hi"],
["Hi!!!","Hi!!"],
["!Hi", "!Hi"],
["!Hi!", "!Hi"],
["Hi! Hi!", "Hi! Hi"],
["Hi", "Hi"],
]
for inp, exp in tests:
test.assert_equals(remove(inp), exp)
|
The solution in Python
Option 1:
1
2
3
4
5
|
def remove(s):
if len(s):
return s[:len(s)-1] if s[::-1][0]=="!" else s
else:
return ""
|
Option 2 (using endswith
):
1
2
|
def remove(s):
return s[:-1] if s.endswith('!') else s
|
Option 3 (simple
):
1
2
|
def remove(s):
return s[:-1] if s and s[-1] == '!' else s
|
Option 4 (using regex
):
1
2
3
|
def remove(s):
import re
return re.sub(r'!$', '', s)
|