Python刷题笔记(2)- 取5位大数字

本文记录了在Python中解决 Codewars 题目的过程,该题要求从一个大数字字符串中找到最大的5位数。作者首先介绍了问题背景,然后分享了其思考过程,包括尝试通过遍历和首位筛选来解决问题。最终实现的解决方案是利用Python的内置函数max()找到最大值,作者反思这种方法比自己手动排序更简洁高效。
摘要由CSDN通过智能技术生成

又到codewar每日刷题时刻,今天抽到的题依旧是难度5

题目:

In the following 6 digit number:

283910

91 is the greatest sequence of 2 digits.

Complete the solution so that it returns the largest five digit number found within the number given.. The number will be passed in as a string of only digits. It should return a five digit integer. The number passed may be as large as 1000 digits.

Adapted from ProjectEuler.net

中文简介

从输入的若干连续数字(输入为字符串)中提取其中连续的最大5位数(整数输出)

想法:

1.最大连续5位数,首先想到的是历遍每个数位,然后截取连续的5位进行,不过这样数字位太多的时候过于繁杂

2.数字大小可以根据首位数的大小进行筛选,这样可以减少一大部分运算,可以通过集合来判断首位最大数

3.从原字符串中截取首位为最大数的5位连续数字,建立列表,然后进行排序

实现:

def solution(digits):
    big = sorted(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值