To generate numbers that don't have num "6" in any of the two number combinations

Viewed 53

I have been given a problem where - whenever the user types the number '6' in the digits (it can be anywhere in the digit's place), he has to type or generate, a combination of two values that add up to the entered number.

Ex1 : if I input number "16" ==> (7,9) or (8,8) or (1,15) etc., but not (0,16) or (6,10) etc.,

Ex2 : if I input number "61" ==> (58,3) or (59,2) or (57,4) etc., but not (0,61) or (60,1) etc.,

Ex3 : if I input number "168" ==> (75,93) or (80,88) or (10,158) etc., but not (168,0) or (60,108) etc.,

I tried to work around this problem and got :

'''

def faulty_num():
    x=str(input())
    result=[]

    for i in range(1, int(x)+1):
        for j in range(1, int(x)+1):
            if (i+j == int(x) and int(x)%10==6):
                result.append((i,j))
 
    print(*result[:(int(x)//2)])

'''

when I input 16 ==> (1, 15) (2, 14) (3, 13) (4, 12) (5, 11) (6, 10) (7, 9) (8, 8)

Kindly forgive me, if my code is messy.

In the code above, I can eliminate combinations having 6 by mentioning (i!=6 ,j!=6) or (i%10==6 , j%10==6) in my "if condition". I won't be able to do that when, the digit's size increases. In the mentioned code block, this if condition =

if (i+j == int(x) and int(x)%10==6):

will only generate the required combinations, when the ones place of the digit is "6" and only generate combinations that have the digit 6. The code only gets more convoluted, the more I think. But, I am honestly clueless at this point. Any suggestion will definitely be helpful. Thank you for your valuable time!

3 Answers

You're doing well with your coding and this is a well-written question. We could search for 6 in the number by looking at it as a string?

def faulty_num():
    x=str(input())
    result=[]

    for i in range(1, int(x)+1):
        for j in range(1, int(x)+1):
            if (i+j == int(x) and "6" not in str(i) and "6" not in str(j)):
                result.append((i,j))
 
    print(*result[:(int(x)//2)])

faulty_num()

EDIT: I've always made you a function that can check to see if there is a six in it. Hopefully it makes sense, comment if you have any questions.

def has_a_six(n):
    """Returns True if there is a six in the number, and False otherwise"""
    while n != 0:
        if n % 10 == 6: return True
        else: n = n//10
    return False


def faulty_num():
    x=input()
    result=[]

    for i in range(1, int(x)+1):
        for j in range(1, int(x)+1):
            if (i+j == int(x) and not has_a_six(i) and not has_a_six(j)):
                result.append((i,j))
 
    print(*result[:(int(x)//2)]) # This line is quite clever, I might not have thought of that.

faulty_num()
def faulty_num():
    a=input('this is a number  ')
    result=[]
    for i in range(int(a)//2+1):
        
        if '6' not in str(i) and '6' not in str(int(a)-i) :
            if (i,int(a)-i) not in result:
                #print(i)
                result.append((i,int(a)-i))
    print(*result)
faulty_num()
x = int(input('Target? '))

for i in range(1, x // 2 + 1):  # assuming you don't want reflections (1,2),(2,1)
    j = x - i                   # i + j will always equal x this way
    if '6' not in f'{i}{j}':    # test for 6 in two numbers concatenated together
        print(i,j)

Output example:

Target? 16
1 15
2 14
3 13
4 12
5 11
7 9
8 8

Of course you should sanitize your input if you require a number that contains a 6. Something like:

def get_x():
    while True:
        s = input('Target number with a six? ')
        try:
            x = int(s)
        except ValueError:
            print('not an integer')
        else:
            if '6' in s:
                return x
            print('needs a 6')

x = get_x()
Related