full-speed-python 习题解答(三)

项目地址:网页链接

full-speed-python书籍地址:https://github.com/joaoventura/full-speed-python

参考链接:https://www.chzzz.club/post/13.html

  • 4.1 Exercise with the math module

    1.Find the greatest common divisor of the following pairs of numbers: (15, 21), (152,
    200), (1988, 9765).
    2.Compute the base-2 logarithm of the following numbers: 0, 1, 2, 6, 9, 15.
    3.Use the “input” function to ask the user for a number and show the result of the
    sine, cosine and tangent of the number. Make sure that you convert the user input
    from string to a number (use the int() or the float() function).

      import math
      print(math.gcd(15,21),math.gcd(152,200),math.gcd(1988,9765))
      print(math.log(1,2),math.log(2,2),math.log(6,2),math.log(9,2),math.log(15,2))
      a=int(input())
      print(math.sin(a),math.cos(a),math.tan(a))
    

    3 8 7
    0.0 1.0 2.584962500721156 3.1699250014423126 3.9068905956085187
    10
    -0.5440211108893698 -0.8390715290764524 0.6483608274590866


  • 4.2 Exercise with functions

    1.Implement the “add2” function that receives two numbers as arguments and returns
    the sum of the numbers. Then implement the “add3” function that receives and
    sums 3 parameters.
    2.Implement a function that returns the greatest of two numbers given as parameters.
    Use the “if” statement to compare both numbers: https://docs.python.org/3/
    tutorial/controlflow.html#if-statements.
    3.Implement a function named “is_divisable” that receives two parameters (named
    “a” and “b”) and returns true if “a” can be divided by “b” or false otherwise. A
    number is divisable by another when the remainder of the division is zero. Use the
    modulo operator (“%”).
    4.Create a function named “average” that computes the average value of a list passed
    as parameter to the function. Use the “sum” and “len” functions.

      def add_two(a,b):
          print('Function got value:',a,b)
          return a+b
      def add_thr(a,b,c):
          print('Function got value:',a,b,c)
          return a+b+c
      sum_two = add_two(1,2)
      sum_thr = add_thr(1,2,3)
      print('sum_two:',sum_two,'sum_three:',sum_thr)
    
      def comp(num_1,num_2):
          if num_1 > num_2:
              return num_1
          else:
              return num_2
      greatest = comp(3,4)  
      print('the greatest one:',greatest)
    
      def is_divisible(a,b):
          if a%b == 0:
              return True
          else:
              return False
      print(is_divisible(8,3))    
    
      def average(alist):
          ave = sum(alist)/len(alist)
          return ave
      print(average([1,2,3,4]))
    

    Function got value: 1 2
    Function got value: 1 2 3
    sum_two: 3 sum_three: 6
    the greatest one: 4
    False
    2.5


  • 4.4 Exercise with recursive functions

1.Implement the factorial function and test it with several different values. Crosscheck
with a calculator.
2.Implement a recursive function to compute the sum of the n first integer numbers
(where n is a function parameter). Start by thinking about the base case (the sum
of the first 0 integers is?) and then think about the recursive case.
3.The Fibonnaci sequence is a sequence of numbers in which each number of the
sequence matches the sum of the previous two terms. Given the following recursive
definition implement fib(n).
fib(n) =0,if x = 0;
      =1,if x =1;
      =fib(n-1)+fib(n-2) otherwise.
Check your results for the first numbers of the sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89, …

    def factorial(x):
        if x == 0:
            return 1
        else:
            return x * factorial(x-1)
    print('5的递归结果',factorial(5))  

    def sum_first(n):
        if n == 0:
            return 0
        else:
            return n + sum_first(n-1)
    print('the sum of n first integer:',sum_first(5))    

    def fib(n):
        if n == 0:
            return 0
        if n == 1:
            return 1
        else:
            return fib(n-1)+fib(n-2)
    for i in range(12):
        print(fib(i))    

5的递归结果 120
the sum of n first integer: 15
0
1
1
2
3
5
8
13
21
34
55
89

猜你喜欢

转载自blog.csdn.net/qq_33251995/article/details/83215237
今日推荐