一些初级python面试题

一些初级python面试题,这里简单罗列一下

# 反转字符串, 这个是最常见的
# 比如给一个字符串hello,将其反转为olleh
def reverse(str):
    alist = list(str)
    alist.reverse()
    new_str = ''.join(alist)
    return new_str

# 写一个方法打印如下内容,每一行打印5个
# server1, server2, server3, server4, server5(换行)
# server6, server7, server8, server9, server10(换行)
# server11, server12, server13, server14, server15(换行)
# server16, server17, server18, server19, server20(换行)
# ...
# server996, server997, server998, server999, server1000

#正确写法1
for i in range(1,1000):
    if (i % 5 == 1):
        a = i + 1
        b = i + 2
        c = i + 3
        d = i + 4
        print('server' + str(i) + ', ' + 'server' + str(a) + ', ' + 'server' + str(b) + ', ' + 'server' + str(c) + ', ' 'server' + str(d))

#正确写法2
for i in range(1,1004):
    if (i % 5 == 0):
        a = i - 4
        b = i - 3
        c = i - 2
        d = i - 1
        print('server' + str(a) + ', ' + 'server' + str(b) + ', ' + 'server' + str(c) + ', ' + 'server' + str(d) + ', ' 'server' + str(i))

#注意难点
#print语句每运行一次,都是另起一行,因此完全不必要写一个\n

#以下是错误写法
for i in range(1,1000):
    if (i % 5 == 0):
       print('server' + str(i) + '\n')
    else:
        print('server' + str(a) + ', ')
#这种写法的运行结果是
server1, 
server2, 
server3, 
server4, 
server5

server6, 
server7, 
server8, 
server9, 
server10

server11, 
server12, 
server13, 
server14, 
server15

server16,
......
# 给一个dir, 循环遍历该dir下面的所有文件(只列出文件, 不要文件夹).
import os
def get_file_names(dir):

    alist = os.listdir(dir)
    path = os.path.realpath(dir)
    result = []
    for i in alist:
        ipath = os.path.join(path,i)
        if os.isdir(ipath):
            #get_file_name(ipath)
            result.extend(get_file_name(ipath))
        else:
            result.append(ipath)
    return result 
# Given an array of integers, return indices of the two numbers
# such that they add up to a specific target.
# You may assume that each input would have exactly one solution.
# 给了一个全是数字的列表,现在让列表中任意2个数字相加, 得到一个指定target,
# 然后返回这2个数字的index
# Example:
# Given nums = [2, 7, 11, 15], target = 9,
#
# Because nums[0] + nums[1] = 2 + 7 = 9,
# return [0, 1].
def two_sum(nums, target):

    for a in (0,len(nums)):
        b = a + 1
        res1 = 0
        res2 = 0

        for i in (b,len(nums)):
            if ( nums[a] + nums[i] == target ):
                res1 = a
                res2 = i
    return [res1, res2]

##########################################################

2018.10.22补充:
假设有以下日志文件, 已知日志文件的最后一列是IP(但不知最后一列是第几列), 现在希望统计重复率最高的10个IP

[23/Nov/2017:12:09:47 +0800] "GET /wp-content/uploads/349.gif HTTP/1.1" 200 24549 - - 40.77.167.120
[23/Nov/2017:12:09:54 +0800] "GET /favicon.ico HTTP/1.1" 200 - - - 183.11.202.184
[23/Nov/2017:12:09:54 +0800] "GET /favicon.ico HTTP/1.1" 200 - - - 183.11.202.184
[23/Nov/2017:12:10:14 +0800] "GET /feed HTTP/1.1" 301 235 - - 35.187.34.70
[23/Nov/2017:12:10:15 +0800] "GET /feed HTTP/1.1" 200 35410 - - 35.187.34.70
[23/Nov/2017:12:10:45 +0800] "GET /archives/tag/%E6%84%9F%E6%82%9F/page/2 HTTP/1.1" 200 35046 - - 164.132.161.49
[23/Nov/2017:12:11:21 +0800] "GET /feed HTTP/1.1" 301 235 - - 35.189.242.65
[23/Nov/2017:12:11:23 +0800] "GET /feed HTTP/1.1" 200 35410 - - 35.189.242.65
[23/Nov/2017:12:11:30 +0800] "GET /archives/412 HTTP/1.1" 200 21010 - - 58.250.125.83
[23/Nov/2017:12:11:40 +0800] "GET /archives/1120 HTTP/1.1" 200 25351 - - 217.182.132.240
[23/Nov/2017:12:12:04 +0800] "POST /wp-login.php HTTP/1.1" 200 3248 - - 185.92.73.31
[23/Nov/2017:12:12:04 +0800] "POST /wp-login.php HTTP/1.1" 200 3248 - - 185.92.73.31
[23/Nov/2017:12:12:04 +0800] "POST /wp-login.php HTTP/1.1" 200 3248 - - 185.92.73.31
[23/Nov/2017:12:12:04 +0800] "GET /wp-content/uploads/323.jpg HTTP/1.1" 301 257 - - 207.46.13.158
[23/Nov/2017:12:12:06 +0800] "GET /wp-content/uploads/323.jpg HTTP/1.1" 200 44140 - - 207.46.13.158
[23/Nov/2017:12:11:21 +0800] "GET /archives/1812 HTTP/1.1" 200 32201 - - 58.250.125.86
[23/Nov/2017:12:12:33 +0800] "GET /feed HTTP/1.1" 301 235 - - 104.199.87.90
[23/Nov/2017:12:12:35 +0800] "GET /feed HTTP/1.1" 200 35410 - - 104.199.87.90
[23/Nov/2017:12:12:39 +0800] "GET /wp-login.php HTTP/1.1" 301 243 - - 61.68.147.138
[23/Nov/2017:12:12:40 +0800] "GET /wp-login.php HTTP/1.1" 200 2279 - - 61.68.147.138
[23/Nov/2017:12:12:41 +0800] "GET / HTTP/1.1" 301 231 - - 61.68.147.138
[23/Nov/2017:12:12:41 +0800] "GET / HTTP/1.1" 200 41971 - - 61.68.147.138
[23/Nov/2017:12:12:46 +0800] "GET /feed HTTP/1.1" 301 235 - - 47.89.44.232
[23/Nov/2017:12:12:47 +0800] "GET /feed HTTP/1.1" 304 - - - 47.89.44.232
[23/Nov/2017:12:12:47 +0800] "GET /www.zhukun.net/archives/4856 HTTP/1.1" 301 - - - 220.181.108.101
[23/Nov/2017:12:12:48 +0800] "GET /archives/7636 HTTP/1.1" 200 21802 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /archives/4856 HTTP/1.1" 200 38867 - - 220.181.108.107
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/plugins/wp-pagenavi/pagenavi-css.css?ver=2.70 HTTP/1.1" 200 374 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/themes/twentyeleven/style.css HTTP/1.1" 200 59086 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/scripts/shCore.js?ver=3.0.9b HTTP/1.1" 200 23934 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/scripts/shBrushPlain.js?ver=3.0.9b HTTP/1.1" 200 821 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-includes/js/comment-reply.min.js?ver=4.9 HTTP/1.1" 200 1078 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-includes/js/wp-embed.min.js?ver=4.9 HTTP/1.1" 200 1398 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/plugins/akismet/_inc/form.js?ver=3.3.4 HTTP/1.1" 200 700 - - 58.48.189.110[23/Nov/2017:12:12:48 +0800] "GET /wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/styles/shCore.css?ver=3.0.9b HTTP/1.1" 200 6786 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/themes/twentyeleven/images/headers/wheel.jpg HTTP/1.1" 200 108002 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/styles/shThemeDjango.css?ver=3.0.9b HTTP/1.1" 200 3098 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-includes/js/wp-emoji-release.min.js?ver=4.9 HTTP/1.1" 200 11915 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/themes/twentyeleven/images/search.png HTTP/1.1" 200 440 - - 58.48.189.110
[23/Nov/2017:12:12:48 +0800] "GET /wp-content/uploads/355.png HTTP/1.1" 200 35906 - - 58.48.189.110
[23/Nov/2017:12:12:49 +0800] "GET /favicon.ico HTTP/1.1" 200 - - - 58.48.189.110
[23/Nov/2017:12:12:49 +0800] "GET /wp-login.php HTTP/1.1" 301 243 - - 179.53.200.239
[23/Nov/2017:12:12:51 +0800] "GET /wp-login.php HTTP/1.1" 200 2279 - - 179.53.200.239
[23/Nov/2017:12:12:51 +0800] "GET / HTTP/1.1" 301 231 - - 179.53.200.239
[23/Nov/2017:12:12:51 +0800] "GET / HTTP/1.1" 200 41970 - - 179.53.200.239
[23/Nov/2017:12:13:01 +0800] "GET /wp-content/uploads/140.jpg HTTP/1.1" 301 257 - - 40.77.167.120
[23/Nov/2017:12:13:01 +0800] "GET /feed HTTP/1.1" 304 - - - 47.89.44.232
[23/Nov/2017:12:13:06 +0800] "GET /wp-content/uploads/140.jpg HTTP/1.1" 200 117347 - - 40.77.167.120
[23/Nov/2017:12:13:08 +0800] "-" 408 -   - - 58.48.189.110
[23/Nov/2017:12:13:27 +0800] "GET /archives/1812 HTTP/1.1" 200 32205 - - 58.250.125.86
[23/Nov/2017:12:13:46 +0800] "GET /feed HTTP/1.1" 301 235 - - 35.190.203.87
[23/Nov/2017:12:13:47 +0800] "GET /feed HTTP/1.1" 200 35410 - - 35.190.203.87
[23/Nov/2017:12:13:52 +0800] "GET /archives/7589/comment-page-1 HTTP/1.1" 301 259 - - 123.125.71.57
[23/Nov/2017:12:14:07 +0800] "GET /wp-content/uploads/yupoo/14283559605b/lvtwgnoz.jpg HTTP/1.1" 301 281 - - 207.46.13.158
[23/Nov/2017:12:14:08 +0800] "GET /wp-content/uploads/yupoo/14283559605b/lvtwgnoz.jpg HTTP/1.1" 200 20881 - - 207.46.13.158
[23/Nov/2017:12:14:42 +0800] "GET /archives/7589/comment-page-1 HTTP/1.1" 301 259 - - 220.181.108.97
[23/Nov/2017:12:14:43 +0800] "GET / HTTP/1.1" 200 41970 - - 207.46.13.158
[23/Nov/2017:12:14:43 +0800] "GET /archives/7589/comment-page-1 HTTP/1.1" 301 259 - - 123.125.71.78
[23/Nov/2017:12:14:46 +0800] "GET /archives/7589/comment-page-1 HTTP/1.1" 301 259 - - 220.181.108.174
[23/Nov/2017:12:14:51 +0800] "GET /archives/156/comment-page-1 HTTP/1.1" 200 20088 - - 217.182.132.150
[23/Nov/2017:12:14:58 +0800] "GET /feed HTTP/1.1" 301 235 - - 35.190.207.132
[23/Nov/2017:12:14:59 +0800] "GET /feed HTTP/1.1" 200 35410 - - 35.190.207.132
[23/Nov/2017:12:15:15 +0800] "GET /favicon.ico HTTP/1.1" 301 242 - - 49.66.90.120
[23/Nov/2017:12:15:15 +0800] "GET /favicon.ico HTTP/1.1" 200 - - - 49.66.90.120
[23/Nov/2017:12:15:24 +0800] "GET / HTTP/1.1" 301 231 - - 207.46.13.139
[23/Nov/2017:12:16:09 +0800] "GET /feed HTTP/1.1" 301 235 - - 104.199.87.90
[23/Nov/2017:12:16:10 +0800] "GET /feed HTTP/1.1" 200 35410 - - 104.199.87.90
[23/Nov/2017:12:16:20 +0800] "GET /archives/8024?share=facebook HTTP/1.1" 200 20669Terminated - - 40.77.167.120
[17/Nov/2017:13:56:29 +0800] "GET /feed HTTP/1.1" 301 235 - - 35.189.242.65
[17/Nov/2017:13:57:11 +0800] "GET / HTTP/1.1" 301 231 - - 106.121.56.179
[17/Nov/2017:13:57:31 +0800] "GET /wp-login.php HTTP/1.1" 301 243 - - 27.63.157.250
[17/Nov/2017:13:57:39 +0800] "GET /wp-login.php HTTP/1.1" 301 243 - - 27.63.157.250
[17/Nov/2017:13:57:47 +0800] "GET / HTTP/1.1" 301 231 - - 27.63.157.250
[17/Nov/2017:13:58:36 +0800] "GET /robots.txt HTTP/1.1" 301 241 - - 123.126.113.109
[17/Nov/2017:13:58:49 +0800] "GET /feed HTTP/1.1" 301 235 - - 35.187.96.204
[17/Nov/2017:14:00:29 +0800] "GET /archives/6096 HTTP/1.1" 301 244 - - 123.126.113.109

Shell写法

awk '{ print $NF } ' 1.log | sort | uniq -c | sort -nr | head -10

注意知识点:
awk里的$NF即是指最后一列;
sort命令里, -n表示按数字排序, -r表示按从大到小的方式排序(即最大的在前), 如果不加-r会导致从数量最少的开始排序

python写法

#!/usr/bin/python
#-*-coding:utf-8-*-
a = {}

with open("1.log") as f:
    for line in f.readlines():
        #print line.split(" ")[-1],
        ip = line.split(" ")[-1].rstrip()    #使用list[-1]取列表最后一个元素
        #print ip
        if a.has_key(ip):
            a[ip] = a[ip] + 1
        else:
            a[ip] = 1    #dict没有extend和append等方法,直接赋值即是向dict中添加元素

#print a                 #此时的a还是一个dict
#print a.items()         #调用.items()方法以后, dict被转化成了一个[("ip1":num1),("ip2":num2),("ip3", num3)]格式的列表
#print sorted(a.items(),key=lambda x:x[1],reverse=True)    #使用lamdba函数按value的大小进行排产
alist = sorted(a.items(),key=lambda x:x[1],reverse=True)
#print alist              #排完序以后,仍是[("ip1":num1),("ip2":num2),("ip3", num3)]格式的列表

for i in range(0,10):    #注意range函数的用法,range(0,10)表示从0到9
    print alist[i][0]

一些初级python面试题》上有1条评论

发表评论

电子邮件地址不会被公开。