CodingTour
ARTS #85

Algorithm

本周选择的算法题是:Determine the Order

规则

The Robots have found an encrypted message. We cannot decrypt it at the moment, but we can take the first steps towards doing so. You have a set of “words”, all in lower case, and each word contains symbols in “alphabetical order”. (it’s not your typical alphabetical order, but a new and different order.) We need to determine the order of the symbols from each “word” and create a single “word” with all of these symbols, placing them in the new alphabetical order. In some cases, if we cannot determine the order for several symbols, you should use the traditional latin alphabetical order. For example: Given words “acb”, “bd”, “zwa”. As we can see “z” and “w” must be before “a” and “d” after “b”. So the result is “zwacbd”.

Input: Words as a list of strings.

Output: The order as a string.

Example:

checkio(["acb", "bd", "zwa"]) == "zwacbd"
checkio(["klm", "kadl", "lsm"]) == "kadlsm"
checkio(["a", "b", "c"]) == "abc"
checkio(["aazzss"]) == "azs"
checkio(["dfg", "frt", "tyg"]) == "dfrtyg"

How it is used: This concept can be useful for the cryptology, helping you to find regularities and patterns in natural text and ciphered messages.

Precondition: For each test, there can be the only one solution. 0 < |words| < 10

Solution

这是来自 CheckiO 的题,问题和解法都挺有意思:

from collections import OrderedDict

def checkio(data):
    data = ["".join(OrderedDict.fromkeys(d)) for d in data]
    letters = sorted(set("".join(data)))
    ans = ""
    while letters:
        for letter in letters:
            if not any(letter in d[1:] for d in data):
                ans += letter
                break
        data = [d.replace(letter, "") for d in data]
        letters.remove(letter)
    return ans

Review

How JSON Web Tokens Work

如果这是大家了解 JWT 的第一篇文章,它能减少非常多的误解和日常冲突。

这篇文章介绍了一些基础的概念:

  • 客户端与服务器之间如何通信
  • Session 是如何实现的
  • 为什么需要 Session

以及最重要的,「有了 Session,为什么还需要 JWT」:

  • 分布式架构下,Session 不同步的问题
  • 跨域服务器之间授权
  • 信息自包含
  • 数字签名验证

Tip

Python Dunder Methods

Dunder 全称是 Double UNDERscore,两个词分别取 D 和 UNDER 后融合而成,表示 Python 中的 __ 方法,如 __init____new__ 等。

这篇文章介绍了 Python 大多数的 dunder 方法,可以作为索引来用,开卷有益~

Share

尝试 Vercel