鱼C论坛

 找回密码
 立即注册
查看: 2972|回复: 7

python小题

[复制链接]
发表于 2021-8-20 13:22:26 | 显示全部楼层 |阅读模式
20鱼币
请问下这个题怎么解

请描述 x 的哪些值使这个布尔值成为真
(x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])

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

使用道具 举报

发表于 2021-8-20 13:44:50 | 显示全部楼层
x是什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-20 13:54:50 | 显示全部楼层
本帖最后由 3236654291 于 2021-8-20 13:56 编辑
from random import *
x = ['p','o','n']

lst = []

num = 10000


while num > 0:
    if (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or \
       (len(x) == 5 and x[3] == x[4]):
        lst.append(x)
        x = ['p','o','n']

    else:
        x.insert(randint(0,len(x)),choice(x))

    num -= 1


if len(lst):
    for el in range(len(lst)-1, -1, -1):
        if lst.count(lst[el]) > 1:
            lst.pop(el)

for i in lst:
    print(i,'\n')


print('over')

随机生产结果,行不?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-20 15:53:38 | 显示全部楼层
本帖最后由 阿奇_o 于 2021-8-20 15:55 编辑
>>> x = 'ppon'
>>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n')
True
>>> x2 = 'abc22'
>>> (len(x2) == 5 and x2[3] == x2[4])
True

由以上试验,再根据or的特性,可以推出 x 可以是 'ppon', 'popn', 'oppn' 
或者是 五个字符长度的 结尾是任意两个相同的字符,即可,如 'abcxx', 'xyz..', '123**', .... 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-20 17:53:42 | 显示全部楼层
本帖最后由 白two 于 2021-8-20 17:56 编辑

俩括号中间用的 or 连接,所以 or 左右两边有一个为真即可

先看左边
x.count('p') == 2 and 'o' in x and x[-1] == 'n'
三者间用的 and 连接,所以必须同时满足三个条件,即:
x 里面要有两个 p 字符,至少得有一个 o 字符,且最后一个字符为 n ;

再看右边
len(x) == 5 and x[3] == x[4]
两者用 and 连接,也必须同时满足两个条件,即:
x 的长度等于 5 , x 第 4 个值等于第 5 个值;

例如:
>>> x = ["p","p","o",1,2,3,4,"n"]#满足右边
>>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
True
>>> x = ('p','p','1','o','2','n')#满足右边
>>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
True
>>> x = "oppn" #满足右边
>>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
True
>>> x = "xyyyy"#满足左边
>>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
True
>>> x = [1,2,3,4,4]#满足左边
>>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
True
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-23 22:02:51 | 显示全部楼层
先看最外层    是or   那只要两边有一边为真结果就是真了
左边为真的情况 : x满足  'p'字符有且仅有两个  'o'字符个数大于等于一个   以字符'n'结尾
右边为真的情况 : x长度为五    最后两个字符一样
贴几个例子
>>> x='ppon'
>>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
True
>>> x='asdwqdpasdpozn'
>>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
True
>>> x='asdxx'
>>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
True
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-24 13:57:12 | 显示全部楼层
or两边有一边为真结果就是真了
左边为真 : x满足p字符有且仅有两个o字符个数大于等于一个并以字符n结尾
右边为真 : x长度为五,最后两个字符一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-26 23:09:31 | 显示全部楼层
#三种情况
#1、字符串形式:
#1.1、最后一个字符是n,含有o,两个p的任意字符串:
>>> x = 'ppon'
>>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
True
#1.2、字符长度为5,最后两个字符相同
>>> x = 'abcdd'
>>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
True
#2、列表形式
#2.1、最后一个元素为‘n’,含有‘o’和两个‘p’
>>> x = ['p','p','o','n']
>>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
True
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-8 00:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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