首页 > 编程语言 >python 2024-9

python 2024-9

时间:2024-09-25 13:55:52浏览次数:3  
标签:return nums python money 2024 int ans children


第一课

问题

a, b 求最大值?分类讨论

if a > b:
        print("最大值 = ", a)
    else:
        print("最大值 = ", b)

a, b, c 求最大值?

条件语句 if ... elif ... else

列表最大值?与参照物循环比较

a = [1.7, 1.65, 1.8, 1.55, 1.6] # 身高列表
    mx = 0 # 初始化最大值
    for x in a:
        if x > mx:
            mx = x
    print("最高身高为", mx)
循环语句 for x in a
内置函数 max

Python 实例教学_01_基础语法

第二课

Python 1-03 条件语句Python 1-05 控制流练习

2591. 将钱分给最多的儿童

class Solution:
    def distMoney(self, money: int, children: int) -> int:
        money -= children  # 每人至少 1 美元
        if money < 0: return -1
        res = min(money // 7, children)  # 初步分配,让尽量多的人分到 8 美元
        money -= res * 7
        children -= res
        # children == 0 and money:必须找一个前面分了 8 美元的人,分配完剩余的钱
        # children == 1 and money == 3:不能有人恰好分到 4 美元
        if children == 0 and money or \ # \ 续行符,money 非零为真,0 为假
           children == 1 and money == 3:
	           res -= 1
        return res
class Solution:
    def distMoney(self, money: int, children: int) -> int:
        money -= children  # 每人至少 1 美元
        if money < 0: return -1
        d = money // 7 # // 整除,/ 除法
        rem = money % 7 # % 取余
        # 分类讨论 每人可分得 8 元,剩余的加在其中某人身上。
        if d > children: return children - 1
        if d == children:
            return children - 1 if rem > 0 else children
        if d == children - 1 and rem == 3: return d - 1
        return d

第三课

2706. 购买两块巧克力

算法:最小值与次最小值

class Solution:
    def buyChoco(self, prices: List[int], money: int) -> int:
        a = b = inf
        for x in prices:
            if x < a:
                b, a = a, x
            elif x < b:
                b = x
        return money - a - b if a + b <= money else money

1413. 逐步求和得到正数的最小值

算法:前缀和

class Solution:
    def minStartValue(self, nums: List[int]) -> int:
        ans, acc = 1, 0
        for x in nums:
            acc += x
            if acc < 0:
                ans = max(ans, 1 - acc)
        return ans

第四课

Python 1-04 循环语句

412. Fizz Buzz

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        res = [''] * 列表包含 n # n 个 空串
        # res = []
        for i in range(1, n + 1):
            if i % 15 == 0: x = "FizzBuzz" # 先处理 15 的倍数
            elif i % 3 == 0: x = "Fizz"
            elif i % 5 == 0: x = "Buzz"
            else: x = str(i) # 转换成字符串
            res[i - 1] = x
            # res.append(x)
        return res

        # return ['Fizz'[i%3*4:]+"Buzz"[i%5*4:] or str(i) for i in range(1, n+1)]

第五课

747. 至少是其他数字两倍的最大数

知识点: 条件表达式

class Solution:
    def dominantIndex(self, nums: List[int]) -> int:
        # 方法一:最大值 >= 2 倍次大值
        first = second = id = 0
        for i, n in enumerate(nums):
            if n > first:
                first, second = n, first
                id = i
            elif n > second:
                second = n
        
        return id if first >= 2 * second else -1
        # 方法二:求最大值
        ans = max_ = -1        
        for i, x in enumerate(nums):  
            if x >= max_ * 2: ans = i # 当前的 max_ 对当前的 x 是满足条件的,先保存下来,但不一定是最终值。
            elif x > max_ // 2: ans = -1 # 说明 max_ 不符合条件,先记 ans = -1           
            max_ = max(max_, x) 
        return ans
        # 方法三:最大值函数
        max_, ans = max(nums), -1
        for i, x in enumerate(nums):
            if max_ == x: ans = i
            elif x > max_ // 2: return -1
        return ans
        # 方法四:离线排序
        q = sorted(range(len(nums)), key=lambda i:nums[i])
        return -1 if nums[q[-2]]*2 > nums[q[-1]] else q[-1]

第六课

1518. 换酒问题

知识点: while, /, %

class Solution:
    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
        res = rem = numBottles # 全部喝完,rem 为空瓶数
        while rem >= numExchange:
            numBottles, rem = divmod(rem, numExchange) # 可换酒 numBottles 瓶,剩余 rem 个空瓶。
            res += numBottles # 全部喝完
            rem += numBottles # + 空瓶           
        return res

第七课

2239. 找到最接近 0 的数字

class Solution:
    def findClosestNumber(self, nums: List[int]) -> int:
        ans = inf
        for x in nums:
            if abs(x) == abs(ans): ans = max(x, ans)
            elif abs(x) < abs(ans):  ans = x                
        return ans

3289. 数字小镇中的捣蛋鬼

标记法

class Solution:
    def getSneakyNumbers(self, nums: List[int]) -> List[int]:
        res = [0, 0]
        n = len(nums)
        i = 0
        for x in nums:
            x %= n
            if nums[x] >= n:
                res[i] = x
                i += 1
                if i == 2: break
            nums[x] += n
        return res

2432… 处理用时最长的那个任务的员工

class Solution:
    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:
        ans = pre = most = 0
        for i, t in logs: 
            dif = t - pre
            pre = t
            if most < dif or most == dif and ans > i:
                ans = i
                most = dif

        return ans


标签:return,nums,python,money,2024,int,ans,children
From: https://blog.51cto.com/u_1439909/12108303

相关文章

  • C#/.NET/.NET Core技术前沿周刊 | 第 5 期(2024年9.9-9.15)
    C#/.NET/.NETCore技术前沿周刊|第5期(2024年9.9-9.15) 思维导航前言.NET9中的性能改进.NET9ReleaseCandidate1现已发布!GitHub模型和.NET为工程师构建生成式AI应用程序AndroidAssetPacksfor.NET&.NETMAUIAndroidApps学习构建您的第一个BlazorHybrid......
  • python-成绩转换/药房管理/求正整数2~n之间的完全数
    一:成绩转换题目描述输入一个百分制的成绩 t ,将其转换成对应的等级,具体转换规则如下:90∼10090∼100 为A;80∼8980∼89为B;70∼7970∼79为C;60∼6960∼69为D;0∼590∼59为E。输入格式输入数据有多组,每组占一行,由一个整数组成。输出格式对于每组输入数据,输出一行。......
  • 基于python+flask框架的老人养老社区服务平台(开题+程序+论文) 计算机毕设
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着全球人口老龄化趋势的加剧,老年人口比例不断上升,如何为老年人提供全面、便捷、高效的养老服务成为社会各界关注的焦点。传统家庭养老模......
  • 基于python+flask框架的乐佳购物商城的设计与实现(开题+程序+论文) 计算机毕设
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展,电子商务已成为现代商业活动不可或缺的一部分,极大地改变了人们的消费习惯。乐佳购物商城的设计与实现正是在这一......
  • 基于python+flask框架的乐乐商城商品进销存管理系统(开题+程序+论文) 计算机毕设
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着电子商务的蓬勃发展,线上商城已成为消费者购物的重要渠道之一。乐乐商城作为新兴的电商平台,面临着日益增长的商品种类与交易量,如何高效......
  • 在Ubuntu 16.04上安装Anaconda Python发行版的方法
    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。简介Anaconda是一个开源的包管理器、环境管理器和Python和R编程语言的发行版。它通常用于大规模数据处理、科学计算和预测性分析,为数据科学家、开发人员、业务分析师......
  • AI推介-大语言模型LLMs论文速览(arXiv方向):2024.08.25-2024.08.31
    文章目录~1.LongRecipe:RecipeforEfficientLongContextGeneralizationinLargeLanguageModels2.GenAI-poweredMulti-AgentParadigmforSmartUrbanMobility:OpportunitiesandChallengesforIntegratingLargeLanguageModels(LLMs)andRetrieval-Augm......
  • AIGC赋能游戏美术新高度,2024年还不会用AI技术的原画师设计师真的out了!
    大家好,我是强哥随着AIGC技术的飞速发展与大模型的不断成熟迭代,使得其应用前景正在越来越宽阔地展现出来,**“AIGC+”也将逐渐成为各类行业发展的新模式,**也极大地提升了各内容行业的想象空间。而在众多应用领域中,游戏相比其他内容形态具备更强的科技属性,这意味着,游戏行业有......
  • python 两个装饰器,怎么执行
    在Python中,当你为一个函数应用多个装饰器时,它们会按照自下而上的顺序依次应用。具体来说,假设有两个装饰器@decorator1和@decorator2,应用到同一个函数func上,如下所示:@decorator1@decorator2deffunc():pass这等价于以下代码:func=decorator1(decorator2(func))......
  • 解决 Python IDLE 横向显示文字的方法
    用pythonIDLE编写代码的时候,遇到中文字体是横向的,如何解决?解决办法,亲测有效。解决办法保存之后,之前的中文横向就竖向显示了。......