[プログラマ-Phython]フルナビゲーション
16548 ワード
フルナビゲーション brute force algorithm
模擬試験
わあ、やっと初めての試みで正解!レベル1だからかもdef solution(answers):
tester1 = [1, 2, 3, 4, 5]
tester2 = [2, 1, 2, 3, 2, 4, 2, 5]
tester3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
rights = [0,0,0]
count = 0
for answer in answers:
if answer == tester1[count%len(tester1)]:
rights[0] += 1
if answer == tester2[count%len(tester2)]:
rights[1] += 1
if answer == tester3[count%len(tester3)]:
rights[2] += 1
count += 1
max = rights[0]
result = [1]
if rights[1] > max:
max = rights[1]
result = [2]
elif rights[1] == max:
result.append(2)
if rights[2] > max:
max = rights[2]
result = [3]
elif rights[2] == max:
result.append(3)
小数点を検索
コンビネーションで再帰的に解くコードを書きたかったのですが、難しすぎてライブラリを書きました.
モンガ...とても塩辛い感じ...#coding:utf8
# 소수 찾기
# 완전 탐색
from itertools import permutations
def solution(numbers):
answer = 0
# 중복 조합 생성
perList = []
for i in range(len(numbers)):
pers = list(permutations(list(numbers),i+1))
for per in pers:
per = int("".join(per))
if per not in perList:
perList.append(per)
# 소수인지 체크
for num in perList:
primeBool = True
if num == 1 or num == 0:
primeBool = False
else:
for j in range(2,num):
if num%j == 0:
primeBool = False
break
if primeBool:
answer += 1
return answer
print(solution("011"))
じゅうたん
あ、私はすぐに解けて、ぼんやりしていて、他の人の草は素晴らしいです.#coding:utf8
# 카펫
# 완전탐색
def solution(brown, yellow):
answer = []
divisors = []
# yellow의 약수구하기
for i in range(1,yellow):
if yellow % i == 0:
if yellow/i < i :
break
divisors.append(yellow/i)
divisors.append(i)
if yellow == 1:
divisors = [1,1]
# 가로세로 길이 구하기
for j in range(0, len(divisors), 2):
if brown == divisors[j]*2 + divisors[j+1]*2 + 4:
answer = [divisors[j]+2,divisors[j+1]+2]
break
return answer
print(solution(10, 2))
ははははははははははははははは
Reference
この問題について([プログラマ-Phython]フルナビゲーション), 我々は、より多くの情報をここで見つけました
https://velog.io/@dchecheb/programmers-python-완전탐색
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
def solution(answers):
tester1 = [1, 2, 3, 4, 5]
tester2 = [2, 1, 2, 3, 2, 4, 2, 5]
tester3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
rights = [0,0,0]
count = 0
for answer in answers:
if answer == tester1[count%len(tester1)]:
rights[0] += 1
if answer == tester2[count%len(tester2)]:
rights[1] += 1
if answer == tester3[count%len(tester3)]:
rights[2] += 1
count += 1
max = rights[0]
result = [1]
if rights[1] > max:
max = rights[1]
result = [2]
elif rights[1] == max:
result.append(2)
if rights[2] > max:
max = rights[2]
result = [3]
elif rights[2] == max:
result.append(3)
#coding:utf8
# 소수 찾기
# 완전 탐색
from itertools import permutations
def solution(numbers):
answer = 0
# 중복 조합 생성
perList = []
for i in range(len(numbers)):
pers = list(permutations(list(numbers),i+1))
for per in pers:
per = int("".join(per))
if per not in perList:
perList.append(per)
# 소수인지 체크
for num in perList:
primeBool = True
if num == 1 or num == 0:
primeBool = False
else:
for j in range(2,num):
if num%j == 0:
primeBool = False
break
if primeBool:
answer += 1
return answer
print(solution("011"))
#coding:utf8
# 카펫
# 완전탐색
def solution(brown, yellow):
answer = []
divisors = []
# yellow의 약수구하기
for i in range(1,yellow):
if yellow % i == 0:
if yellow/i < i :
break
divisors.append(yellow/i)
divisors.append(i)
if yellow == 1:
divisors = [1,1]
# 가로세로 길이 구하기
for j in range(0, len(divisors), 2):
if brown == divisors[j]*2 + divisors[j+1]*2 + 4:
answer = [divisors[j]+2,divisors[j+1]+2]
break
return answer
print(solution(10, 2))
Reference
この問題について([プログラマ-Phython]フルナビゲーション), 我々は、より多くの情報をここで見つけました https://velog.io/@dchecheb/programmers-python-완전탐색テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol