Python: How can I divide a number completely by 2 and 3 and return -1 if not possible

Question:

I want to make a function where given a number like 7 I want to factorise the number by as many 3s and 2s. If left with a remainder then return -1.

Note: Through further examples it seems any number can be made up of the addition of multiples of 3s and 2s so -1 for remainder not needed. Goal is to get as many multiples of 3 before having to add multiples of 2 to factorise completely

For example given the number 11 I want the function to return 3:3 and 2:1 as 3 fits into 11 3 times and 2 once ie. 3+2+2=7, 3+3+3+2=11, 3+3+3+2+2=13. The preference should be being able to fit as many 3s first.

This is part of a wider problem:

from collections import Counter
#Choose two packages of the same weight
#Choose three packages of the same weight 
#Minimum number of trips to complete all the deliveries else return -1

def getMinimumTrips(weights):
    weights_counted = Counter(weights)
    minimum_trips = 0
    print(weights_counted)
    for i in weights_counted:
        if weights_counted[i]==1:
            return -1
        elif weights_counted[i]%3==0:
            minimum_trips += (weights_counted[i]//3)
        elif weights_counted[i]%2==0:
            minimum_trips += (weights_counted[i]//2)
    return minimum_trips

print(getMinimumTrips([2, 4, 6, 6, 4, 2, 4]))

Possible solution:

#Looking at inputs that are not a multiple of 3 or 2 eg, 5, 7, 11, 13
def get_components(n):
    f3 = 0
    f2 = 0
    if n%3==1:
        f3 = (n//3)-1
        f2 = 2
    elif n%3==2:
        f3 = (n//3)
        f2=1
    return f"3:{f3}, 2:{f2}"
Asked By: TS01

||

Answers:

Try this method using math.floor()

import math


def get_components(n: int) -> str:
    q3 = math.floor(n / 3)
    q2 = math.floor(q3 / 2)
    if not (q3 and q2):
        return '-1'  # returning '-1' as a string here for consistency
    return f'3:{q3}, 2:{q2}'
Answered By: JRiggles

This will return 0 if you can completely factorise the number, or -1 if 1 is remaining:

return -(i % 3 % 2)

If this helps?

Answered By: CambridgeCro

If we are given some integer value x we have 3 different cases:

  1. x == 3 * n, solution: return 3 n times. The easiest case.
  2. x == 3 * n + 1, solution: return 3 n - 1 times, then return 2 2 times, Note that we can put 3 * n + 1 == 3 * (n - 1) + 2 + 2
  3. x == 3 * n + 2, solution: return 3 n times, then return 2.

As one can see in cases #1 and #3 solutions ever exist; in case #2 there’s no solution for x = 1 (we can’t return 3 -1 times). So far so good if x <= 1 we return -1 (no solutions), otherwise we perform integer division // and obtain n, then find remainder % and get the case (remainder 0 stands for case #1, 1 for case #2, 2 for case #3). Since the problem looks like a homework let me leave the rest (i.e. exact code) for you to implement.

Answered By: Dmitry Bychenko
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.