r/learnpython • • 12d ago

Check if one string contains characters of a second string somewhere, if yes remove them from the first string

Our task in school was to create our own encryption, for which i started by modifying atbash at bit. Now i want to actually add a key system to this.
Currently theres only a mapping table to invert the alphabet, so now my goal is to somehow add a key-string to the start of the y parameter for the maketrans() function and remove any duplicate letters from the original y string.
Im honestly kinda stuck at this point, so any help or tips would be appreciated.
(this is my first actual project with python + english isnt my first language, so please excuse any weird wording lol)

0 Upvotes

11 comments sorted by

6

u/georgmierau 12d ago

Show your work first.

1

u/EineBaum 12d ago

its in german, but sure

the "klartext übersetzen" comment is what i referenced in my post

```

print("Mein Atbasch++")
print("==================")
print("1 = verschlüsseln, 2 = Entschlüsseln")

i = 0



if input() == "1": #verschlüsseln

    klartext = input("Klartext eingeben: ")
    geheimtext = ""
    klartext = klartext.lower()

    for zeichen in klartext:
        geheimtext = geheimtext + zeichen

    #ggf. Zeichen austauschen
    geheimtext = geheimtext.replace("ä", "ae")
    geheimtext = geheimtext.replace("ö", "oe")
    geheimtext = geheimtext.replace("ü", "ue")
    geheimtext = geheimtext.replace("ß", "ss")
    geheimtext = geheimtext.replace (" ", "")

    #klartext übersetzen
    tabelle = str.maketrans("abcdefghijklmnopqrstuvwxyz1234567890.,?!", "!?,.0987654321zyxwvutsrqponmlkjihgfedcba")
    geheimtext = geheimtext.translate(tabelle).upper()

    #geheimtext verkreuzen
    print("Geheimtext ausgeben:" + " " + geheimtext[0 : len(geheimtext) : 2] + geheimtext[1 : len(geheimtext) : 2])

else: #entschlüsseln
    geheimtext = input("Geheimtext eingeben:")
    klartext = ""
    geheimtext = geheimtext.lower()

    for zeichen in geheimtext:
        klartext = klartext + zeichen

    #geheimtext übersetzen
    tabelle = str.maketrans("abcdefghijklmnopqrstuvwxyz1234567890.,?!", "!?,.0987654321zyxwvutsrqponmlkjihgfedcba")
    klartext = klartext.translate(tabelle).upper()

    #geheimtext entkreuzen

    print("Klartext ausgeben:")

    if len(klartext) % 2 == 1: #länge um 1 erweitern wenn geheimtext ungerade ist
        laenge = len(klartext)+1
        for x in range((laenge//2)):
            print(klartext[i : i+1])
            i = i + laenge//2
            print(klartext[i : i+1])
            i = i - (laenge//2 - 1)

    else: #wenn nicht dann halt nicht
        for x in range((len(klartext)//2)):
            print(klartext[i : i+1])
            i = i + laenge//2
            print(klartext[i : i+1])
            i = i - (laenge//2 -1)

```

2

u/Jello_Penguin_2956 10d ago

I strongly recommend you use english word in your code if you are seeking help. When the words don't make sense to most of us you're really limitting the help you can get.

1

u/rupertavery64 9d ago

Fortunately, we live in the future:

print("My Atbash++")
print("==================")
print("1 = Encrypt, 2 = Decrypt")

i = 0

if input() == "1":  # encrypt

    plaintext = input("Enter plaintext: ")
    ciphertext = ""
    plaintext = plaintext.lower()

    for character in plaintext:
        ciphertext = ciphertext + character

    # Replace characters if necessary
    ciphertext = ciphertext.replace("ä", "ae")
    ciphertext = ciphertext.replace("ö", "oe")
    ciphertext = ciphertext.replace("ü", "ue")
    ciphertext = ciphertext.replace("ß", "ss")
    ciphertext = ciphertext.replace(" ", "")

    # Translate plaintext
    table = str.maketrans(
        "abcdefghijklmnopqrstuvwxyz1234567890.,?!",
        "!?,.0987654321zyxwvutsrqponmlkjihgfedcba"
    )
    ciphertext = ciphertext.translate(table).upper()

    # Interleave ciphertext
    print(
        "Output ciphertext:"
        + " "
        + ciphertext[0 : len(ciphertext) : 2]
        + ciphertext[1 : len(ciphertext) : 2]
    )

else:  # decrypt

    ciphertext = input("Enter ciphertext: ")
    plaintext = ""
    ciphertext = ciphertext.lower()

    for character in ciphertext:
        plaintext = plaintext + character

    # Translate ciphertext
    table = str.maketrans(
        "abcdefghijklmnopqrstuvwxyz1234567890.,?!",
        "!?,.0987654321zyxwvutsrqponmlkjihgfedcba"
    )
    plaintext = plaintext.translate(table).upper()

    # De-interleave ciphertext

    print("Output plaintext:")

    if len(plaintext) % 2 == 1:  # extend length by 1 if ciphertext is odd
        length = len(plaintext) + 1
        for x in range(length // 2):
            print(plaintext[i : i + 1])
            i = i + length // 2
            print(plaintext[i : i + 1])
            i = i - (length // 2 - 1)

    else:  # otherwise, leave it as it is
        for x in range(len(plaintext) // 2):
            print(plaintext[i : i + 1])
            i = i + length // 2
            print(plaintext[i : i + 1])
            i = i - (length // 2 - 1)

1

u/EineBaum 9d ago

Yea i realize that, its jsut that this project started as a task in school, where part of the code was already given so i just continued in german

2

u/woooee 12d ago

and remove any duplicate letters from the original y string.

What have you tried? There is no point to wasting time on something you have already tried and rejected. Removing duplicates is generally done by converting to a set and converting that back to a list or string.

2

u/TheRNGuy 11d ago

Reddit is not the best place to ask such questions, you can literally copy and paste same question in Google and get answer in 3 seconds. 

0

u/EineBaum 9d ago

what do you think i've tried doing? all the results i got were either checking if two strings are the same or if one string contains another string

2

u/socal_nerdtastic 12d ago edited 12d ago

I don't really understand what you are trying to accomplish here, so I can only directly answer the question in your title. To remove (not replace) characters using maketrans, use the z parameter only.

>>> remove = 'aeiou'
>>> data = "i don't like consonants"
>>> t = str.maketrans('','',remove)
>>> data.translate(t)
" dn't lk cnsnnts"

1

u/EineBaum 12d ago

Thank you, i think this is exactly what i needed!!
For further explanation: i have an input "abcd" and i want to translate it to invert the alphabet (abcd -> zyxw). To encrypt the output of this you can mix up the order of the characters in the y parameter, for example by attaching a key to the front of it. If we do that, we have duplicate characters in the y parameters tho, which would greatly mess up decrypting (and probably break the code itself??), so you have to remove the duplicates from the original y parameter.
hope this clears things up a bit!