【CodinGame】 CLASH OF CODE 趣味算法 - 20240719

计算字符串前缀

在这里插入图片描述
在这里插入图片描述

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

# 前缀
x = input()
# 字符串的个数
n = int(input())

answer_count = 0
# 拿到每个字符串
for i in range(n):
    y = input()
    # 记录前缀为x的字符串的个数
    answer_count += y.startswith(x)

print(answer_count)


今天看一个前缀字符串的函数startswith()
以上面这个题为例子,startswith(x) 用于检查字符串是否以指定的前缀 x 开头。如果字符串以 x 开头,则返回 True,否则返回 False

startswith()函数

1.用法

str.startswith(prefix[, start[, end]])

2.参数

  • prefix:要检查的前缀,可以是一个字符串或一个包含多个字符串的元组。
  • start(可选):字符串中开始检查的位置,默认为 0。
  • end(可选):字符串中结束检查的位置,默认为字符串的长度。

3.返回值

  • 如果字符串以指定的前缀开头,返回 True;否则返回 False

4.举个例子

基本用法
text = "Hello, world!"
print(text.startswith("Hello"))  # 输出: True
print(text.startswith("world"))  # 输出: False
使用起始和结束位置
text = "Hello, world!"
print(text.startswith("world", 7))  # 输出: True
print(text.startswith("world", 0, 5))  # 输出: False(注意是每个字符一位)
检查多个前缀(注意这个是任意一个成立就true)
text = "Hello, world!"
print(text.startswith(("Hello", "Hi")))  # 输出: True
print(text.startswith(("Hi", "Hey")))  # 输出: False(两个都不满足所以false)

5.注意事项

  • startswith() 方法对大小写敏感。
  • 如果 prefix 是一个空字符串,startswith() 总是返回 True。(所以提前得检查)

6.实际应用

startswith() 方法常用于文件名过滤、数据验证和字符串解析等场景。例如:

filenames = ["data1.csv", "data2.csv", "image1.png", "image2.png"]
csv_files = [f for f in filenames if f.startswith("data")]
print(csv_files)  # 输出: ['data1.csv', 'data2.csv']

可以有效地帮助过滤和处理大量字符串数据

或者手动算(作者:Stephen10121)


import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

x = input()
f=0
n = int(input())
for i in range(n):
    y = input()
    f+=1 if y[0:len(x)]==x else 0
print(f)

这里直接获取前面的 l e n ( x ) len(x) len(x)个字符,也是一种方法



获取最大公约数

在这里插入图片描述

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

a, b = [int(i) for i in input().split()]

# 输出两个数的最大公约数
print(math.gcd(a, b))


输出文件路径

在这里插入图片描述

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
folder_list = []
num = int(input())
for i in range(num):
    folder = input()
    # 每个folder都用/隔开
    folder_list.append(folder)

filename = input()
extension = input()

# 输出文件地址
print('/'.join(folder_list) + '/' + filename + '.' + extension)


游戏对抗系统的设定

在这里插入图片描述

在这里插入图片描述


import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

power = input()
side = input()


#     Your opponent's       |          Your
#  power | attack direction | response | response direction 
# -------+------------------+----------+--------------------
#  usual | left/right/above |  defence | left/right/above
#        |                  |          | (same as your
#        |                  |          |  opponent)
# -------+------------------+----------+--------------------
#  hard  | left             |  dodging | right
#        |------------------|          |--------------------
#        | right            |          | left
#        |------------------|          |--------------------
#        | above            |          | any


response = str()
response_direction = str()

# 创建一个字典,用于存储不同情况下的响应和响应方向
response_dict = {'usual':{'left': 'defence', 'right': 'defence', 'above': 'defence'}, 
                  'hard': {'left': 'dodging', 'right': 'dodging', 'above': 'dodging'}}
response_direction_dict = {'usual': {'left': 'left', 'right': 'right', 'above': 'above'},
                           'hard': {'left': 'right', 'right': 'left', 'above': 'any'}}

# 根据输入的power和side,从字典中获取相应的响应和响应方向
response = response_dict[power][side]
response_direction = response_direction_dict[power][side]


print(response)
print(response_direction)

END

相关推荐

  1. 趣味算法】13_素数

    2024-07-19 13:28:03       39 阅读
  2. 趣味算法】11_黑洞数

    2024-07-19 13:28:03       31 阅读
  3. 趣味算法】16_递归练习

    2024-07-19 13:28:03       28 阅读
  4. 20240715题目

    2024-07-19 13:28:03       19 阅读
  5. 趣味算法】00_百鸡百钱

    2024-07-19 13:28:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 13:28:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 13:28:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 13:28:03       57 阅读
  4. Python语言-面向对象

    2024-07-19 13:28:03       68 阅读

热门阅读

  1. [Makefile] 第四章:大型项目中的makefile

    2024-07-19 13:28:03       17 阅读
  2. 前端传值到后端,后端解析为科学计数法

    2024-07-19 13:28:03       16 阅读
  3. 2024 年 6 大 SwaggerHub 替代方案

    2024-07-19 13:28:03       16 阅读
  4. PHP 与 1688 详情 API 接口的完美对接

    2024-07-19 13:28:03       17 阅读
  5. 在状态流图中重用自定义C代码

    2024-07-19 13:28:03       18 阅读
  6. element导出.csv

    2024-07-19 13:28:03       21 阅读
  7. 2024年预测智能家居未来十年发展趋势(必看)

    2024-07-19 13:28:03       21 阅读