-
Notifications
You must be signed in to change notification settings - Fork 0
/
101_Simple letter removal.py
46 lines (33 loc) · 1.07 KB
/
101_Simple letter removal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Codewars Coding Challenge
Day 101/366
Level 7kyu
Simple letter removal
In this Kata, you will be given a lower case string and your task will be to remove k characters from that string using the following rule:
- first remove all letter 'a', followed by letter 'b', then 'c', etc...
- remove the leftmost character first.
For example:
solve('abracadabra', 1) = 'bracadabra' # remove the leftmost 'a'.
solve('abracadabra', 2) = 'brcadabra' # remove 2 'a' from the left.
solve('abracadabra', 6) = 'rcdbr' # remove 5 'a', remove 1 'b'
solve('abracadabra', 8) = 'rdr'
solve('abracadabra',50) = ''
More examples in the test cases. Good luck!
Please also try:
Simple time difference
Simple remove duplicates
def solve(st,k):
pass
https://www.codewars.com/kata/5b728f801db5cec7320000c7/train/python
"""
# Solution
def solve(st, k):
for char in sorted(set(st)):
count = st.count(char)
if k >= count:
st = st.replace(char, '', count)
k -= count
else:
st = st.replace(char, '', k)
break
return st