Created
December 8, 2013 06:58
-
-
Save kurehajime/7854107 to your computer and use it in GitHub Desktop.
いくつかの質問に答えると、次の回答を予想するプログラム ref: http://qiita.com/kurehajime/items/ca0a90ba063a23b714fc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
犬より猫が好きですか? 1.はい,0.いいえ :1 | |
理系ですか? 1.はい,0.いいえ :1 | |
パンよりご飯が好きですか? 1.はい,0.いいえ :1 | |
★あなたは男ですね?(確率52.50%) 1.はい,0.いいえ : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import random | |
questions = ["犬より猫が好き", | |
"理系", | |
"男", | |
"パンよりご飯が好き", | |
] | |
ourAnswers = [[0,1,1,1], | |
[1,1,1,0], | |
[0,0,1,0], | |
[0,1,0,1], | |
[1,0,0,1], | |
] | |
def quiz(): | |
u"""いくつか質問をします。その後、あなたが答えるであろう回答を予想します。 | |
""" | |
target = random.randint(0,len(questions)-1) | |
yourAnswer = len(questions)*[0] | |
rates = len(questions)*[0] | |
for i, question in enumerate(questions): | |
if i == target: | |
continue | |
yourAnswer[i] = int(raw_input(question+"ですか? 1.はい,0.いいえ :")) | |
for i in xrange(len(questions)): | |
i_is1count = 0 | |
target_is1count = 0 | |
for ourAns in ourAnswers: | |
if ourAns[i] == yourAnswer[i] or i == target: | |
i_is1count += 1 | |
if ourAns[target] == 1: | |
target_is1count += 1 | |
if i_is1count == 0: | |
rates[i] = 0.5 | |
else: | |
rates[i] = float(target_is1count) / i_is1count | |
rate = sum(rates)/len(rates) | |
if rate >= 0.5: | |
yourAnswer[target] = int(raw_input("★あなたは{0}ですね?(確率{1:.2f}%) 1.はい,0.いいえ :".format(questions[target],rate*100))) | |
else: | |
yourAnswer[target] = 1 - int(raw_input("★あなたは{0}ではないですね?(確率{1:.2f}%) 1.はい,0.いいえ :".format(questions[target],100-rate*100))) | |
ourAnswers.append(yourAnswer[:]) | |
if __name__ == "__main__": | |
while(True): | |
quiz() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment