快乐冲浪与生活

多体验、多体会、多体悟

0%

这是一篇关于 React Hooks 的技术文章翻译,原文地址: https://dev.to/michael_osas/understanding-react-hooks-how-to-use-useref-usememo-and-usecallback-for-more-efficient-code-3ceh ,翻译不当之处请指正。

译者评价

文章主要介绍了 useRef、useMemo 和 useCallback 3 个 React Hook,读者可以通过此文了解 3 种 Hook 的使用方式、场景,但文章也存在一些缺点:

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/26

"""
PAT 乙级 1046
"""

if __name__ == '__main__':
    n = int(input())
    a = 0
    b = 0

    for i in range(n):
        tokens = list(map(int, input().split(' ')))
        if tokens[1] == tokens[3]:
            continue
        total = tokens[0] + tokens[2]
        if tokens[1] == total:
            b += 1
            continue
        if tokens[3] == total:
            a += 1
            continue

    print(a, b)

history 命令可用于浏览历史执行过的命令,该命令在 Bourne Shell 中不可用,但 Bash 和 Korn 则支持该特性。在 Bash 和 Korn 中,每一次命令的执行都被视为一次事件,并且配有相应的事件号(Event Number)。在需要的情况下,可以通过事件号将执行过的命令再次执行。

不带选项的 history 会在终端输出所有执行过的历史命令。

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/25

"""
PAT 乙级 1044
"""

mars_digits = [
    'tret',
    'jan', 'feb', 'mar', 'apr',
    'may', 'jun', 'jly', 'aug',
    'sep', 'oct', 'nov', 'dec',
]

mars_carries = [
    'tam', 'hel', 'maa', 'huh',
    'tou', 'kes', 'hei', 'elo',
    'syy', 'lok', 'mer', 'jou',
]


def is_earth(s: str):
    return s.isdigit()


def is_mars(s: str):
    return not is_earth(s)


def to_earth(s: str):
    tokens = s.split(' ')
    if len(tokens) == 1:
        if tokens[0] in mars_digits:
            return mars_digits.index(tokens[0])
        if tokens[0] in mars_carries:
            return (mars_carries.index(tokens[0]) + 1) * 13
    else:
        return (mars_carries.index(tokens[0]) + 1) * 13 + mars_digits.index(tokens[1])


def to_mars(s: str):
    num = int(s)
    if num < 13:
        return mars_digits[num]

    if num % 13 == 0:
        return f'{mars_carries[num // 13 - 1]}'
    else:
        return f'{mars_carries[num // 13 - 1]} {mars_digits[num % 13]}'


if __name__ == '__main__':
    n = int(input())
    lines = []
    for _ in range(n):
        line = input()
        lines.append(line)

    for line in lines:
        if is_earth(line):
            print(to_mars(line))
        else:
            print(to_earth(line))

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/25

"""
PAT 乙级 1043
"""

if __name__ == '__main__':
    line = input()
    nums = [0] * 6
    chars = ['P', 'A', 'T', 'e', 's', 't']
    for c in line:
        if c == 'P':
            nums[0] += 1
        if c == 'A':
            nums[1] += 1
        if c == 'T':
            nums[2] += 1
        if c == 'e':
            nums[3] += 1
        if c == 's':
            nums[4] += 1
        if c == 't':
            nums[5] += 1

    total = sum(nums)

    while total > 0:
        for i in range(6):
            if nums[i] != 0:
                print(chars[i], end='')
                nums[i] -= 1
                total -= 1

    print('')

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/25

"""
PAT 乙级 1042
"""

if __name__ == '__main__':
    line = input()
    stat = [0] * 26

    for c in line:
        if ord('a') <= ord(c) <= ord('z'):
            stat[ord(c) - ord('a')] += 1
        if ord('A') <= ord(c) <= ord('Z'):
            stat[ord(c) - ord('A')] += 1

    max_num = max(stat)

    print(chr(ord('a') + stat.index(max_num)), max_num)