Created
April 9, 2016 01:27
-
-
Save PseudoSky/3659be9836e0affe95f0fcb631f5441a to your computer and use it in GitHub Desktop.
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
#python common_friends.py friend.txt -q | |
from mrjob.job import MRJob | |
from mrjob.step import MRStep | |
from itertools import chain | |
class MAPR(MRJob): | |
def mapper(self, _, line): | |
p=line.split(" : ") | |
f=sorted(p[1].split()) | |
for p2 in f: yield(sorted([p[0],p2]),f) | |
def reducer(self, key, values): | |
values=list(values) | |
yield(key,sorted(list(set(values[0]) & set(values[1])))) | |
# ["A", "B"] ["C", "D"] | |
# ["A", "C"] ["B", "D"] | |
# ["A", "D"] ["B", "C"] | |
# ["B", "C"] ["A", "D", "E"] | |
# ["B", "D"] ["A", "C", "E"] | |
# ["B", "E"] ["C", "D"] | |
# ["C", "D"] ["A", "B", "E"] | |
# ["C", "E"] ["B", "D"] | |
# ["D", "E"] ["B", "C"] | |
if __name__ == '__main__': | |
MAPR.run() |
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
#python common_friends.py konigsberg.txt -q | |
from mrjob.job import MRJob | |
from mrjob.step import MRStep | |
from itertools import chain | |
def len(iterable): | |
try: | |
return iterable.__len__() | |
except AttributeError: | |
return sum(1 for _ in iterable) | |
class MAPR(MRJob): | |
def mapper(self, _, line): | |
p=line.split() | |
yield(p[0],p[1]) | |
yield(p[1],p[0]) | |
def combiner(self, key, values): | |
l=list(values) | |
yield('euler',[int(key),len(l)]) | |
def reducer(self,key, values): | |
values=list(values) | |
euler=True | |
for v in values: | |
yield(v[0],v[1]) | |
if (v[1]%2)==1: euler=False | |
yield('euler',euler) | |
if __name__ == '__main__': | |
MAPR.run() | |
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
A : B C D | |
B : A C D E | |
C : A B D E | |
D : A B C E | |
E : B C D |
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
#python common_friends.py jumble.txt -q | |
from mrjob.job import MRJob | |
from mrjob.step import MRStep | |
from itertools import permutations | |
diction=set(line.strip() for line in open('sowpods.txt')) | |
class MAPR(MRJob): | |
def mapper(self, _, line): | |
w=line.split()[0] | |
for p in set(permutations(w)): | |
yield('?'+w,''.join(p)) | |
def combiner(self,key, values): | |
for v in values: | |
if(v in diction): | |
if(v!=key[1:]):yield(key,v) | |
def reducer(self, key, values): | |
values=sorted([key]+list(values)) | |
yield(len(values),values) | |
if __name__ == '__main__': | |
MAPR.run() | |
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
laisa ? | |
laurr ? | |
bureek ? | |
prouot ? | |
clofa ? | |
circle ? | |
mursee ? | |
mirpte ? | |
velga ? | |
pluit ? | |
siccur ? | |
impage ? |
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 | |
0 1 | |
0 2 | |
1 2 | |
2 3 | |
1 3 | |
3 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment