不死の野心 发表于 2020-2-10 17:38:05

20课的课后作业第二题

有没有大师能够为我讲解下小甲鱼的代码和我的代码的优缺点,对完答案后把我看懵了,完全没理解小甲鱼为啥能写这么多?

在此先提前感谢各位回答的鱼友了{:7_123:}

这个是题目:
请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
    a) 每位密码为单个小写字母
    b) 每位密码的左右两边均有且只有三个大写字母

这个是我的(str1没加,太长了):

lenth = len(str1)
for i in range(lenth-3):
    if str1.islower():
      if str1.isupper() and str1.isupper() and str1.isupper()and not str1.isupper():
            ifstr1.isupper() and str1.isupper() and str1.isupper() and not str1.isupper():
                print(str1,end='')

这个是小甲鱼的:
str1 = '''ABSaDKSbRIHcRHGcdDIF'''

countA = 0# 统计前边的大写字母
countB = 0# 统计小写字母
countC = 0# 统计后边的大写字母
length = len(str1)

for i in range(length):
    if str1 == '\n':
      continue

    """
    |如果str1是大写字母:
    |-- 如果已经出现小写字母:
    |-- -- 统计后边的大写字母
    |-- 如果未出现小写字母:
    |-- -- 清空后边大写字母的统计
    |-- -- 统计前边的大写字母
    """
    if str1.isupper():
      if countB:
            countC += 1
      else:
            countC = 0
            countA += 1

    """
    |如果str1是小写字母:
    |-- 如果小写字母前边不是三个大写字母(不符合条件):
    |-- -- 清空所有记录,重新统计
    |-- 如果小写字母前边是三个大写字母(符合条件):
    |-- -- 如果已经存在小写字母:
    |-- -- -- 清空所有记录,重新统计(出现两个小写字母)
    |-- -- 如果该小写字母是唯一的:
    |-- -- -- countB记录出现小写字母,准备开始统计countC
    """
    if str1.islower():
      if countA != 3:
            countA = 0
            countB = 0
            countC = 0
      else:
            if countB:
                countA = 0
                countB = 0
                countC = 0
            else:
                countB = 1
                countC = 0
                target = i

    """
    |如果前边和后边都是三个大写字母:
    |-- 如果后边第四个字母也是大写字母(不符合条件):
    |-- -- 清空记录B和C,重新统计
    |-- 如果后边仅有三个大写字母(符合所有条件):
    |-- -- 打印结果,并清空所有记录,进入下一轮统计
    """
    if countA == 3 and countC == 3:
      if i+1 != length and str1.isupper():
            countB = 0
            countC = 0
      else:
            print(str1, end='')
            countA = 3
            countB = 0
            countC = 0

wp231957 发表于 2020-2-10 17:41:50

如果第一位就是小写呢,它前面的你咋断

不死の野心 发表于 2020-2-10 18:34:10

wp231957 发表于 2020-2-10 17:41
如果第一位就是小写呢,它前面的你咋断

感谢回答,如果第一位小写会判断负号位,但是如果恰好遇到负号位出现满足条件时会有BUG

yangxuebabe 发表于 2020-4-7 17:34:26

wp231957 发表于 2020-2-10 17:41
如果第一位就是小写呢,它前面的你咋断

可是前三位和后三位根本不用判断啊

两个要求必须同时满足,要想满足第二个条件,第一位密码最早从第四位开始,所以只要判定前三位是不是大写就好了

string = 'ABSaDKSbRIHcRHGcdDIF'
list1 = list(string)
length = len(string)
for i in range(3,length-3):
    if (list1).islower():
      l1 = list1
      l2 = list1
      l3 = list1
      r1 = list1
      r2 = list1
      r3 = list1
      if i != 3 and i != length-4:
            l4 = list1
            r4 = list1      
            if l1.isupper() and l2.isupper() and l3.isupper() and r1.isupper() and r2.isupper() and r3.isupper():
                a = l4.isupper()
                b = r4.isupper()
                if a == False and b == False:
                  print(list1,end='')
      else :
            if l1.isupper() and l2.isupper() and l3.isupper() and r1.isupper() and r2.isupper() and r3.isupper():
                print(list1,end='')
页: [1]
查看完整版本: 20课的课后作业第二题