practice1_fish
#!/usr/bin/python3
"""
穷举法:穷尽所有可能直到找到正确答案
"""
def main():
"""分鱼"""
fish = 1
while True:
enough = True
total = fish
for _ in range(5):
if (total - 1) % 5 == 0:
total = (total - 1) // 5 *4
else:
enough = False
break
if enough:
print(fish)
break
fish += 1
if __name__ == "__main__":
main()
practice2_joseph
#!/usr/bin/python3
"""约瑟夫环"""
def main():
"""主函数"""
persons = [True] * 30
# 设置变量,计数,下标,报数
counter, index, number = 0, 0, 0
# 循环选出15个人
while counter < 15:
if persons[index]:
# 每次数数加一
number += 1
# 数到9的人,弄死
if number == 9:
# 把它的值变为False
persons[index] = False
# 每选出一个人,counter数量加一
counter += 1
# 弄死的人,后面人又从0开始数数
number = 0
# 移动下标
index += 1
# 保证有效的下标
index %= len(persons)
for person in persons:
print("基" if person else "非", end = "")
print()
if __name__ == "__main__":
main()
####practice3_salary
#!/usr/bin/python3
from abc import ABCMeta, abstractmethod
"""
面向对象的程序设计基本步骤:
1. 定义类
- 数据抽象: 找到对象的静态特征-属性(名词)
- 行为抽象: 找到对象的动态特征-方法(动词)
2. 创建对象(隐藏实现细节,暴露简单的调用接口)
3. 给对象发消息
面向对象的四大支柱:抽象、封装、继承、多态
抽象:定义类的过程就是提取共性的抽象过程
封装:将数据和操作数据方法从逻辑上组成一个整体-对象
继承:从已有的类创建新类的过程
- 提供继承信息的称为父类
- 得到继承信息的称为子类
多态:调用相同的方法做了不同的事情
- 同样的方法在运行时表现出不同行为
- 子类重写父类的方法,不同的子类给出不同的实现版本
"""
class Employee(metaclass=ABCMeta):
# metaclass=ABCMeta 原类,让子类不能再创建这个抽象对象,给别人继承
def __init__(self, name):
self.name = name
# 装饰器
@abstractmethod
def salary(self):
pass
class Manager(Employee):
"""经理"""
@property
def salary(self):
return 15000
class Programmer(Employee):
"""程序员"""
def __init__(self, name):
super().__init__(name)
# self.name = name
self.working_hour = 0
@property
def salary(self):
return 200 * self.working_hour
class Salesman(Employee):
"""销售员"""
def __init__(self, name):
self.name = name
self.sales = 0
@property
def salary(self):
return 1800 + self.sales * 0.05
"""工资结算"""
def main():
"""主函数"""
emps = [
Manager("刘备"), Manager("曹操"),
Programmer("诸葛亮"), Programmer("荀彧"),
Salesman("貂蝉")
]
for emp in emps:
# isinstance函数可以进行运行时类型识别,判断函数类型
if isinstance(emp, Programmer):
emp.working_hour = int(input(f'请输入{emp.name}本月工作时间: '))
elif isinstance(emp, Salesman):
emp.sales = float(input(f'请输入{emp.name}本月销售额: '))
#print('%s: %.2f元' % (emp.name, emp.salary))
print(f"{emp.name}本月的工资为: {emp.salary}元")
if __name__ == "__main__":
main()
practice_poker
#!/usr/bin/python3
from enum import Enum, unique
import random
# 定义花色的常量(尽量用符号常量代替字面常量)
# 枚举类型是定义符号常量的最佳选择
# @unique 类的装饰器,类里面的变量不能重复,独一无二的
"""花色枚举类,用来定义黑桃、红心、梅花、方块常量"""
@unique
class Suite(Enum):
SPADE = 0
HEART = 1
CLUB = 2
DIAMOND = 3
"""定义扑克类"""
class Card(object):
def __init__(self, suite, face):
self.suite = suite
self.face = face
def show(self):
suites = ["黑", "红", "梅", "方"]
faces = [
"", "A", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "J", "Q", "K"
]
return f"{suites[self.suite.value]}{faces[self.face]}"
"""
# 比较大小
def __lt__(self, other):
if self.suite == other.suite:
return self.face < other.face
return self.suite.value < other.suite.value
"""
# 把对象变成字符串(魔术方法)
def __str__(self):
return self.show()
# 把对象变成字符串(魔术方法)
def __repr__(self):
return self.show()
"""牌类"""
class Poker(object):
def __init__(self):
self.index = 0
# 生成一副牌,生成式
self.cards = [Card(suite, face)
for suite in Suite
for face in range(1, 14)
]
def shuffle(self):
"""洗牌,调用随机乱序的函数"""
random.shuffle(self.cards)
def deal(self):
"""发牌"""
card = self.cards[self.index]
self.index += 1
return card
@property
def has_more(self):
"""判断是否有牌发"""
return self.index < len(self.cards)
class Player(object):
"""玩家"""
def __init__(self, name):
self.name = name
self.cards = []
def get_one(self, card):
"""摸一张牌"""
self.cards.append(card)
# *后面的是命名关键字参数,必须写参数名key
def sort_cards(self, *, key=lambda card:
(card.suite.value, card.face)):
""" 整理手上的牌"""
self.cards.sort(key=key)
def main():
poker = Poker()
poker.shuffle()
players = [
Player("东邪"), Player("西毒"), Player("南帝"),
Player("北丐")
]
for _ in range(13):
for player in players:
player.get_one(poker.deal())
for player in players:
player.sort_cards(key=lambda card: card.face)
print(player.name, end=": ")
print(player.cards)
if __name__ == "__main__":
main()
practice_mycal
#!/usr/bin/python3
"""
万年历
"""
from datetime import datetime
import sys
def is_leap(year):
"""判断指定年份是否是闰年"""
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
def get_month_days(year, month):
"""获得指定的月份的天数"""
days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 修改2月份的天数
if month == 2 and is_leap(year):
days[2] = 29
return days[month]
def main():
# 通过sys模块的argv可以获取命令行参数
if len(sys.argv) == 3:
year = int(sys.argv[2])
month = int(sys.argv[1])
else:
# 拿到现在系统的时间和日期
now = datetime.now()
year = now.year
month = now.month
# 拿到年,月小于2,算到上一年
y = year if month > 2 else year - 1
# 拿到月,月小于2,算到13月或者14月
m = month if month > 2 else month + 12
# 取到世纪,年份的前两位数
c = y // 100
# 取到年的后两位
y = y % 100
# 计算每个月1号是星期几
w = y + y // 4 + c // 4 - 2 * c + 26 * (m + 1) // 10
# 对7求余数,0对应星期日
w %= 7
months = [
"", "January", " February", "March", "April", "May", " June",
" July", " August", "September", "October", " November", " December"
]
print(f"{months[month]} {year}".center(20))
print("Su Mo Tu We Th Fr Sa")
print (" " * 3 * w,end="")
days = get_month_days(year, month)
# 遍历月份的天数,进行排版
for day in range(1, days + 1):
print(f"{day}".rjust(2), end=" ")
w += 1
if w % 7 == 0:
print()
print()
if __name__ == "__main__":
main()