[Python] Rational number generator (Python)

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo


    1. Why is it getting rid of the [1,3],[3,2] entry and replacing it with [2,3],[3,1]?


    2. Note, [2,3],[3,1] is the correct branch, but it should not override the previous entry, it should be appended to it.

      def rational():
      treecount = 0
      tree = [[[1,1]]]
      left=[0,0]
      right=[0,0]
      while(True):
      treecount+=1
      tree.append([])
      left=[0,0]
      right=[0,0]
      for z in range(len(tree[treecount-1])):
      left[0] = tree[treecount-1][z][0]
      right[0] = tree[treecount-1][z][0] + tree[treecount-1][z][1]
      left[1] = tree[treecount-1][z][1] + tree[treecount-1][z][0]
      right[1] = tree[treecount-1][z][1]
      tree[treecount].append(left)
      tree[treecount].append(right)
      yield( tree)

    Output looks like this:

    [[[1, 1]], [[1, 2], [2, 1]]] [[[1, 1]], [[1, 2], [2, 1]], [[1, 3], [3, 2]]] [[[1, 1]], [[1, 2], [2, 1]], [[2, 3], [3, 1], [2, 3], [3, 1]]]

    Continue reading...

Compartilhe esta Página