diff options
author | Haidong Ji | 2018-09-07 21:22:29 -0500 |
---|---|---|
committer | Haidong Ji | 2018-09-07 21:22:29 -0500 |
commit | 123adf9d800d9f33e043240292946b8baf908a2d (patch) | |
tree | f2b674f9285ac7b90198c3dc529f083ebf7225a8 | |
parent | 7c4273cabf7202d1a7a84c675a1d5eb2d4781592 (diff) |
max dot product done
aka maximize online ad revenue.
-rw-r--r-- | AlgoDesignAndTechniqueEdxPython/sources/max_dot_product.py | 20 | ||||
-rw-r--r-- | AlgoDesignAndTechniqueEdxPython/tests/max_dot_prodTest.py | 31 |
2 files changed, 51 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxPython/sources/max_dot_product.py b/AlgoDesignAndTechniqueEdxPython/sources/max_dot_product.py new file mode 100644 index 0000000..268a765 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/sources/max_dot_product.py @@ -0,0 +1,20 @@ +#Uses python3 + +import sys + +def getMaxDotProduct(a, b): + a.sort() + b.sort() + result = 0 + for i, j in zip(a,b): + result = result + i*j + return result + +if __name__ == '__main__': + input = sys.stdin.read() + data = list(map(int, input.split())) + n = data[0] + a = data[1:(n + 1)] + b = data[(n + 1):] + print(getMaxDotProduct(a, b)) +
\ No newline at end of file diff --git a/AlgoDesignAndTechniqueEdxPython/tests/max_dot_prodTest.py b/AlgoDesignAndTechniqueEdxPython/tests/max_dot_prodTest.py new file mode 100644 index 0000000..2f8f7a2 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/tests/max_dot_prodTest.py @@ -0,0 +1,31 @@ +''' +Created on Sep 7, 2018 + +@author: haidong +''' +import unittest + +from sources.max_dot_product import getMaxDotProduct + +class Test(unittest.TestCase): + + + def testName(self): + a = [60, 100, 120] + b = [20, 50, 30] + self.assertEqual(getMaxDotProduct(a, b), 10200) + + def testName1(self): + a = [23] + b = [39] + self.assertEqual(getMaxDotProduct(a, b), 897) + + def testName2(self): + a = [1, 3, -5] + b = [-2,4,1] + self.assertEqual(getMaxDotProduct(a, b), 23) + + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testName'] + unittest.main()
\ No newline at end of file |