Lesson 17 small turtle function Lego reflect after-school summary

4. How does this function how many arguments?

1.  def MyFun((x, y), (a, b)):
2.    return x * y - a * b

Answer two is wrong, the answer is zero
because the (x, y) and (a, b) is in xyab Ganso parameters, and function calls are variable, Ganso is unchanged
the same if this was changed to:

def MyFun([x, y], [a, b]):
    return x * y - a * b

print(MyFun([1,2],[3,4]))

Also being given, and therefore can be found, when defining the function brackets variable infinite but is generally (x, y, c, a , d, f)
and x may be a list, after the programming code may be written x [0] to reference the value of x.
So after the change is this:

def MyFun(x,y):
    return x[0] * x[1] - y[0] * y[1]

print(MyFun([1,2],[3,4]))

Programming problems
0. write a function power () built-in function analog value pow (), i.e. power (x, y) to calculate and return the power of x y.

def power(x,y):
    return x**y

print(power(2,3))

1. Write a function, using the greatest common divisor Euclidean algorithm, e.g. GCD (x, y) returns the greatest common divisor values ​​of parameters x and y parameters.

def gcd(x,y):
    i = 1
    b = 0
    while i < 100:
       if x % i ==0 and y % i == 0:
           if i > b :
               b = i
         
       i += 1
       if i > x and i >y :
           break
               
    return b

print(gcd(52,64))

#没有注意到欧几里得算法:俩数小的数除俩数相除的余数
def gcd(x,y):
    while y:
        t = x % y
        x = y
        y = t
    return x
print(gcd(18,9))

2. Prepare a decimal to binary conversion function, it requires "modulo 2 addition" method, the results of calling the bin () to return the same string.

先附上我的答案:
def binbin(x):
    temp=[]
    while x:
        y = x % 2
        x = x // 2
        temp.append(y)
    temp.reverse()
    print(temp)
print(binbin(10))

My thoughts:
First, the algorithm no major problems, but the main answer is beginning to take from the last, that algorithm figure out the answer is actually 0101 and 1010. However, string and no built-in functions can be flipped directly. Therefore defines an empty sequence, flipped with built-in functions into reverse after addition (note: the reversed operator sequences, are used in descending order, and the need for external output list).

Problem:
1. Print the result will be a None. The reason is because the last print the results in def in, this time function does not return a value, its default value is a return None, if then print again, None appears. There are two solutions, the first to the last line read: binbin (10), so that the output is directly the value of temp. The second is the print (temp) instead, return temp.
2. Print out a list form, and a string of different needs, and if using str () into a string list, comma, brackets will still be in.

Learn the answer:
new knowledge: 1 if the list is equivalent to 0 empty.
Value 2.return must have defined (result) at the start, or will be error

prior knowledge review: pop can delete the last digit and call list, so the list can be operated string becomes flashback

def binbin(x):
    temp=[]
    result = ''
    while x:
        y = x % 2
        x = x // 2
        temp.append(y)
    while temp:
        result += str(temp.pop())
    return result
print(binbin(10))

Summary: The last question will be programmed strings, integers, are memories of a list of knowledge, especially in the list, use append added at the end, and in turn taken out with a pop, the purpose of flipping. String itself does not flip built-in functions, and although there is a list, but the output still looked very troublesome. Note distinguish string and a list of built-in functions use the built-in functions

Published 17 original articles · won praise 1 · views 356

Guess you like

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105294026