python - Google Kickstart 2020 C 轮 - 倒计时。无法理解为什么我的解决方案不正确
问题描述
我尝试了谷歌的 kickstart 2020 挑战。第 C 轮问题 1 让我有些难过。我尝试了许多不同的方式来完成挑战。问题看起来很简单,但我无法完成。问题是我不明白我做错了什么。请指出我正确的方向或用我的代码指出问题。
问题
Google Kickstart 2020 - C 轮 | 问题 1 https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff43/00000000003380d2
Avery 有一个由 N 个正整数组成的数组。数组的第 i 个整数是 Ai。 如果连续子数组的长度为 m 并且按顺序包含整数 m、m-1、m-2、...、2、1,则它是 m 倒计时。例如,[3, 2, 1] 是 3 倒计时。 你能帮 Avery 计算她的数组中的 K 倒计时数吗? 输入 输入的第一行给出了测试用例的数量,T.T 测试用例紧随其后。每个测试用例都以包含整数 N 和 K 的行开始。第二行包含 N 个整数。第 i 个整数是 Ai。 输出 对于每个测试用例,输出一行包含 Case #x: y,其中 x 是测试用例编号(从 1 开始),y 是她的数组中的 K 倒计时数。
伪代码
获取案例数量 范围内循环(案例数): 获取N(元素个数),K(初始倒计时值) 获取值数组 生成倒计时序列的数组 [K ... 1] - 签名 计数器 = 0 范围内的循环元素(元素数): 如果元素 == K: 如果有空间对数组进行切片(签名长度) - 可能的签名 如果可能的话签名==签名: 计数器 += 1 打印(计数器)
Python 3 代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
noc = int(input('')) # getting the number of cases # NOC- number of cases
# Loop over the # of cases
for c in range(noc):
(N, K) = [int(i) for i in input('').split(' ')] # getting N, K
# N - number of elements given
# K - initial countdown value
# getting the elements
caseList = [int(i) for i in input('').split(' ')]
# generating a 'signature' or list of factorial for the countdown
steps = [i for i in range(1, K + 1)][::-1]
# counter for number of matches
countdown = 0 # init value
# loop over each element i n list
for i in range(N):
# if element == K(init countdown number)
if caseList[i] == K:
# make sure there is space to get the sliced array
if i <= len(caseList) - len(steps):
# get the next m numbers if
if caseList[i:i + len(steps)] == steps:
countdown += 1 # increment
print countdown # print the number of matches
解决方案
您的解决方案似乎很好,除了输出不是指定的并且不是 Python 3 而是 2,只需将其更改为:
print(f'Case {c}: {countdown}')
除此之外,您所做的工作比需要的要多。您真的只需要浏览整个列表一次即可计算 K 倒计时。
例如:
import sys
from io import StringIO
sys.stdin = StringIO('3\n2 2\n2 1\n8 2\n0 2 1 2 2 1 2 1 0\n0 2\n\n')
t = int(input())
for c in range(t):
(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
# initialise goal, position in array and count
goal, i, count = k, 0, 0
while i < n:
# if item in current position matches current goal
if a[i] == goal:
# then next goal item is one less
goal -= 1
# until all in K-countdown were found
if goal == 0:
# then start over and increase count
goal = k
count += 1
# look at the next position
i += 1
# else (current item doesn't match goal), if already looking for start of K-countdown
elif goal == k:
# look at the next position
i += 1
# else (current item doesn't match goal, goal wasn't start of K-countdown)
else:
# look for start of K-countdown
goal = k
print(f'Case #{c}: {count}')
推荐阅读
- python - 如何将python中的图像保存在不同的目录中?
- java - JFrame 窗口不显示
- java - 为什么在解析 JSON 的方法中设置字符串时返回 null?(底部返回全部为空)
- hbase - 通过 NiFi 将 HBase 数据库转储到不同的集群
- python - Get the contents of a specific attribute in span tag
- lets-encrypt - 为什么 Let's Encrypt 颁发的证书在一个小时左右无效?
- c# - 如何在剃刀视图中使用左外连接处理来自linq的空值的ViewBag
- python-3.x - 将图像从 Subreddit 保存到文件夹 python
- r - 在更新到 read_xlsx 之前返回 .name_repair 结果?
- sqlite - 如何在 react-native 中将对象存储在 sqlite 表中?