Answers for "word pattern python"

3

word pattern python

import string

def word_to_pattern(word):
    # Stores actual-letter to pattern-placeholder mapping
    mapping = {}

    # ZYXW... so we cap pop letters starting with A from the end
    available_pattern_letters = list(string.ascii_uppercase)[::-1]

    pattern = []
    for letter in word.upper():
        if letter not in string.ascii_uppercase:
            # for punctuation etc
            pattern.append(letter)
            continue
        if letter not in mapping:
            # new letter we haven't seen yet in this word
            mapping[letter] = available_pattern_letters.pop()
        pattern.append(mapping[letter])
    return pattern.join("")
Posted by: Guest on July-17-2021

Python Answers by Framework

Browse Popular Code Answers by Language