Skip to content

Instantly share code, notes, and snippets.

@Surgo
Created January 27, 2011 13:19
Show Gist options
  • Save Surgo/798490 to your computer and use it in GitHub Desktop.
Save Surgo/798490 to your computer and use it in GitHub Desktop.
SRM494 - div2 - level one
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""SRM494 - div2 - level one
Mr. White is a very versatile person - absolutely everything is interesting to him.
Perhaps this is why he has many friends. Quite unfortunately, however, none of his
friends are versatile at all. Each of them is interested only in two topics and
refuses to talk about anything else. Therefore, each time Mr. White organizes a party,
it's a big problem for him to decide whom to invite so that the party is interesting
to everybody. Now that Mr. White has a lot of experience in organizing parties,
he knows for sure that a party will be interesting if and only if there's a topic
interesting to each of the invited friends.
You will be given String[]s first and second. The i-th friend of Mr. White is
interested in topics first[i] and second[i]. Return the largest number of friends
that Mr. White can invite to his party so that the party will be interesting.
Definition:
Class: InterestingParty
Method: bestInvitation
Parameters: String[], String[]
Returns: int
Method signature: int bestInvitation(String[] first, String[] second)
"""
class InterestingParty(object):
"""Interesting Party
Constraints:
- first will contain between 1 and 50 elements, inclusive.
- second will contain the same number of elements as first.
- Each element of first and second will contain between 1 and 15 characters, inclusive.
- Each character in first and second will be a lowercase letter ('a'-'z').
- For each valid i, first[i] will not be the same as second[i].
>>> party = InterestingParty()
>>> print party.bestInvitation(
... ["fishing", "gardening", "swimming", "fishing", ],
... ["hunting", "fishing", "fishing", "biting", ])
4
>>> print party.bestInvitation(
... ["variety", "diversity", "loquacity", "courtesy", ],
... ["talking", "speaking", "discussion", "meeting", ])
1
>>> print party.bestInvitation(
... ["snakes", "programming", "cobra", "monty", ],
... ["python", "python", "anaconda", "python", ])
3
>>> print party.bestInvitation(
... ["t", "o", "p", "c", "o", "d", "e", "r", "s", "i", "n", "g", "l", "e", "r",
... "o", "u", "n", "d", "m", "a", "t", "c", "h", "f", "o", "u", "r", "n", "i", ],
... ["n", "e", "f", "o", "u", "r", "j", "a", "n", "u", "a", "r", "y", "t", "w",
... "e", "n", "t", "y", "t", "w", "o", "s", "a", "t", "u", "r", "d", "a", "y"])
6
"""
def __init__(self):
pass
def bestInvitation(self, first=[], second=[]):
topics = {}
for i in range(len(first)):
if first[i] not in topics.keys():
topics[first[i]] = 0
if second[i] not in topics.keys():
topics[second[i]] = 0
topics[first[i]] += 1
topics[second[i]] += 1
return max([topic for topic in topics.values()])
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment