上午是一道困难题,但是题目难度实际上不高。本质上和之前周赛做的拿到题目是一样的,是一道有两个变量,可以通过自定义排序的思想解决,只不过第二层次的排序只要求获得最大收益值,所以使用heapq而不是再进行一次排序,heapq的时间复杂度普遍在logn.
然后是今天的讨论课讲的是高阶函数和嵌套函数的内容:
https://composingprograms.com/pages/16-higher-order-functions.html
首先是定义
assert的作用
断言函数是对表达式布尔值的判断,要求表达式计算值必须为真。可用于自动调试。如果表达式为假,触发异常;如果表达式为真,不执行任何操作。
高阶函数:一个函数可以作为参数传给另外一个函数,或者一个函数的返回值为另外一个函数(若返回值为该函数本身,则为递归),满足其一则为高阶函数。
函数嵌套: 在一个函数中定义了另外一个函数,当外部函数被调用的时候,内部函数都会被重新定义,如果内部函数不被返回,则外部函数执行结束后,内部函数对象就会被销毁
写的discussion:
- What are nested functions and higher-order functions?
A higher-order function is a function that takes a function as an argument or returns a function.
Nested functions: When another function is defined in one function when the external function is called, the internal function will be redefined. If the internal function is not returned, the internal function object will be destroyed after the external function is executed.
- Give a very simple example of a use of closures
A function and its environment variables together form a closure. In Python, the so-called closure is a function object that contains the values of environment variables. The value of the environment variable is stored in the closure attribute of the function object. For example, the following code:
def line_conf():
b = 15
def line(x):
return 2*x+b
return line # return a function object
b = 5
my_line = line_conf()
print(my_line.__closure__)
print('the innner variable of b store in the __closure__ is b=',my_line.__closure__[0].cell_contents)
print('the final answer is',my_line(5))
and the result is :
(<cell at 0x000001CB46E613A8: int object at 0x00007FF891F9A360>,)
the innner variable of b store in the __closure__ is b= 15
the final answer is 25
You can simply find that the inner variable store in closure.
Give a very simple example where either map, filter or reduce (or a combination of them) is more readable/intuitive/concise than using loops/recursion
Google has a paper named “MapReduce: Simplified Data Processing on Large Clusters” introduces the concept of Map and reduce firstly. In this paper, it states the map and reduce are the typical way to abstract algorithm. For example, how can we achieve the basic Built-in function int? if we use list, it will take about 20 lines. But if we use map and reduce, it is very simply:
from functools import reduce
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char2num(s):
return DIGITS[s]
def str2int(s):
return reduce(lambda x, y: x * 10 + y, map(char2num, s))
4. Describe, in no more than three sentences, what Functional Programming is.
Any functional programming has the following two characteristics: 1) the meaning of programs is centered around evaluation expressions rather than executing instructions. 2) functions are first-class, that is, functions are values which can be used in exactly the same ways as any other sort of value. In short, functional programming is an expression language. All functions can be assigned to variables and used as arguments for other functions.
- (Opinion, no right or wrong answer) Share some things you like/dislike about Functional Programming
Functional Programming is very cool! it is logical and hardly make bug. But most time, it will not have very high efficiency. For example, in the leetcode, many people use it to show the skill. but this code is hard to read.