快乐冲浪与生活

多体验、多体会、多体悟

0%

代码

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/13

"""
PAT 乙级 1036
"""

if __name__ == '__main__':
    tokens = input().split(' ')

    n = int(tokens[0])
    c = tokens[1]

    row_num = str(1.0 * n / 2)
    if '.' in row_num:
        num, fraction = row_num.split('.')
        if 5 <= int(fraction[0]) <= 9:
            num = int(num) + 1
        else:
            num = int(num)
    else:
        num = int(row_num)

    num = int(num)

    for i in range(num):
        if i == 0 or i == num - 1:
            print(''.join([c] * n))
            continue
        print(''.join([c] + [' '] * (n - 2) + [c]))

代码

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

"""
PAT 乙级 1033
"""

if __name__ == '__main__':
    bad_keys = input()
    shift_bad = '+' in bad_keys

    for v in input():
        if v in bad_keys or v.upper() in bad_keys:
            continue

        if shift_bad and ord('A') <= ord(v) <= ord('Z'):
            continue

        print(v, end='')

原文:Differences Between Single and Double Brackets in Bash

概述

当我们在 Bash 中做变量比较时,通常可以交换地使用单括号 [] 和双括号 [[]]。比如,我们可以使用表达式 [ 3 -eq 3 ][[ 3 -eq 3 ]] 来比较 3 是否等于 3。两个表达式都会执行成功,那两者的区别是什么呢?

在本文中,我们会讨论单括号和双括号之间的一些区别。

代码

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/12

"""
PAT 乙级 1031
"""
weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
m = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']


def weight_sum(digits):
    total = 0
    for i, digit in enumerate(digits):
        total += digit * weights[i]
    return total


if __name__ == '__main__':
    n = int(input())

    not_passed = []
    for _ in range(n):
        line = input()
        if len(line) != 18:
            not_passed.append(line)
            continue

        all_digit = True
        for c in line[:17]:
            if not c.isdigit():
                all_digit = False
        if not all_digit:
            not_passed.append(line)
            continue

        ws = weight_sum(list(map(int, line[:17])))
        z = ws % 11
        if m[z] != line[17]:
            not_passed.append(line)

    if len(not_passed) == 0:
        print('All passed')
    else:
        for item in not_passed:
            print(item)

代码

 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/12

"""
PAT 乙级 1029
"""

if __name__ == '__main__':
    statement1 = input()
    statement2 = input()

    got_dict = {}

    statement1 = statement1.upper()
    statement2 = statement2.upper()
    for c in statement1:
        if c not in got_dict:
            if c not in statement2:
                got_dict[c] = True
                print(c, end='')

jobs 命令主要用于显示系统中的任务列表及运行状态。在 Linux 中,每一个 job 都有一个唯一 ID,系统管理员通过任务 ID 对任务进行管理,可使其在前后或后台运行。通常任务和进程是等价的,只在于说侧重不同。即任务之于用户,相应地,进程之于系统。

代码

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

"""
PAT 乙级 1026
"""

if __name__ == '__main__':
    start_clock, end_clock = map(int, input().split(' '))
    duration = 1.0 * (end_clock - start_clock) / 100

    h = int(duration // 3600)
    m = int(duration % 3600 // 60)
    s = str(duration % 3600 % 60)

    tokens = s.split('.')

    add = 1 if int(tokens[1][0]) >= 5 else 0
    s = int(tokens[0]) + add

    print('%02d:%02d:%02d' % (h, m, s))

flag 包定义了一系列函数,可用于定义命令行参数,支持的参数类型如下:

  • string:flag.StringVar 函数
  • bool:flag.BoolVar 函数
  • time.Duration: flag.DurationVar 函数
  • int: flag.IntVar 函数
  • uint: flag.UintVar 函数
  • float64: flag.Float64Var 函数
  • int64: flag.Int64Var 函数
  • uint64: flag.Uint64Var 函数

在命令行工具的开发过程中,我们常常需要设置一个同时支持短名称和长名称的选项,如 -d 等价于 --debug-p 等价于 --password。在使用 Go flag 包的情况下,该需求的实现相当简单,只需要定义两个不同的 flag 指向同一个变量即可。

代码

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/8

"""
PAT 乙级 1024
"""
from typing import List


class SciNumber:
    def __init__(self, s: str):
        self.sign = '-' if s[0] == '-' else ''
        nums, exp = s[1:].split('E')
        self.nums = [v for v in nums]
        self.left_move = exp[0] == '-'
        self.exp = int(exp[1:])

        # print(self.sign, self.nums, self.left_move, self.exp)

    def to_number(self) -> List[str]:
        if self.left_move:
            ret = ['0'] * self.exp + self.nums
            del ret[ret.index('.')]
            return ret[0:1] + ['.'] + ret[1:]
        else:
            n = len(self.nums[2:])
            if self.exp >= n:
                ret = self.nums + ['0'] * (self.exp - n)
                del ret[1]
            else:
                ret = self.nums
                for i in range(1, 1 + self.exp):
                    ret[i], ret[i + 1] = ret[i + 1], ret[i]

            if ret[0] == '0':
                ret = ret[1:]
            return ret


if __name__ == '__main__':
    sci_number = SciNumber(input())
    print(sci_number.sign + ''.join(sci_number.to_number()))