1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] T9 algorithm python

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    type heret9 = "22233344455566677778889999"

    # Map letters to digits according to T9 keyboard layout
    def letter_to_digit(x):
    assert 'a' <= x <= 'z'
    return t9[ord(x) - ord('a')]

    def code_word(word):
    return ''.join(map(letter_to_digit, word))

    def predictive_text(dic):
    # total_weight[p] = total weight of words having prefix p
    total_weight = {}

    for word, weight in dic:
    prefix = ""
    for x in word:
    prefix += x # build the prefix one letter at a time

    # Update total weight for this prefix
    if prefix in total_weight:
    total_weight[prefix] += weight
    else:
    total_weight[prefix] = weight

    # prop = prefix to display for s
    prop = {}

    for prefix in total_weight:
    code = code_word(prefix)
    # Choose the prefix with the highest weight for the given code
    if (code not in prop or total_weight[prop
    Código:
    ] < total_weight[prefix]):
                prop[code] = prefix
                
        return prop
    
    def propose(prop, seq):
        if seq in prop:
            return prop[seq]
        return None
    
    
    BOOK TITLE [Competitive Programming in Python: 128 Algorithms by Durr] -> FIRST 2 functions are easy to me , but def predictive_text(dic) and def propose(prop, seq) is black magic to me, I'm currently aiming to do data science so It's good idea to master data structures/algorithms and one of the questions in my book is this problem that I was staring at for like HOURS and it drives me crazy I'm also asking CHAT-GPT for clues/visualisation so i can grasp the concept but still nothing, so im asking you guys for help maybe humnan can do it better. What is the concept of weights and the logic behind it?
    
    [url="https://stackoverflow.com/questions/79058215/t9-algorithm-python"]Continue reading...[/url]

Compartilhe esta Página