# Untitled

By: a guest on Sep 7th, 2012  |  syntax: None  |  size: 1.79 KB  |  hits: 6  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #!/usr/bin/env python
2. # -*- coding: utf-8 -*-
3. #
4. #       hamming.py
5. #
6. #       Copyright 2012 Javier Rovegno Campos <javier.rovegno@gmail.com>
7. #
8. #       This program is free software; you can redistribute it and/or modify
10. #       the Free Software Foundation; either version 2 of the License, or
11. #       (at your option) any later version.
12. #
13. #       This program is distributed in the hope that it will be useful,
14. #       but WITHOUT ANY WARRANTY; without even the implied warranty of
15. #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16. #       GNU General Public License for more details.
17. #
18. #       You should have received a copy of the GNU General Public License
19. #       along with this program; if not, write to the Free Software
20. #       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21. #       MA 02110-1301, USA.
22.
23. import sys
24.
25. def H(p1, p2, p3, i):
26.     """
27.     >>> H(2,3,5,5)
28.     6
29.     >>> H(2,3,5,9)
30.     12
31.     >>> H(7,13,19,100)
32.     26590291
33.     """
34.     p1, p2, p3, i = int(p1), int(p2), int(p3), int(i)
35.     binomial = [120, 1140, 4060, 9880, 19600,
36.                 34220, 54740, 82160, 117480,
37.                 161700, 215820, 280840, 357760]
38.     combina = 0
39.     for val in binomial:
40.         combina += 10
41.         if val > i:
42.             break
43.     s = range(combina)
44.     p = [[k,l,m] for k in s for l in s for m in s]
45.     aux = []
46.     for j in p:
47.         aux.append(p1**j[0] * p2**j[1] * p3**j[2])
48.     aux.sort()
49.     return aux[i]
50.
51.
52. def test():
53.     import doctest
54.     doctest.testmod()
55.
56. def main():
57.     try:
58.         args = sys.argv[1:]
59.         print H(args[0], args[1], args[2], args[3])
60.     except:
61.         print "Use: python hamming.py 7 13 19 100"
62.
63. if __name__ == '__main__':
64.     main()