【Python】 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题学习记录全十期了,希望大家能够有所收获呀

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

“学海无涯苦作舟”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值