• [^] # Re: Problème 3

    Posté par . En réponse au message Challenge Codingame n°3. Évalué à 2.

    j'ai pas testé mais ça devrait ressembler à :

    import sys
    morse = raw_input()
    voc = []
    for i in xrange(int(raw_input())):
     voc.append(raw_input())
    dict = {"A": ".-",
    "B": "-...",
     "C": "-.-.",
     "D": "-..",
     "E": ".",
     "F": "..-.",
     "G": "--.",
     "H": "....",
     "I": "..",
     "J": ".---",
     "K": "-.-",
     "L": ".-..",
     "M": "--",
     "N": "-.",
     "O": "---",
     "P": ".--.",
     "Q": "--.-", 
     "R": ".-.", 
     "S": "...",
     "T": "-",
     "U": "..-",
     "V": "...-",
     "W": ".--",
     "X": "-..-",
     "Y": "-.--",
     "Z": "--.."}
    struct = [[], []];
    def insert_in_struct(v):
     curr_struct = struct
     for i in list(v):
     if i == ".":
     if curr_struct[0] == []:
     curr_struct[0] = [[], []]
     curr_struct = curr_struct[0]
     else:
     if curr_struct[1] == []:
     curr_struct[1] = [[], []]
     curr_struct = curr_struct[1]
     curr_struct.append(v)
    # insert_in_struct("..--.")
    # insert_in_struct("..--..-")
    # insert_in_struct("..--.-")
    # insert_in_struct("--.-")
    # insert_in_struct("--...")
    # a quoi resemble la structure de donnée
    # [[[[], [[], [[[[], [[], [], '..--..-']], [[], [], '..--.-'], '..--.'], []]]], []], [[], [[[[[], [], '--...'], []], [[], [], '--.-']], []]]]
    def possible_words(msg):
     curr_struct = struct
     res = []
     for i in list(msg):
     if len(curr_struct) > 2:
     res += curr_struct[2:]
     if curr_struct == []:
     return res
     if i == ".":
     curr_struct = curr_struct[0]
     else:
     curr_struct = curr_struct[1]
     return res
    # print possible_words("..--..--")
    # avec les valeurs précédentes ['..--.', '..--..-']
    def to_morse(l):
     return dict[l.upper()]
    for i in voc:
     insert_in_struct("".join(map(to_morse, list(i))))
    def decode(morse,nb):
     if morse == "":
     return [nb]
     a = possible_words(morse)
     if a == []:
     return [0]
     a = map(lambda x: morse[len(x):])
     r = []
     for i in a:
     r += decode(i, nb + 1)
     return r
    print max(decode(morse,0))