Sajjad Abbas
Sajjad Abbas

Reputation: 37

Bug with finding the largest number divisible by 3

def solution(l):
    for x in (range(len(l))):
        sum_l = sum(l)
        num = l[x]
        y = sum_l%3
        l.sort()

        if len(l) == 1 and l[0]%3 != 0:

            return 0
        elif ((num%3) == y and y != 0) :
            l.remove(num)

            l.sort(reverse=True)
            strings = [str(number) for number in l]
            l_string = ''.join(strings)
            l_int = int(l_string)

            return l_int
        elif y == 0:
            l.sort(reverse=True)
            strings = [str(number) for number in l]
            l_string = ''.join(strings)
            l_int = int(l_string)

            return l_int
    else:

      return 0

Here is the code. Basically, I am asked to find the largest number that can be made by the integers in the list divisible by 3. It passes all the test cases except one (blind case), and I have literally no idea what the issue is. I've tried every possible type of list, meaning, 1 digit, 2 digits, etc. The list can have numbers 0-9, with 1-9 numbers in it. Can I get some help as to what I might be missing?

Upvotes: 0

Views: 2135

Answers (3)

Hamdi Yilmaz
Hamdi Yilmaz

Reputation: 306

def cozum(list):
    a=[]
    for i in range(1,len(list)):
        p = permutations(list,i) 
        for pe in p:
            a.append(pe)
        toplamlar=[ tup for tup in a if sum(tup)%3 == 0 ]
        if toplamlar:
            res = max([ int(''.join(map(str, sayi))) for sayi in toplamlar])

            return res
        else:
            return "No number divisible by 3"

Upvotes: 0

Kobina Folson
Kobina Folson

Reputation: 11

from itertools import permutations


def solution(l):
    if not l:
        return 0

    if len(l) == 1 and l[0] % 3 == 0:
        return l[0]

    if len(l) == 1 and l[0] % 3 != 0:
        return 0

    p1 = [s for i in range(2, len(l) + 1) for s in
          permutations(l, i)] 

    p2 = []
    for each in p1:
        num = ""
        for i in each:
            num += str(i)

        if int(num) % 3 == 0:
            p2.append(int(num))

    x = sorted(list(set(p2)))

    ans = 0
    if len(x) != 0:
        ans = max(x)

    if ans != 0:
        return ans
    return 0

Upvotes: 1

Red
Red

Reputation: 27547

Here is how you can find the largest number that can be made by the integers in the list divisible by 3:

from itertools import permutations

def solution(l):
    p1 = [s for i in range(2,len(l)+1) for s in permutations(l,i)] # List of tuples with all possible combinations of numbers
    p2 = [str(t) for t in p1 if not sum(t)%3] # List of tuples with elements adding together divisable by 3
    return int(''.join([n for n in max(p2) if n.isdigit()])) # Return greatest tuple converted to number form    

print(solution([4,6,9,1,3,3,2,4]))

Output:

9644331

The best way to find whether a number is divisible by 3 with only its digits is by adding up all the digits. If the answer is divisible by three, then so is any number made up of the combination of all the digits.

Upvotes: 1

Related Questions