Guest User

Untitled

a guest
Feb 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #Reduce function for computing matrix multiply A*B
  3.  
  4. #Input arguments:
  5. #variable n should be set to the inner dimension of the matrix product (i.e., the number of columns of A/rows of B)
  6.  
  7. import sys
  8. import string
  9. import numpy
  10. #number of columns of A/rows of B
  11. n = int(sys.argv[1])
  12.  
  13. #Create data structures to hold the current row/column values (if needed; your code goes here)
  14. is_first = True
  15. globalStore = {}
  16. globalSum = {}
  17.  
  18. # input comes from STDIN (stream data that goes to the program)
  19. for line in sys.stdin:
  20. #Remove leading and trailing whitespace
  21. line = line.strip()
  22. #Get key/value
  23. key, value = line.split('\t',1)
  24. #Parse key/value input (your code goes here)
  25. #string to tuple
  26. value_tuple = tuple(value[1:-1].split(','))
  27. which_matrix = value_tuple[0].replace("'",'')
  28. which_value = int(value_tuple[1])
  29. single_value = float(value_tuple[2])
  30. currentkey = key
  31.  
  32.  
  33. #If we are still on the same key...
  34. if currentkey in globalStore:
  35.  
  36. #Process key/value pair (your code goes here)
  37. currentStore = globalStore[currentkey]
  38. if which_value in currentStore:
  39. another_value = currentStore.pop(which_value, None)
  40. globalSum[currentkey] += single_value * another_value
  41. else:
  42. currentStore[which_value] = single_value
  43.  
  44.  
  45. #Otherwise, if this is a new key...
  46. else:
  47. #If this is a new key and not the first key we've seen
  48. if not is_first:
  49.  
  50. #compute/output result to STDOUT (your code goes here)
  51. print (currentkey, globalSum[currentkey])
  52.  
  53.  
  54.  
  55. #Process input for new key (your code goes here)
  56. is_first = False
  57. newKeyStore = {}
  58. newKeyStore[which_value] = single_value
  59. globalStore[currentkey] = newKeyStore
  60. globalSum[currentkey] = 0
  61.  
  62. #Compute/output result for the last key (your code goes here)
  63. print (currentkey, globalSum[currentkey])
Add Comment
Please, Sign In to add comment