Skip to content

Instantly share code, notes, and snippets.

@hortinstein
Created March 16, 2019 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hortinstein/e8dc35d13ffc7ffed0696150c6e21c6f to your computer and use it in GitHub Desktop.
Save hortinstein/e8dc35d13ffc7ffed0696150c6e21c6f to your computer and use it in GitHub Desktop.
help
def map (k,v):
ret_sum = {}
for key in k:
if key in ret_sum:
ret_sum[key] +=1
else:
ret_sum[key] = 1
for val in v:
if val in ret_sum:
ret_sum[val] -= 1
else:
ret_sum[val] = 0
ret_key = []
ret_val = []
for tk, tv in ret_sum.iteritems():
ret_key.append(tk)
ret_val.append(tv)
return ret_key,ret_val
def reducer(k,v):
ret_red = {}
#i is indext to each list
for i in range(len(k)):
print k[i],v[i]
if v[i] in ret_red:
ret_red[v[i]] += 1
else:
ret_red[v[i]] = 1
ret_key = []
ret_val = []
for tk, tv in ret_red.iteritems():
ret_key.append(tk)
ret_val.append(tv)
return ret_key,ret_val
theinput = ((1,2),
(2,1),
(2,3),
(3,2),
(4,2),
(4,3))
theoutput = ((-1,2),
(0,1),
(2,1))
map_k = []
map_v = []
for k,v in theinput:
map_k.append(k)
map_v.append(v)
print "this is the map input key:", map_k
print "this is the map input val:", map_v
reduce_input_key,reduce_input_value = map(map_k,map_v)
print "this is the reduce output key that will be fed into reduce:",reduce_input_key
print "this is the reduce output value that will be fed into reduce:",reduce_input_value
red_ret_k,red_ret_v =reducer(reduce_input_key,reduce_input_value)
print "return keys:", red_ret_k
print "return vals:", red_ret_v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment