def solution(id_list, report, k):
answer = [0] * len(id_list)
reports = {x : 0 for x in id_list}
for r in set(report):
reports[r.split()[1]] += 1
for r in set(report):
if reports[r.split()[1]] >= k:
answer[id_list.index(r.split()[0])] += 1
return answer
나의 코드
from dataclasses import dataclass
from typing import List, TypeVar
T = TypeVar('T')
@dataclass
class User:
id: str
targets: List[T]
report_count: int = 0
def __init__(self, id):
self.id = id
self.targets = list()
def report_count_increment(self):
self.report_count += 1
def append_target(self, user: T):
self.targets.append(user)
def is_out(self, k: int) -> bool:
if self.report_count >= k:
return True
return False
def solution(id_list, report, k):
answer = []
users = list()
report = set(report)
for id in id_list:
users.append(User(id=id))
for re in report:
splited_report = re.split(' ')
user = get_user_by_id(users, splited_report[0])
target = get_user_by_id(users, splited_report[1])
target.report_count_increment()
user.append_target(target)
for user in users:
cnt = 0
for target in user.targets:
if target.is_out(k):
cnt += 1
answer.append(cnt)
return answer
def get_user_by_id(users: List[User], id: str):
for user in users:
if user.id == id:
return user
def solution(N, number):
answer = -1
if number == N:
return 1
_li = [set() for i in range(8)]
for i in range(len(_li)):
_li[i].add(int(str(N)*(i+1)))
for i in range(1,8):
for j in range(i):
for op1 in _li[j]:
for op2 in _li[i-j-1]:
_li[i].add(op1+op2)
_li[i].add(op1-op2)
_li[i].add(op1*op2)
if op2 != 0:
_li[i].add(op1//op2)
if number in _li[i]:
answer = i+1
break
return answer
8. 이 폴더 내에서 runserver 명령어와 manage.py 파일을 통해 개발용 웹 서버를 띄울 수 있습니다.
>> python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 26, 2018 - 07:06:30
Django version 2.1.2, using settings 'mytestsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
9. 아래와 같이 뜨면 성공입니다!
(웹 브라우저를 띄워서 http://localhost:8000으로 들어가면 됩니다.)
10. 지금까지 컴퓨터에 파이썬과 Django를 설치하면 기본적인 웹 화면을 띄워보았습니다.
A non-empty array A consisting of N integers is given.
Apeakis an array element which is larger than its neighbors. More precisely, it is an index P such that 0 < P < N − 1, A[P − 1] < A[P] and A[P] > A[P + 1].
We want to divide this array into blocks containing the same number of elements. More precisely, we want to choose a number K that will yield the following blocks:
A[0], A[1], ..., A[K − 1],
A[K], A[K + 1], ..., A[2K − 1], ...
A[N − K], A[N − K + 1], ..., A[N − 1].
What's more, every block should contain at least one peak. Notice that extreme elements of the blocks (for example A[K − 1] or A[K]) can also be peaks, but only if they have both neighbors (including one in an adjacent blocks).
The goal is to find the maximum number of blocks into which the array A can be divided.
Array A can be divided into blocks as follows:
one block (1, 2, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2). This block contains three peaks.
two blocks (1, 2, 3, 4, 3, 4) and (1, 2, 3, 4, 6, 2). Every block has a peak.
three blocks (1, 2, 3, 4), (3, 4, 1, 2), (3, 4, 6, 2). Every block has a peak. Notice in particular that the first block (1, 2, 3, 4) has a peak at A[3], because A[2] < A[3] > A[4], even though A[4] is in the adjacent block.
However, array A cannot be divided into four blocks, (1, 2, 3), (4, 3, 4), (1, 2, 3) and (4, 6, 2), because the (1, 2, 3) blocks do not contain a peak. Notice in particular that the (4, 3, 4) block contains two peaks: A[3] and A[5].
The maximum number of blocks that array A can be divided into is three.
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A consisting of N integers, returns the maximum number of blocks into which A can be divided.
If A cannot be divided into some number of blocks, the function should return 0.
Write anefficientalgorithm for the following assumptions:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [0..1,000,000,000].
Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Peaks 문제는 봉우리를 최소 1개씩 포함하고 있는 Block의 개수를 구하는 문제이다.
최대 블록의 개수를 Return 해주면 되는데,
그 방법을 구체적으로 어떻게 짜야되는지 다양하게 시도하다가
결국 답지를 보고 풀었다.ㅜㅜ!!
그래도 이 방법 잘 알아두어야겠다.
정답 코드
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
peaks = []
for i in range(1, len(A)-1):
if A[i] > A[i-1] and A[i] > A[i+1]:
peaks.append(i)
for i in range(len(peaks),0,-1):
if len(A) % i == 0:
block_size = len(A)//i
block = [False]*i
block_cnt = 0
for j in range(len(peaks)):
idx = peaks[j] // block_size
if block[idx] == False:
block[idx] = True
block_cnt += 1
if block_cnt == i:
return block_cnt
return 0
pass