dimanche 24 décembre 2017

Scrambling a string in Python without using random.shuffle()

I'm trying to scramble a string, "string", without using random.shuffle(), but my code keeps producing output that has missing and repeating characters, e.g. gtrgtg, gnrtnn, etc. I'm not sure what I'm doing wrong.

    import random
    s = "string"
    new_s=[]
    for c in s:
      if random.choice(s) not in new_s:
        new_s.append(random.choice(s))

    print(''.join(new_s))




Aucun commentaire:

Enregistrer un commentaire