suppr.py

Created by schraf

Created on September 18, 2018

271 Bytes

On vous donne un mot en minuscules et vous devez supprimer k caractères en respectant les règles suivantes :

  • Supprimer les lettres “a” puis “b” puis “c”, etc.
  • Supprimer les lettres en partant de la gauche

Exemples :
>> suppr("abracadabra", 1)
'bracadabra' # on supprime le 'a' le plus à gauche
>> suppr('abracadabra', 2)
'brcadabra' # suppr des 2 'a' à gauche
>> suppr('abracadabra', 6)
'rcdbr' # 5 'a' puis 1 'b'
>> suppr('abracadabra', 8)
'rdr'
>> suppr('abracadabra',50)
''

Explications en vidéo


def suppr(s, k):
  r=sorted(s)[:k]
  for c in r:
    s=s.replace(c,"",1)
  return s
  
# Version 2

def supprv2(s, k):
  for c in "abcdefghijklmnopqrstuvwxyz":
    while s.count(c)>0 and k>0:
      s = s.replace(c,"",1)
      k -= 1
      if k==0: return s

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.