defcomposite_identity(f, g):""" Return a function with one parameter x that returns True if f(g(x)) is equal to g(f(x)). You can assume the result of g(x) is a valid input for f and vice versa.>>> add_one = lambda x: x + 1 # adds one to x>>> square = lambda x: x**2 # squares x [returns x^2]>>> b1 = composite_identity(square, add_one)>>> b1(0) # (0 + 1) ** 2 == 0 ** 2 + 1 True>>> b1(4) # (4 + 1) ** 2 != 4 ** 2 + 1 False """returnlambda x: f(g(x))==g(f(x))
按照题意返回一个lambda函数,传入x即可
Count Cond
predicate function: 返回True或False的函数
题意:写一个函数,传入一个两个参数的predicate function
condition,返回一个含有参数n的函数,判断1-n中有几个数满足condition函数
defcount_cond(condition):"""Returns a function with one parameter N that counts all the numbers from 1 to N that satisfy the two-argument predicate function Condition, where the first argument for Condition is N and the second argument is the number from 1 to N.>>> count_fives = count_cond(lambda n, i: sum_digits(n * i) == 5)>>> count_fives(10) # 50 (10 * 5) 1>>> count_fives(50) # 50 (50 * 1), 500 (50 * 10), 1400 (50 * 28), 2300 (50 * 46) 4>>> is_i_prime = lambda n, i: is_prime(i) # need to pass 2-argument function into count_cond>>> count_primes = count_cond(is_i_prime)>>> count_primes(2) # 2 1>>> count_primes(3) # 2, 3 2>>> count_primes(4) # 2, 3 2>>> count_primes(5) # 2, 3, 5 3>>> count_primes(20) # 2, 3, 5, 7, 11, 13, 17, 19 8 """defjudge(n): cnt =0for i inrange(1,n+1):if condition(n,i): cnt+=1return cntreturn judge
遍历1-n,传入condition函数并计数即可
注意返回的是函数,传入n
Multiple
题意:写一个函数求参数a,b的最小公倍数
1 2 3 4 5 6 7 8 9 10 11 12 13
defmultiple(a, b):"""Return the smallest number n that is a multiple of both a and b.>>> multiple(3, 4) 12>>> multiple(14, 21) 42 """defgcd(a,b):if b==0:return areturn gcd(b,a%b)return a*b//gcd(a,b)
from math importpowdefidentity(k):return kdefcube(k):returnint(pow(k,3))defsummation(n,term):"""sum the first N terms of a sequence.>>> summation(5,cube) 225 """ total,k=0,1while k<=n: total,k = total + term(k), k+1return totaldefsum_naturals(n):"""sum the first N natural numbers>>> sum_naturals(5) 15 """return summation(n,identity) # 传入返回原值函数进行计算defsum_cubes(n):"""sum the first N cubes of natural numbers>>> sum_cubes(5) 225 """return summation(n,cube) # 传入返回每个值的立方函数进行计算
其中,summation函数中的term参数与传入的函数有关
identity与cube函数作为单个参数传入summation函数,以处理不同情况下的求和
形参term函数在计算total时被回调,回调的是传入的对应函数
函数作为返回值
当一个函数在另一个函数体内定义,该函数的名称绑定在本地作用域中
如下:
1 2 3 4 5 6 7 8 9
defmake_adder(n):"""return a function that takes one argument called k and return k+N>>> add_three = make_adder(3)>>> add_three(4) 7 """defadder(k):return k+n # adder函数返回数值k+nreturn adder # make_adder函数返回adder函数
defsearch(f):"""find a number that one more than the square root of the number put in the positive function>>> search(positive) 11 """ x=0whilenot f(x): # 当positive函数返回为0时进入循环 x+=1return xdefsquare(x):return x**2defpositive(x):returnmax(0,square(x)-100)definverse(f):"""return g(y) such that g(f(x)) ->x>>> inverse(square)(16) 4 """returnlambda y: search(lambda x:f(x)==y) # 返回某个完全平方数的平方根
#include<stdio.h>intmain(){ // 一个整形指针int a =1025; // 在二进制中为四字节:00000000 00000000 00000100 00000001int* p; p =&a;printf("size of integer is %d\n", sizeof(int));printf("Address = %d, value = %d\n",p,*p); // 一个字符指针char*c; c = (char*)p; // 进行强制类型转换printf("size of integer is %d\n", sizeof(char)); // 由于char指针只有一个字节,则机器只看从右边开始的一个字节 即00000001printf("Address = %d, value = %d\n",c,*c); // 增加一个字节,则指针指向从右边开始的第一个字节 即00000100printf("Address = %d, value = %d\n",c+1,*(c+1)); // 一个void指针void*p0; p0 = p; // 不需要显式的类型转换 // 当p0没指向任何特定类型时,不能解引用printf("Address = %d, value = %d\n",p0,*p0); // 也不要进行算术运算printf("Address = %d%d\n",p0,p0+1);}
指针算术运算
对一个指针进行加1操作,相当于将该指针增加一个该指针数据类型所占字节数的字节数
例如对int *p=&a; p++;得到p的值为a的地址加4
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdio.h>intmain(){int a =10;int*p; p =&a; // 指针加法 : +1代表增加一个数据类型的字节数printf("Address p is %d\n",p); // p的地址printf("Value at address p is %d\n",*p); // p指向的值printf("size of integer is %d bytes\n", sizeof(int)); // int类型所占的字节数printf("Address p+1 is %d\n",p+1); // p+1指向的地址printf("Value at address p+1 is %d\n",*(p+1)); // p+1指向的值(垃圾值)}
#include<stdio.h>voidf(intx){printf("Address of x in f is:%d\n",&x); x=x+1;}intmain(){int x=10;f(x);printf("Address of x in main is:%d\n",&x);return0;}
#include<stdio.h>#include<stdlib.h>// 现在该函数返回的是一个int类型的指针,即c的地址voidprint(){printf("hello world");}int*add(int*a,int*b){int c = (*a)+(*b);return&c;}intmain(){int x =2,y=4;int* p =add(&x,&y);print();printf("sum=%d",*p);}