day9-作业

  1. 编写函数,求1+2+3+…N的和
def zh_sum(n: int):
    count = 0
    for i in range(n+1):
        count += i
    print("和:", count)
  1. 编写一个函数,求多个数中的最大值
def zh_max(*nums: float):
    print("最大值:", max(nums))
  1. 编写一个函数,实现摇骰子的功能,打印N个骰子的点数和
from random import randint
def zh_dice(n: int):
    count = 0
    for _ in range(n):
        num = randint(1, 6)
        count += num
    print("{}个骰子的点数和:{}".format(n, count))
  1. 编写一个函数,交换指定字典的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. 编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
    例如: 传入'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. 写一个函数,求多个数的平均值
def zh_avg(*nums: float):
    sum1 = sum(nums)
    print("平均值:", sum1/len(nums))
  1. 写一个函数,默认求10的阶乘,也可以求其他数字的阶乘
def zh_fac(n=10):
    sum1 = 1
    for i in range(1, n+1):
        sum1 *= i
    print(sum1)
  1. 写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
    例如: '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. 写一个自己的endswith函数,判断一个字符串是否以指定的字符串结束
    例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: 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. 写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
    例如: '1234921' 结果: True
    '23函数' 结果: False
    'a2390' 结果: False
def zh_isdigit(str1: str):
    for item in str1:
        if not ('0' <= item <= '9'):
            print("False")
            break
    else:
        print("True")
  1. 写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
    例如: 'abH23好rp1' 结果: '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. 写一个自己的rjust函数,创建一个字符串的长度是指定长度,
    原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
    例如: 原字符:'abc' 宽度: 7 字符:'^' 结果: '^^^^abc'
    原字符:'你好吗' 宽度: 5 字符:'0' 结果: '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. 写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
    例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0] 元素: 1 结果: 0,4,6
    列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '赵云' 结果: 0,4
    列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '关羽' 结果: -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. 写一个自己的len函数,统计指定序列中元素的个数
    例如: 序列:[1, 3, 5, 6] 结果: 4
    序列:(1, 34, 'a', 45, 'bbb') 结果: 5
    序列:'hello w' 结果: 7
def zh_len(sequence):
    count = 0
    for _ in sequence:
        count += 1
    print(count)
  1. 写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
    例如: 序列:[-7, -12, -1, -9] 结果: -1
    序列:'abcdpzasdz' 结果: 'z'
    序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 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. 写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
    例如: 序列: (12, 90, 'abc') 元素: '90' 结果: False
    序列: [12, 90, 'abc'] 元素: 90 结果: True
def zh_in(sequence, element):
    for item in sequence:
        if item == element:
            print("True")
            break
    else:
        print("False")
  1. 写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
    例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: '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. 写四个函数,分别实现求两个列表的交集、并集、差集、对称差集的功能
    a.两个列表的交
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.两个列表的并集

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.两个列表的差集

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.两个列表的对称差集

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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容

  • 编写函数,求1+2+3+…N的和 sum2(100) 编写一个函数,求多个数中的最大值 编写一个函数,实现摇骰子的...
    嘿嘿_9c52阅读 221评论 0 0
  • 1. 编写函数,求1+2+3+…N的和 2. 编写一个函数,求多个数中的最大值 3. 编写一个函数,实现摇骰子的功...
    itachhh阅读 499评论 0 0
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,338评论 0 5
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 8,994评论 0 3
  • 0.写一个匿名函数,判断指定的年是否是闰年程序: 结果: 1.写一个函数将一个指定的列表中的元素逆序( 如[1, ...
    2333_11f6阅读 209评论 0 0