0

I am trying to write a program that takes an input number T(number of test cases), and then asks for the numbers N.

This is my code:

T = int(raw_input())
L = [int(raw_input()) for i in range(T)]
L1 = []
for i in range(0,L[i]):
    if (i%3 == 0 or i%5 ==0):
        L1.append(i)
print L1

Input: 2 10 20

Output: [0, 3, 5, 6, 9, 10, 12, 15, 18]

I would like the output to be of the following format:

[[0, 3, 5, 6, 9], [0, 3, 5, 6, 9, 10, 12, 15, 18]]

Here [0, 3, 5, 6, 9] is the list that has elements with both multiples of 3 and 5 for number 10

[0, 3, 5, 6, 9, 10, 12, 15, 18] is the list that has elements with both multiples of 3 and 5 for number 20

I am new to python. kindly let me know how I should proceed on this.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
SpaceOddity
  • 371
  • 3
  • 9
  • 20

4 Answers4

2

I think what you want is splitting a list by input values. Hope it helps

num = int(raw_input())
upperBounds= [int(raw_input()) for i in range(num)]
res= []
for upperBound in upperBounds:
    res.append([i for i in range(0,upperBound) if not (i % 3 and i % 5)])

output:

2
10
20
[[0, 3, 5, 6, 9], [0, 3, 5, 6, 9, 10, 12, 15, 18]]
galaxyan
  • 5,944
  • 2
  • 19
  • 43
2

The following will produce a list of lists containing all the multiples of 3 and 5 that are less than the given number.

L = [10,20]
L1 = []
for i in L:
    L2 = [] # initialize a new list
    for j in range(i):
        if not (j%3 and j%5): # use falsy values and DeMorgan's Law
            L2.append(j) # append to this list
    if L2: # use this if you don't want to keep empty lists
        L1.append(L2)

>>> L1
[[0, 3, 5, 6, 9], [0, 3, 5, 6, 9, 10, 12, 15, 18]]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

This can be easily done by applying appropriate logic:

if the element at index 0 we have to iterate from 0 to that element

else we have to iterate form L[index-1] to L[index]

T = int(raw_input())
L = [int(raw_input()) for i in range(T)]
L1 = []
for j in xrange(len(L)):
   temp = []
   get = 0 if not j else L[j-1]
   # if j==0:
   #    get = 0
   # else:
   #    get = L[j-1]
   for i in range(get, L[j]):
      if (i%3 == 0 or i%5 ==0):
         temp.append(i)
   L1.append(temp)
print L1

>>> [[0, 3, 5, 6, 9], [10, 12, 15, 18]]

Or a more Pythonic and compacted version may look like:

T = int(raw_input())
L = [int(raw_input()) for i in range(T)]
L1 = []
for j in xrange(len(L)):
   get = 0 if not j else L[j-1]
   L1.append([i for i in range(get, L[j]) if (i%3 == 0 or i%5 ==0)])
print L1 
Community
  • 1
  • 1
ZdaR
  • 22,343
  • 7
  • 66
  • 87
0

You can simply generate a list of multiples with range(l,u,s) with l the lower bounds, u the upper bound and d the step.

Now if we want to generate multiples of i for a given range, we can use the following function:

def multiples(factor, lower, upper) :
    return set(range(lower+(factor-lower)%factor,upper,factor))

We thus manipulate the lower bound as lower+(factor-lower)%factor in order to search - in constant time - the first multiple that is greater than or equal to lower.

Next we need to multiples of 3 and 5:

def multiples35(lower, upper):
    return sorted(list(multiples(3,lower,upper)|multiples(5,lower,upper)))

Now we only need to iterate over the list of values and generate the list of multiples for each two numbers:

def func(B):
    return [multiples35(0,upper) for upper in B]

Or as full code:

import sets

def multiples(factor, lower, upper) :
    return set(range(lower+(factor-lower)%factor,upper,factor))

def multiples35(lower, upper):
    return sorted(list(multiples(3,lower,upper)|multiples(5,lower,upper)))

def func(B):
    return [multiples35(0,upper) for upper in B]

The main function reads then:

T = int(raw_input())
B = [int(raw_input()) for i in range(T)]
print func(B)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555