Jamie's Blog

Talk is cheap, show me the code.

刷题 hackerrank Python 部分,多种解答方式和知识点归纳

Print Function

https://www.hackerrank.com/challenges/python-print/problem

打印出123…N 之间的整数

方法一 用 for 循环依次打印出

1
2
3
n = 3
for i in range(1,n+1):
print(i,end='')
123

方法二 用 print 内置函数的参数

1
2
n=3
print(*range(1, n+1), sep='')
123

print()函数


知识点 print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

objects – object 复数形式,表示可以一次输出多个对象。输出多个对象时,需要用 , 分隔。

sep – 用来间隔多个对象,默认值是一个空格。

end – 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符串。

file – 要写入的文件对象。

*objects – 表示可以用可变参数

在函数调用时,在 str/list/tuple/range 参数前加 *,意思是把 str/list/tuple/range 序列中的元素一个一个传到函数里面


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = '123456'
b = ['1','2','3','4','5','6']
c = ('1','2','3','4','5','6')
d = range(1,7)

# 打印出来的元素之间有空格
print(*a)
print(*b)
print(*c)
print(*d)

print('ju','p','t','er',sep='-')
print(*['ju','p','t','er'],sep='-')
print(*('ju','p','t','er'),sep='-')
print(*(range(1,7)),sep='-')


1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
ju-p-t-er
ju-p-t-er
ju-p-t-er
1-2-3-4-5-6


# Lists

https://www.hackerrank.com/challenges/python-lists/problem

根据输入执行相应的命令

方法一 if…elif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 正常思路

L = []
N = int(input())

for i in range(N):
c = input().split(' ')
for i in range(1,len(c)):
c[i] = int(c[i])
command = c[0]

if command == 'insert':
L.insert(c[1],c[2])
elif command == 'print':
print(L)
elif command == 'remove':
L.remove(c[1])
elif command == 'append':
L.append(c[1])
elif command == 'extend':
L.extend(c[1:])
elif command == 'sort':
L.sort()
elif command == 'pop':
L.pop()
elif command == 'reverse':
L.reverse()


2
append 9
print
[9]


方法二 eval()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#简化

n = int(input())
l = []
for i in range(n):
c = input().split()
command = c[0]
if len(c) > 0:
args = c[1:]
print(args)
if command !="print":
command += "("+ ",".join(args) +")"
eval("l."+command)
else:
print(l)


2
append 9
[‘9’]
print
[]
[9]


join() 函数

知识点 ‘sep’.join(seq)

将字符串、列表、元组、字典中的元素以指定的字符(分隔符)连接生成一个新的字符串,

sep – 分隔符。可以为空

seq – 要连接的元素序列:字符串、列表、元组、字典。

以sep作为分隔符,将seq所有的元素合并成一个新的字符串。返回一个以分隔符sep连接各个元素后生成的字符串

str.join(sequence) 函数中的 sequence 中的元素必须的字符串,否则会报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#seq内元素只能是可迭代字符串类型,数字不可以

#对字符串进行操作
seq1 = 'HelloWorldIamaprogrammer'
print((' ').join(seq1))

#对列表进行操作
seq2 = ['Hello','World','I','am','a','programmer']
print((' ').join(seq2))

#对元祖进行操作
seq3 = ('Hello','World','I','am','a','programmer')
print((':').join(seq3))

#对字典进行操作
seq4 = {'hello':1,'world':2,'I':3,'am':4,'a':5,'programmer':6}
print(('-').join(seq4))


#seq内元素数量的不同,结果不同

#元素有2或多个个

num3 = ['9','9','9']
print((',').join(num3))

#元素只有1个,结果没有分隔符

num1 =['9']
print((',').join(num1))

#应用:合并参数

print('('+','.join(['0', '5'])+')')
print('('+','.join(['0'])+')')
H e l l o W o r l d I a m a p r o g r a m m e r
Hello World I am a programmer
Hello:World:I:am:a:programmer
hello-world-I-am-a-programmer
9,9,9
9
(0,5)
(0)

eval()函数


知识点 eval()函数

将字符串str当成有效的表达式来求值并返回计算结果。所以,结合math当成一个计算器很好用。


1
2
3
4
5
6
7
8
9
10
11
12
#计算字符串中有效的表达式
print(eval('pow(2,2)'))
print(eval('2+2'))

#将字符串转成相应的对象(如list、tuple、dict和string之间的转换)
a="[[1,2], [3,4], [5,6], [7,8], [9,0]]"
b="(1,2,3,4)"
c="{1:'xx',2:'yy'}"

print(eval(a))
print(eval(b))
print(eval(c))


4
4
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
(1, 2, 3, 4)
{1: ‘xx’, 2: ‘yy’}


# List Comprehensions

https://www.hackerrank.com/challenges/list-comprehensions/problem

根据要求属于三维坐标

方法一 for 循环


1
2
3
4
5
6
7
8
9
10
11
12
# 正常思路
x = int(input())
y = int(input())
z = int(input())
n = int(input())
l = []
for a in range(x+1):
for b in range(y+1):
for c in range(z+1):
if a+b+c != n:
l.append([a,b,c])
print(l)


1
1
1
2
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]


方法二 简化方法


1
2
3
4
5
# 简化4次input,列表生成式
x,y,z,n = [int(input()) for i in range(4)]
l = [[a,b,c] for a in range(x+1) for b in range(y+1) for c in range(z+1) if (a+b+c) != n]

print(l)


1
1
1
2
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]


# Find the Runner-Up Score!

https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list/editorial

找到列表中第二大的数字

方法一 用 set() , sorted()


1
2
3
n = int(input())
l = set(map(int,input().split()))
print(sorted(l)[-2])


5
2 3 6 6 5
5


方法二 不用 set() , sorted()


1
2
3
4
5
6
7
8
9
10
#最基本的算法
n = int(input())
l = list(map(int,input().split()))
maximum = max(l)
m = -999999
for i in l:
if i > m and i != maximum:
m = i

print(m)


5
2 3 6 6 5
5


方法三 collections.Counter()


1
2
3
4
from collections import Counter
n = int(input())
l = Counter(map(int,input().split())).keys()
sorted(l)[-2]


5
2 3 6 6 5





5



方法四 函数


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
l = [2, 3, 6, 1, 5]
# 列表最大值
def maximum(l):
d = -9999999
for i in l:
if i > d:
d = i
return(d)

#maximum(l)

#列表第二大值
def second(l):
m = maximum(l)
e = -9999999
for i in l:
if (i > e) and (i != m):
e = i
return(e)

second(l)


5



拓展 求列表最大值


1
2
3
4
5
6
7
8
9
10
11
12
13
# 列表最大值
from functools import reduce
def get_max(x,y):
if x > y:
return x
else:
return y
l = [2, 3, 6, 1, 5]
reduce(get_max,l)

#简化
from functools import reduce
reduce(lambda x,y:x if x>y else y,l)


6



# Nested Lists

https://www.hackerrank.com/challenges/nested-list/problem

找到成绩排名第二的学生

第一种 用字典


1
2
3
4
5
6
7
8
9
10
#创建数据
students = []
for i in range(int(input())):
name = input()
score = float(input())
students.append([name,score])
print(students)

#简化
students=[[input(),float(input())] for i in range(int(input()))]


5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
[[‘Harry’, 37.21], [‘Berry’, 37.21], [‘Tina’, 37.2], [‘Akriti’, 41.0], [‘Harsh’, 39.0]]



1
2
3
4
5
6
7
8
9
10
11
12
students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41.0], ['Harsh', 39.0]]
c = dict(students)
print(c)
second = sorted(set(c.values()))[1]
s = [i[0] for i in students if i[1] == second]

#第一种打印方式
for i in sorted(s):
print(i)

#第二种打印方式
print('\n'.join(sorted(s)))


{‘Harry’: 37.21, ‘Berry’: 37.21, ‘Tina’: 37.2, ‘Akriti’: 41.0, ‘Harsh’: 39.0}
Berry
Harry
Berry
Harry


第二种 不用字典 简化


1
2
3
4
5
students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41.0], ['Harsh', 39.0]]

second = sorted(set([i[1] for i in students]))[1]
for i in sorted([i[0] for i in students if i[1] == second]):
print(i)


Berry
Harry


第三种


1
2
3
4
5
#第三种方法
students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41.0], ['Harsh', 39.0]]

second_highest = sorted(set([y for x,y in students]))[1]
print('\n'.join([a for a,b in sorted(students) if b == second_highest]))


Berry
Harry


迭代嵌套列表


知识点 迭代嵌套列表


1
2
3
4
5
6
7
8
9
10
11
students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41.0], ['Harsh', 39.0]]
a = []
b = []
for i,x in students:
a.append(i)
b.append(x)
print(a)
print(b)

c = [['Harry', 37.21,3], ['Berry', 37.21,2], ['Tina', 37.2,1], ['Akriti', 41.0,4], ['Harsh', 39.0,4]]
print([z for x,y,z in c])


[‘Harry’, ‘Berry’, ‘Tina’, ‘Akriti’, ‘Harsh’]
[37.21, 37.21, 37.2, 41.0, 39.0]
[3, 2, 1, 4, 4]


通过字典的value获取其key


知识点 通过字典的value获取其key,这个方法只适合没有相同value的字典


1
2
3
4
5
d = {'a':'001', 'b':'002'}
list(d.keys())[list(d.values()).index("001")]
#index() 函数用于从列表中找出某个值第一个匹配项的索引位置。

#???如果要获取所有的索引位置


‘a’



# Finding the percentage

https://www.hackerrank.com/challenges/finding-the-percentage/problem

把学生和3门成绩的平均值一起存在字典中


1
2
3
4
5
6
7
#创建数据
n = int(input())
d = {}
for i in range(n):
text = input().split()
d[text[0]] = list(map(float,text[1:])) #d[text[0]] = [float(i) for i in text[1:]]
print(d)


3
Krishna 67 68 69
Arjun 70 98 63
Malika 52 56 60
{‘Krishna’: [67, 68, 69], ‘Arjun’: [70, 98, 63], ‘Malika’: [52, 56, 60]}



1
2
3
4
5
6
7
scores = {'Krishna': [67, 68, 69], 'Arjun': [70, 98, 63], 'Malika': [52, 56, 60]}
name = input()
sum = 0
for i in scores[name]:
sum += i
average_score = sum/len(scores[name])
print('{:.2f}'.format(average_score))


Krishna
68.00



1
2
3
4
5
6
7
8
9
# 把学生和3门成绩的平均值一起存在字典中

n = int(input())

d = {}
for i in range(n):
text = input().split()
d[text[0]] ='{:.2f}'.format(sum((map(float,text[1:]))/3)
print(d[input()])


3
Krishna 67 68 69
Arjun 70 98 63
Malika 52 56 60
Krishna
68.00



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
s = 'HackerRan.' 
def swap_case(s):
for i in s:

if i.islower():
i.upper()
else :
i.lower()

return s

swap_case(s)

#没有任何改变
#因为字符串是不可变类型,而且方法有返回值的,一定要储存,要不然就白弄了


‘HackerRan.’




1
2
3
4
5
6
7
8
9
10
11
12
13
s = 'HackerRan.' 
S = []
def swap_case(s):
for i in s:

if i.islower():
S.append(i.upper())
else :
S.append(i.lower())

return ''.join(S)

swap_case(s)


‘hACKERrAN.’




1
[for i in s if]


1
s.swapcase()


‘hACKERrAN.’



# String Formatting
https://www.hackerrank.com/challenges/python-string-formatting/problem


1
2
3
4
5
6
#第一种方法

n = 17
width = len(bin(n)[2:])
for i in range(1,n+1):
print(str(i).rjust(width),str(oct(i))[2:].rjust(width),str(hex(i))[2:].upper().rjust(width),str(bin(i))[2:].rjust(width))


1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 10 8 1000
9 11 9 1001
10 12 A 1010
11 13 B 1011
12 14 C 1100
13 15 D 1101
14 16 E 1110
15 17 F 1111
16 20 10 10000
17 21 11 10001



1
2
3
4
5
#第二种方法
n = 17
width = len(bin(n))-2
for i in range(1,n+1):
print('{0:{width}d} {0:{width}o} {0:{width}x} {0:{width}b}'.format(i,width = width))


1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 10 8 1000
9 11 9 1001
10 12 a 1010
11 13 b 1011
12 14 c 1100
13 15 d 1101
14 16 e 1110
15 17 f 1111
16 20 10 10000
17 21 11 10001


# Alphabet Rangoli
https://www.hackerrank.com/challenges/alphabet-rangoli/problem


1
2
3
4
5
6
7
8
9
10
11
12
13
n = 5
alpha = [chr(i) for i in range(97,123)]
print(alpha)

#top corn
for i in range(n-1,-1,-1):
print(+alpha[i].center(4*n-3,'-')+)

#middle belt
print(alpha[0].center(4*n-3,'-'))
#below corn
for i in range(n):
print(alpha[i].center(4*n-3,'-'))


[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]
——–e——–
——–d——–
——–c——–
——–b——–
——–a——–
——–a——–
——–a——–
——–b——–
——–c——–
——–d——–
——–e——–



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
n = 5
a = 'a-'

#top corn

for i in range(0,2*n-2,2):
print((a*(i+1)).center(4*n-3,'-'))

#middle belt

print(((a*(2*n-1)).center(4*n-3,'-').rstrip('-')))

#below corn

for i in range(2*n-4,-2,-2):
print((a*(i+1)).center(4*n-3,'-'))


——–a——–
——a-a-a——
—-a-a-a-a-a—-
–a-a-a-a-a-a-a–
a-a-a-a-a-a-a-a-a
–a-a-a-a-a-a-a–
—-a-a-a-a-a—-
——a-a-a——
——–a——–



1
2
3
4
5
6
7
8
9
10
11
n = 5
icon = 'e-'

alpha = [chr(i) for i in range(97,123)]


for i in range(5):
s = alpha[i:n]
b = alpha[i+1:n]
line = ('-').join(s[::-1]+b)
print(line.center(4*n-3,'-'))


e-d-c-b-a-b-c-d-e
–e-d-c-b-c-d-e–
—-e-d-c-d-e—-
——e-d-e——
——–e——–



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#方法一
n = 5

alpha = [chr(i) for i in range(97,123)]
line = []

for i in range(5):
rstr = alpha[i:n]
lstr = alpha[i+1:n]
line.append('-'.join(lstr[::-1]+rstr))
print(line)


for l in line[:0:-1]:
print(l.center(4*n-3,'-'))

for l in line:
print(l.center(4*n-3,'-'))


[‘e-d-c-b-a-b-c-d-e’, ‘e-d-c-b-c-d-e’, ‘e-d-c-d-e’, ‘e-d-e’, ‘e’]
——–e——–
——e-d-e——
—-e-d-c-d-e—-
–e-d-c-b-c-d-e–
e-d-c-b-a-b-c-d-e
–e-d-c-b-c-d-e–
—-e-d-c-d-e—-
——e-d-e——
——–e——–



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#方法二
n = 5

alpha = [chr(i) for i in range(97,123)]
line = []

for i in range(5):
s = '-'.join(alpha[i:n])
print(s)
line.append((s[::-1]+s[1:]).center(4*n-3,'-'))
print(line)
print('\n'.join(line[:1:-1]+line))
print('\n'.join(line[-1:1:-1]+line))
print('\n'.join(line[:-4:-1]+line))
print('\n'.join(line[-1:-4:-1]+line))


a-b-c-d-e
b-c-d-e
c-d-e
d-e
e
[‘e-d-c-b-a-b-c-d-e’, ‘–e-d-c-b-c-d-e–’, ‘—-e-d-c-d-e—-‘, ‘——e-d-e——‘, ‘——–e——–’]
——–e——–
——e-d-e——
—-e-d-c-d-e—-
e-d-c-b-a-b-c-d-e
–e-d-c-b-c-d-e–
—-e-d-c-d-e—-
——e-d-e——
——–e——–
——–e——–
——e-d-e——
—-e-d-c-d-e—-
e-d-c-b-a-b-c-d-e
–e-d-c-b-c-d-e–
—-e-d-c-d-e—-
——e-d-e——
——–e——–
——–e——–
——e-d-e——
—-e-d-c-d-e—-
e-d-c-b-a-b-c-d-e
–e-d-c-b-c-d-e–
—-e-d-c-d-e—-
——e-d-e——
——–e——–
——–e——–
——e-d-e——
—-e-d-c-d-e—-
e-d-c-b-a-b-c-d-e
–e-d-c-b-c-d-e–
—-e-d-c-d-e—-
——e-d-e——
——–e——–



1
2
3
name = 'chris alan'
for n in name.split(' '):
print(n.capitalize(),end=' ')


Chris Alan


1
(' ').join([n.capitalize() for n in name.split(' ')])


‘Chris Alan’



title() 与 capitalize() 的区别

capitalize()与title()都可以实现字符串首字母大写.

主要区别在于:
capitalize(): 字符串第一个字母大写
title(): 字符串内的所有单词的首字母大写

1
2
3
4
5
6
7
8
9
10
11
12
# 字母开头的情况

str='huang bi quan'
print(str.capitalize()) #第一个字母大写
print(str.title()) #所有单词的首字母大写


#非字母开头的情况

str='深圳luohu'
print(str.capitalize()) #输出内容不变
print(str.title()) #第一个字母大写
Huang bi quan
Huang Bi Quan
深圳luohu
深圳Luohu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
print('title()方法')
a = 'abcd'
print(a.title())

b = '12abcd'
print(b.title())

c = 'ab cd'
print(c.title())

print('capitalize()方法')
d = 'abcd'
print(d.capitalize())

e = '12abcd'
print(e.capitalize())

f = 'ab cd'
print(f.capitalize())
title()
Abcd
12Abcd
Ab Cd
capitalize()
Abcd
12abcd
Ab cd

The Minion Game

https://www.hackerrank.com/challenges/the-minion-game/problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#方法一  但是当string很多的时候,就不行了
string = 'BANANA'
S_score = 0
K_score = 0
vowel = 'AEIOU'

for cut_len in range(len(string)):
for s in range(len(string)):
if len(string[s:s+cut_len+1]) == cut_len+1:
if string[s:s+cut_len+1][0] in vowel:
K_score += 1
else:
S_score += 1

if S_score > K_score:
print('Stuart',S_score)
elif S_score < K_score:
print('Kevin',K_score)
elif S_score == K_score:
print('Draw')
Stuart 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#方法二
string = 'BANANA'
S_score = 0
K_score = 0
vowel = 'AEIOU'

for i in range(len(string)):
if string[i] in vowel:
K_score += len(string) - i
else:
S_score += len(string) - i

if S_score > K_score:
print('Stuart',S_score)
elif S_score < K_score:
print('Kevin',K_score)
elif S_score == K_score:
print('Draw')
Stuart 12
1
2
3
4
5
6
7
8
9
10
string = 'BANANA'
combstr = []
for i in range(len(string)):
combstr.append(string[i:])
combstr.append(string[:i+1])
combstr.append(string[i])


print(set(combstr))
print(len(combstr))
{'N', 'BA', 'B', 'NANA', 'BANAN', 'ANA', 'ANANA', 'BANA', 'BANANA', 'NA', 'BAN', 'A'}
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string = 'BANANA'
substrings = []

for cut_len in range(len(string)):
for s in range(len(string)):
if len(string[s:s+cut_len+1]) == cut_len+1:
substrings.append(string[s:s+cut_len+1])

substrings = set(substrings)
print(substrings)
print(len(substrings))

palyers = {'Stuart':[],'Kevin':[]}

vowel = 'AEIOU'
for i in substrings:
if i[0] in vowel:
palyers['kevin'].append(i)
else:
palyers['Stuart'].append(i)
print(palyers)
{'ANAN', 'N', 'BA', 'B', 'NANA', 'BANAN', 'ANA', 'BANA', 'ANANA', 'AN', 'NAN', 'BANANA', 'NA', 'BAN', 'A'}
15
{'Stuart': ['N', 'BA', 'B', 'NANA', 'BANAN', 'BANA', 'NAN', 'BANANA', 'NA', 'BAN'], 'Kevin': ['ANAN', 'ANA', 'ANANA', 'AN', 'A']}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
string = 'BANANA'
substrings = []

for cut_len in range(len(string)):
for s in range(len(string)):
if len(string[s:s+cut_len+1]) == cut_len+1:
substrings.append(string[s:s+cut_len+1])

print(substrings)

Stuart = []
Kevin = []
vowel = 'AEIOU'
for i in substrings:
if i[0] in vowel:
Kevin.append(i)
else:
Stuart.append(i)

from collections import Counter


S_dict = dict(Counter(Stuart))
K_dict = dict(Counter(Kevin))

S_score = 0
K_score = 0

for v in S_dict.values():
S_score += v

for v in K_dict.values():
K_score += v

print(S_score,K_score)

if S_score > K_score:
print('Stuart',S_score)
elif S_score < K_score:
print('Kevin',K_score)
elif S_score == K_score:
print('Draw')
['B', 'A', 'N', 'A', 'N', 'A', 'BA', 'AN', 'NA', 'AN', 'NA', 'BAN', 'ANA', 'NAN', 'ANA', 'BANA', 'ANAN', 'NANA', 'BANAN', 'ANANA', 'BANANA']
12 9
Stuart 12
1
2
3
4
5
6
7
8
9
10
11
12
#按照长度切分字符串,变成n个子字符串
string = 'BANANA'

def cut_word(n,string):
substrings = []
for i in range(len(string)):
if len(string[i:i+n]) == n:
substrings.append(string[i:i+n])
print(substrings)

for n in range(len(string)):
cut_word(n+1,string)
['B', 'A', 'N', 'A', 'N', 'A']
['BA', 'AN', 'NA', 'AN', 'NA']
['BAN', 'ANA', 'NAN', 'ANA']
['BANA', 'ANAN', 'NANA']
['BANAN', 'ANANA']
['BANANA']
1
2
3
4
5
6
7
8
9
10
11
12
#切分子字符串
string = 'BANANA'

def cut_word(string):
substrings = []
for n in range(len(string)):
for i in range(len(string)):
if len(string[i:i+n+1]) == n+1:
substrings.append(string[i:i+n+1])
print(substrings)

cut_word(string)
['B', 'A', 'N', 'A', 'N', 'A', 'BA', 'AN', 'NA', 'AN', 'NA', 'BAN', 'ANA', 'NAN', 'ANA', 'BANA', 'ANAN', 'NANA', 'BANAN', 'ANANA', 'BANANA']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
s = 'BANANA'
vowels = 'AEIOU'

kevsc = 0
stusc = 0
for i in range(len(s)):
if s[i] in vowels:
kevsc += (len(s)-i)
else:
stusc += (len(s)-i)

if kevsc > stusc:
print "Kevin", kevsc
elif kevsc < stusc:
print "Stuart", stusc
else:
print "Draw"

Merge the Tools!

https://www.hackerrank.com/challenges/merge-the-tools/problem

1
2
3
4
5
6
7
string = 'AABCAAADA'
k = 3
cut_len = int(len(string)/k)
cut_word = []
for i in range(0,len(string),cut_len):
cut_word.append(string[i:i+cut_len])
print(cut_word)
['AAB', 'CAA', 'ADA']
1
2
3
4
5
6
7
8
9
10
string = 'AABCAAADA'
s = list(string)
k = 3
split_number = int(len(string)/k)
i = 0
while i < len(s)-1:
if s[i] == s[i+1]:
del s[i]
else:
i += 1
['A', 'B', 'C', 'A', 'D', 'A']
1
9/3
3.0
1
2
3
4
5
6
7
8
9
10
#方法一
string = 'AABCAAADA'
k = 3

for i in range(0,len(string),k):
s = []
for item in string[i:i+k]:
if item not in s:
s.append(item)
print(''.join(s))
AB
CA
AD

列表生成式理解出错的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string = 'AABCAAADA'
k = 3
s = []

for item in string:
if item not in s:
s.append(item)
print(s)

#我们把上面的代码块简化成下面的
a = []
print([item for item in string if item not in a]) #2

#但是结果却不一样,原因在哪里?
'''
#2处,因为比较的a列表中一直都是空的,所以条件一直都是True
'''

# 或者这样
r = []
print([r.append(item) for item in string if item not in r])
#由于 append()方法无返回值,所以打印出的是none
print(r)
['A', 'B', 'C', 'D']
['A', 'A', 'B', 'C', 'A', 'A', 'A', 'D', 'A']
[None, None, None, None]
['A', 'B', 'C', 'D']
1
2
3
4
5
6
7
#方法二
string = 'AABCAAADA'
k = 3
for part in zip(*[iter(string)] * k):
d = dict()
print(''.join([ d.setdefault(c, c) for c in part if c not in d ]))
print(d)
AB
{'A': 'A', 'B': 'B'}
CA
{'C': 'C', 'A': 'A'}
AD
{'A': 'A', 'D': 'D'}

dict.setdefault(key, default=None)

Python 字典 setdefault() 方法和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值。

参数
key – 查找的键值。
default – 键不存在时,设置的默认键值。

返回值
如果 key 在 字典中,返回对应的值。如果不在字典中,则插入 key 及设置的默认值 default,并返回 default ,default 默认值为 None。

dict.get(key, default=None)

Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值。

参数
key – 字典中要查找的键。
default – 如果指定键的值不存在时,返回该默认值值。

返回值
返回指定键的值,如果值不在字典中返回默认值 None。

1
2
3
4
5
6
7
8
a = {'A': 'A', 'B': 'B'}

print(a.get('A','NA'))
print(a.get('C','NA'))
print(a)
print(a.setdefault('A','NA'))
print(a.setdefault('C','NA'))
print(a)
A
NA
{'A': 'A', 'B': 'B'}
A
NA
{'A': 'A', 'B': 'B', 'C': 'NA'}

iter()函数

凡是可作用于for循环的对象都是Iterable类型;
凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;

生成器都是Iterator对象,但list、dict、str虽然是Iterable,却不是Iterator。
把list、dict、str等Iterable变成Iterator可以使用iter()函数

iterator 对象 迭代器

可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator。

Python的Iterator对象表示的是一个数据流,Iterator对象可以被next()函数调用并不断返回下一个数据,直到没有数据时抛出StopIteration错误。可以把这个数据流看做是一个有序序列,但我们却不能提前知道序列的长度,只能不断通过next()函数实现按需计算下一个数据,所以Iterator的计算是惰性的,只有在需要返回下一个数据时它才会计算。

Iterator甚至可以表示一个无限大的数据流,例如全体自然数。而使用list是永远不可能存储全体自然数的。

zip(iterables)函数


https://docs.python.org/3/library/functions.html#zip

Python3中的zip函数可以把两个或者两个以上的迭代器封装成生成器,这种zip生成器会从每个迭代器中获取该迭代器的下一个值,然后把这些值组装成元组(tuple)。这样,zip函数就实现了平行地遍历多个迭代器。

如果输入的迭代器长度不同,那么,只要有一个迭代器遍历完,zip就不再产生元组了,zip会提前终止,这可能导致意外的结果,不可不察。

zip([iter(s)]n)将数据序列聚类为n长度组的习语成为可能。这重复了相同的迭代器n次,以使每个输出元组具有对迭代器的n次调用的结果。这具有将输入划分为n个长块的效果。

zip() 与
操作符一起可以用来 unzip 一个列表


1
2
3
4
5
#很简洁地实现了同时遍历两个列表
list1 = ['a','b','c','d']
list2 = ['apple','boy','cat','dog']
for x,y in zip(list1,list2):
print(x,'is',y)


a is apple
b is boy
c is cat
d is dog



1
2
3
4
s = 'AABCAAADA'
i = iter(s)
print(list(zip(i,i,i)))
#其中3个i都是指向同一个iterator对象,它的内容只能被消费一次,每next()调用一次,就到下一个数据


[(‘A’, ‘A’, ‘B’), (‘C’, ‘A’, ‘A’), (‘A’, ‘D’, ‘A’)]



1
2
3
4
5
6
7
l = [1,2,3]
n = l*3
m = [l]*3
print(n)
print(m)
m[0][0]=9
print(m)


[1, 2, 3, 1, 2, 3, 1, 2, 3]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[9, 2, 3], [9, 2, 3], [9, 2, 3]]


list.sort() 方法 与 sorted() 函数

sorted(iterable,key=None,reverse=False),返回新的列表,对所有可迭代的对象均有效

list.sort(key=None,reverse=False) 就地改变列表reverse:True反序;False 正序

sorted 和list.sort 都接受key, reverse定制。但是区别是。list.sort()是列表中的方法,只能用于列表。而sorted可以用于任何可迭代的对象。list.sort()是在原序列上进行修改,不会产生新的序列。所以如果你不需要旧的序列,可以选择list.sort()。 sorted() 会返回一个新的序列。旧的对象依然存在。

Symmetric Difference

https://www.hackerrank.com/challenges/symmetric-difference/problem

1
2
3
4
5
6
M = '2 4 5 9'
N = '2 4 11 12'
M = set(map(int,M.split(' ')))
N = set(map(int,N.split(' ')))
for i in sorted(M^N):
print(i)
5
9
11
12
1
2
3
4
5
6
7
8
9
M = '2 4 5 9'
N = '2 4 11 12'
M = set(M.split(' '))
N = set(N.split(' '))
sym_dif = list(M^N)
print(sym_dif)
print(sorted(sym_dif))
print(sorted(sym_dif,key=int))
print('\n'.join(sorted(sym_dif,key=int)))
['11', '5', '9', '12']
['11', '12', '5', '9']
['5', '9', '11', '12']
5
9
11
12
1
2
3
4
5
6
7
8
9
10
#方法一
m,M,n,N =[input() for i in range(4)]
M = set(map(int,M.split(' ')))
N = set(map(int,N.split(' ')))

for i in sorted(M^N):
print(i)

#上面的for循环等同于下面的代码块
print(*sorted(M^N),sep='\n')
4
2 4 5 9
4
2 4 11 12
5
9
11
12
5 9 11 12
1
2
3
#方法二 简化
a,b = [set(input().split()) for i in range(4)][1::2]
print(*sorted(a^b, key=int), sep='\n')
4 
2 4 5 9 
4
2 4 11 12
5
9
11
12
1
2
3
#方法三
a,b = [set(input().split()) for i in range(4)][1::2]
print ('\n'.join(sorted(a^b, key=int)))
4  
2 4 5 9  
4 
2 4 11 12
5
9
11
12

‘sep’.join(seq)


sep:分隔符。可以为空
seq:要连接的元素序列、字符串、元组,里面的元素必须是字符串类型
如果是整数类型,
可以先把它转化为字符串类型 print(‘\n’.join(map(str,a)))
或者用 print(*a,sep=’\n’)


1
2
3
4
5
6
7
8
a = ['5', '9', '11', '12']
print('\n'.join(a))
print(*a,sep='\n')

a = [5, 9, 11, 12]
print(*a,sep='\n')
#print('\n'.join(a)) 报错,因为a里面的元素是整数型
print('\n'.join(map(str,a))) #先把元素转化为字符串类型


5
9
11
12
5
9
11
12
5
9
11
12
5
9
11
12



1
2
3
4
5
6
# 按照数字的大小,从小到大换行打印每一个元素
a = ['11', '12', '5', '9']

print('\n'.join(sorted(a,key = int)))

print(*(sorted(a,key=int)),sep='\n')


5
9
11
12
5
9
11
12


# No Idea!
https://www.hackerrank.com/challenges/no-idea/problem


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#方法一  如果序列太大了,太耗时了
n = 3
m = 2
arry = [1,5,3]
A = {3,1}
B = {5,7}
happiness = 0

for i in arry:
if i in A:
happiness += arry.count(i)
elif i in B:
happiness -= arry.count(i)
print(happiness)


1



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#方法二 简化
n = 3
m = 2
arry = [1,5,3]
A = {3,1}
B = {5,7}
happiness = 0
count = []
for i in arry:
if i in A:
count.append(1)
elif i in B:
count.append(-1)
else:
count.append(0)
print(sum(count))


1



1
2
3
4
5
6
7
8
9
10
11
#方法三
n = 3
m = 2
arry = [1,5,3]
A = {3,1}
B = {5,7}
happiness = 0
count = []
for i in arry:
count.append((i in A) - (i in B))
print(count)


[1, -1, 1]



1
2
print(int(True))
print(int(False))


1
0



1
2
3
4
5
n,m = list(map(int,input().split(' ')))
arry = list(map(int,input().split(' ')))
A = set(map(int,input().split(' ')))
B = set(map(int,input().split(' ')))
print(n,m,arry,A,B)


3 2
1 5 3
3 1
5 7
3 2 [1, 5, 3] {1, 3} {5, 7}



1
2
3
4
5
n,m = input().split(' ')
arry = input().split(' ')
A = set(input().split(' '))
B = set(input().split(' '))
print(n,m,arry,A,B)


3 2
1 5 3
3 1
5 7
3 2 [‘1’, ‘5’, ‘3’] {‘3’, ‘1’} {‘5’, ‘7’}



1
2
3
n = int(input())
country = set(input().strip() for i in range(n))
print(country,len(country))


7
UK
China
USA
France
New Zealand
UK
France
{‘France’, ‘China’, ‘USA’, ‘UK’, ‘New Zealand’} 5



1
2
3
n = int(input())
s = set(map(int,input().split(' ')))
print(s)


9
1 2 3 4 5 6 7 8 9
{1, 2, 3, 4, 5, 6, 7, 8, 9}



1
2
3
4
Eng,Fre = (set(input().split(' ')) for i in range(2))
print(Eng,Fre)

print(Eng - Fre)


1 2 3 4 5 6 7 8 9
10 1 2 3 11 21 55 6 8
{‘6’, ‘2’, ‘5’, ‘8’, ‘3’, ‘ ‘, ‘7’, ‘9’, ‘4’, ‘1’} {‘6’, ‘2’, ‘5’, ‘0’, ‘8’, ‘3’, ‘ ‘, ‘1’}
{‘4’, ‘7’, ‘9’}


# Set Mutations
https://www.hackerrank.com/challenges/py-set-mutations/problem

方法一 if


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
num_ele = input()
set_A = set(map(int,input().split(' ')))
num = int(input())
for i in range(num):
command = input().split(' ')[0]
set_a = set(map(int,input().split(' ')))
if command == 'intersection_update':
set_A &= set_a
if command == 'update':
set_A |= set_a
if command == 'symmetric_difference_update':
set_A ^= set_a
if command == 'difference_update':
set_A -= set_a
print(sum(set_A))


16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 24 52
{‘10’, ‘2’, ‘12’, ‘7’, ‘11’, ‘4’, ‘13’, ‘6’, ‘3’, ‘8’, ‘1’, ‘14’, ‘52’, ‘24’, ‘5’, ‘9’}
1
intersection_update 10
2 3 5 6 8 9 1 4 7 11
{‘2’, ‘7’, ‘11’, ‘4’, ‘8’, ‘6’, ‘3’, ‘1’, ‘5’, ‘9’}
10


方法二 getattr()


1
2
3
4
5
6
7
8
num_ele = input()
set_A = set(input().split(' '))
num = int(input())
for i in range(num):
command = input().split(' ')[0]
set_a = set(input().split(' '))
getattr(set_A,command)(set_a)
print(sum(map(int,set_A)))


getattr()

getattr(object, name[,default])
获取对象object的属性或者方法,如果存在打印出来,如果不存在,打印出默认值,默认值可选。
需要注意的是,如果是返回的对象的方法,返回的是方法的内存地址,如果需要运行这个方法,
可以在后面添加一对括号。

1
2
3
4
5
6
7
8
9
10
11
class test():
name = 'hackerrank'
def run(self):
return 'Hello Hackerrank'

t = test()
print(getattr(t,'name')) #获取name属性,存在就打印出来
print(getattr(t,'run')) #获取run方法,存在就打印出方法的内存地址
getattr(t, 'run')() #获取run方法,后面加括号可以将这个方法运行
print(getattr(t, "age","18")) #若属性不存在,返回一个默认值。
print(getattr(t, 'age')) #获取一个不存在的属性
hackerrank
<bound method test.run of <__main__.test object at 0x000001C07C87F390>>
18



---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-11-7e96e4c12be7> in <module>()
      9 getattr(t, 'run')()  #获取run方法,后面加括号可以将这个方法运行
     10 print(getattr(t, "age","18"))  #若属性不存在,返回一个默认值。
---> 11 print(getattr(t, 'age'))  #获取一个不存在的属性


AttributeError: 'test' object has no attribute 'age'

dir()

dir()函数可以查看对像内所有属于及方法,在python中任何东西都是对像,一种数据类型,一个模块等,都有自己的属性和方法,除了常用方法外,其它的你不需要全部记住它,交给dir()函数就好了。

1
2
a = {1,2,3,4}
dir(a)
['__and__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__iand__',
 '__init__',
 '__init_subclass__',
 '__ior__',
 '__isub__',
 '__iter__',
 '__ixor__',
 '__le__',
 '__len__',
 '__lt__',
 '__ne__',
 '__new__',
 '__or__',
 '__rand__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__ror__',
 '__rsub__',
 '__rxor__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__xor__',
 'add',
 'clear',
 'copy',
 'difference',
 'difference_update',
 'discard',
 'intersection',
 'intersection_update',
 'isdisjoint',
 'issubset',
 'issuperset',
 'pop',
 'remove',
 'symmetric_difference',
 'symmetric_difference_update',
 'union',
 'update']

The Captain’s Room

https://www.hackerrank.com/challenges/py-the-captains-room/problem

思路一:正常思路,找到出现频率为1次的项

1
2
3
4
5
6
7
8
9
10
11
12
#方法一 循环
k = int(input())
num_list = input().split()
num_times = {}
for i in num_list:
if i not in num_times:
num_times[i] = 0
num_times[i] += 1

for i in num_times.keys():
if num_times[i] == 1:
print(i)
5
1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2
{'1': 5, '2': 5, '3': 5, '6': 5, '5': 5, '4': 5, '8': 1}
8
1
2
3
4
5
6
7
8
9
10
11
12
#方法二 fromkeys() 
k = int(input())
num_list = input().split()
num_times = dict.fromkeys(set(num_list),0)
for i in num_list:
if i in num_times:
num_times[i] += 1
print(num_times)

for i in num_times.keys():
if num_times[i] == 1:
print(i)
1
2
3
4
5
6
#方法三 collections.Counter
from collections import Counter
k = int(input())
num_list = input().split()
num_times = Counter(num_list)
print(num_times.most_common()[-1][0])
5
1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2 
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

l = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,6]
d = dict.fromkeys(set(l),0)
print(d)
for i in l:
if i in d:
d[i] += 1
print(d)

from collections import Counter
c = Counter(d)
print(c)
m = c.most_common()
print(m[-1])
print(m[-1][0])


l = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,6]
s = set(l)
print(s)
n = int((len(l)-1)/5)
print(n)
c = (sum(s)*n - sum(l))/(n-1)
print(int(c))
{1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
{1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 1}
Counter({1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 1})
(6, 1)
6
{1, 2, 3, 4, 5, 6}
3
6

思路二:数学思路

L = [1,2,3,4,1,2,3,4,1,2,3,4,5] S = (1,2,3,4,5)

C 是L中只出现一次的5 K 是 [1,2,3,4]在L中出现的次数

L = S * K - (K-1) * C

C = (S * K - L)/(K-1)

1
2
3
4
5
k = int(input())
num_list = list(map(int,input().split()))
s = set(num_list)
Captin = (sum(s)*k - sum(num_list))/(k-1)
print(int(Captin))
5
1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2 
8

思路三:集合差

创建一个包括所有元素的集合,再创建一个包括出现频率超过一次的集合,两个集合求差

A 与 B 的差集是所有属于 A 且不属于 B 的元素构成的集合

1
2
3
4
5
6
7
8
9
10
k = int(input())
num_list = list(map(int,input().split()))
s1 = set()
s2 = set()
for i in num_list:
if i not in s1:
s1.add(i)
else:
s2.add(i)
print((s1-s2).pop())
5
1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2 
8

思路四:集合对称差 完全想不到

将列表进行排序后,交错分成2个列表,一个奇数列表,一个偶数列表,将这两个列表变成集合后,求对称差

两个集合的对称差是只属于其中一个集合,而不属于另一个集合的元素组成的集合。

1
2
3
4
5
k = int(input())
rooms = input().split()
rooms.sort()
capt_room = (set(rooms[0::2]) ^ set(rooms[1::2]))
print(capt_room.pop())
5
1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2
8

Check Subset

https://www.hackerrank.com/challenges/py-check-subset/problem

s.issubset(t)


s <= t # 测试是否 s 中的每一个元素都在 t 中

s.issuperset(t)


s >= t # 测试是否 t 中的每一个元素都在 s 中


1
2
3
4
5
6
7
8
9
# 正常思路求A和B的交集数量是否是还是A的数量

T = int(input())
for i in range(T):
A_len = int(input())
A = set(input().split())
B_len = int(input())
B = set(input().split())
print(len(A&B)==A_len)


1
5
1 2 3 5 6
9
9 8 5 6 3 2 1 4 7
True



1
2
3
4
5
6
7
8
9
# s.issubset(t)

T = int(input())
for i in range(T):
A_len = int(input())
A = set(input().split())
B_len = int(input())
B = set(input().split())
print(A<=B)


1
5
1 2 3 5 6
9
9 8 5 6 3 2 1 4 7
True


# Check Strict Superset
https://www.hackerrank.com/challenges/py-check-strict-superset/problem


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 自己的思路

A = set(input().split())
N = int(input())
result = 0
for i in range(N):
B = set(input().split())
if A>B:
result += 1
else:
result -= 1
if result=N:
print('True')
else:
print('False')


True
True
True
True
False
False



1
2
3
4
# 用all() 函数 和 生成器 generator

A = set(input().split())
print(all((A>set(input().split()) for i in range(int(input()))))


1 2 3 4 5 6 7 8 9 10 11 12 23 45 84 78
2
1 2 3 4 5
100 11 12
False


all() 函数

all() 函数用于判断给定的可迭代参数 iterable 中的所有元素是否不为 0、’’、False 或者 iterable 为空,如果是返回 True,否则返回 False。

注意:空元组、空列表返回值为True,这里要特别注意。

all函数等价于下列代码段:

1
2
3
4
5
def all(iterable):
for element in iterable:
if not element:
return False
return True
1
2
3
4
5
print(all(['a', 'b', 'c', 'd']) ) # 列表list,元素都不为空或0
print(all(['a', 'b', '', 'd']) ) # 列表list,存在一个为空的元素
print(all([0, 1,2, 3]) ) # 列表list,存在一个为0的元素
print(all([])) # 空列表
print(all(())) # 空元组
True
False
False
True
True

Find Angle MBC

https://www.hackerrank.com/challenges/find-angle/problem

1
2
3
4
5
6
from math import sin
from cmath import sqrt
a = 10
b = 10
print(float(sqrt(a/a**a+b**b)))
#print(sin(a/sqrt(a**a+b**b)))
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-13-042b1c12f4e0> in <module>()
      3 a = 10
      4 b = 10
----> 5 print(float(sqrt(a/a**a+b**b)))
      6 #print(sin(a/sqrt(a**a+b**b)))


TypeError: can't convert complex to float

itertools.product()

https://www.hackerrank.com/challenges/itertools-permutations/problem

笛卡尔积

product(A, B) returns the same as ((x,y) for x in A for y in B)

1
2
3
4
5
6
7
8
9
10
11
12
from itertools import product

print(list(product([1,0],[2,4])))
print(list(product([1,2],repeat=2)))
print(list(product([1,2],[1,2],[1,2])))

a=[1,2]
b=[3,4]
for i in product(a,b):
print(i,end=' ')
print()
print(*product(a,b))
[(1, 2), (1, 4), (0, 2), (0, 4)]
[(1, 1), (1, 2), (2, 1), (2, 2)]
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
(1, 3) (1, 4) (2, 3) (2, 4) 
(1, 3) (1, 4) (2, 3) (2, 4)

itertools.permutations

https://www.hackerrank.com/challenges/itertools-permutations/problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# permutations 排列  对顺序敏感({1, 2}, {2, 1} 是不同的排列)
# combinations 组合 对顺序不敏感({1, 2, 3}, {1, 3, 2}, … 都是相同的组合)


from itertools import permutations,combinations


listA = [1,2,3]
listB = 'abcd'
print(permutations(listA)) # 返回对象
print(list(permutations(listA))) # 返回迭代对象中所有的排列
print(list(permutations(listB,2))) # 返回迭代对象中2元素的排列


print('--------------combinations---------------------')
print(list(combinations(listA,3))) # 返回迭代对象中所有的组合
print(list(combinations(listB,2))) # 返回迭代对象中2元素的组合
A = [1,1,3,3,3]
print(list(combinations(A,4)))
B = [1,2,3,4,5]
print(list(combinations(B,4)))

print('--------------test-----------------------')

test = 'HACK'
num = 2
print(sorted(list(permutations(test,num))))
for i in sorted(list(permutations(test,num))):
print(*i,sep='')

print('--------------等同-----------------------')
for i in list(permutations(sorted(test),num)):
print(''.join(i))
<itertools.permutations object at 0x10edc89e8>
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'a'), ('b', 'c'), ('b', 'd'), ('c', 'a'), ('c', 'b'), ('c', 'd'), ('d', 'a'), ('d', 'b'), ('d', 'c')]
--------------combinations---------------------
[(1, 2, 3)]
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
[(1, 1, 3, 3), (1, 1, 3, 3), (1, 1, 3, 3), (1, 3, 3, 3), (1, 3, 3, 3)]
[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]
--------------test-----------------------
[('A', 'C'), ('A', 'H'), ('A', 'K'), ('C', 'A'), ('C', 'H'), ('C', 'K'), ('H', 'A'), ('H', 'C'), ('H', 'K'), ('K', 'A'), ('K', 'C'), ('K', 'H')]
AC
AH
AK
CA
CH
CK
HA
HC
HK
KA
KC
KH
--------------等同-----------------------
AC
AH
AK
CA
CH
CK
HA
HC
HK
KA
KC
KH
1
2
3
4
5
6
7
from itertools import combinations

test = 'HACK'
num = 2
for i in range(num):
for c in combinations(sorted(test),i+1):
print(*c,sep='')
[('A',), ('C',), ('H',), ('K',)]
A
C
H
K
AC
AH
AK
CH
CK
HK

itertools.groupby()

https://www.hackerrank.com/challenges/compress-the-string/problem?h_r=next-challenge&h_r%5B%5D%5B%5D=next-challenge&h_r%5B%5D%5B%5D=next-challenge&h_v=zen&h_v%5B%5D%5B%5D=zen&h_v%5B%5D%5B%5D=zen

1
2
3
4
from itertools import groupby

test = '1222311'
print(groupby(test))
<itertools.groupby object at 0x10ee17048>

collections.Counter()

https://www.hackerrank.com/challenges/collections-counter/problem

1
2
3
4
5
6
7
from collections import Counter

myList = [1,1,2,3,4,5,6,3,6]
print(Counter(myList))
print(Counter(myList).items())
print(Counter(myList).keys())
print(Counter(myList).values())
Counter({1: 2, 3: 2, 6: 2, 2: 1, 4: 1, 5: 1})
dict_items([(1, 2), (2, 1), (3, 2), (4, 1), (5, 1), (6, 2)])
dict_keys([1, 2, 3, 4, 5, 6])
dict_values([2, 1, 2, 1, 1, 2])
1
2
3
4
5
6
7
from collections import Counter

x = int(input())
s = list(map(int,input().split()))
n = int(input())

print(Counter(s))
10
2 3 4 5 6 8 7 6 5 18
6
Counter({5: 2, 6: 2, 2: 1, 3: 1, 4: 1, 8: 1, 7: 1, 18: 1})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from collections import Counter

s = [2,3,4,5,6,8,7,6,5,18]
c = Counter(s)
print(c)
d = dict(c)
v = [6,55]
total = 0

for i in range(6):
v = list(map(int,input().split()))
if not v[0] in d:
print('Size {} no longer available,so no purchase'.format(v[0]))
elif d[v[0]]>0:
d[v[0]] -= 1
print('Purchased size {} shoe for {}'.format(v[0],v[1]))
total += v[1]
else:
print('Size {} no longer available,so no purchase'.format(v[0]))

print(total)
Counter({5: 2, 6: 2, 2: 1, 3: 1, 4: 1, 8: 1, 7: 1, 18: 1})
6 55
Purchased size 6 shoe for 55
6 45
Purchased size 6 shoe for 45
6 55
Size 6 no longer available,so no purchase
4 40
Purchased size 4 shoe for 40
18 60
Purchased size 18 shoe for 60
10 50
Size 10 no longer available,so no purchase
200

Detect Floating Point Number

https://www.hackerrank.com/challenges/introduction-to-regex/editorial

1
2
3
4
5
6
7
8
9
10
11
12
import re
test = '+.4'
test1 = '.5'
test2 = '-+4.5'
test3 = '12.'
test4 = '4.0o0'
reg = r'^[-+]?\d*\.\d+$'
print(bool(re.search(reg,test)))
print(bool(re.search(reg,test1)))
print(bool(re.search(reg,test2)))
print(bool(re.search(reg,test3)))
print(bool(re.search(reg,test4)))
True
True
False
False
False

Re.split()

https://www.hackerrank.com/challenges/re-split/problem?h_r=next-challenge&h_v=zen

1
2
3
4
5
6
import re

test = '100,000,000.000'
reg = r'[,,\.]'
for i in re.split(reg,test):
print(i)
100
000
000
000

re.split()

多个分隔符,复杂的分隔情况,使用re.split

原型: re.split(pattern, string, maxsplit=0)

通过正则表达式将字符串分离。如果用括号将正则表达式括起来,那么匹配的字符串也会被列入到list中返回。maxsplit是分离的次数,maxsplit=1分离一次,默认为0,不限制次数。

1
2
3
4
5
6
7
8
9
import re

# 不保留匹配项
m = re.split(r'\d+','123abc321cba')
print(m)

# 保留匹配项
m = re.split(r'(\d+)','123abc321cba')
print(m)
['', 'abc', 'cba']
['', '123', 'abc', '321', 'cba']

Group(), Groups() & Groupdict()

https://www.hackerrank.com/challenges/re-group-groups/problem?h_r=next-challenge&h_v=zen

1
2
3
4
5
6
7
8
9
10
11
12
13
import re

test = '..1234567891011213141516171820212223'
reg = r'([0-9a-zA-Z])\1+' #回溯引用,表示与前面相同的字符
m =re.search(reg,test)
mport re

test = 'aaadaa'
print(re.finditer(r'(?=('++'))'))
if m:
print(m.group(1))
else:
print('-1')
1

Re.start() & Re.end()

https://www.hackerrank.com/challenges/re-start-re-end/problem

match和search一旦匹配成功,就是一个match object对象,而match object对象有以下方法:
start() 返回匹配开始的位置,子串第一个字符的索引
end() 返回匹配结束的位置,子串最后一个字符的索引+1
span() 返回一个元组包含匹配 (开始,结束) 的位置

1
2
3
4
5
6
7
8
9
10
# 匹配叠加项, pattern.match(string[, pos[, endpos]])

import re

test = 'aaadaa'
pattern = re.compile('aa')
for i in range(len(test)):
p = pattern.match(test,i)
if p:
print((p.start(),p.end()-1))
(0, 1)
(1, 2)
(4, 5)

第一种方法,figured it our by myself, 从每一个索引找起

1
2
3
4
5
6
7
8
9
10
11
12
import re

test = input()
pattern = re.compile(input())

if pattern.search(test):
for i in range(len(test)):
p = pattern.match(test,i)
if p:
print((p.start(),p.end()-1))
else:
print((-1,-1))
aaadaa
aa
(0, 1)
(1, 2)
(4, 5)

第二种方法,网站提供的参考方法,从匹配到的项的开头索引下一位作为下一次查找的开头,相对第一种方法,更快捷,不需要从头查找到尾

1
2
3
4
5
6
7
8
9
10
11
import re

test = 'aaadaa'
reg = 'aa'
patter = re.compile(reg)
r = pattern.search(test)
if not r:
print((-1,-1))
while r:
print('({0},{1})'.format(r.start(),r.end()-1))
r = pattern.search(test,r.start()+1)
(0,1)
(1,2)
(4,5)

第三种方法,论坛用户提供的,用了前瞻 前瞻:exp1(?=exp2) 表示exp1后面的内容要匹配exp2,返回exp1(前瞻), finditer() 匹配aa前面,返回的Match对象中有区间span,

1
2
3
4
5
6
7
8
9
import re

test = 'aaadaa'
reg = 'aa'
m = re.finditer(r'(?={})'.format(reg),test)
if not m:
print((-1,-1))
else:
print(*[(i.start(),i.end()+1) for i in m], sep='\n')
(0, 1)
(1, 2)
(4, 5)

finderiter() 函数

返回string中所有与pattern相匹配的全部字串,返回形式为迭代器。若匹配成功,match()/search()返回的是Match对象,finditer()返回的也是Match对象的迭代器,获取匹配结果需要调用Match对象的group()、groups或group(index)方法。

1
2
3
4
5
6
7
8
import re

test = 'aaadaa'
reg = 'aa'
m = re.finditer(r'(?={})'.format(reg),test)
x = re.findall(r'(?={})'.format(reg),test)
print(list(m))
print(x)
[<_sre.SRE_Match object; span=(0, 0), match=''>, <_sre.SRE_Match object; span=(1, 1), match=''>, <_sre.SRE_Match object; span=(4, 4), match=''>]
['', '', '']

Regex Substitution

https://www.hackerrank.com/challenges/re-sub-regex-substitution/problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 第一次尝试  部分例子失败

import re

text = '''
a = 1;
b = input();
if a + b > 0 && a - b < 0:
start()
elif a*b > 10 || a/b < 1:
stop()
print set(list(a)) | set(list(b))
'''

pattern = r' && | \|\| '
def change(match):
if match.group(0) == r' && ':
return ' and '
elif match.group(0) == r' || ':
return ' or '
print(re.sub(pattern,change,text))
print(re.findall(pattern,text))
a = 1;
b = input();
if a + b > 0 and a - b < 0:
    start()
elif a*b > 10 or a/b < 1:
    stop()
print set(list(a)) | set(list(b))

[' && ', ' || ']

出错的地方在于:如果出现 ‘ || && ‘, ‘ && || ‘,’ || || ‘,’ && && ‘ 两个连续的字符中间只有一个空格时候,上述代码只会匹配第一个,因为两个字符中间的空格已经消耗掉了,第二个就匹配不成功

需要运用了前瞻后顾祝贺 (?<=exp2)exp1(?=exp2)

exp1(?=exp2) 表示exp1后面的内容要匹配exp2,返回exp1(前瞻)

(?<=exp2)exp1 表示exp1前面的内容要匹配exp2,返回exp1 (后顾)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 第二次尝试 失败

import re

text = '''
a = 1;
b = input();
|| &&
if a + b > 0 && a - b < 0:
start()
elif a*b > 10 || a/b < 1:
stop()
&& ||
print set(list(a)) | set(list(b))

#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.
'''

pattern = r'(?<= )&&|\|\|(?= )'
def change(match):
if match.group(0) == r'&&':
return 'and'
elif match.group(0) == r'||':
return 'or'
print(re.sub(pattern,change,text))
print(re.findall(pattern,text))
a = 1;
b = input();
 or and
if a + b > 0 and a - b < 0:
    start()
elif a*b > 10 or a/b < 1:
    stop()
  and or 
print set(list(a)) | set(list(b))

#Note do not change and& or |or or & or |
#Only change those '&&' which have space on both sides.
#Only change those 'or which have space on both sides.   

['||', '&&', '&&', '||', '&&', '||', '&&', '||', '||']

出错的地方在于: 上述代码还成功匹配了’ &&& ‘和’ ||| ‘,原因是 pattern = r’(?<= )&&|||(?= )’ 其实分成了两组,第一组 ?<= )&& 和 第二组 ||(?= ),所以这其实可以匹配4种

‘ || ‘,’ && ‘,’ &&’,’|| ‘

加括号后
pattern = r’(?<= )(&&|||)(?= )

第一种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 第三次尝试 成功

import re

text = '''
a = 1;
b = input();
|| &&
if a + b > 0 && a - b < 0:
start()
elif a*b > 10 || a/b < 1:
stop()
&& ||
print set(list(a)) | set(list(b))

#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.
'''

pattern = r'(?<= )(&&|\|\|)(?= )'
def change(match):
if match.group(0) == r'&&':
return 'and'
elif match.group(0) == r'||':
return 'or'
print(re.sub(pattern,change,text))
print(re.findall(pattern,text))
a = 1;
b = input();
 or and 
if a + b > 0 and a - b < 0:
    start()
elif a*b > 10 or a/b < 1:
    stop()
  and or 
print set(list(a)) | set(list(b))

#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.   

['||', '&&', '&&', '||', '&&', '||']
1
第二种方法 第一种方法简化 用匿名函数替换 change 函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import re

text = '''
a = 1;
b = input();
|| &&
if a + b > 0 && a - b < 0:
start()
elif a*b > 10 || a/b < 1:
stop()
&& ||
print set(list(a)) | set(list(b))

#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.
'''

pattern = r'(?<= )(&&|\|\|)(?= )'

print(re.sub(pattern,lambda m: 'and' if m.group(0) == '&&' else 'or' ,text))
a = 1;
b = input();
 or and 
if a + b > 0 and a - b < 0:
    start()
elif a*b > 10 or a/b < 1:
    stop()
  and or 
print set(list(a)) | set(list(b))

#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.   

第三种方法 参考方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import re

text = '''
a = 1;
b = input();
|| &&
if a + b > 0 && a - b < 0:
start()
elif a*b > 10 || a/b < 1:
stop()
&& ||
print set(list(a)) | set(list(b))

#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.
'''

text = re.sub(r'(?<= )&&(?= )','and',text)
text = re.sub(r'(?<= )\|\|(?= )','or',text)
print(text)
a = 1;
b = input();
 or and 
if a + b > 0 and a - b < 0:
    start()
elif a*b > 10 or a/b < 1:
    stop()
  and or 
print set(list(a)) | set(list(b))

#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.   

re.sub()

re.sub(pattern, repl, string, count=0, flags=0)

1
2
3
4
5
6
7
import re
test = 'hello James, nihao James'
replacedtest = re.sub(r'hello (\w+), nihao \1','Jamie',test) # 匹配到整个字符串,并用Jamie替换掉整个字符串
print(replacedtest)

retest= re.sub(r'hello (\w+), nihao \1',r'\1',test) # 匹配到整个字符串,并用组1替换掉整个字符串
print(retest)
Jamie
James
1
2
3
4
5
import re
s = 'hello world ! hello hz !'
print(re.findall(r'(\w+) (\w+)',s)) # 可以看到匹配到2了项
r = re.sub(r'(\w+) (\w+)',r'\2 \1',s) # 每项中的2组位置互换
print(r)
[('hello', 'world'), ('hello', 'hz')]
world hello ! hz hello !
1
2
3
4
5
6
7
8
9
10
import re

s = 'Hello, *world*!'
s1 = 'Hello, *world* *good* !'
pattern = r'\*([^\*]+)\*'
print(re.search(pattern,s).group(1)) # 匹配项中的组1
print(re.sub(pattern,r'<em>\1</em>',s))

print(re.findall(pattern,s1))
print(re.sub(pattern,r'<em>\1</em>',s1)) # 匹配到两项,每项的组1替换匹配项
world
Hello, <em>world</em>!
['world', 'good']
Hello, <em>world</em> <em>good</em> !

Validating Roman Numerals

https://www.hackerrank.com/challenges/validate-a-roman-number/problem

1
2
3
4
import re
test = 'CDXXI'
pattern = r'^M{0,3}(CD|CM|D?C{0,3})(XL|XC|L?X{0,3})(IV|IX|V?I{0,3})$'
print(re.match(pattern,test))
<_sre.SRE_Match object; span=(0, 5), match='CDXXI'>

分解

^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$

M{0,4}

This specifies the thousands section and basically restrains it to between 0 and 4000. It’s a relatively simple:

0: matched by M{0}

1000: M matched by M{1}

2000: MM matched by M{2}

3000: MMM matched by M{3}

4000: MMMM matched by M{4}

(CM|CD|D?C{0,3})

Slightly more complex, this is for the hundreds section and covers all the possibilities:

0: matched by D?C{0} (with D not there)

100: C matched by D?C{1} (with D not there)

200: CC matched by D?C{2} (with D not there)

300: CCC matched by D?C{3} (with D not there)

400: CD matched by CD

500: D matched by D?C{0} (with D there)

600: DC matched by D?C{1} (with D there)

700: DCC matched by D?C{2} (with D there)

800: DCCC matched by D?C{3} (with D there)

900: CM matched by CM

(XC|XL|L?X{0,3})

Same rules as previous section but for the tens place:

0: matched by L?X{0} (with L not there)

10: X matched by L?X{1} (with L not there)

20: XX matched by L?X{2} (with L not there)

30: XXX matched by L?X{3} (with L not there)

40: XL matched by XL

50: L matched by L?X{0} (with L there)

60: LX matched by L?X{1} (with L there)

70: LXX matched by L?X{2} (with L there)

80: LXXX matched by L?X{3} (with L there)

90: XC matched by XC

(IX|IV|V?I{0,3})

This is the units section, handling 0 through 9 and also similar to the previous two sections (Roman numerals, despite their seeming weirdness, follow some logical rules once you figure out what they are):

0: matched by V?I{0} (with V not there)

1: I matched by V?I{1} (with V not there)

2: II matched by V?I{2} (with V not there)

3: III matched by V?I{3} (with V not there)

4: IV matched by IV

5: V matched by V?I{0} (with V there)

6: VI matched by V?I{1} (with V there)

7: VII matched by V?I{2} (with V there)

8: VIII matched by V?I{3} (with V there)

9: IX matched by IX

Validating and Parsing Email Addresses

https://www.hackerrank.com/challenges/validating-named-email-addresses/problem?h_r=next-challenge&h_v=zen

1
2
3
4
5
6
7
import email.utils,re

e = 'DEXTER <dexter@hotmail.com>'
pattern = r'^[A-Za-z]+[A-Za-z0-9-\._]+@[A-Za-z]+\.[A-Za-z]{1,3}$'
v = email.utils.parseaddr(e)
if re.search(pattern,v[-1]):
print(e)
DOSHI@hackerrank.com

参考答案提供的正则表达式

^[a-zA-Z][\w-.]*@[A-Za-z]+.[a-zA-Z]{1,3}$

对比自己写的正则

^[A-Za-z]+[A-Za-z0-9-._]+@[A-Za-z]+.[A-Za-z]{1,3}$

\w 等同于[a-z0-9A-Z_]匹配大小写字母、数字和下划线

* 匹配前面的字符0次或多次(>=0)

Hex Color Code

https://www.hackerrank.com/challenges/hex-color-code/problem?h_r%5B%5D=next-challenge&h_r%5B%5D=next-challenge&h_v%5B%5D=zen&h_v%5B%5D=zen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 第一种方法

import re
pattern = r'.+?(#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3})'

txt ='''
#BED
{
color: #FfFdF8; background-color:#aef;
font-size: 123px;
background: -webkit-linear-gradient(top, #f9f9f9, #fff);
}
#Cab
{
background-color: #ABC;
border: 2px dashed #fff;
}
'''
print(re.findall(pattern,txt))


css = []
css.extend(re.findall(pattern,txt))
print(*css,sep='\n')
None
None
None
None
None
None
['#FfFdF8', '#aef', '#f9f9f9', '#fff', '#ABC', '#fff']
#FfFdF8
#aef
#f9f9f9
#fff
#ABC
#fff

对比自己写的

.+?(#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3})

论坛用户提供的正则表达式

(?<!^)(#(?:[\da-fA-F]{3}){1,2})

(?<!exp2)exp1 表示exp1前面的内容不能匹配exp2,返回exp1 (负后顾)

(?<!^) 不从开头匹配

(?:) 不作为分组

([\da-fA-F]{3}){1,2} 表示 [\da-fA-F]{3}或 [\da-fA-F]{6}

用户提供的另一种正则

[\s:](#[a-fA-F0-9]{6}|#[a-fA-F0-9]{3})

[\s:] 匹配空格或冒号

HTML Parser - Part 1

https://www.hackerrank.com/challenges/html-parser-part-1/problem?h_r=next-challenge&h_r%5B%5D%5B%5D=next-challenge&h_r%5B%5D%5B%5D=next-challenge&h_v=zen&h_v%5B%5D%5B%5D=zen&h_v%5B%5D%5B%5D=zen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):

def handle_starttag(self,tag,attrs):
print('Start : {}'.format(tag))
for i in attrs:
print('-> {} > {}'.format(i[0],i[1]))

def handle_endtag(self,tag):
print('End : {}'.format(tag))

def handle_startendtag(self,tag,attrs):
print('Empty : {}'.format(tag))
for i in attrs:
print('-> {} > {}'.format(i[0],i[1]))

code = '\n'.join(input() for i in range(int(input())))
newcode = re.sub(r'<!--.*?-->','',code)
parser = MyHTMLParser()
parser.feed(newcode)
Start : html
Start : head
Start : title
Start : body
-> data-modal-target > None
-> class > 1
Start : h1
Start : br

HTML Parser - Part 2

https://www.hackerrank.com/challenges/html-parser-part-2/problem?h_r=next-challenge&h_r%5B%5D%5B%5D%5B%5D=next-challenge&h_r%5B%5D%5B%5D%5B%5D=next-challenge&h_v=zen&h_v%5B%5D%5B%5D%5B%5D=zen&h_v%5B%5D%5B%5D%5B%5D=zen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from html.parser import HTMLParser


class MyHTMLParser(HTMLParser):

def handle_comment(self,data):

if '\n' in data:
print('>>> Multi-line Comment\n{}'.format(data))
else:
print('>>> Single-line Comment\n{}'.format(data))


def handle_data(self,data):
if data != '\n':
print('>>> Data\n{}'.format(data))


text='''<!--[if IE 9]>IE9-specific content\n<![endif]-->\n<div> Welcome to HackerRank</div>\n<!--[if IE 9]>IE9-specific content<![endif]-->'''

paser = MyHTMLParser()
paser.feed(text)
>>> Multi-line Comment
[if IE 9]>IE9-specific content
<![endif]
>>> Data
 Welcome to HackerRank
>>> Single-line Comment
[if IE 9]>IE9-specific content<![endif]

第一次尝试用了正则

reg = r' <!--.*?>.*>/n.*?<!.*?--> '

re.search(reg,data) 匹配不成功

但是却可以成功匹配到原文text

原因是data是tag之间的内容,去掉标签了,所以匹配不到

Detect HTML Tags, Attributes and Attribute Values

https://www.hackerrank.com/challenges/detect-html-tags-attributes-and-attribute-values/problem?h_r=next-challenge&h_r%5B%5D%5B%5D%5B%5D=next-challenge&h_r%5B%5D%5B%5D%5B%5D=next-challenge&h_v=zen&h_v%5B%5D%5B%5D%5B%5D=zen&h_v%5B%5D%5B%5D%5B%5D=zen&h_r=next-challenge&h_v=zen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from html.parser import HTMLParser
import re

class MyHTMLParser(HTMLParser):

def handle_starttag(self,tag,attrs):
print(tag)
for i in attrs:
print('-> {} > {}'.format(i[0],i[1]))



code='''<head>
<title>HTML</title>
</head>
<object type="application/x-flash"
data="your-file.swf"
width="0" height="0">
<!-- <param name="movie" value="your-file.swf" /> -->
<param name="quality" value="high"/>
</object>'''
newcode = re.sub(r'<!--.*?-->','',code)
parser = MyHTMLParser()
parser.feed(newcode)
head
title
object
-> type > application/x-flash
-> data > your-file.swf
-> width > 0
-> height > 0
param
-> name > quality
-> value > high

Validating UID

https://www.hackerrank.com/challenges/validating-uid/problem?h_r%5B%5D=next-challenge&h_r%5B%5D=next-challenge&h_r%5B%5D=next-challenge&h_r%5B%5D%5B%5D%5B%5D%5B%5D=next-challenge&h_r%5B%5D%5B%5D%5B%5D%5B%5D=next-challenge&h_v%5B%5D=zen&h_v%5B%5D=zen&h_v%5B%5D=zen&h_v%5B%5D%5B%5D%5B%5D%5B%5D=zen&h_v%5B%5D%5B%5D%5B%5D%5B%5D=zen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 第一种方法 自己写的

import re

uid = 'B1CD102354'


def check_UID(UID):
reg = r'[a-zA-Z0-9]{10}'
upper = 0
digits = 0

for i in uid:
if i.isupper():
upper += 1
if i.isdigit():
digits += 1

if (upper>=2) and (digits>=3) and bool(re.search(reg,uid)) and len(set(uid)) == 10:
return 'Valid'
else:
return 'Invalid'

check_UID(uid)
'Valid'

思路:

str.isupper(),str.isdigit() 计算大写字母和数字的个数,并判断

len(set(uid)) == 10 用set()去掉重复字符,用len()判断长度

bool(re.search(r'[a-zA-Z0-9]{10}',uid)) 判断是否由字母数字组成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 第二种方法 官方参考答案

import re

N = 'B1CD102354'

if N.isalnum() and len(N) == 10:
if bool(re.search(r'(.*[A-Z]){2,}',N)) and bool(re.search(r'(.*[0-9]){3,}',N)):
if re.search(r'.*(.).*\1+.*',N):
print('Invalid')
else:
print('Valid')
else:
print('Invalid')
else:
print('Invalid')
Invalid

知识点

str.isalnum() 检测字符串是否由字母和数字组成。

str.islower() 检测字符串是否由小写字母组成。

str.isupper() 检测字符串中所有的字母是否都为大写。

str.isdigit() 检测字符串是否只由数字组成。

str.isalpha() 检测字符串是否只由字母组成。

str.isspace() 检测字符串是否只由空格组成。

str.istitle() 检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。

思路

1
2
3
4
5
6
7
8
9
import re

uid = 'B7D1D02354'
reg = r'(.*?[A-Z])'
print(re.findall(reg,uid))
reg = r'(.*?[A-Z]){2,}'
print(re.search(reg,uid).group())
reg = r'(?:.*?[A-Z]){2,}'
print(re.findall(reg,uid))
['B', '7D', '1D']
B7D1D
['B7D1D']

Proudly powered by Hexo and Theme by Hacker
© 2019 Jamie