day10作业2

11.写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母

例如: 'abH23好rp1'

结果: 'ABH23好RP1'

def cq_upper(str1):
    list1 = list(str1)
    str2 = ''
    for index in range(len(list1)):
        if 'a' <= list1[index] <= 'z':
            list1[index] = chr(ord(list1[index])-32)
        str2 += list1[index]
    return print(str2)

cq_upper('abH23好rp1')

12.写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充

例如: 原字符:'abc'

宽度: 7

字符: '^'

结果: '^^^^abc'

原字符: '你好吗'

宽度: 5

字符: '0'

结果: '00你好吗'

def cq_rjust(str1,width,char1):
    length1 = len(str1)
    length2 = width - length1
    str2 = char1 * length2 + str1
    print(str2)

cq_rjust('abc',7,'^')

13.写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回 - 1

例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0]

元素: 1

结果: 0, 4, 6

列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']

元素: '赵云'

结果: 0, 4

列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']

元素: '关羽'

结果: -1

def cq_index(list1,str1):
    num1 =0
    for index in range(len(list1)):
        if list1[index] == str1:
            print(index,end=' ')
            num1 += 1
    if num1 == 0:
        return print(-1)

cq_index([1, 2, 45, 'abc', 1, '你好', 1, 0],1)

14.写一个自己的len函数,统计指定序列中元素的个数

例如: 序列:[1, 3, 5, 6]

结果: 4

序列: (1, 34, 'a', 45, 'bbb')

结果: 5

序列: 'hello w'

结果: 7

def cq_len(list1):
    sum1 = 0
    for char in list1:
        sum1 += 1
    return print(sum1)

cq_len('hello w')

15.写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值

例如: 序列:[-7, -12, -1, -9]

结果: -1

序列: 'abcdpzasdz'

结果: 'z'

序列: {'小明': 90, '张三': 76, '路飞': 30, '小花': 98}

结果: 98

def cq_max(sequence):
    if type(sequence) != dict:
        max1 = sequence[0]
        for char in sequence:
            if char > max1:
                max1 =char
    else:
        max1 = 0
        for x in sequence.values():
            if x > max1:
                max1 = x
    return print(max1)
  
cq_max({'小明': 90, '张三': 76, '路飞': 30, '小花': 98})

16.写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在

例如: 序列: (12, 90, 'abc')

元素: '90'

结果: False

序列: [12, 90, 'abc']

元素: 90

结果: True

def cq_in(sequence,elt):
    if elt in sequence:
        print(True)
    else:
        print(False)

cq_in((12, 90, 'abc'), '90')
cq_in([12, 90, 'abc'], 90)

17.写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串

例如: 原字符串: 'how are you? and you?'

旧字符串: 'you'

新字符串: 'me'

结果: 'how are me? and me?'

def cq_replace(str1,str2,str3):
    num1 = 0
    num2 = len(str2)
    new_str = ''
    while num1 <= len(str1) - 1:
        if str1[num1:num2] == str2:
            new_str += str3
            num1 += 3
            num2 += 3
        else:
            new_str += str1[num1]
            num1 += 1
            num2 += 1
    return print(new_str)

cq_replace('how are you? and you?', 'you', 'me')

18.写四个函数,分别实现求两个列表的交集、并集、差集、补集的功能

#交集
def cq_intersection(list1,list2):
    set1 = set(list1)
    set2 = set(list2)
    set3 = set1 | set2
    list3 = list(set3)
    return print(list3)

cq_intersection([1,2,3,4,5],[3,4,5,6,7,8])
#并集
def cq_union_set(list1, list2):
    set1 = set(list1)
    set2 = set(list2)
    set3 = set1 & set2
    list3 = list(set3)
    return print(list3)

cq_union_set([1,2,3,4,5],[3,4,5,6,7,8])
#差集
def cq_difference_set(list1, list2):
    set1 = set(list1)
    set2 = set(list2)
    set3 = set1 - set2
    list3 = list(set3)
    return print(list3)

cq_difference_set([1,2,3,4,5],[3,4,5,6,7,8])
#补集
def cq_complementary_set (list1, list2):
    set1 = set(list1)
    set2 = set(list2)
    set3 = set1 ^ set2
    list3 = list(set3)
    return print(list3)

cq_complementary_set([1,2,3,4,5],[3,4,5,6,7,8])
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容