# 写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
def my_digit(old_str):
forsin old_str:
if not '0'<= s <='9':return False
return True
print(my_digit('65434567'))
print(my_digit('23454fds'))
小写变大写
# 写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母# a ==> 97 A ==>65
def my_upper(old_str):
new_str =''forsin old_str:
if'a'<=s<='z':
upper_s = chr(ord(s) - 32)
new_str += upper_s
else:
new_str += s
return new_str
print(my_upper('he345llo'))
指定元素是否存在
# 写一个函数实现自己的in操作,判断指定序列中,指定的元素是否存在
def my_in(it, ele):
foriin it:
if i == ele:
return True
else:
return False
print(my_in(['zhangsan', 'lisi', 'wangwu'], 'losi'))
字符串替换
# 写一个自己的replace函数,将指定字符串中指定的旧字符串转换成新指定的字符串
def my_replace(all_str, old_str, new_str):
result =''
i =0while i < len(all_str):
temp = all_str[i:i + len(old_str)]if temp != old_str:
result += all_str[i]
i +=1
else:
result += new_str
i += len(old_str)return result
print(my_replace('how you and you fine you ok', 'you', 'me'))
自己的max函数
# 写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
def get_max2(seq):
if type(seq)== dict:
seq= list(seq.values())
x = seq[0]foriin seq:
if i > x:
x = i
return x
print(get_max2([2, 4, 8, 1, 9, 0, 7, 5]))
print(get_max2({'x':10, 'y':29, 'z':32, 'a':23, 'b':19, 'c':98}))