一:题目:

使用python打印一颗圣诞树

要求:使用最基本的语法实现

#!/usr/bin/env python
#coding:utf-8
height = 5
stars = 1
for i in range(height):
print((' ' * (height - i)) + ('*' * stars))
stars += 2
print((' ' * height) + '|')

二:题目:

使用python3从N个数组中,进行排列组合,打印排列后的所有列表。

要求:尝试使用笛卡尔算法

#!/usr/bin/env python
#coding:utf-8
class Cartesian():
# 初始化
def __init__(self, datagroup):
self.datagroup = datagroup
# 二维数组从后往前下标值
self.counterIndex = len(datagroup)-1
# 每次输出数组数值的下标值数组(初始化为0)
self.counter = [0 for i in range(0, len(self.datagroup))]
# 计算数组长度
def countlength(self):
i = 0
length = 1
while(i < len(self.datagroup)):
length *= len(self.datagroup[i])
i += 1
return length
# 递归处理输出下标
def handle(self):
# 定位输出下标数组开始从最后一位递增
self.counter[self.counterIndex]+=1
# 判断定位数组最后一位是否超过长度,超过长度,第一次最后一位已遍历结束
if self.counter[self.counterIndex] >= len(self.datagroup[self.counterIndex]):
# 重置末位下标
self.counter[self.counterIndex] = 0
# 标记counter中前一位
self.counterIndex -= 1
# 当标记位大于等于0,递归调用
if self.counterIndex >= 0:
self.handle()
# 重置标记
self.counterIndex = len(self.datagroup)-1
# 排列组合输出
def assemble(self):
length = self.countlength()
i = 0
while(i < length):
attrlist = []
j = 0
while(j
attrlist.append(self.datagroup[j][self.counter[j]])
j += 1
print(attrlist)
self.handle()
i += 1
if __name__ == "__main__":
# 构造二维数组
datagroup = [['aa1', ], ['bb1', 'bb2'], ['cc1', 'cc2', 'cc3']]
# 创建cartesian对象
cartesian = Cartesian(datagroup)
cartesian.assemble()

三:题目:

使用python把每隔一分钟访问200次的IP,加到黑名单。

要求:每隔一分钟读取一下日志文件,把统计到的Ip添加到黑名单。

import time
pin = 0
while True:
ips = []
fr = open('assce.log')
fr.seek(pin)
for line in fr:
ip = line.split()[0] # 因为日志文件中每行的首个字符串是ip,与后面字符之间的分割是符号空格,所以用split()分割后,返回的list中第一个值就是ip地址,取【0】
ips.append(ip)
new_ips = set(ips) # 转换为集合
for new_ip in new_ips:
if ips.count(new_ip) > 200:
print('加入黑名单的ip是:%s' % new_ip)
pin = fr.tell() # 记录读完的指针位置
time.sleep(60)

四:题目:

一辆卡车违反交通规则,撞人后逃跑.现场有三人目击事件,但都没有记住车号,只记下车号的一些特征.

甲说:牌照的前两位数字是相同的;

乙说:牌照的后两位数字是相同的,但与前两位不同;

丙是数学家,他说:四位的车号所构成的数字正好等于某一个整数的平方.

请根据以上线索求出车号.

提示:四位整数中的完全平方数 的取值范围:32的平方—99的平方.

#!/usr/bin/env python
#coding:utf-8
from math import sqrt
for i in range(1,10):
for j in range(1,10):
number = 1000 * i + 100 * i + 10 * j + j
if sqrt(number)==int(sqrt(number)):
print(number)

五:题目:

要求对目录下的文件进行增和删除的监控,如果有增和删打印出来,并打印文件名。

要求:使用python3使用最简单的方法。

#!/usr/bin/env python
#coding:utf-8
import os, time
path_to_watch = "."
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
time.sleep (10)
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
removed = [f for f in before if not f in after]
if added: print("Added: ", ", ".join (added))
if removed: print("Removed: ", ", ".join (removed))
before = after

六:题目:

使用python统计一下ip以及出现的次数。

要求:内存比较小,所以不能在内存里计算。

import re,time
def mail_log(file_path):
global count
log=open(file_path,'r')
C=r'\.'.join([r'\d{1,3}']*4)
find=re.compile(C)
count={}
for i in log:
for ip in find.findall(i):
count[ip]=count.get(ip,1)+1
if __name__ == '__main__':
print(time.clock())
num=0
mail_log(r'assce.log')
R=count.items()
for i in R:
if i[1]>0: #提取出现次数大于0的IP
print(i)
num+=1
print('符合要求数量:%s耗时(%s)'%(num,time.clock()))

实战100例,精彩后敬请期待....想提前看到的