Rewrite the code so he can do less (now 20 lines)



  • Your job is to simplify my program, but to keep it functional.

    According to the Collatany number can be translated into a sequence of figures that always ends with units. This sequence is called Unsolved mathematics problems'Cause that hypothesis was never proven. The sequence of words is very simple: there is a function in which we hand over any number. If the number's clear, then we'll split it by two. число / 2 ) If it's not clear, then we multiply it by 3 + 1 (i.e. число * 3 + 1 ) The importance that we have regained our function is once again a function.

        def collatz( number ) :
          if number % 2 == 0 : # чётное
            result = number // 2
          if number % 2 == 1 : # нечётное
            result = 3 * number + 1
          print( result )
          return result
    
    error = True
    while error :
      error = True
      try :
        num = int( input( 'Введите число больше нуля: ' ) )
        if num > 0 :
          error = False
      except :
        error = True
    
    firstTime = True
    while num != 1 or firstTime :
      firstTime = False
      num = collatz( num )
    
    # если number - чётное, тогда это число надо разделить на 2 без остатка
    



  • Your job is to simplify my program, but to keep it functional.

    a Your task is to learn to correctly formulate the question : ) or to deal with the issue as a teaching assignment.

    Option 1:

    def collatz(number):
        result = (number // 2) if number % 2 == 0 else (3 * number + 1)
        print(result)
        if result != 1:
            collatz(result)
    

    num = 0

    while num <= 0:
    try:
    num = int(input('Введите число больше нуля: '))
    except:
    num = 0

    collatz(num)

    The function can be redesigned in such a way that the number 1 is immediately processed and the entire combination can be removed from the first.

    def collatz(number):
    print(number)
    if number != 1:
    result = (number // 2) if number % 2 == 0 else (3 * number + 1)
    collatz(result)

    No. print within the function, i.e. the establishment of the list and the withdrawal of the list:

    def collatz(number):
    res = [number]
    if number != 1:
    result = (number // 2) if number % 2 == 0 else (3 * number + 1)
    res += collatz(result)
    return res

    num = 0

    while num <= 0:
    try:
    num = int(input('Введите число больше нуля: '))
    except:
    num = 0

    print(*collatz(num), sep='\n')

    And the decision is in line 1:

    def collatz(number):
    return [number] + ([] if number == 1 else (collatz((number // 2) if number % 2 == 0 else (3 * number + 1))))

    num = 0

    while num <= 0:
    try:
    num = int(input('Введите число больше нуля: '))
    except:
    num = 0

    print(*collatz(num), sep='\n')

    A shorter version of the function:

    def collatz(value):
    return [value] + ([] if value == 1 else (collatz((3 * value + 1) if value % 2 else (value // 2))))

    Now, if there is no need to check the wrong entry, the code can be turned down as follows:

    def collatz(i):
    return [i] + ([] if i <= 1 else (collatz((3 * i + 1) if i % 2 else (i // 2))))

    print(*collatz(int(input('Введите число больше нуля: '))), sep='\n')

    - 3 lines:)



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2