Last active
August 29, 2015 14:08
-
-
Save KentaYamada/63eb031ce8f9fe876b1f to your computer and use it in GitHub Desktop.
TopCoderの全探索に関する問題を解いてみた
This file contains hidden or 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 -*- | |
| # Listゆうに指定した値がいくつか数えたら速くない?ってなってやってみる | |
| def bestInvitation(first, second): | |
| l = first + second | |
| for i in l: | |
| if i not in dic: | |
| dic.setdefault(i, l.count(i)) | |
| return max(dic.values()) |
This file contains hidden or 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 -*- | |
| # 最近ハマってる内包表記で書く | |
| def bestInvitation(first, second): | |
| l = first + second | |
| dic = {i:l.count(i) for i in l} | |
| return max(dic.values()) |
This file contains hidden or 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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| namespace ConsoleApplication1 | |
| { | |
| class Program | |
| { | |
| public int bestInvitation(string[] first, string[] second) | |
| { | |
| var dic = new Dictionary<string, int>(); | |
| foreach (string s in first.Concat(second)) | |
| { | |
| if (!dic.ContainsKey(s)) | |
| { | |
| dic.Add(s, 1); | |
| } | |
| else | |
| { | |
| dic[s] += 1; | |
| } | |
| } | |
| return dic.Values.Max(); | |
| } | |
| } | |
| } |
This file contains hidden or 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 -*- | |
| # 初見で作ってみたコード | |
| # 重複が気になる… | |
| def bestInvitation(first, second): | |
| dic = {} | |
| for i in first: | |
| if i in dic: | |
| dic[i] = dic[i] + 1 | |
| else: | |
| dic.setdefault(i, 1) | |
| for j in second: | |
| if j in dic: | |
| dic[j] = dic[j] + 1 | |
| else: | |
| dic.setdefault(j, 1) | |
| return max(dic.values()) |
This file contains hidden or 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 -*- | |
| # Listオブジェクトって連結できるんちゃう?って調べたらできた。 | |
| # これでLoopを1つ書かずに済んだ。 | |
| def bestInvitation(first, second): | |
| l = first + second | |
| for i in l: | |
| if i in dic: | |
| dic[i] = dic[i] + 1 | |
| else: | |
| dic.setdefault(i, 1) | |
| return max(dic.values()) |
This file contains hidden or 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する時はテストしたいモジュールをImportしてね | |
| from best_invitation import bestInvitation | |
| import unittest | |
| class BestInvitationTest(unittest.TestCase): | |
| def test_pattern1(self): | |
| first = ['fishing', 'gardening', 'swimming', 'fishing'] | |
| second = ['hunting', 'fishing', 'fishing', 'biting'] | |
| result = bestInvitation(first, second) | |
| self.assertEqual(4, result) | |
| def test_pattern2(self): | |
| first = ['variety', 'diversity', 'loquacity', 'courtesy'] | |
| second = ['talking', 'speaking', 'discusson', 'meeting'] | |
| result = bestInvitation(first, second) | |
| self.assertEqual(1, result) | |
| if __name__ == '__main__': | |
| unittest.main() |
This file contains hidden or 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
| 問題文要約 | |
| Aさんは多才で、友達がたくさんいます。 | |
| Aさんの友達は各々2つの話題に興味があり、自分が関心のある話題を話すを拒むそうな(困ったもんだ) | |
| そこで、共通の話題を持った友人たちをパーティーに招待しようと考えました。 | |
| パーティーに招待するためにAさんの友達の中でそれぞれ共通の趣味を持っていうのが何人かを数えるってのが問題です。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment