# Issue

I am trying to build a program that, given a list of tuples and an integer, prints all the tuples were every element can be divided by k. It is important, that the information is entered using input(). So for example, if I type: [(6, 24, 12),(60, 12, 6),(12, 18, 21)] and 6, the output would be [(6, 24, 12),(60, 12, 6 )]. However, I can't seem to change the running variable y and I don't know what I am doing wrong, because I also don't generate any output. Additionally to solving the problem with the idea that you can see in my code below, I am happy to receive other, more efficient suggestions, because it seems like a very complicated solution to me, I just could not think of something else. Thank you so much!

```
def dos():
temp = True
lista = input()
k = input()
count = 0
lista = lista.split('),(') # split the string into different parts corresponding to the entered tuples
for x in range(len(lista) - 1): # for all elements of the list
for y in range(len(lista[0])): # go through the list elements
if ((lista[x])[y - 1:y]).isdigit(): #is our sign a number or a comma/something else?
while ((lista[x])[y:y + 1]).isdigit(): # make sure we include numbers with more than one digit
count = count + 1 # count how many digits our number has
y += 1 # skip ahead as to not repeat the following digits
if (int(((lista[x])[y - (count + 1):y])) % int(k)) != 0: # if our number can't be divided by the input
temp = False
count = 0
if temp: # if we did not encounter a number that cant be divided by our integer
print(lista[x])
temp = True
```

# Solution

You can use regex to match the tuple strings, but also to identify the individual integers with each string and thereby create actual python tuples which you can check for your divisible condition.

```
import re
def dos():
lista = input()
k = int(input())
found = []
for items in re.findall(r'\([-\d, ]+\)', lista):
tup = tuple(map(int, re.findall(r'-?\d+', items)))
if all([i % k == 0 for i in tup]):
found.append(tup)
return found
```

Answered By - bn_ln Answer Checked By - Mildred Charles (PHPFixing Admin)

## 0 Comments:

## Post a Comment

Note: Only a member of this blog may post a comment.