Skip to content

Commit

Permalink
reformat to adhere to PEP8 style
Browse files Browse the repository at this point in the history
  • Loading branch information
idf committed Oct 27, 2015
1 parent 1f5121f commit e03a790
Show file tree
Hide file tree
Showing 113 changed files with 5,626 additions and 5,596 deletions.
150 changes: 75 additions & 75 deletions A Chocolate Fiesta.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
# -*- coding: utf-8 -*-
"""
Problem Statement
Welcome to the exciting class of Professor Manasa. In each lecture she used to play some game while teaching a new
concept. Today's topic is Set Theory. For today's game, she had given a set A = {a1, a2, ...aN} of N integers to her
students and asked them to play the game as follows.
At each step of the game she calls a random student and asks him/her to select a non-empty subset from set A such that
this subset had not been selected earlier and the sum of subset should be even. This game ends when all possible subsets
had been selected. Manasa needs your help in counting the total number of times students can be called assuming each
student gives the right answer. While it is given that if two numbers are same in the given set, they have different
colors. It means that if a1 = a2, then choosing a1 and choosing a2 will be considered as different sets.
"""
__author__ = 'Danyang'
MOD = 10**9+7


class Solution(object):
def solve_error(self, cipher):
"""
count odd number and even number
:param cipher: the cipher
"""
N, lst = cipher
odd_cnt = len(filter(lambda x: x%2==1, lst))
even_cnt = N-odd_cnt

a = (2**even_cnt)%MOD

result = 0
result += a-1

i = 2
comb = (odd_cnt)*(odd_cnt-1)/(1*2)
while i<=odd_cnt:
result += comb*a
result %= MOD
i += 2
comb *= (odd_cnt-i+1)*(odd_cnt-i)/((i-1)*i)
comb %= MOD

return result

def solve(self, cipher):
"""
count odd number and even number
:param cipher: the cipher
"""
N, lst = cipher
odd_cnt = len(filter(lambda x: x%2==1, lst))
even_cnt = N-odd_cnt

a = (2**even_cnt)%MOD
b = (2**(odd_cnt-1))%MOD

if odd_cnt!=0:
result = a-1+(b-1)*a
else:
result = a-1

return result%MOD


if __name__=="__main__":
import sys

f = open("1.in", "r")
# f = sys.stdin
N = int(f.readline().strip())
lst = map(int, f.readline().strip().split(' '))
cipher = N, lst
# solve
s = "%s\n"%(Solution().solve(cipher))
print s,
# -*- coding: utf-8 -*-
"""
Problem Statement
Welcome to the exciting class of Professor Manasa. In each lecture she used to play some game while teaching a new
concept. Today's topic is Set Theory. For today's game, she had given a set A = {a1, a2, ...aN} of N integers to her
students and asked them to play the game as follows.
At each step of the game she calls a random student and asks him/her to select a non-empty subset from set A such that
this subset had not been selected earlier and the sum of subset should be even. This game ends when all possible subsets
had been selected. Manasa needs your help in counting the total number of times students can be called assuming each
student gives the right answer. While it is given that if two numbers are same in the given set, they have different
colors. It means that if a1 = a2, then choosing a1 and choosing a2 will be considered as different sets.
"""
__author__ = 'Danyang'
MOD = 10 ** 9 + 7


class Solution(object):
def solve_error(self, cipher):
"""
count odd number and even number
:param cipher: the cipher
"""
N, lst = cipher
odd_cnt = len(filter(lambda x: x % 2 == 1, lst))
even_cnt = N - odd_cnt

a = (2 ** even_cnt) % MOD

result = 0
result += a - 1

i = 2
comb = (odd_cnt) * (odd_cnt - 1) / (1 * 2)
while i <= odd_cnt:
result += comb * a
result %= MOD
i += 2
comb *= (odd_cnt - i + 1) * (odd_cnt - i) / ((i - 1) * i)
comb %= MOD

return result

def solve(self, cipher):
"""
count odd number and even number
:param cipher: the cipher
"""
N, lst = cipher
odd_cnt = len(filter(lambda x: x % 2 == 1, lst))
even_cnt = N - odd_cnt

a = (2 ** even_cnt) % MOD
b = (2 ** (odd_cnt - 1)) % MOD

if odd_cnt != 0:
result = a - 1 + (b - 1) * a
else:
result = a - 1

return result % MOD


if __name__ == "__main__":
import sys

f = open("1.in", "r")
# f = sys.stdin
N = int(f.readline().strip())
lst = map(int, f.readline().strip().split(' '))
cipher = N, lst
# solve
s = "%s\n" % (Solution().solve(cipher))
print s,
160 changes: 80 additions & 80 deletions A journey to the Moon.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
"""
Problem Statement
The member states of the UN are planning to send 2 people to the Moon. But there is a problem. In line with their
principles of global unity, they want to pair astronauts of 2 different countries.
There are N trained astronauts numbered from 0 to N-1. But those in charge of the mission did not receive information
about the citizenship of each astronaut. The only information they have is that some particular pairs of astronauts
belong to the same country.
"""
__author__ = 'Danyang'


class DisjointSet(object):
def __init__(self, n):
self.rank = [1 for _ in xrange(n)]
self.parent = [i for i in xrange(n)]
self.n = n

def find(self, i):
if i==self.parent[i]:
return i
else:
self.parent[i] = self.find(self.parent[i]) # directly point to ancestor
return self.parent[i]

def union(self, i, j):
x = self.find(i)
y = self.find(j)

if x==y: return
self.parent[x] = y
self.rank[y] += self.rank[x]

def card(self):
card = 0
for i in xrange(self.n):
if self.parent[i]==i:
card += 1
return card


class Solution(object):
def solve(self, cipher):
"""
Disjoint-set data structure
Union-Find Algorithm
:param cipher: the cipher
"""
N, pairs = cipher
djs = DisjointSet(N)
for a, b in pairs:
djs.union(a, b)

result = 0
for i in xrange(N):
if djs.find(i)==i: # set representative
# result += (djs.rank[i])*(N-djs.rank[i])/2 # otherwise rounding error
result += (djs.rank[i])*(N-djs.rank[i])
return result/2


if __name__=="__main__":
import sys

f = open("1.in", "r")
# f = sys.stdin
solution = Solution()

N, I = map(int, f.readline().strip().split(' '))

pairs = []
for i in xrange(I):
pairs.append(map(int, f.readline().strip().split(' ')))

cipher = N, pairs
# solve
s = "%s\n"%(solution.solve(cipher))
print s,
"""
Problem Statement
The member states of the UN are planning to send 2 people to the Moon. But there is a problem. In line with their
principles of global unity, they want to pair astronauts of 2 different countries.
There are N trained astronauts numbered from 0 to N-1. But those in charge of the mission did not receive information
about the citizenship of each astronaut. The only information they have is that some particular pairs of astronauts
belong to the same country.
"""
__author__ = 'Danyang'


class DisjointSet(object):
def __init__(self, n):
self.rank = [1 for _ in xrange(n)]
self.parent = [i for i in xrange(n)]
self.n = n

def find(self, i):
if i == self.parent[i]:
return i
else:
self.parent[i] = self.find(self.parent[i]) # directly point to ancestor
return self.parent[i]

def union(self, i, j):
x = self.find(i)
y = self.find(j)

if x == y: return
self.parent[x] = y
self.rank[y] += self.rank[x]

def card(self):
card = 0
for i in xrange(self.n):
if self.parent[i] == i:
card += 1
return card


class Solution(object):
def solve(self, cipher):
"""
Disjoint-set data structure
Union-Find Algorithm
:param cipher: the cipher
"""
N, pairs = cipher
djs = DisjointSet(N)
for a, b in pairs:
djs.union(a, b)

result = 0
for i in xrange(N):
if djs.find(i) == i: # set representative
# result += (djs.rank[i])*(N-djs.rank[i])/2 # otherwise rounding error
result += (djs.rank[i]) * (N - djs.rank[i])
return result / 2


if __name__ == "__main__":
import sys

f = open("1.in", "r")
# f = sys.stdin
solution = Solution()

N, I = map(int, f.readline().strip().split(' '))

pairs = []
for i in xrange(I):
pairs.append(map(int, f.readline().strip().split(' ')))

cipher = N, pairs
# solve
s = "%s\n" % (solution.solve(cipher))
print s,
Loading

0 comments on commit e03a790

Please sign in to comment.