Skip to content

Commit cb1ff50

Browse files
committed
黄哥所写的python代码
1 parent 6760117 commit cb1ff50

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

python/python_answer_array.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#用python写一个程序,找出数组中差值为K的数共有几对
2+
##知乎上有人问“用python写一个程序,找出数组中差值为K的数共有几对”
3+
4+
[点击黄哥python培训试看视频播放地址](https://github.com/pythonpeixun/article/blob/master/python_shiping.md)
5+
6+
[黄哥python远程视频培训班](https://github.com/pythonpeixun/article/blob/master/index.md)
7+
8+
9+
10+
#coding:utf-8
11+
"""
12+
13+
用python写一个程序,找出数组中差值为K的数共有几对
14+
15+
16+
17+
示例:
18+
19+
差值k=4 and 数组是[7, 6, 23,19,10,11, 9, 3, 15]
20+
21+
这样的结果是(7,11) (7,3) (6,10) (19,23) (15,19) (15,11) 共6对
22+
23+
24+
25+
从标准输入读入两行数据
26+
27+
5 2
28+
29+
1 5 3 4 2
30+
31+
32+
33+
第一行代表N和K, N是数组是一共有多少数字,K是所要求的差值
34+
35+
第二是数组,空白分格
36+
37+
38+
39+
输出到标准输出
40+
41+
42+
43+
44+
45+
Sample Input #00:
46+
47+
5 2
48+
49+
1 5 3 4 2
50+
51+
Sample Output #00:
52+
53+
3
54+
55+
Sample Input #01:
56+
57+
10 1
58+
59+
363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793
60+
61+
Sample Output #01:
62+
63+
0
64+
65+
"""
66+
67+
68+
69+
def diff_of_element_list(lst, k):
70+
71+
"""黄哥python远程视频培训班
72+
73+
https://github.com/pythonpeixun/article/blob/master/index.md
74+
75+
76+
77+
黄哥python培训试看视频播放地址
78+
79+
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
80+
81+
"""
82+
83+
newlst = [i + k for i in lst]
84+
85+
return len(set(lst) & set(newlst))
86+
87+
88+
89+
if __name__ == '__main__':
90+
91+
lst = [7, 6, 23,19,10,11, 9, 3, 15]
92+
93+
k = 4
94+
95+
print(diff_of_element_list(lst, k))
96+
97+
lst = [1,5, 3, 4, 2]
98+
99+
k = 2
100+
101+
print(diff_of_element_list(lst, k))
102+
103+
104+
105+
n, k = input("please input n and k:\n").split()
106+
107+
lst = input("plesae input {0} number:\n".format(n)).split()
108+
109+
lst = [int(i) for i in lst]
110+
111+
print(diff_of_element_list(lst, int(k)))

0 commit comments

Comments
 (0)