def search_movies(request): target = request.GET.get('word') target_words = target.split() target_words = [word.strip() for word in target_words] movies = Movie.objects.none() # case 1 : From TMDB, using search api searchURL = URLMaker() for word in target_words: res = requests.get(searchURL.searchMovie(word)).json()['results'] for movie in res: if Movie.objects.filter(title=movie['title']).exists(): movies |= Movie.objects.filter(title=movie['title']) else: new_movie = add_new_movie(movie) movies |= new_movie # case 2 : search word in DB for i in range(len(target_words), 0, -1): for com in coms(target_words, i): movies |= Movie.objects.filter( reduce(operator.and_, (Q(title__contains=x) for x in com)) | reduce(operator.and_, (Q(origin_title__contains=x) for x in com)) | reduce(operator.and_, (Q(overview=x) for x in com))) if len(movies) == 0: return Response({'error': 'Not fount'}, status=status.HTTP_404_NOT_FOUND) serializer = MovieSerializer(movies, many=True) return Response(serializer.data)
def recommend_movies(request): movies = Movie.objects.none() if request.user.is_authenticated: user_pk = request.user.pk user = get_object_or_404(get_user_model(), pk=user_pk) like_movies = user.like_movies.all() if like_movies.exists(): prefer_dict = {} for movie in like_movies: for genre in movie.genres.all(): if genre.id in prefer_dict: prefer_dict[genre.id] += 1 else: prefer_dict[genre.id] = 1 user_prefer = {} for key, value in prefer_dict.items(): if value in user_prefer: user_prefer[value].append(key) else: user_prefer[value] = [key] keys = sorted(list(user_prefer.keys()), reverse=True) # case 1 : Recommend movie using TMDB api recommendURL = URLMaker() like_movie_ids = [m.id for m in like_movies] for id in like_movie_ids: data = request.get( recommendURL.recommendMovie(id)).json()['results'] for movie in data: if Movie.objects.get(title=movie['title']).exists(): movies |= Movie.objects.filter(title=movie['title']) else: new_movie = add_new_movie(movie) movies |= new_movie # case 2 : Recommend movie by genre in DB for i in range(0, len(keys)): temp_genres = reduce( lambda x, m: x + m, [user_prefer[keys[j]] for j in range(i + 1)], []) for j in range(len(temp_genres), 0, -1): for com in coms(temp_genres, j): temp_movies = Movie.objects.all() for cur_genre in com: temp_movies = temp_movies.filter(genres=cur_genre) movies |= temp_movies if len(movies) >= 36: pks = random.sample([x.id for x in movies], 12) temp_movies = movies.filter(id__in=pks).order_by('?') serializer = MovieSerializer(temp_movies, many=True) return Response(serializer.data) if len(movies) < 12: pks = random.sample(range(1, 1001), 12 - len(movies)) temp_movies = Movie.objects.filter(id__in=pks) movies |= temp_movies movies = movies.order_by('?') serializer = MovieSerializer(movies, many=True) return Response(serializer.data)
def solution(nums): check=[1]*3001 check[:2]=[0,0] for i in range(2,3001): if check[i]==0: continue for j in range(2*i,3001,i): check[j]=0 return sum([check[sum(com)] for com in coms(nums,3)])
def solution(relation): answer = [] for num in range(1, len(relation[0]) + 1): for com in coms([i for i in range(len(relation[0]))], num): ck = [] for x in range(len(relation)): ck.append(tuple([relation[x][y] for y in com])) if len(ck) == len(set(ck)): answer.append(list(com)) last = [1] * len(answer) for i in range(len(answer)): if last[i] == 0: continue for j in range(i + 1, len(answer)): for k in range(len(answer[i])): if not answer[i][k] in answer[j]: break else: last[j] = 0 return sum(last)
def move(enemies): for i in range(len(enemies)): if alive[i]: if enemies[i][0] < N - 1: enemies[i][0] += 1 else: alive[i] = False N, M, D = map(int, input().split()) field = [list(map(int, input().split())) for _ in range(N)] s_enemies = [] for r in range(N): for c in range(M): if field[r][c]: s_enemies.append([r, c]) answer = 0 for archers in coms(zip([N] * M, range(M)), 3): kill = 0 enemies = deepcopy(s_enemies) alive = [True] * len(enemies) while any(alive): shoot(archers) move(enemies) if kill > answer: answer = kill print(answer)
room[ny][nx] = 2 q.append((ny, nx)) return cnt N, M = map(int, input().split()) room_origin = [[int(x) for x in input().split()] for _ in range(N)] viruses, walls, empties = [], [], [] len_viruses = 0 len_walls = 3 for r in range(N): for c in range(M): if room_origin[r][c] == 1: walls.append((r, c)) len_walls += 1 elif room_origin[r][c] == 2: viruses.append((r, c)) len_viruses += 1 else: empties.append((r,c)) answer = N * M - len_walls for com in coms(empties, 3): room = deepcopy(room_origin) for r, c in com: room[r][c] = 1 cnt_infected = infected() if cnt_infected and answer > cnt_infected: answer = cnt_infected print(N*M-len_walls-answer)
from itertools import combinations as coms N = int(input()) p = [list(map(int, input().split())) for j in range(N)] answer = 1e9 for t1 in coms(range(N), N // 2): t2 = [i for i in range(N) if i not in t1] s1, s2 = 0, 0 for i in range(N): if i in t1: for j in t1: s1 += p[i][j] else: for j in t2: s2 += p[i][j] answer = min(answer, abs(s1 - s2)) print(answer)
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1]) def find_nearest_store(house, stores): short_dist = (2 * N)**2 for store in stores: if dist(house, store) < short_dist: short_dist = dist(house, store) return short_dist N, M = map(int, input().split()) board = [list(map(int, input().split())) for _ in range(N)] houses = [] stores = [] for j in range(N): for i in range(N): if board[j][i] == 1: houses.append((j, i)) elif board[j][i] == 2: stores.append((j, i)) ans = (2 * N)**2 for com in coms(stores, M): temp_sum = 0 for house in houses: temp_sum += find_nearest_store(house, com) ans = min(ans, temp_sum) print(ans)
while q: cur_edge = q.pop() for next_edge in MAP[cur_edge]: if not check[next_edge]: q.append(next_edge) check[next_edge] = True return all(check) MAX = 0xFFFFFFF N = int(input()) ppls = list(map(int, input().split())) MAP = {i: [] for i in range(1, N + 1)} for i in range(1, N + 1): MAP[i] = list(map(int, input()[2:].split())) answer = MAX for i in range(1, N // 2 + 1): for group1 in coms(range(1, N + 1), i): group2 = [k for k in range(1, N + 1) if k not in group1] if is_section(group1) and is_section(group2): answer = min( answer, abs( sum([ppls[x - 1] for x in group1]) - sum([ppls[x - 1] for x in group2]))) if answer == MAX: answer = -1 print(answer)
if alive[i]: if enemies[i][0] < N: enemies[i][0] += 1 else: alive[i] = False N, M, D = map(int, input().split()) field = [list(map(int, input().split())) for _ in range(N)] s_enemies = [] for r in range(N): for c in range(M): if field[r][c]: s_enemies.append([r, c]) answer = 0 for archers in coms(zip([N] * N, range(N)), 3): kill = 0 enemies = deepcopy(s_enemies) alive = [True] * len(enemies) while any(alive): print(enemies) print(archers) shoot(archers) move(enemies) if kill > answer: answer = kill print(answer)