鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: zltzlt

Python:每日一题 353

[复制链接]
发表于 2020-3-16 23:36:53 | 显示全部楼层

最起码比我六批,我连题目该咋解读都还没懂,还不知道从哪下手
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 01:28:24 | 显示全部楼层
  1. def f353(num_str):
  2.     num_temp = []
  3.     for num in num_str:
  4.         if num.isdigit():
  5.             num_temp.append(int(num))
  6.             
  7.     num_forever = num_temp.copy()
  8.     a = num_temp.pop()
  9.     check = False
  10.     for i in range(len(num_temp)):
  11.         if check:
  12.             break
  13.         b = num_temp[i]
  14.         num_temp.remove(b)
  15.         count1 = count(a,b)
  16.         c = num_temp[0]
  17.         d = num_temp[1]
  18.         count2 = count(c,d)
  19.         for ab in count1:
  20.             if check:
  21.                 break
  22.             for cd in count2:
  23.                 if check:
  24.                     break
  25.                 count3 = count(ab,cd)
  26.                 for abcd in count3:
  27.                     if abcd == 24:
  28.                         check = True
  29.                         break
  30.         if not check:
  31.             num_temp.insert(i,b)
  32.     if not check:
  33.         print('False')
  34.                
  35. def count(a,b):
  36.     count_list = []
  37.     c = a+ b
  38.     d = abs(a - b)
  39.     e = a * b
  40.     if b :
  41.         f = a / b
  42.     else:
  43.         f = 0
  44.     count_list.extend((c,d,e,f))
  45.     return count_list


  46. numlist = input("请输入4个数字:")
  47. f353(numlist)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 05:09:02 | 显示全部楼层
global a,aa,pp,b,e,ee
a=[int(m) for m in input().split()]
b=[]
pp=0
e=[]
ee=[]

while True:
    random.shuffle(a)
    d=copy.copy(a)
    c=d[0]
    d.pop(0)
   e=[]
   x(d,c)
   if pp==24:
   break

try:
    for i in range(len(a)):
    ee.append(a[i])
    ee.append(e[i])
except:
    None

def x(d,c):
    global pp
    global e
    if len(d)>0:
        p=random.randint(1,4)
        if p==1:
            e.append('+')
            x1(d,c)
        if p==2:
            e.append('-')
            x2(d,c)
        if p==3:
            e.append('*')
            x3(d,c)
        if p==4:
            e.append('/')
            x4(d,c)
    else:
        pp=c

def x1(d,c):
    c+=d[0]
    d.pop(0)
    x(d,c)

def x2(d,c):
    c-=d[0]
    d.pop(0)
    x(d,c)

def x3(d,c):
    c*=d[0]
    d.pop(0)
    x(d,c)

def x4(d,c):
    c/=d[0]
    d.pop(0)
    x(d,c)

def po():
    global u1,u2
    g1.delete(0,tk.END)
    g1.insert(0,u1)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 09:18:52 | 显示全部楼层
  1. from fractions import Fraction


  2. def operation(num1, num2):
  3.     operater = '+-*'
  4.     result = []
  5.     for i in operater:
  6.         try:
  7.             expression = eval(str(num1) + i + str(num2))
  8.             result.append(expression)
  9.         except:
  10.             pass
  11.     try:
  12.         result.append(num2 - num1)
  13.         result.append(Fraction(num1, num2))
  14.         result.append(Fraction(num2, num1))
  15.     except:
  16.         pass
  17.     return list(set(result))
  18.         

  19. def test(lista):
  20.     for a in lista:
  21.         temp1 = lista[:]
  22.         temp1.remove(a)
  23.         for b in temp1:
  24.             res1 = operation(a, b)
  25.             for c in res1:
  26.                 temp2 = lista[:]
  27.                 temp2.remove(a)
  28.                 temp2.remove(b)
  29.                 res2 = operation(temp2[0], temp2[1])
  30.                 for d in res2:
  31.                     res3 = operation(c, d)
  32.                     if 24 in res3:
  33.                         return True
  34.                 for e in temp2:
  35.                     res4 = operation(c, e)
  36.                     for f in res4:
  37.                         temp3 = lista[:]
  38.                         temp3.remove(a)
  39.                         temp3.remove(b)
  40.                         temp3.remove(e)
  41.                         for g in temp3:
  42.                             res5 = operation(f, g)
  43.                         if 24 in res5:
  44.                             return True
  45.     return False
复制代码

到处修修补补,运行效率极低

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 10:28:59 | 显示全部楼层
  1. import math
  2. def f353(arr):
  3.     def solve(n):
  4.         if(1 == n):
  5.             if(math.fabs(24 - tempArr[0]) < 1E-6):
  6.                 return True
  7.             else:
  8.                 return False
  9.         for i in range(0, n):
  10.             for j in range(i+1, n):
  11.                 a, b = tempArr[i], tempArr[j]               
  12.                 tempArr[j] = tempArr[n - 1]
  13.                 tempArr[i] = a + b
  14.                 if ( solve(n - 1) ) :
  15.                     return True
  16.                 tempArr[i] = a - b
  17.                 if solve(n - 1) :
  18.                     return True
  19.                 tempArr[i] = b - a
  20.                 if solve(n - 1):
  21.                     return True
  22.                 tempArr[i] = a * b
  23.                 if solve(n - 1):
  24.                     return True;
  25.                 if b != 0:
  26.                     tempArr[i] = a / b
  27.                     if solve(n - 1):
  28.                         return True
  29.                 if a != 0:
  30.                     tempArr[i] = b / a
  31.                     if solve(n - 1):
  32.                         return True
  33.                 tempArr[i], tempArr[j] = a, b        
  34.         return False        
  35.     tempArr = arr  
  36.     return True if solve(4) else False
复制代码

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 11:54:32 | 显示全部楼层
import itertools
def func353(h):
    from itertools import permutations as p
    for a,b,c,d in p(list(map(str,h)),4):
        for x,y,z in p(list("+++---***///"),3):
            if eval((str(eval(a+x+b))+y+c+z+d))==24:
                return True
            if eval(str(eval(a+x+b))+y+str(eval(c+z+d)))==24:
                return True
            if eval(str(eval(str(eval(a+x+b))+y+c))+z+d)==24:
                    return True
    return False

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 12:16:02 | 显示全部楼层
  1. from itertools import permutations

  2. def fun353(x):
  3.     for cards in permutations(x):
  4.         for i in ['+','-','*','/']:
  5.             for j in ['+','-','*','/']:
  6.                 for n in ['+','-','*','/']:
  7.                     if eval('%s%s%s%s%s%s%s'%(cards[0],i,cards[1],j,cards[2],n,cards[3])) == 24:
  8.                         return True
  9.         cards_1 = cards[0]
  10.         cards_2 = cards[1:]
  11.         for i in ['+','-']:
  12.             for j in ['+','-']:
  13.                 sum_cards_2 = eval('%s%s%s%s%s'%(cards_2[0],i,cards_2[1],j,cards_2[2]))
  14.                 try:
  15.                     if cards_1*sum_cards_2==24 or cards_1/sum_cards_2==24:
  16.                         return True
  17.                 except ZeroDivisionError:
  18.                     continue
  19.         cards_2 = cards[3]
  20.         cards_1 = cards[:3]
  21.         for i in ['+','-']:
  22.             for j in ['+','-']:
  23.                 sum_cards_1 = eval('%s%s%s%s%s'%(cards_1[0],i,cards_1[1],j,cards_1[2]))
  24.                 try:
  25.                     if sum_cards_1*cards_2==24 or sum_cards_1/cards_2==24:
  26.                         return True
  27.                 except ZeroDivisionError:
  28.                     continue
  29.         cards_1 = cards[:2]
  30.         cards_2 = cards[2:]
  31.         for i in ['+','-']:
  32.             for j in ['+','-']:
  33.                 sum_cards_1 = eval('%s%s%s'%(cards_1[0],i,cards_1[1]))
  34.                 sum_cards_2 = eval('%s%s%s'%(cards_2[0],i,cards_2[1]))
  35.                 try:
  36.                     if sum_cards_1*sum_cards_2==24 or sum_cards_1*sum_cards_2==24:
  37.                         return True
  38.                 except ZeroDivisionError:
  39.                     continue
  40.     return False
复制代码

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 13:21:56 | 显示全部楼层
l0stparadise 发表于 2020-3-16 19:32
感觉自己的方法好蠢。。。。

解答错误

输入:[1, 3, 4, 6]
输出:False
预期结果:True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 13:23:00 | 显示全部楼层

解答错误

输入:[1, 5, 9, 1]
输出:True
预期结果:False
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 13:24:55 | 显示全部楼层

解答错误

输入:[1, 3, 4, 6]
输出:False
预期结果:True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 13:25:44 | 显示全部楼层
钙硼钡锗 发表于 2020-3-17 05:09
global a,aa,pp,b,e,ee
a=
b=[]

缩进改一下哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 13:26:16 | 显示全部楼层
wcshds 发表于 2020-3-17 09:18
到处修修补补,运行效率极低

1204 ms
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 13:27:08 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 13:27:36 | 显示全部楼层
whosyourdaddy 发表于 2020-3-17 11:54
import itertools
def func353(h):
    from itertools import permutations as p

输入 [1, 2, 1, 2] 出错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 13:28:07 | 显示全部楼层

解答错误

输入:[1, 3, 4, 6]
输出:False
预期结果:True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 13:46:18 | 显示全部楼层
zltzlt 发表于 2020-3-17 13:23
解答错误

输入:[1, 5, 9, 1]
  1. def f353(x):
  2.     from itertools import combinations as cb
  3.     def com(a,b):
  4.         l=len(a)
  5.         if l==1:
  6.             return a[0]==b
  7.         m=eval('*'.join(map(str,a)))
  8.         if m==b:
  9.             return True
  10.         elif m<b:
  11.             return False
  12.         elif l==2:
  13.             e,f=a
  14.             return com([e],b+f) or com([e],b-f) or com([e],b*f) or com([e],b/f)
  15.         else:
  16.             for e,f in cb(a,2):
  17.                 if f<e:
  18.                     e,f=f,e
  19.                 t=a[:]
  20.                 t.remove(e)
  21.                 t.remove(f)
  22.                 for g in [f+e,f-e,f*e,f/e]:
  23.                     if com(t,b+g) or com(t,b-g) or com(t,b*g) or com(t,b/g) if g else 0:
  24.                         return True
  25.             return False
  26.     return com(x,24)
复制代码

恩,再看看- -
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 17:54:37 | 显示全部楼层
import itertools
def func353(h):
    from itertools import permutations as p
    for a,b,c,d in p(list(map(str,h)),4):
        for x,y,z in p(list("+++---***///"),3):
            try:
                if eval((str(eval(a+x+b))+y+c+z+d))==24:
                    return True
                if eval(str(eval(a+x+b))+y+str(eval(c+z+d)))==24:
                    return True
                if eval(str(eval(str(eval(a+x+b))+y+c))+z+d)==24:
                    return True
            except ZeroDivisionError:
                continue
    return False
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-19 17:35:40 | 显示全部楼层
  1. from operator import add,sub,mul,truediv
  2. import itertools
  3. operators = (add, sub, mul, truediv)
  4. sucess = []
  5. def n353(list1):

  6.     for h in itertools.permutations(list1, 4):
  7.         for i in operators:
  8.             for j in operators:
  9.                 for k in operators:
  10.                     try:
  11.                         result = i(j(k(h[2], h[3]), h[1]), h[0])
  12.                         if result == 24:
  13.                             return True
  14.                     except:
  15.                         pass  
  16.                     try:
  17.                         result = i(j(h[2], h[3]), k(h[1], h[0]))
  18.                         if result == 24:
  19.                             return True
  20.                     except:
  21.                         pass
  22.     return False
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 07:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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