相关链接:
- 2017年度python超纲20问(一)
- 2017年度python超纲20问(二)
我知道,我知道大家技术面的时候问的大多都是相关于项目的问题,我也是,但是到三面时候高层会心血来潮问你个极其底层的问题(没错是我本人了),这次与底层关系不大,是一些笔试题,难度均不是很大,不过善始善终嘛。
What happens when you assign the value of one variable to another variable in Python?
在Python中,当你用一个变量给另外一个变量赋值时会发生什么?
>>> spam = 42
>>> cheese = spam
>>> spam = 100
>>> spam
100
>>> cheese
42
不知道大家C语言基础怎么样,其实完全可以当做c的指针来理解。
之前文章中说过些概念:
- Python所有对象都在heap上,因此每个对象都是指针。
- Pytohn的数字也是对象,你定义的整形1实际上是对对象1的引用。而且Python使用引用计数,所以当一个对象放在一个新的容器,它的引用计数必须递增,所以pytohn不仅仅是复制引用。
在这里顺序是这样:
>>> spam = 42 # spam 指向对象42
>>> cheese = spam # spam 将值(对象42的地址)赋值给 cheese
# cheese 指向spam所指向的对象(也就是42)
>>> spam = 100 # spam 指向对象100
>>> spam # spam 指向对象100
100
>>> cheese # cheese 指向对象100
42
指针赋值不改变所指对象的内容。
Understanding *x ,= list
如何理解语句 *x ,= list
首先这是python3语法,如果你用的python2瞄一眼就好了,嗯...其实python3瞄一眼也好了,今天的题目真的好简单。
其实就是语法糖,觉得跟x = p[:]
差不多。看代码:
>>> p = [1, 2, 3]
>>> *x, = p
>>> x == p
True
>>> x is p
False
>>>y, *x = p
>>>y
1
>>>*x
[2, 3]
>>> x = p # 直接赋值 浅拷贝
>>> x == p
True
>>> x is p
True
关于图的算法题 难度低于acm
Problem
Say you have two lists A = [a_1, a_2, ..., a_n] and B = [b_1, b_2, ..., b_n] of integers. We say A is potentially-divisible by B if there is a permutation of B that makes a_i divisible by b_i for all i. The problem is then: is it possible to reorder (i.e. permute) B so that a_i is divisible by b_i for all i? For example, if you have
A = [6, 12, 8] B = [3, 4, 6]
Then the answer would be True, as B can be reordered to be B = [3, 6, 4] and then we would have that a_1 / b_1 = 2, a_2 / b_2 = 2, and a_3 / b_3 = 2, all of which are integers, so A is potentially-divisible by B.
As an example which should output False, we could have:
A = [10, 12, 6, 5, 21, 25] B = [2, 7, 5, 3, 12, 3]
The reason this is False is that we can't reorder B as 25 and 5 are in A, but the only divisor in B would be 5, so one would be left out.
Approach
Obviously the straightforward approach would be to get all the permutations of B and see if one would satisfy potential-divisibility, something along the lines of:
import itertools def is_potentially_divisible(A, B): perms = itertools.permutations(B) divisible = lambda ls: all( x % y == 0 for x, y in zip(A, ls)) return any(divisible(perm) for perm in perms)
Question
What is the fastest way to know if a list is potentially-divisible by another list? Any thoughts? I was thinking if there's is a clever way to do this with primes, but I couldn't come up with a solution.
就关于图的算法题,比大学时候acm简单太多,不翻译了啊,反正愿意看这题的一定觉得算法题是英文才亲切吧,就有兴趣的看一下代码,逻辑不通的私信我帮你讲解~
import networkx as nx
def is_potentially_divisible(multiples, divisors):
if len(multiples) != len(divisors):
return False
g = nx.Graph()
g.add_nodes_from([('A', a, i) for i, a in enumerate(multiples)], bipartite=0)
g.add_nodes_from([('B', b, j) for j, b in enumerate(divisors)], bipartite=1)
edges = [(('A', a, i), ('B', b, j)) for i, a in enumerate(multiples)
for j, b in enumerate(divisors) if a % b == 0]
g.add_edges_from(edges)
m = nx.bipartite.maximum_matching(g)
return len(m) // 2 == len(multiples)
print(is_potentially_divisible([6, 12, 8], [3, 4, 6]))
# True
print(is_potentially_divisible([6, 12, 8], [3, 4, 3]))
# True
print(is_potentially_divisible([10, 12, 6, 5, 21, 25], [2, 7, 5, 3, 12, 3]))
# False
真的比acm简单太多,嗯...假如你是学生在看我文章,恰巧你准备参加acm之类的比赛,那你加油~过来人跟你说,这类比赛真的对你有帮助。
不行了,怕是不能善始善终了,剩下的问题就真的很无聊,比如:
- 增加Python代码逻辑层数的上限个数
- python是有逻辑层数上限20没错了,但是谁会超过20啊...
- 把一个变量赋值给自己,是什么作用?
- 其实就是局部变量给同名全局变量,提问的人没给上下文,大家的回答一顿瞎猜,直到提问的人贴了代码
- ...
昨天失眠,今天困的头疼,抽空在休息时间把文章写了出来,如果这次文章哪里不到位大家评论指正就好了,谢谢包涵。
之后的文章将说下python实现异步爬虫,这两天上网看了下,发现网上这一类文档不算写的太明白,大部分代码一贴完事了,我准备自己梳理下写下来,如果有兴趣可以关注我,这周内更新(吧)。
新的一周,生活愉快~