Respuesta :
Answer:
def doubles(ls):
new = []
for i in ls:
for a in ls:
if (i == a*2) and (i not in new) and i != 0:
new.append(i)
break
return new
ls = [3, 0, 1, 2, 3, 6, 2, 4, 5, 6, 5]
print(doubles(ls))
Explanation:
The code is written in python.
The function doubles is created and it has one parameter.
In the function, a new list is created to hold the values that are exactly twice the previous integer in the list.
Two nested for loops are used with an if statement to achieve the desired result. The first FOR loop gets a number from the list entered by the user and holds that number while it waits for the second for loop and the if statement. The second FOR loop, iterates through all the elements in the list and compares them with the number being held by the first for loop through the IF statement.
The IF statement checks 3 things:
- if the first number is x2 the second
- if it is not already contained in the list
- if the number is not zero
All these conditions must pass as True before the number is appended to the new list.
The break statement is there to ensure that the loop does not start evaluating unnecessary conditions, so when a match is found for one element the second loop breaks thereby allowing the first FOR loop to move to the next number.
Finally, the new list is returned and the function is called.
I have attached a picture of the result.
