Jaccorot 日事日毕日清日高

ARTS-003

2020-06-12

一、Algorithm

random

10个小球,随机分到12个盒子里,求恰好10个盒子都为空的概率。要求用Python程序模拟十万次,暴力求出该概率。

# 直接计算概率: 10个小球,随机分到12个盒子。每个小球都有12个可能,事件总数是12^10。事件“恰好10个盒子为空”意味着:将10个球随机分到12个盒子的两个盒子中(这两个盒子不能有任何一个为空)。C(12,2)决定是哪两个盒子不为空;将10个球分到两个盒子中,有2^10种可能(不能确保两个盒子均为非空的),需要再减去两个盒子中有一个盒子为空的情况(共2种可能,设盒子为A、B,即“A为空并且B非空”和“A非空并且B为空”这两种可能)。所以最后结果为C(12,2)*(2^10-2)/(12^10)。
import random
random.seed(4)
n=100000
cnt=0
for i in range(n): 
    arr=[0]*12
    for j in range(10):
        rnd=random.randint(0,11)
        arr[rnd]+=1
    cnt0=0 
    for j in range(12):
        if arr[j]==0:
            cnt0+=1
    if cnt0==10:
        cnt+=1
print(cnt/n)

二分查找

二分查找元素在有序数组中的位置,如果不存在,输出-1,如果存在,输出下标(存在多个,输出下标最小的)。

#比如4,4,5,6,7里面找4,我的代码返回了1,本该返回0。
def getPos(arr,val):
    n=len(arr)
    l=0
    r=n-1
    while l<r:
        mid=(l+r)//2
        if arr[mid]>=val:
            r=mid
        else:
            l=mid+1
    if arr[l]!=val:
        return -1
    return l

二、Review

PMP申请报名,全程英文,要求写三个项目小作文,捡了捡扔下去的英文

The goal of the project is to promote R&D management best practices through selection and refinement.

As a project manager,my main responsibility is to define the high-level scope of the project in the initiating process and to develop a project schedule in the planning process.Execute the tasks as defined in the project plan in the executing process.Communicate project status to stakeholders in the controlling and monitoring process.Distribute the final project report in the closing process.

Finally,the project was completed on schedule, I learned to how to communnicate more effciently.







The goal of the project is to develop an APP by using H5 editor for business promotion with mobile equipments.

As a project manager,my main responsibility is to perform project assessment in the initiating process.Create the work breakdown structure in the planning process.Implement the quality management plan in the executing process.Ensure that project deliverables conform to the quality standards in the controlling and monitoring process.Obtain final acceptance of the project delivables in the closing process.

Finally,customers accepted the project results and expressed their satisfaction,I learned to summarize the mistakes in project work.




The goal of the project is to implement a new website as a editorable SaaS platform with HTML5.

As a project manager,my main responsibility is to assumptions and constraints in the initiating process and to develop a project schedule in the planning process.Obtain and manage project resources in the executing process.Manage changes to the project scope,schedule, and costs in the controlling and monitoring process.Measure customer satisfaction in the closing process.

Finally,the project is delivered to the customer at the specified time,I learned to better response to risks in projects.

三、Tip

python:f操作

我们可以在字符串前面加上f表示格式操作,并且在格式操作当中也支持运算,比如可以嵌套上len函数等。不过要注意,只有Python3.6以上的版本支持f操作。

  • You can also format using f-strings or formatted string literals (in Python 3.6+)
  • You can basically put any Python statement inside the braces and it will be output in the string.
name = "Reiko"
f"She said her name is {name}." # => "She said her name is Reiko"

f"{name} is {len(name)} characters long." # => "Reiko is 5 characters long."

python: dict setdefault

setdefault方法可以为不存在的key插入一个value,如果key已经存在,则不会覆盖它:

filled_dict.setdefault("five", 5)  # filled_dict["five"] is set to 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] is still 5

python: dict 解压

Python3.5以上的版本支持使用**来解压一个dict:

{'a': 1, **{'b': 2}}  # => {'a': 1, 'b': 2}
{'a': 1, **{'a': 2}}  # => {'a': 2}

python: set

set是用来存储不重复元素的容器,当中的元素都是不同的,相同的元素会被删除。我们可以通过set(),或者通过{}来进行初始化。注意当我们使用{}的时候,必须要传入数据,否则Python会将它和dict弄混。

empty_set = set()

some_set = {1, 1, 2, 2, 3, 4}  # some_set is now {1, 2, 3, 4}

#set当中的元素也必须是不可变对象,因此list不能传入set。

invalid_set = {[1], 1}  # => Raises a TypeError: unhashable type: 'list'
valid_set = {(1,), 1}

#可以调用add方法为set插入元素:

filled_set = some_set
filled_set.add(5)  # filled_set is now {1, 2, 3, 4, 5}
# Sets do not have duplicate elements
filled_set.add(5)  # it remains as before {1, 2, 3, 4, 5}

#set还可以被认为是集合,所以它还支持一些集合交叉并补的操作。
# Do set intersection with &
# 计算交集
other_set = {3, 4, 5, 6}
filled_set & other_set  # => {3, 4, 5}

# Do set union with |
# 计算并集
filled_set | other_set  # => {1, 2, 3, 4, 5, 6}

# Do set difference with -
# 计算差集
{1, 2, 3, 4} - {2, 3, 5}  # => {1, 4}

# Do set symmetric difference with ^
# 这个有点特殊,计算对称集,也就是去掉重复元素剩下的内容
{1, 2, 3, 4} ^ {2, 3, 5}  # => {1, 4, 5}

# set还支持超集和子集的判断,我们可以用大于等于和小于等于号判断一个set是不是另一个的超集或子集:
# Check if set on the left is a superset of set on the right
{1, 2} >= {1, 2, 3} # => False

# Check if set on the left is a subset of set on the right
{1, 2} <= {1, 2, 3} # => True

# 和dict一样,我们可以使用in判断元素在不在set当中。用copy可以拷贝一个set。
# Check for existence in a set with in
2 in filled_set   # => True
10 in filled_set  # => False

# Make a one layer deep copy
filled_set = some_set.copy()  # filled_set is {1, 2, 3, 4, 5}
filled_set is some_set        # => False

python: enumerate

如果使用enumerate函数,可以同时迭代一个list的下标和元素:

#To loop over a list, and retrieve both the index and the value of each item in the list
#prints:
#    0 dog
#    1 cat
#    2 mouse
animals = ["dog", "cat", "mouse"]
for i, value in enumerate(animals):
    print(i, value)

python: 可迭代对象

凡是可以使用in语句来迭代的对象都叫做可迭代对象,它和迭代器不是一个含义。 当我们调用dict当中的keys方法的时候,返回的结果就是一个可迭代对象。

# Python offers a fundamental abstraction called the Iterable.
# An iterable is an object that can be treated as a sequence.
# The object returned by the range function, is an iterable.

filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable)  # => dict_keys(['one', 'two', 'three']). This is an object that implements our Iterable interface.

# We can loop over it.
for i in our_iterable:
    print(i)  # Prints one, two, three

#我们不能使用下标来访问可迭代对象,但我们可以用iter将它转化成迭代器,使用next关键字来获取下一个元素。也可以将它转化成list类型,变成一个list。
# However we cannot address elements by index.
our_iterable[1]  # Raises a TypeError

# An iterable is an object that knows how to create an iterator.
our_iterator = iter(our_iterable)

# Our iterator is an object that can remember the state as we traverse through it.
# We get the next object with "next()".
next(our_iterator)  # => "one"

# It maintains state as we iterate.
next(our_iterator)  # => "two"
next(our_iterator)  # => "three"

# After the iterator has returned all of its data, it raises a StopIteration exception
next(our_iterator)  # Raises StopIteration

# We can also loop over it, in fact, "for" does this implicitly!
our_iterator = iter(our_iterable)
for i in our_iterator:
    print(i)  # Prints one, two, three

# You can grab all the elements of an iterable or iterator by calling list() on it.
list(our_iterable)  # => Returns ["one", "two", "three"]
list(our_iterator)  # => Returns [] because state is saved

摘自万字干货,Python语法大合集,一篇文章带你入门

四、Share

管理大师戴明说过,85%的质量管理来自管理者,项目经理需要积极作为以确保团队成员遵守项目管理相关的各种规章制度以及流程,并以此为契机建立起“追求质量”的工作氛围。

Similar Posts

上一篇 ARTS-002

下一篇 ARTS-004