0

I am learning to write efficient code in Python.

I came across a problem wherein I had to check divisibility by 8 of all possible combinations of 968 or maybe even a larger int (not sure about long int but let's say including those as well). So how can I do that efficiently?

I think breaking it up and using ''.join() is giving me a Segmentation Error and Runtime Error in few cases.

Ex: 968 combinations are:

[('9',), ('6',), ('8',), ('9', '6'), ('9', '8'), ('6', '8'), ('9', '6', '8')]`enter code here`

Edited - Question is am I doing it efficiently? or can I reduce my time with any other import? Everything (including stackoverflow) is very new to me)

import itertools
import math




n = 8 #example
string = '968' #example

lis=[]



# we will use combinations(string,i) for creating sorted ordered
#non-repeating tuples combinations('ABCD', 2) ---> AB AC AD BC BD CD


for i in range(1,n+1):
    lis+=list(itertools.combinations(string,i))

print lis 
#output is  [('9',), ('6',), ('8',), ('9', '6'), ('9', '8'), ('6', '8'), ('9', '6', '8')]




fin=[]

#we will join all tuple-item and append it in fin ---> ''.join()
for item in lis:
    fin.append(int(''.join(item)))

print fin
#output is [9,6,8,96,98,68,968]


ans=[]

#we will check divisibility with 8
for item in fin:
    if item%8==0:
        ans.append(item)

print ans
#ouput is [8,96,968]
Viraj Verma
  • 19
  • 1
  • 3
  • What does a list of tuples of chars have to do with divisibility by 8? – Stephen Rauch Jan 14 '17 at 05:43
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Jan 14 '17 at 05:44
  • Also, see http://stackoverflow.com/questions/3099987/generating-permutations-with-repetitions-in-python. – TigerhawkT3 Jan 14 '17 at 05:44

1 Answers1

0

If I understand you correctly. This gives all possible combinations of 9, 6 and 8

lst = [9,6,8]
import itertools
set(itertools.permutations(lst))

Out:

{(6, 8, 9), (6, 9, 8), (8, 6, 9), (8, 9, 6), (9, 6, 8), (9, 8, 6)}
nipy
  • 5,138
  • 5
  • 31
  • 72
  • 1
    This is obviously different from the desired output. – TigerhawkT3 Jan 14 '17 at 05:47
  • Also see http://meta.stackoverflow.com/questions/310827/what-can-we-do-about-fastest-gun-in-the-west-answers-that-dump-out-garbage-and. – TigerhawkT3 Jan 14 '17 at 05:49
  • I don't think desired output has been clearly explained. – nipy Jan 14 '17 at 05:50
  • 1. It gives an exact example, `[('9',), ('6',), ('8',), ('9', '6'), ('9', '8'), ('6', '8'), ('9', '6', '8')]`. 2. If you don't think the intent is clear, why answer with a random guess? – TigerhawkT3 Jan 14 '17 at 05:51
  • Question was 'How do I create integer combinations of 968' and that is what I tried to provide. Just trying to help the OP. – nipy Jan 14 '17 at 05:55
  • Look at the desired output that clarifies what the OP wants, and look at what your code produces. You will occasionally have to read the body of a post in order to actually be helpful. – TigerhawkT3 Jan 14 '17 at 05:56