CCF-CSP 201403-3 command line options (Python)

Problem Description

  Please write a command line analysis program to analyze which options are included in a given command line. Each command line is composed of several strings, and they are separated by exactly one space. The first of these strings is the name of the command line tool, which is composed of lowercase letters, and your program does not need to process it. Several options may be included after the tool name, and then some parameters that are not options may be included.
  There are two types of options: options with parameters and options without parameters. The form of a legal no-argument option is a minus sign followed by a single lowercase letter, such as "-a" or "-b". The option with parameters is composed of two strings separated by spaces. The format of the former is the same as the option without parameters, and the latter is the parameter of the option, which is a non-empty string composed of lowercase letters, numbers and minus signs. .
  The author of the command line tool provides you with a format string to specify which options his command line tool needs to accept. This string consists of several lowercase letters and colons, each of which represents an option accepted by the program. If the lowercase letter is immediately followed by a colon, it means an option with parameters, otherwise it is an option without parameters. For example, " ab:m:" means that the program accepts three options, namely "-a" (without parameters), "-b" (with parameters), and "-m" (with parameters).
  The author of the command line tool has prepared several command lines to test your program. For each command line, your tool should always analyze backwards. When your tool encounters a string that is neither a legal option nor a parameter of a legal option, the analysis stops. The remaining unanalyzed parts of the command line do not constitute options for the command, so your program should ignore them.

Input format

  The first line of input is a format string, which contains at least one character and does not exceed 52 in length. The format string only contains lowercase letters and colons. It is guaranteed that each lowercase letter appears at most once, there will be no two adjacent colons, and it will not start with a colon.
  The second line of input is a positive integer N (1 ≤ N ≤ 20), indicating the number of command lines you need to process.
  There are N lines next, each line is a command line to be processed, which includes no more than 256 characters. The command line must consist of several strings separated by a single space, each string containing only lowercase letters, numbers and minus signs.

Output format

  The output has N lines. The i-th line starts with " Case i:", and there should be exactly one space, and then the names of all the options used in the command line should be output in ascending alphabetical order. For options with parameters, they should be output after their name. Parameters. If an option appears multiple times in the command line, it will only be output once. If an option with parameters appears multiple times in the command line, only the parameter with the last occurrence will be output.

Sample input

albw:x
4
ls -a -l -a documents -b
ls
ls -w 10 -x -w 15
ls -a -b -c -d -e -l

Sample output

Case 1: -a -l
Case 2:
Case 3: -w 15 -x
Case 4: -a -b

solution

# 格式字符串
fmt_str = input()
fmt = []
# 格式字符串转存为list
for s in fmt_str:
    if s == ":":
        fmt[len(fmt) - 1] += s
        continue
    fmt.append(s)
# 命令行的行数
N = int(input())
# 存储命令行
cmds = []
for i in range(N):
    cmds.append(input())
for i, cmd in enumerate(cmds):
    cmd = cmd.split()
    # 每条命令行的第一个字符串为工具的名字,不需要处理
    cmd.pop(0)
    print_str = {
    
    }
    # 该字符串是否为带参数的选项
    flag = False
    for j, c in enumerate(cmd):
        # 若上一个字符串为带参数的选项,则现在这个字符串就是参数,就不用再判断是否为选项了
        if flag:
            flag = False
            print_str[cmd[j - 1][1]] = c
            continue
        # 判断是否为"-a""-b"的形式
        if c.startswith("-") and (len(c) == 2):
            # 若为不带参数的选项且也不在print_str里   and c[1] not in print_str
            if c[1] in fmt:
                print_str[c[1]] = ""
            # 若为带参数的选项,并且下一个字符串得有,即得有参数
            elif (c[1] + ":") in fmt and (j + 1) < len(cmd):
                flag = True
                # 若不在print_str里
                if c[1] not in print_str:
                    print_str[c[1]] = ""
            # 虽然是选项的形式,但不是合法的选项,或者是带参数的选项,但无参。该命令行分析停止
            else:
                break
        # 既不是参数,也不是选项,该命令行分析停止
        else:
            break
    i += 1
    i = str(i)
    print_str2 = ""
    # sorted(print_str.keys())为按键值升序
    for k in sorted(print_str.keys()):
        print_str2 += " -" + k
        if print_str[k] != "":
            print_str2 += " " + print_str[k]
    print("Case " + i + ":" + print_str2)

Guess you like

Origin blog.csdn.net/qq_43503670/article/details/115035176