Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #Reduce function for computing matrix multiply A*B
- #Input arguments:
- #variable n should be set to the inner dimension of the matrix product (i.e., the number of columns of A/rows of B)
- import sys
- import string
- import numpy
- #number of columns of A/rows of B
- n = int(sys.argv[1])
- #Create data structures to hold the current row/column values (if needed; your code goes here)
- is_first = True
- globalStore = {}
- globalSum = {}
- # input comes from STDIN (stream data that goes to the program)
- for line in sys.stdin:
- #Remove leading and trailing whitespace
- line = line.strip()
- #Get key/value
- key, value = line.split('\t',1)
- #Parse key/value input (your code goes here)
- #string to tuple
- value_tuple = tuple(value[1:-1].split(','))
- which_matrix = value_tuple[0].replace("'",'')
- which_value = int(value_tuple[1])
- single_value = float(value_tuple[2])
- currentkey = key
- #If we are still on the same key...
- if currentkey in globalStore:
- #Process key/value pair (your code goes here)
- currentStore = globalStore[currentkey]
- if which_value in currentStore:
- another_value = currentStore.pop(which_value, None)
- globalSum[currentkey] += single_value * another_value
- else:
- currentStore[which_value] = single_value
- #Otherwise, if this is a new key...
- else:
- #If this is a new key and not the first key we've seen
- if not is_first:
- #compute/output result to STDOUT (your code goes here)
- print (currentkey, globalSum[currentkey])
- #Process input for new key (your code goes here)
- is_first = False
- newKeyStore = {}
- newKeyStore[which_value] = single_value
- globalStore[currentkey] = newKeyStore
- globalSum[currentkey] = 0
- #Compute/output result for the last key (your code goes here)
- print (currentkey, globalSum[currentkey])
Add Comment
Please, Sign In to add comment