day9-作業(yè)

  1. 編寫(xiě)函數(shù),求1+2+3+…N的和
def zh_sum(n: int):
    count = 0
    for i in range(n+1):
        count += i
    print("和:", count)
  1. 編寫(xiě)一個(gè)函數(shù),求多個(gè)數(shù)中的最大值
def zh_max(*nums: float):
    print("最大值:", max(nums))
  1. 編寫(xiě)一個(gè)函數(shù),實(shí)現(xiàn)搖骰子的功能,打印N個(gè)骰子的點(diǎn)數(shù)和
from random import randint
def zh_dice(n: int):
    count = 0
    for _ in range(n):
        num = randint(1, 6)
        count += num
    print("{}個(gè)骰子的點(diǎn)數(shù)和:{}".format(n, count))
  1. 編寫(xiě)一個(gè)函數(shù),交換指定字典的key和value。
    例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'}
def zh_swap(dict1: dict):
    new_dict = {}
    for item in dict1:
        value = dict1[item]
        new_dict[value] = item
    dict1 = new_dict
    print(dict1)
  1. 編寫(xiě)一個(gè)函數(shù),提取指定字符串中所有的字母,然后拼接在一起產(chǎn)生一個(gè)新的字符串
    例如: 傳入'12a&bc12d-+' --> 'abcd'
def zh_spl(str1: str):
    new_str = ''
    for item in str1:
        if 'A' <= item <= 'Z' or 'a' <= item <= 'z':
            new_str += item
    print(new_str)
  1. 寫(xiě)一個(gè)函數(shù),求多個(gè)數(shù)的平均值
def zh_avg(*nums: float):
    sum1 = sum(nums)
    print("平均值:", sum1/len(nums))
  1. 寫(xiě)一個(gè)函數(shù),默認(rèn)求10的階乘,也可以求其他數(shù)字的階乘
def zh_fac(n=10):
    sum1 = 1
    for i in range(1, n+1):
        sum1 *= i
    print(sum1)
  1. 寫(xiě)一個(gè)自己的capitalize函數(shù),能夠?qū)⒅付ㄗ址氖鬃帜缸兂纱髮?xiě)字母
    例如: 'abc' -> 'Abc' '12asd' --> '12asd'
def zh_capitalize(str1: str):
    if 'a' <= str1[0] <= 'z':
        str1 = chr(ord(str1[0]) - 32) + str1[1:]
    print(str1)
  1. 寫(xiě)一個(gè)自己的endswith函數(shù),判斷一個(gè)字符串是否以指定的字符串結(jié)束
    例如: 字符串1:'abc231ab' 字符串2:'ab' 函數(shù)結(jié)果為: True
def zh_endswith(str1: str, str2: str):
    length = len(str2)
    if len(str1) >= length:
        if str1[-length:] == str2:
            print('True')
        else:
            print('False')
    else:
        print('False')
  1. 寫(xiě)一個(gè)自己的isdigit函數(shù),判斷一個(gè)字符串是否是純數(shù)字字符串
    例如: '1234921' 結(jié)果: True
    '23函數(shù)' 結(jié)果: False
    'a2390' 結(jié)果: False
def zh_isdigit(str1: str):
    for item in str1:
        if not ('0' <= item <= '9'):
            print("False")
            break
    else:
        print("True")
  1. 寫(xiě)一個(gè)自己的upper函數(shù),將一個(gè)字符串中所有的小寫(xiě)字母變成大寫(xiě)字母
    例如: 'abH23好rp1' 結(jié)果: 'ABH23好RP1'
def zh_upper(str1: str):
    new_str1 = ''
    for index in range(len(str1)):
        if 'a' <= str1[index] <= 'z':
            new_str1 += chr(ord(str1[index]) - 32)
        else:
            new_str1 += str1[index]
    str1 = new_str1
    print(str1)
  1. 寫(xiě)一個(gè)自己的rjust函數(shù),創(chuàng)建一個(gè)字符串的長(zhǎng)度是指定長(zhǎng)度,
    原字符串在新字符串中右對(duì)齊,剩下的部分用指定的字符填充
    例如: 原字符:'abc' 寬度: 7 字符:'^' 結(jié)果: '^^^^abc'
    原字符:'你好嗎' 寬度: 5 字符:'0' 結(jié)果: '00你好嗎'
def zh_rjust(str1: str, length: int, str2: str):
    length1 = len(str1)
    new_str1 = ''
    for _ in range(length - length1):
        new_str1 += str2
    print(new_str1 + str1)
  1. 寫(xiě)一個(gè)自己的index函數(shù),統(tǒng)計(jì)指定列表中指定元素的所有下標(biāo),如果列表中沒(méi)有指定元素返回-1
    例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0] 元素: 1 結(jié)果: 0,4,6
    列表: ['趙云', '郭嘉', '諸葛亮', '曹操', '趙云', '孫權(quán)'] 元素: '趙云' 結(jié)果: 0,4
    列表: ['趙云', '郭嘉', '諸葛亮', '曹操', '趙云', '孫權(quán)'] 元素: '關(guān)羽' 結(jié)果: -1
def zh_index(list1: list, item):
    new_str = ''
    for index in range(len(list1)):
        if list1[index] == item:
            new_str += str(index)
    if len(new_str):
        print(','.join(new_str))
    else:
        print(-1)
  1. 寫(xiě)一個(gè)自己的len函數(shù),統(tǒng)計(jì)指定序列中元素的個(gè)數(shù)
    例如: 序列:[1, 3, 5, 6] 結(jié)果: 4
    序列:(1, 34, 'a', 45, 'bbb') 結(jié)果: 5
    序列:'hello w' 結(jié)果: 7
def zh_len(sequence):
    count = 0
    for _ in sequence:
        count += 1
    print(count)
  1. 寫(xiě)一個(gè)自己的max函數(shù),獲取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
    例如: 序列:[-7, -12, -1, -9] 結(jié)果: -1
    序列:'abcdpzasdz' 結(jié)果: 'z'
    序列:{'小明':90, '張三': 76, '路飛':30, '小花': 98} 結(jié)果: 98
def zh_max(sequence):
    pass
    if type(sequence) == dict:
        sequence = list(sequence.values())
    if type(sequence) == set:
        sequence = list(sequence)
    max1 = sequence[0]
    for item in sequence:
        if item > max1:
            max1 = item
    print(max1)
  1. 寫(xiě)一個(gè)函數(shù)實(shí)現(xiàn)自己in操作,判斷指定序列中,指定的元素是否存在
    例如: 序列: (12, 90, 'abc') 元素: '90' 結(jié)果: False
    序列: [12, 90, 'abc'] 元素: 90 結(jié)果: True
def zh_in(sequence, element):
    for item in sequence:
        if item == element:
            print("True")
            break
    else:
        print("False")
  1. 寫(xiě)一個(gè)自己的replace函數(shù),將指定字符串中指定的舊字符串轉(zhuǎn)換成指定的新字符串
    例如: 原字符串: 'how are you? and you?' 舊字符串: 'you' 新字符串:'me' 結(jié)果: 'how are me? and me?'
def zh_replace(str1: str, str2: str, str3: str):
    length = len(str2)
    new_str1 = ''
    index = 0
    while index < len(str1):
        if str1[index: index+length] == str2:
            new_str1 += str3
            index += length
        else:
            new_str1 += str1[index]
            index += 1
    print(new_str1)
  1. 寫(xiě)四個(gè)函數(shù),分別實(shí)現(xiàn)求兩個(gè)列表的交集、并集、差集、對(duì)稱差集的功能
    a.兩個(gè)列表的交
def zh_intersection(list1: list, list2: list):
    new_list = []
    for item in list1:
        if item in list2:
            if item not in new_list:
                new_list.append(item)
    print(new_list)

b.兩個(gè)列表的并集

def zh_union(list1: list, list2: list):
    new_list = []
    for item in list1:
        if item not in new_list:
            new_list.append(item)
    for item in list2:
        if item not in new_list:
            new_list.append(item)
    print(new_list)

c.兩個(gè)列表的差集

def zh_diff(list1: list, list2: list):
    new_list = []
    for item in list1:
        if item not in list2:
            new_list.append(item)
    print(new_list)

d.兩個(gè)列表的對(duì)稱差集

def zh_com(list1: list, list2: list):
    new_list = []
    list_sum = list1 + list2
    for item in list_sum:
        if not (item in list1 and item in list2):
            new_list.append(item)
    print(new_list)
zh_com(list1, list2)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 編寫(xiě)函數(shù),求1+2+3+…N的和 sum2(100) 編寫(xiě)一個(gè)函數(shù),求多個(gè)數(shù)中的最大值 編寫(xiě)一個(gè)函數(shù),實(shí)現(xiàn)搖骰子的...
    嘿嘿_9c52閱讀 265評(píng)論 0 0
  • 1. 編寫(xiě)函數(shù),求1+2+3+…N的和 2. 編寫(xiě)一個(gè)函數(shù),求多個(gè)數(shù)中的最大值 3. 編寫(xiě)一個(gè)函數(shù),實(shí)現(xiàn)搖骰子的功...
    itachhh閱讀 532評(píng)論 0 0
  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,707評(píng)論 0 5
  • 概要 64學(xué)時(shí) 3.5學(xué)分 章節(jié)安排 電子商務(wù)網(wǎng)站概況 HTML5+CSS3 JavaScript Node 電子...
    阿啊阿吖丁閱讀 9,800評(píng)論 0 3
  • 0.寫(xiě)一個(gè)匿名函數(shù),判斷指定的年是否是閏年程序: 結(jié)果: 1.寫(xiě)一個(gè)函數(shù)將一個(gè)指定的列表中的元素逆序( 如[1, ...
    2333_11f6閱讀 253評(píng)論 0 0

友情鏈接更多精彩內(nèi)容