鱼C论坛

 找回密码
 立即注册
查看: 3568|回复: 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])

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

使用道具 举报

发表于 2021-8-20 13:44:50 | 显示全部楼层
x是什么
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  3. lst = []

  4. num = 10000


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

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

  12.     num -= 1


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

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


  19. print('over')
复制代码


随机生产结果,行不?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  7. 由以上试验,再根据or的特性,可以推出 x 可以是 'ppon', 'popn', 'oppn'
  8. 或者是 五个字符长度的 结尾是任意两个相同的字符,即可,如 'abcxx', 'xyz..', '123**', ....
复制代码
小甲鱼最新课程 -> https://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 个值;

例如:

  1. >>> x = ["p","p","o",1,2,3,4,"n"]#满足右边
  2. >>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
  3. True
  4. >>> x = ('p','p','1','o','2','n')#满足右边
  5. >>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
  6. True
  7. >>> x = "oppn" #满足右边
  8. >>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
  9. True
  10. >>> x = "xyyyy"#满足左边
  11. >>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
  12. True
  13. >>> x = [1,2,3,4,4]#满足左边
  14. >>> print((x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4]))
  15. True
复制代码

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

发表于 2021-8-26 23:09:31 | 显示全部楼层
  1. #三种情况
  2. #1、字符串形式:
  3. #1.1、最后一个字符是n,含有o,两个p的任意字符串:
  4. >>> x = 'ppon'
  5. >>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
  6. True
  7. #1.2、字符长度为5,最后两个字符相同
  8. >>> x = 'abcdd'
  9. >>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
  10. True
  11. #2、列表形式
  12. #2.1、最后一个元素为‘n’,含有‘o’和两个‘p’
  13. >>> x = ['p','p','o','n']
  14. >>> (x.count('p') == 2 and 'o' in x and x[-1] == 'n') or (len(x) == 5 and x[3] == x[4])
  15. True
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-19 16:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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