ARTS第六周

Algorithm

Remove Element

Given an array nums and a value val, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Answer
1
2
3
4
5
6
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
for i in range(len(nums)-1, -1, -1):
if nums[i] == val:
nums.pop(i)
return len(nums)

Review

https://towardsdatascience.com/5-advanced-features-of-python-and-how-to-use-them-73bffa373c84

文章介绍Python的五个高级特性,分别是 Lambda、Maps、Filtering、Itertools、Generators。
Lambda可定义匿名函数,适用于简单函数,无需定义函数名称,使用方便。

map()是python的内置函数,你可以通过提供参数列表(单个或多个列表)调用某个函数。
filter()与map()类似,它会返回使调用函数返回True的参数列表。
Itertools模块中zip_longest()将两个列表合并为元组列表;count()返回从0开始往后递增的迭代器;groupby()可以列表中相邻的相同元素进行分组统计。

Generators可以极大节省内存空间。

Tip

近期项目编写单元测试的实践及总结:
https://jingjing0506.github.io/2019/04/26/Python%E7%BC%96%E5%86%99%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E5%AE%9E%E8%B7%B5/

Share

https://uwsgi-docs.readthedocs.io/en/latest/ThingsToKnow.html

使用uwsgi须知,最佳实践的建议。由于平时使用uwsgi的场景比较简单,所以涉及的配置比较少。但是在平常使用过程(nginx+uwsgi+flask)中,会出现请求长时间未响应的情况。该情况下,网络、CPU、内存使用都不高,还不清楚具体耗时出现在哪个环节。后期准备通过对uwsgi进行配置更改,进行压力测试,测试是否是因为uwsgi配置不合理造成的。

根据上面的建议,平时项目使用过程中较受用的包括:

  • 使用uwsgitop对uwsgi进行监控。
  • uwsgi配置参数processes、threads的设置没有统一的设置建议。它的配置更多依赖于应用和系统。你需要通过监控自己的服务,不断调整processes、threads的配置值,以便获得最佳配置。uwsgitop是你获得最佳值的很好辅助工具。

uwsgitop官方文档: https://github.com/xrmx/uwsgitop