Skip to content

Instantly share code, notes, and snippets.

@jrovegno
Created June 14, 2012 18:42
Show Gist options
  • Save jrovegno/2932107 to your computer and use it in GitHub Desktop.
Save jrovegno/2932107 to your computer and use it in GitHub Desktop.
Desafío 2012-06 El problema de Hamming
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# hamming.py
#
# Copyright 2012 Javier Rovegno Campos <javier.rovegno@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import sys
def H(p1, p2, p3, i):
"""
>>> H(2,3,5,5)
6
>>> H(2,3,5,9)
12
>>> H(7,13,19,100)
26590291
"""
p1, p2, p3, i = int(p1), int(p2), int(p3), int(i)
binomial = [120, 1140, 4060, 9880, 19600,
34220, 54740, 82160, 117480,
161700, 215820, 280840, 357760]
combina = 0
for val in binomial:
combina += 10
if val > i:
break
s = range(combina)
p = [[k,l,m] for k in s for l in s for m in s]
aux = []
for j in p:
aux.append(p1**j[0] * p2**j[1] * p3**j[2])
aux.sort()
return aux[i]
def test():
import doctest
doctest.testmod()
def main():
try:
args = sys.argv[1:]
print H(args[0], args[1], args[2], args[3])
except:
print "Use: python hamming.py 7 13 19 100"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment