鱼C论坛

 找回密码
 立即注册
查看: 2193|回复: 2

[已解决]第20讲,这代码前面录入字符串的时候应该有问题

[复制链接]
发表于 2023-7-21 22:32:53 | 显示全部楼层 |阅读模式

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

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

x
第20讲,这代码前面录入字符串的时候应该有问题,我不知道怎么处理,求大佬指点一下。
https://fishc.com.cn/thread-167926-1-1.html
动动手第0题
  1. s = input('请输入测试字符串:')
  2. list1 = []
  3. list1 = list1.extend(s)
  4. i = 0
  5. is_bool = True
  6. num = len(s) - 1
  7. if len(s)%2 == 0:
  8.     pass
  9. else:
  10.     is_bool = False

  11. for i in range(len(s)//2):
  12.     if list1[i] == '(':
  13.         pass
  14.     elif list1[i] == ')':
  15.         pass
  16.     elif list1[i] == '[':
  17.         pass
  18.     elif list1[i] == ']':
  19.         pass
  20.     elif list1[i] == '{':
  21.         pass
  22.     elif list1[i] == '}':
  23.         pass
  24.     else:
  25.         is_bool = False
  26.         break
  27. #第一个判断结束

  28. for i in range(len(s)//2):
  29.     for j in range( len(s)//2,len(s)):
  30.         if list1(i) == list1(num):
  31.             pass
  32.         else:
  33.             is_bool = False
  34.             break   
  35.         num -= 1      
  36. else:
  37.     is_bool = True
  38. #第二、三个判断结束

  39. if is_bool:
  40.     print('非法T_T')
  41. else:
  42.     print('合法^o^')
  43.         
复制代码
最佳答案
2023-7-21 22:33:05
问题分析:
根据您提供的代码,可能存在以下问题:
1. 在将输入的字符串转换为列表时,使用了list1.extend(s),但是extend()方法没有返回新的列表,所以后续对list1的操作会出现问题。
2. 在第一个判断结束之后的循环中,使用了list1(i)list1(num)来访问列表元素,但是应该使用方括号[]来进行索引操作,即list1[i]list1[num]
3. 在第二、三个判断结束后的条件判断中,缺少冒号(:)。
4. 在第三个判断结束后的循环中,应该使用下标num -= 1来更新索引值。

优化后的代码:
  1. s = input('请输入测试字符串:')
  2. list1 = list(s)  # 将输入的字符串转换为列表
  3. is_bool = True
  4. num = len(s) - 1
  5. if len(s) % 2 == 0:
  6.     pass
  7. else:
  8.     is_bool = False
  9.    
  10. for i in range(len(s)//2):
  11.     if list1[i] == '(':
  12.         pass
  13.     elif list1[i] == ')':
  14.         pass
  15.     elif list1[i] == '[':
  16.         pass
  17.     elif list1[i] == ']':
  18.         pass
  19.     elif list1[i] == '{':
  20.         pass
  21.     elif list1[i] == '}':
  22.         pass
  23.     else:
  24.         is_bool = False
  25.         break  # 第一个判断结束
  26. for i in range(len(s)//2):
  27.     for j in range(len(s)//2, len(s)):
  28.         if list1[i] == list1[num]:
  29.             pass
  30.         else:
  31.             is_bool = False
  32.             break
  33.         num -= 1
  34. else:
  35.     is_bool = True  # 第二、三个判断结束
  36. if is_bool:
  37.     print('非法T_T')
  38. else:
  39.     print('合法^o^')
复制代码

希望以上修改能帮助您解决问题。如果还有其他疑问,请随时提出。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-21 22:33:05 | 显示全部楼层    本楼为最佳答案   
问题分析:
根据您提供的代码,可能存在以下问题:
1. 在将输入的字符串转换为列表时,使用了list1.extend(s),但是extend()方法没有返回新的列表,所以后续对list1的操作会出现问题。
2. 在第一个判断结束之后的循环中,使用了list1(i)list1(num)来访问列表元素,但是应该使用方括号[]来进行索引操作,即list1[i]list1[num]
3. 在第二、三个判断结束后的条件判断中,缺少冒号(:)。
4. 在第三个判断结束后的循环中,应该使用下标num -= 1来更新索引值。

优化后的代码:
  1. s = input('请输入测试字符串:')
  2. list1 = list(s)  # 将输入的字符串转换为列表
  3. is_bool = True
  4. num = len(s) - 1
  5. if len(s) % 2 == 0:
  6.     pass
  7. else:
  8.     is_bool = False
  9.    
  10. for i in range(len(s)//2):
  11.     if list1[i] == '(':
  12.         pass
  13.     elif list1[i] == ')':
  14.         pass
  15.     elif list1[i] == '[':
  16.         pass
  17.     elif list1[i] == ']':
  18.         pass
  19.     elif list1[i] == '{':
  20.         pass
  21.     elif list1[i] == '}':
  22.         pass
  23.     else:
  24.         is_bool = False
  25.         break  # 第一个判断结束
  26. for i in range(len(s)//2):
  27.     for j in range(len(s)//2, len(s)):
  28.         if list1[i] == list1[num]:
  29.             pass
  30.         else:
  31.             is_bool = False
  32.             break
  33.         num -= 1
  34. else:
  35.     is_bool = True  # 第二、三个判断结束
  36. if is_bool:
  37.     print('非法T_T')
  38. else:
  39.     print('合法^o^')
复制代码

希望以上修改能帮助您解决问题。如果还有其他疑问,请随时提出。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-21 22:33:23 | 显示全部楼层
根据您提供的代码,看起来您试图检查输入的字符串是否是一个有效的括号匹配序列。然而,代码有几个错误。下面是修改后的正确代码:

  1. s = input('请输入测试字符串:')
  2. list1 = list(s)
  3. is_bool = True

  4. # 有效的括号匹配应该是偶数长度
  5. if len(list1) % 2 != 0:
  6.     is_bool = False
  7. else:
  8.     # 建立一个字典来定义括号匹配关系
  9.     match = {'(': ')', '[': ']', '{': '}'}
  10.     # 使用栈来存储待匹配的左括号
  11.     stack = []
  12.     for ch in list1:
  13.         # 如果ch是左括号,将其压入栈中
  14.         if ch in match:
  15.             stack.append(ch)
  16.         # 如果ch是右括号,判断栈是否为空或者栈顶的左括号与ch是否匹配
  17.         elif not stack or match[stack.pop()] != ch:
  18.             is_bool = False
  19.             break

  20. if is_bool:
  21.     print('合法^o^')
  22. else:
  23.     print('非法T_T')
复制代码

这段代码的逻辑是这样的:遍历输入的字符串,如果字符是左括号,就将其压入栈中;如果字符是右括号,就检查栈顶的左括号是否与其匹配。如果不匹配,或者在检查时栈为空(这意味着没有对应的左括号),则字符串为非法。如果整个字符串遍历完成,都没有发现非法情况,就认为字符串是合法的。

这是一个标准的括号匹配问题的解决方案,用到了栈这一数据结构的特性(后进先出)。在实际编程中,括号匹配问题是非常常见的,例如编译器就需要检查源代码中的括号是否匹配。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 06:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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