original_docstring: Re-arrange 'term' or 'arith_expr' expressions to combine numbers.
      Numbers go first unless in a sum the numeric term is negative and not all
      the remaining terms are negative.
docstring: Re-arrange 'term' or 'arith_expr' expressions to combine numbers.
  Numbers go first unless in a sum the numeric term is negative and not all
  the remaining terms are negative.
docstring_tokens: Re - arrange ' term ' or ' arith_expr ' expressions to combine numbers . Numbers go first unless in a sum the numeric term is negative and not all the remaining terms are negative .
code: def collect_numbers(t):
      """Re-arrange 'term' or 'arith_expr' expressions to combine numbers.
      Numbers go first unless in a sum the numeric term is negative and not all
      the remaining terms are negative."""
      # number may be prefixed by a 'factor'
  ##    print "collect called with %s"%ast2string(t), "\n", t, "\n"
      if t[0] == 'arith_expr':
          args = [simplify(a) for a in t[1::2]]
          numargs = len(args)
          if args[0][0] == 'factor' and args[0][1][0] == 'MINUS':
              ops = [-1]
              # remove negative sign from term as we've recorded the sign for the sum
              args[0] = args[0][2]
          else:
              ops = [1]
          for op_t in t[2::2]:
              if op_t[0]=='PLUS':
                  ops.append(1)
              else:
                  ops.append(-1)
          num_ixs = []
          oth_ixs = []
          for i, a in enumerate(args):
              if a[0]=='NUMBER':
                  num_ixs.append(i)
              else:
                  oth_ixs.append(i)
          res_num = '0'
          # enter numbers first
          for nix in num_ixs:
              if ops[nix] > 0:
                  res_num=doadd(res_num,ast2string(simplify(args[nix])))
              else:
                  res_num=dosub(res_num,ast2string(simplify(args[nix])))
          # follow by other terms
          res_oth = '0'
          for oix in oth_ixs:
              if ops[oix] > 0:
                  res_oth=doadd(res_oth,ast2string(simplify(args[oix])))
              else:
                  res_oth=dosub(res_oth,ast2string(simplify(args[oix])))
          if res_num[0] == '-' and res_oth[0] != '-':
              # switch order
              return string2ast(doadd(res_oth,res_num))
          else:
              return string2ast(doadd(res_num,res_oth))
      elif t[0] == 'term':
          args = [simplify(a) for a in t[1::2]]
          numargs = len(args)
          ops = [1]
          for op_t in t[2::2]:
              if op_t[0]=='STAR':
                  ops.append(1)
              else:
                  ops.append(-1)
          num_ixs = []
          oth_ixs = []
          for i, a in enumerate(args):
              if a[0]=='NUMBER':
                  num_ixs.append(i)
              else:
                  oth_ixs.append(i)
          res_numerator = '1'
          res_denominator = '1'
          # enter numbers first
          for nix in num_ixs:
              if ops[nix] > 0:
                  res_numerator=domul(res_numerator,ast2string(simplify(args[nix])))
              else:
                  res_denominator=domul(res_denominator,ast2string(simplify(args[nix])))
          # follow by other terms
          for oix in oth_ixs:
              if ops[oix] > 0:
                  res_numerator=domul(res_numerator,ast2string(simplify(args[oix])))
              else:
                  res_denominator=domul(res_denominator,ast2string(simplify(args[oix])))
          return string2ast(dodiv(res_numerator,res_denominator))
      else:
          return t
code_tokens: def collect_numbers ( t ) : if t [ 0 ] == 'arith_expr' : args = [ simplify ( a ) for a in t [ 1 : : 2 ] ] numargs = len ( args ) if args [ 0 ] [ 0 ] == 'factor' and args [ 0 ] [ 1 ] [ 0 ] == 'MINUS' : ops = [ - 1 ] args [ 0 ] = args [ 0 ] [ 2 ] else : ops = [ 1 ] for op_t in t [ 2 : : 2 ] : if op_t [ 0 ] == 'PLUS' : ops . append ( 1 ) else : ops . append ( - 1 ) num_ixs = [ ] oth_ixs = [ ] for i , a in enumerate ( args ) : if a [ 0 ] == 'NUMBER' : num_ixs . append ( i ) else : oth_ixs . append ( i ) res_num = '0' for nix in num_ixs : if ops [ nix ] > 0 : res_num = doadd ( res_num , ast2string ( simplify ( args [ nix ] ) ) ) else : res_num = dosub ( res_num , ast2string ( simplify ( args [ nix ] ) ) ) res_oth = '0' for oix in oth_ixs : if ops [ oix ] > 0 : res_oth = doadd ( res_oth , ast2string ( simplify ( args [ oix ] ) ) ) else : res_oth = dosub ( res_oth , ast2string ( simplify ( args [ oix ] ) ) ) if res_num [ 0 ] == '-' and res_oth [ 0 ] != '-' : return string2ast ( doadd ( res_oth , res_num ) ) else : return string2ast ( doadd ( res_num , res_oth ) ) elif t [ 0 ] == 'term' : args = [ simplify ( a ) for a in t [ 1 : : 2 ] ] numargs = len ( args ) ops = [ 1 ] for op_t in t [ 2 : : 2 ] : if op_t [ 0 ] == 'STAR' : ops . append ( 1 ) else : ops . append ( - 1 ) num_ixs = [ ] oth_ixs = [ ] for i , a in enumerate ( args ) : if a [ 0 ] == 'NUMBER' : num_ixs . append ( i ) else : oth_ixs . append ( i ) res_numerator = '1' res_denominator = '1' for nix in num_ixs : if ops [ nix ] > 0 : res_numerator = domul ( res_numerator , ast2string ( simplify ( args [ nix ] ) ) ) else : res_denominator = domul ( res_denominator , ast2string ( simplify ( args [ nix ] ) ) ) for oix in oth_ixs : if ops [ oix ] > 0 : res_numerator = domul ( res_numerator , ast2string ( simplify ( args [ oix ] ) ) ) else : res_denominator = domul ( res_denominator , ast2string ( simplify ( args [ oix ] ) ) ) return string2ast ( dodiv ( res_numerator , res_denominator ) ) else : return t
short_docstring: Re-arrange 'term' or 'arith_expr' expressions to combine numbers.
short_docstring_tokens: Re - arrange ' term ' or ' arith_expr ' expressions to combine numbers .
comment: """Re-arrange 'term' or 'arith_expr' expressions to combine numbers.
      Numbers go first unless in a sum the numeric term is negative and not all
      the remaining terms are negative."""$SEP$# number may be prefixed by a 'factor'$SEP$##    print "collect called with %s"%ast2string(t), "\n", t, "\n"$SEP$# remove negative sign from term as we've recorded the sign for the sum$SEP$# enter numbers first$SEP$# follow by other terms$SEP$# switch order$SEP$# enter numbers first$SEP$# follow by other terms