duplik.py

Created by schraf

Created on September 14, 2018

750 Bytes

Vous devez écrire une fonction duplik admettant en paramètre une chaine de caractères. En sortie on doit obtenir une chaine où chaque caractère est soit “(“ (si le caractère n’apparait qu’une seule fois dans la chaine initiale), soit “)” (si le caractère apparait plusieurs fois).

>> duplik("bacasable")
'))()())(('

Explications en vidéo


def duplik(txt):
  sortie=""
  for c in txt:
    if txt.find(c)==txt.rfind(c):
      sortie+="("
    else:
      sortie+=")"
  return sortie
  
# Version 2

def duplikv2(txt):
  sortie=""
  for c in txt:
    sortie+= "(" if txt.find(c)==txt.rfind(c) else ")"
  return sortie
  
# Version 3

def duplikv3(txt):
  return "".join(list(map(lambda c: "(" if txt.find(c)==txt.rfind(c) else ")", txt)))
  
# Version 4

def duplikv4(txt):
  sortie=""
  for c in txt:
    sortie+= ")" if txt.count(c)>1 else "("
  return sortie 
  
# Version 5

def duplikv5(txt, sortie=""):
  for c in txt:
    sortie+= "()"[txt.count(c)>1]
  return sortie 
  
# Version 6

def duplikv6(txt):
  return "".join(list(map(lambda c: "()"[txt.count(c)>1], txt)))

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>.