美文网首页
python每日一题

python每日一题

作者: DarknessShadow | 来源:发表于2020-04-11 22:43 被阅读0次

实例001:"数字组合"

题目有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

nums = [1, 2, 3, 4]
nums = list(map(str, nums))
temp = []
st1, st2, st3 = '', '', ''

def getNums(nums, i):
    tmp = []
    for num in nums:
        if i == num:
            continue
        tmp.append(num)
    return tmp

for i in nums:
    st1 = i
    temp_st1 = getNums(nums, i)
    for j in temp_st1:
        st2 = j
        for h in getNums(temp_st1, j):
            st3 = h
            s = st1 + st2 + st3
            temp.append(s)

print(len(temp))
print(temp)

简便方法

import itertools
num = [1, 2, 3, 4]
selects = []
for i in itertools.permutations(num, 3):
    selects.append(i)

print(selects)
print(len(selects))
# permutations类提供的简便方法它会直接将数组中的元素按照后面给定数字获得指定数目的元素不重复的所有组合的列表

实例002:"个税计算"

题目 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?

程序分析 分区间计算即可。

# 枫
# 每日一题 002 "个税计算"
class NegativeNumberException(Exception):
    def __init__(self):
        pass

    def __str__(self):
        print("亲,输入的利润不能为负数哦~")

def getaward():
    while True:
        try:
            profile = input('亲,请输入当月企业利润(单位:万元):')
            if not profile.isdigit():
                raise ValueError
            elif decimal.Decimal(profile) < 0:
                raise NegativeNumberException
            return calculate(decimal.Decimal(profile))
        except ValueError:
            print('亲,您输入的企业利润是非法字符,请输入正确的企业利润~')
        except NegativeNumberException:
            print('亲,您输入的企业利润是非法字符,请输入正确的企业利润~')

def calculate(profile):
    with decimal.localcontext() as ext:
        ext.prec = 4
        if profile <= 10:
            return profile * decimal.Decimal(0.1)
        elif profile <= 20:
            return calculate(10) + (profile - 10) * decimal.Decimal(0.075)
        elif profile <= 40:
            return calculate(20) + (profile - 20) * decimal.Decimal(0.05)
        elif profile <= 60:
            return calculate(40) + (profile - 40) * decimal.Decimal(0.03)
        elif profile <= 100:
            return calculate(60) + (profile - 60) * decimal.Decimal(0.015)
        else:
            return calculate(100) + (profile - 100) * decimal.Decimal(0.01)


if __name__ == '__main__':
    print('亲亲,你所以获得的提成为:%s万元,很棒有呦,继续加油~' % str(getaward()))

相关文章

  • python每日一题

    实例001:"数字组合" 题目有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少? 简...

  • Day 4 Project 我的微信好友

    附:每日一题

  • Python每日一题:第一题

    最近加入了Python之禅的知识星球Python之禅和他朋友们军哥在星球内分享干货,组织交流,让我这个编程小白获益...

  • Python 每日一题-杂记

    中文Python教程 https://yiyibooks.cn/xx/python_352/tutorial/in...

  • 2016-12-19

    今天写了两个python每日一题的题目。redis的操作感觉还是云里雾里的。当然mysql虽然正常一点但还是不太熟...

  • python每日一题总结8

    20180625 qzd 每日一题26 -- 有效单词词广场 给定一个单词序列,检查它是否构成一个有效单词广场。一...

  • leetcode每日一题 python解法 3月3日

    每日一题开始!有学python的小伙伴一起嘛 看来今天做的蛮早的,竟然击败了这么多人 难度:简单 题目内容: 给定...

  • 每日一题-2017-09-01

    2017.9.1每日一题: A senior manager responsible for business t...

  • 【mysql经典题】数据准备

    注意: 每日一题,大家一起监督、讨论学习。

  • Python每日一题:第二题

    今天我们学习下Python之禅和他朋友们的第二道题目,了解分析下军哥的代码 题目 设计一个猜数字的游戏,系统随机生...

网友评论

      本文标题:python每日一题

      本文链接:https://www.haomeiwen.com/subject/digumhtx.html