代码规范 结对要求

此作业的要求:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2147

结对伙伴:张帅

(1)       所有符号的左右两边一定要加空格,如果不加空格,会极大的降低代码的可读性。

 list = [['(', '', '', ')', '', ''], ['', '(', '', '', ')', ''], ['', '', '(', '', '', ')'], ['(', '', '(', ')', '', ')'], \

         ['((', '', '', ')', ')', ''], ['', '(', '(', '', '', '))'], ['(', '(', '', '', '))', ''], ['', '((', '', '', ')', ')'],\

         ['(', '', '', '', ')', ''], ['', '(', '', '', '', ')']]

 

 

(2)       定义的函数的代码行数不宜过长,10行左右最佳。同样是增加代码的可读性。但是我们的能力有限,有几个函数都超过了10行。

 

 def zyz(fenmu):

      s = []

      while fenmu not in [1]:# 循环保证递归

          for index in xrange(2, fenmu + 1):

              if fenmu % index == 0:

                  fenmu /= index # let n equal to it n/index

                  if fenmu == 1: # This is the point

                      s.append(index)

                  else : # index 一定是素数

                     s.append(index)

                 break

     return s

(3)       在写代码时,在能够通过其他方式解决问题的时候,不要使用枚举法,费时费力。(我们曾尝试使用枚举法列举8种除号 ’/’ 存在的位置的情况,虽然实现了功能,但时很费时)。下面是一段已经注释掉的代码:

  # if operator1 == '/' and operator2 != '/' and operator3 != '/':

      #     truth = eval('Fraction(a,b)' + operator2 + 'c' + operator3 + 'd')

      # elif operator1 == '/' and operator2 == '/' and operator3 != '/':

      #     truth = eval('Fraction(Fraction(a,b),c)' + operator3 + 'd')

      # elif operator1 == '/' and operator2 == '/' and operator3 == '/':

      #     truth = eval('Fraction(Fraction(Fraction(a,b),c),d)')

      # elif operator1 != '/' and operator2 == '/' and operator3 != '/':

      #     truth = eval('a' + operator1 + 'Fraction(b,c)' + operator3 + 'd')

      # elif operator1 != '/' and operator2 == '/' and operator3 == '/':

     #     truth = eval('a' + operator1 + 'Fraction(Fraction(b,c),d)')

     # elif operator1 != '/' and operator2 != '/' and operator3 == '/':

     #     truth = eval('a' + operator1 + 'b' + operator2 + 'Fraction(c,d)')

     # elif operator1 == '/' and operator2 != '/' and operator3 == '/':

     #     truth = eval('Fraction(a,b)' + operator2 + 'Fraction(c,d)')

     # else:

     #     truth = eval('a' + operator1 + 'b' + operator2 + 'c' + operator3 + 'd')

     # if operator1 == '/' or operator2 == '/' or operator3 == '/':

 

 

(4)       在定义变量名时,选择用英文释义的方式定义变量,如果随便选择一个名字,会在复查代码时浪费很多时间。(如operator代表运算符,bkl与bkr分别代表左括号与右括号)。

(5)       在定义多个类型和作用都相同的变量时,可以采用同时赋值的方式,可以让代码很简洁。

如:

 

tag1, tag2, tag3 = random.randint(0, 3), random.randint(0, 3), random.randint(0, 3)

 a, b, c, d = random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), random.randint(0, 9)

 

 

(6)       在一行代码过长时,应该使用反斜杠 ’\’ 换行。如第(1)条所示。

 

(7)       注意缩进,每次保证用4个空格的缩进,尤其是在Python中,缩进起到了逻辑分层的至关重要的作用。

 

(8)       对关键代码要进行注释。

如:

##后缀表达式计算结果

def calEndSuffixResult(list):

    stack = PyStack()

    sumEnd = 0

    if len(list) == 0:

        return sumEnd

    for i in list:

        if isnumber(i):

            stack.push(Fraction(i))

 

posted @ 2018-10-07 20:11  徐常实  阅读(170)  评论(0编辑  收藏  举报