Pregunta

I have a little python script what is giving back all 7 combinations of range(1, 36) and writting it to the txt file.

from itertools import combinations

f = open('combinations.txt', 'w')
for comb in combinations(range(1,36), 7):
    f.write(str(comb))
    f.write('\n')
f.close()

But becouse it would be a very big file I do not want to write those whose are 7 and 6 and 5 consecutive numbers.

For example:

  • 7 consecutive numbers: 1 2 3 4 5 6 7 and 2 3 4 5 6 7 8 and.. 29 30 31 32 33 34 35
  • 6 consecutive numbers: 1 2 3 4 5 6 +(one any other more) and.. 29 30 31 32 33 34 +(one any other more)
  • 5 ..

Any idea how can I do it? And how big would be my txt?

¿Fue útil?

Solución

If I am not mistaken, you can do it like this:

from itertools import combinations

def count_consecutive(l):
    counts = [1]
    counts_index = 0
    for i in range(1, len(l)):
        if l[i] == l[i-1] + 1:
            counts[counts_index] = counts[counts_index] + 1
        else:
            counts.append(1)
            counts_index += 1
    return max(counts)

f = open('C:/combinations.txt', 'w')
for comb in combinations(range(1,36), 7):
    if count_consecutive(comb) not in [5, 6, 7]:
        f.write(str(comb))
        f.write('\n')
f.close()

It saves 12,615 of 6,724,520, which is like 0.18%, resulting in 180.5 MB file.

Otros consejos

I had a little problem. The txt file is too small to store data like this so I decided to use database. But after my new code I got this error message:

File "C:\Users\zolka\workspace\LottoAllCombination\LottoCombinations.py", line 34, in <module>
    c.execute("INSERT INTO test (word) VALUES (?)", (i,))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type. 




from itertools import combinations, groupby
from operator import itemgetter
import sqlite3

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]

def find_consecutive(lst, min_string=5):
    for k, g in groupby(enumerate(lst), lambda (i,x):i-x):
        num_string = map(itemgetter(1), g)
        if len(num_string) >= 5:
            yield num_string

con = sqlite3.connect("dictionary.sqlite")
c = con.cursor()
c.execute("CREATE TABLE test (word char(14))")
for i in combinations(data, 7):
    if not any(find_consecutive(i, min_string=5)):
        c.execute("INSERT INTO test (word) VALUES (?)", (i,))
con.commit()

This should get you on your way:

from itertools import combinations

def foo(a):
    if len(a) == 2:
        return a[1] - a[0] == 1
    return a[1] - a[0] == 1 and foo(a[1:])
##print foo('1234567')
##print foo('1234678')

# all combinations of 35 things taken 7 at a time
z = [comb for comb in combinations(range(1,36), 7)]
print len(z)
# remove combinations with consecutive numbers
z = [s for s in z if not foo(s)]
print len(z)
# make it into a string
z = '\n'.join(''.join(map(str,thing)) for thing in z)
print len(z)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top