In this issue you need to follow a well-defined logic. This logic should follow the following steps:
- Implement a vector with 20 elements;
- Mount another vector with only positive values of the first vector;
- Mount another vector with only positive values and no repetitions of the first vector;
- Display the three vectors.
There are several ways you can implement the vector. You can mount the vector with the repetition loop for, while, list compherensio or with the function map.
After you’ve implemented the vector. You need to mount another vector with only positive values and another vector with only positive values and no repetitions. Then display the respective vectors.
One of the ways to implement a code that satisfies this logic is:
vet = list(map(int, input('Digite os valores: ').split()))
pos = list()
semdup = list()
for i in vet:
if i > 0:
pos.append(i)
if i not in semdup:
semdup.append(i)
print(f'O vetor "vet" é: {vet}')
print(f'O vetor "pos" é: {pos}')
print(f'O vetor "semdup" é: {semdup}')
When we executed this code, we received the following message: Digite os valores:
. Right now we must type all 20 values, in the same line, separated for a single space and, press ENTER
.
From this moment the code will assemble a list of all the values typed. Later the loop for will traverse the entire vector vet
and with the help of 1st block if, will be verified if each value of the respective interaction is greater than 0. If positive, this value will be added to the vector pos. Then the 2nd block if verify that the respective interaction value does not exist in the vector semdup. If this value does not exist in the vector semdup, it will be added to the vector semdup.
After having performed these operations the vectors vet, pos and semdup are displayed.
Observing:
In this code I only used a repeat loop for. With this, we can better greatly optimize the execution of the code.
Let’s test the code
When executing the code, we received the message: Digite os valores:
. At this moment we must enter the 20 values, in the same line, separated by a single space and press enter
. So we typed...
-4 2 -3 6 6 7 7 -8 9 -10 11 -11 13 13 13 14 -15 18 19 20
...and press enter
.
Then the code will perform all operations and provide us with the following output:
O vetor "vet" é: [-4, 2, -3, 6, 6, 7, 7, -8, 9, -10, 11, -11, 13, 13, 13, 14, -15, 18, 19, 20]
O vetor "pos" é: [2, 6, 6, 7, 7, 9, 11, 13, 13, 13, 14, 18, 19, 20]
O vetor "semdup" é: [2, 6, 7, 9, 11, 13, 14, 18, 19, 20]
Hello, what error appeared when you ran your program? I tested it and it seemed to work right for me
– Flavio Moraes
Camila, good morning! How Flavio said your code works. It may be that you have some wrong indentation. Hug!
– lmonferrari
It is always good to put the error messages displayed by the compiler. From a read on Stack Overflow Question Checklist.
– Augusto Vasques