程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python: Find the first character in a string that occurs at least k consecutive times

編輯:Python

Description: Given a string, find the first character in the string that occurs at least k consecutive times.

Enter:

  • The first line contains a positive integer k, indicating at least the number of consecutive occurrences required.1<=k<=1000.
  • The second line contains the string to look for.The string length is between 1 and 1000 and does not contain any whitespace.

Output: If there is a character that appears at least k times in a row, output the character; otherwise, output No.

k=int(input()) #Number 4s = list(input()) #The string is converted to a list adfdfdfddfdfdfdffdddd​f = s[0] # first letter valuecounts = 0 #count the number of consecutive occurrencesfor i in range(0,len(s)): # Traverse the stringif f == s[i]:counts += 1else:f=s[i+1]counts = 0if counts>=k:print("Number:",counts,s[i])breakif counts


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved