Hexo博客地址:牛客网华为机试:Using Python
第一题
题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述
一行字符串,非空,长度小于5000。
输出描述
整数N,最后一个单词的长度。
示例1
输入:hello world 输出:5
def function():
a = input()
if ' ' not in a:
print(len(a))
else:
sp = a.split(' ')
print(len(sp[-1]))
function()
第二题
题目描述
写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
输入描述
输入一个有字母和数字以及空格组成的字符串,和一个字符。
输出描述
输出输入字符串中含有该字符的个数。
示例1
输入:ABCDEF A 输出:1
def function():
a = input()
b = input()
count = 0
if (ord(b) >= 97) and (ord(b) <= 122):
for el in a:
if (ord(el) == ord(b)) or (ord(el) == (ord(b) - 32)):
count += 1
elif (ord(b) >= 65) and