A(65), Z(90), a(97), z(122)
from collections import Counter
w = "hello world"
print(Counter(w))
# {'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
# 리스트를 인자로 받으면
print(Counter(['hi', 'hey', 'hi', 'hi', 'hello', 'hey']))
# {'hey': 2, 'hi': 3, 'hello': 1}
print(Counter(['hi', 'hey', 'hi', 'hi', 'hello', 'hey']).most_common())
# {'hi': 3, 'hey': 2, 'hello': 1}
# 리스트를 문자열로
result = ' '.join(map(str, str_list))
# 문자열을 리스트로
result = list(str)
result = list(map(int, arr[i][0].split(":")))
import re
p = re.compile('[a-z]+') # 패턴 객체
m = p.match('python')
print(m) # <re.Match object; span=(0,6), match='python'>
m = p.match('3 python')
print(m) # None
import re
p = re.compile('[a-z]+')
m = p.search('python')
print(m) # <re.Match object; span=(0,6), match='python'>
m = p.match('3 python')
print(m) # <re.Match object; span=(2,8), match='python'>
m = re.search('[a-z]+', 'python)
print(m) # <re.Match object; span=(0,6), match='python'>
import re
p = re.compile('[a-z]+')
m = p.findall('life is too short')
print(m) # ['life', 'is', 'too', 'short']
import re
p = re.compile('[a-z]+')
m = p.finditer('life is too short')
for r in m:
print(r)
# <re.Match object; span=(0,4), match='life'>
# <re.Match object; span=(5,7), match='is'>
# <re.Match object; span=(8,11), match='too'>
# <re.Match object; span=(12,17), match='short'>
import re
p = re.compile('[a-z]+')
m = p.match('python')
print(m.group()) # python
print(m.start()) # 0
print(m.end()) # 6
print(m.span()) # (0,6)
p = re.compile('a.b', re.DOTALL) # = re.compile('a.b', re.S)
m = p.match('a\nb')
print(m) # <re.Match object; span=(0,3), match='a\nb'>
p = re.compile('[a-z]', re.I)
print(p.match('python')) # <re.Match object; span=(0,1), match='p'>
print(p.match('Python')) # <re.Match object; span=(0,1), match='P'>
print(p.match('PYTHON')) # <re.Match object; span=(0,1), match='P'>
data = """python one
life is too short
python two
you need python
python three"""
p = re.compile("^python\s\w+", re.M)
print(p.findall(data)) # ['python one', 'python two', 'python three']
charref = re.compile(r"""
&[#]
(
0[0-7]+
| [0-9]+
| x[0-9a-fA-F]+
)
;
""", re.X)
팰린드롬은 앞뒤를 뒤집어도 똑같은 문자열을 말함
복잡하게 문자열을 절반으로 나눠서 비교할 생각하지 말고 걍 전체를 뒤집는 게 쉽다!
https://school.programmers.co.kr/learn/courses/30/lessons/42893
pages는 다음과 같이 3개의 웹페이지에 해당하는 HTML 문자열이 순서대로 들어있음
<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta property="og:url" content="https://a.com"/>
</head>
<body>
Blind Lorem Blind ipsum dolor Blind test sit amet, consectetur adipiscing elit.
<a href="https://b.com"> Link to b </a>
</body>
</html>
import re
def solution(word, pages):
webpage = []
webpageName = []
webpageGraph = dict() # 나를 가리키는 외부 링크
for page in pages:
url = re.search('<meta property="og:url" content="(\S+)"', page).group(1)
basicScore = 0
for f in re.findall(r'[a-zA-Z]+', page.lower()):
if f == word.lower():
basicScore += 1
exiosLink = re.findall('<a href="(https://[\S]*)"', page)
for link in exiosLink:
if link in webpageGraph:
webpageGraph[link].append(url)
else:
webpageGraph[link] = [url]
webpageName.append(url)
webpage.append([url, basicScore, len(exiosLink)])
# 링크점수 = 해당 웹페이지로 링크가 걸린 다른 웹페이지의 기본점수 ÷ 외부 링크 수의 총합
# 매칭점수 = 기본점수 + 링크점수
maxValue = 0
result = 0
for i in range(len(webpage)):
url = webpage[i][0]
score = webpage[i][1]
if url in webpageGraph.keys():
# 나를 가리키는 다른 링크의 기본점수 ÷ 외부 링크 수의 총합을 구하기 위해
for link in webpageGraph[url]:
a, b, c = webpage[webpageName.index(link)]
score += (b / c)
if maxValue < score:
maxValue = score
result = i
return result