while1:try:
s =input()
words =0
digits =0
spaces =0
others =0for i in s:if i.isalpha():
words+=1elif i.isdigit():
digits+=1elif i.isspace():
spaces+=1else:
others+=1print(words)print(spaces)print(digits)print(others)except:break
HJ101-输入整型数组和排序标识,对其元素按照升序或降序进行排序
while1:try:
n =int(input())
s =list(map(int,input().strip().split()))
sort_num =input()
sort_s =[]if sort_num =='0':
s.sort()else:
s.sort(reverse=True)print(*s)except:break
HJ8-合并表
#数据表记录包含表索引index和数值value(int范围的正整数),# 请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照index值升序进行输出。
num =int(input())
dic ={}for i inrange(num):
line =input().split()
key =int(line[0])
value =int(line[1])
dic[key]= dic.get(key,0)+ value
for key insorted(dic):print(key,dic.get(key))
HJ14-字符串排序
'''
给定 n 个字符串,请对 n 个字符串按照字典序排列。
数据范围: 1 \le n \le 1000 \1≤n≤1000 ,字符串长度满足 1 \le len \le 100 \1≤len≤100
'''import sys
try:
lst =[]
n =int(input())for s inrange(n):
line_s =input()
lst.append(line_s)
lst =sorted(lst)for i in lst:print(i)except:pass
HJ27-查找兄弟单词
whileTrue:try:pass
s_input =input().split()
n =int(s_input[0])dict= s_input[1:n+1]print(dict)print(type(dict))
s = s_input[-2]
k =int(s_input[-1])
temp =[]for i indict:iflen(i)==len(s)and i != s andsorted(i)==sorted(s):
temp.append(i)print(temp)print(len(temp))if k<=len(temp):print(sorted(temp)[k-1])except:break
HJ68-成绩排序
# 给定一些同学的信息(名字,成绩)序列,请你将他们的信息按照成绩从高到低或从低到高的排列,相同成绩## 都按先录入排列在前的规则处理。while1:try:
n =int(input())
sort_type =input()
student =[]for i inrange(n):
info =input().split()
student.append([info[0],int(info[1])])if sort_type =="0":
student =sorted(student, key=lambda x: x[1], reverse=True)else:
student =sorted(student, key=lambda x: x[1])for i inrange(n):print(student[i][0], student[i][1])except:break
NC37-合并区间
'''
给出一组区间,请合并所有重叠的区间。
请保证合并后的区间按区间起点升序排列。
输入:
[[10,30],[20,60],[80,100],[150,180]]
复制
返回值:
[[10,60],[80,100],[150,180]]
'''classSolution:defmerge(self , intervals ):ifnot intervals:return[]
intervals.sort(key=lambda x:x.start)
res =[intervals[0]]for i in intervals[1:]:if res[-1].end<i.start:
res.append(i)elif res[-1].end<i.end:
res[-1].end=i.end
return res
leetcode1614-括号的最大嵌套深度
classSolution:defmaxDepth(self, s:str):
n =0
num =[]for i in s:if i =='(':
n+=1elif i ==')':
n-=1
num.append(n)returnmax(num)
NC52-有效括号排序
'''
给出一个仅包含字符'(',')','{','}','['和']',的字符串,判断给出的字符串是否是合法的括号序列
括号必须以正确的顺序关闭,"()"和"()[]{}"都是合法的括号序列,但"(]"和"([)]"不合法。
'''classSolution:defisValid(self , s:str)->bool:# write code hereiflen(s)%2==1:returnFalsewhile'{}'in s or'()'in s or'[]'in s:
s = s.replace('{}','').replace('()','').replace('[]','')if s =='':returnTrueelse:returnFalse
leetcode面试题 08.08. 有重复字符串的排列组合
import itertools
from itertools import permutations
classSolution:defpermutation(self, S):
tmp =[]for i inset(itertools.permutations(S)):
tmp.append(''.join(i))return tmp