Пример #1
0
def results(tiles, dd, t, rows):

    rack = Rack(tiles, dd, t)
    cutoffs = {0: 15, 1: 10, 2: 7, 3: 0}

    time_0 = time.time()

    if rack.num_letters >= cutoffs[min(rack.num_blanks, 3)]:
        res = rack.frequency_solve()
        solver = 'F'
    else:
        res = rack.permute_solve()
        solver = 'P'

    time_1 = time.time()
    time_ms = 1000 * (time_1 - time_0)

    if len(res) < 1:
        print("\nNo valid words found. ({}-Solve runtime: {:.2f} ms)".format(
            solver, time_ms))
        return

    entries = []  # cells of the table
    lines = [""] * rows  # rows of the table
    pad_wc, pad_bc = 4, 8  # padding within column, padding between columns
    shift = pad_wc + len(res[0])

    for play in res:
        spaces = shift - len(play) if len(play) < 10 else shift - 1 - len(play)
        entries.append(play + (" " * spaces) + str(len(play)))
    print("\nLongest words: ({}-Solve runtime: {:.2f} ms)".format(
        solver, time_ms))

    for i, entry in enumerate(entries):
        spaces = " " * pad_bc if i >= rows else ""
        lines[i % rows] += spaces + entry

    for line in lines:
        if line:
            print(line)
Пример #2
0
def solve_tiles(tiles, dd, pref, suff, substring, includes, t=100):
    time_0 = time.time()

    def get_runtime():
        return round(1000 * (time.time() - time_0), 2)

    for c in tiles:
        if not (65 <= ord(c) <= 90) and not (97 <= ord(c) <= 122) and c != '?':
            return ['Invalid Input', get_runtime(), []]
    for x in [pref, suff, substring]:
        for c in x:
            if not (65 <= ord(c) <= 90) and not (97 <= ord(c) <=
                                                 122) and c != '-':
                return ['Invalid Input', get_runtime(), []]
    for c in includes:
        if not (65 <= ord(c) <= 90) and not (97 <= ord(c) <= 122):
            return ['Invalid Input', get_runtime(), []]

    for c in pref + suff + substring:
        if c != '-':
            tiles += c

    rack = Rack(tiles, dd, t, pref, suff, substring, includes)
    cutoffs = {0: 15, 1: 10, 2: 7, 3: 0}

    if rack.num_letters >= cutoffs[min(rack.num_blanks, 3)]:
        res = rack.frequency_solve()
        solver = 'F'
    else:
        res = rack.permute_solve()
        solver = 'P'

    if len(res) < 1:
        return ['No Valid Words', get_runtime(), []]

    def blanks(s, r):
        nb = r.num_blanks
        pool = Counter(r.tiles + r.pref + r.suff + r.substring)
        b = ''
        res = []
        for c in s:
            if c not in pool:
                b += c
            else:
                if pool[c] > 0:
                    pool[c] -= 1
                else:
                    b += c
        j = 0
        for i in range(len(s)):
            if j + 1 > len(b):
                return res
            if s[i] == b[j]:
                j += 1
                res.append(i)
        return res

    return [
        '{}-Solve'.format(solver),
        get_runtime(),
        [[word, len(word), set(blanks(word, rack))] for word in res[:t]]
    ]