鱼C论坛

 找回密码
 立即注册
查看: 6403|回复: 51

[已解决]Python:每日一题 278

[复制链接]
发表于 2019-11-20 21:00:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zltzlt 于 2019-11-20 21:00 编辑

今天的题目:


给定一个整数数组,返回它们的总和。但 13 是个不幸运的数字。如果整数数组中出现 13,13 以及它后面的 5 个数字都不计入总和

说明:13 后面的 5 个数字不包含 13。

示例 1:

输入:[1, 2, 2]
输出:5
示例 2:

输入:[1, 2, 13, 5, 4, 3, 4, 5, 6]
输出:9
解释:13 以及后面的 5 个数字 5, 4, 3, 4, 5 都不计入总和。
示例 3:

输入:[5, 13, 7]
输出:5


欢迎大家一起答题!
最佳答案
2019-11-21 11:18:15
def f278(nums:List[zxsq-anti-bbcode-int])->int:
    n = len(nums)
    i, s = 0, 0
    while i < n:
        if nums[zxsq-anti-bbcode-i] == 13:
            i += 6
            continue
        s += nums[zxsq-anti-bbcode-i]
        i += 1
    return s

代码撞了,我换个写法
def f278(nums:List[zxsq-anti-bbcode-int])->int:
    n = len(nums)
    i, t, s = 0, -6, 0
    while i < n:
        try:
            t = nums.index(13, t+6)
            s += sum(nums[i:t])
            i = t + 6
        except ValueError:
            return s + sum(nums[i:])
    return s

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-11-20 21:16:50 | 显示全部楼层
def solve(nums):
    i = 0
    while True:
        try:
            if nums[i] == 13:
                for j in range(6):
                    nums[i+j] = 0
                i += 5
            i += 1
        except IndexError:
            break
    return sum(nums)

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 21:39:52 | 显示全部楼层
def fun(lst):
    sum1 = 0
    i = 0
    while i < len(lst):
        if lst[i] == 13:
            i += 6
        else:
            sum1 += lst[i]
            i += 1
    return sum1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 21:40:53 | 显示全部楼层
def f(x):
    res=0
    flag=0
    for e in x:
        if e==13:
            flag=5
        else:
            if flag:
                flag-=1
            else:
                res+=e            
    return res
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-20 21:48:12 | 显示全部楼层

恭喜通过!

执行用时:136 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-20 21:48:35 | 显示全部楼层

恭喜通过!

执行用时:328 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-20 21:48:58 | 显示全部楼层

恭喜通过!

执行用时:106 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 21:53:30 | 显示全部楼层
本帖最后由 阴阳神万物主 于 2019-11-20 22:00 编辑

我来凑个热闹~~~
def solve(nums):
    unlucky = 0
    res = 0
    for each in nums:
        if each == 13:
            unlucky = 5
            continue
        elif unlucky:
            unlucky -= 1
            continue
        else:
            res += each
    return res

if __name__ == '__main__':
    print('示例1 5 输出:',solve([1,2,2]))
    print('示例2 9 输出:',solve([1,2,13,5,4,3,4,5,6]))
    print('示例3 5 输出:',solve([5,13,7]))
再来一个,感觉比上一个要快
def solve(nums):
#def solve_2(nums):
    i = 0
    res = 0
    while i < len(nums):
        if nums[i] == 13:
            i += 5
        else:
            res += nums[i]
        i += 1
    return res

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 21:54:24 | 显示全部楼层
本帖最后由 __mrsq__ 于 2019-11-20 21:58 编辑

我瞎了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 21:55:36 | 显示全部楼层
def solve(list1):
    result = 0
    count = 0
    while count < len(list1):
        if list1[count] != 13:
            result += list1[count]
            count += 1
        else:
            if count + 5 < len(list1):
                count += 6
            else:
                count = len(list1)
    return result

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-20 21:57:41 | 显示全部楼层
__mrsq__ 发表于 2019-11-20 21:54
[1, 2, 13, 13 ,5, 4, 3, 4, 5, 6]这种情况?

13 后面的 5 个数字不包含 13。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 22:51:18 | 显示全部楼层
ef app(list):
    sum=0
    i=0
    while True:
        try:
            if list[i] != 13:
                sum+=list[i]
                i+=1
            else:
                i+=6
                if list[i] !=13:
                    sum+=list[i]
                    i+=1
        except:
            print(sum)
            break

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 22:51:39 | 显示全部楼层
def check_sum(list1):
     bad_num = 13
     head = 0
     sum_good = 0
     while True:
          if head == len(list1):
               break
          if list1[head] == bad_num:
               if len(list1[head+1:]) >= 5:   
                    head += 6
               else:
                    break
          else:
               if head <= len(list1)-1:
                    sum_good += list1[head]
                    head += 1  
               else:
                    break

     return sum_good


if __name__ == '__main__':
     list1 = [1,2,13,5,4,3,4,5,6]
     print(check_sum(list1))

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 23:09:23 | 显示全部楼层
本帖最后由 angtn 于 2019-11-20 23:49 编辑

过来学习一下。
def echo(ls):
        for i in ls:
                if i == 13:
                        del ls[ls.index(13):ls.index(13)+6]
        return sum(ls)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 23:39:23 | 显示全部楼层
def solve(ls):
    while 13 in ls:
        idx = ls.index(13)
        del ls[idx: idx + 6]
    return sum(ls)

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-20 23:55:06 | 显示全部楼层
def fun278(nums:list):
    flag = 0
    result = 0
    for each in nums:
        if each == 13:
            flag = 5
            continue
        if flag:
            flag -=1
            continue
        result += each
    return result

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-21 00:18:18 | 显示全部楼层
def f(x,y=13):
        n=len(x)
        lst=[]
        if y not in x:
                return sum(x)
        else:
                while y in x:
                        n=x.index(y)
                        lst.append(x[:n])
                        x=x[n+1:]
                lst.append(x)
                lst1=lst[0]
                lst2=lst[1:]
                s=sum(lst1)
                for i in lst2:
                        if len(i)>5:
                                s+=sum(i[5:])
        return s
a=[1,2,2]
b=[1, 2, 13, 5, 4, 3, 4, 5, 6]
c=[5,13,7]
print(f(a))
print(f(b))
print(f(c))

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-21 00:50:26 | 显示全部楼层
本帖最后由 Python3005 于 2019-11-21 04:32 编辑
def fun(lst):
        if 13 not in lst:
                print(sum(lst))
        else:
                del lst[lst.index(13):lst.index(13)+6]
                fun(lst)

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-21 09:49:29 | 显示全部楼层
def f278(a):
    while 13 in a:
        loc=a.index(13)
        if loc==len(a)-1:
            del a[loc]
        else:
            b=a[loc+1:min(loc+6,len(a))]
            if 13 not in b:
                del a[loc:min(loc+6,len(a))]
            else:
                del a[loc:a.index(13,loc+1)]
    return sum(a)

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
zltzlt + 1 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-21 10:04:52 | 显示全部楼层
def fun278(nums):
    sums=sum(nums)
    index=0
    while True:
        try:
            index=nums.index(13,index)
            if index+6<=len(nums):
                sums-=sum(nums[index:index+6])
                index+=1
            else:
                sums -= sum(nums[index:])
                break
        except ValueError:
            break
    return sums
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-17 14:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表