栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【GitHub】 Python编程基础练习100题学习记录第十期(91~93)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【GitHub】 Python编程基础练习100题学习记录第十期(91~93)

1.此为GitHub项目的学习记录,记录着我的思考,代码基本都有注释。
2.可以作为Python初学者巩固基础的绝佳练习,原题有些不妥的地方我也做了一些修正。
3.建议大家进行Python编程时使用英语。
4.6~17题为level1难度,18-22题为level3难度,其余都为level1难度。
项目名称:
100+ Python challenging programming exercises for Python 3

#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Please write a program which accepts a string from console and print the characters that have even indexes.

Example: If the following string is given as input to the program:

H1e2l3l4o5w6o7r8l9d

Then, the output of the program should be:
Helloworld
"""

'''Hints: Use list[::2] to iterate a list by step 2.'''


temp = input('Please input:')
l1 = temp[::2]   # 列表切片操作,指定步长为2,提取元素
print(l1)

#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Please write a program which prints all permutations of [1,2,3]
"""

'''Hints: Use itertools.permutations() to get permutations of list.'''


from itertools import permutations
# permutations(iterable, r=None) 函数用来在 iterable 中取出 r 个元素进行排列组合,各个排列组合中的元素有先后顺序区别
# 当 r = None 时,就是对 iterable 中的所有元素进行排列。

l1 = [1, 2, 3]
l2 = list(permutations(l1))
print(l2)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Write a program to solve a classic ancient Chinese puzzle:
We count 35 heads and 94 legs among the chickens and rabbits in a farm.
How many rabbits and how many chickens do we have?
"""

'''Hint: Use for loop to iterate all possible solutions.'''

# 我的代码
heads = 35
legs = 94
for i in range(heads + 1):  # for循环匹配答案
    for j in range(heads + 1):
        if 2 * i + 4 * j == legs:  # 正确答案判定条件,鸡和兔的总腿数加起来为94只
            print("There are %d chickens and %d rabbits" % (i, j))  # 字符串匹配


# 源代码,用到了递归思想
"""def solve(num_heads, num_legs):
    ns = 'No solutions!'
    for i in range(num_heads + 1):
        j = num_heads - i
        if 2 * i + 4 * j == num_legs:
            return i, j
    return ns, ns


num_heads = 35
num_legs = 94
solutions = solve(num_heads, num_legs)
print(solutions)"""

好了,这就是Python编程基础练习100题学习记录全十期了,希望大家能够有所收获呀,附上所有链接:
【GitHub】 Python编程基础练习100题学习记录第一期(1~10)
【GitHub】 Python编程基础练习100题学习记录第二期(11~20)
【GitHub】 Python编程基础练习100题学习记录第三期(21~30)
【GitHub】 Python编程基础练习100题学习记录第四期(31~40)
【GitHub】 Python编程基础练习100题学习记录第五期(41~50)
【GitHub】 Python编程基础练习100题学习记录第六期(51~60)
【GitHub】 Python编程基础练习100题学习记录第七期(61~70)
【GitHub】 Python编程基础练习100题学习记录第八期(71~80)
【GitHub】 Python编程基础练习100题学习记录第九期(81~90)
【GitHub】 Python编程基础练习100题学习记录第十期(91~93)

再附上Python常用标准库供大家学习使用:
Python一些常用标准库解释文章集合索引(方便翻看)

“学海无涯苦作舟”

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/619310.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号