Reference no: EM133059368
Learning Activity: Programming Exercise - Remove duplicates in a list
NOTE: this learning activity requires Python 3.6 or higher. In Module 1.1, you were asked to install any Python version higher than 3.6. If you use a version lower than Python 3.6, some of the code will not work.
Removing duplicates in a list is commonly used in everyday life. For example, when merging Google contacts with your iCloud contacts, you may find many duplicated contacts that need to be removed. Sometimes, when you combine multiple sources of photos, you may find duplicates. In this learning activity, you are asked to implement an algorithm that removes duplicated elements in a list.
Your program will first start with the following two lines of code:
import random
l = random.choices(range(50), k = 100)
These two lines of code create a list, l, which contains 100 integers randomly samples from range [0, 50). This range includes the left-hand number 0, but does not include the right-hand side number 50. For example, this can be a list generated by these two lines of code:
[42, 43, 3, 28, 44, 32, 33, 8, 20, 2, 16, 23, 17, 9, 46, 26, 21, 14, 14, 40, 36, 36, 13, 2, 41, 41, 12, 18, 1, 28, 29, 16, 7, 16, 6, 25, 29, 6, 38, 21, 3, 33, 10, 6, 0, 14, 37, 8, 8, 42, 27, 34, 32, 44, 46, 33, 4, 19, 10, 4, 30, 21, 47, 31, 12, 46, 22, 47, 45, 33, 6, 45, 32, 21, 16, 32, 46, 36, 16, 43, 46, 8, 6, 23, 9, 33, 1, 11, 37, 39, 24, 9, 36, 20, 8, 17, 36, 40, 17, 21]
You will first print this list generated by the two lines of code above. You will then write an algorithm remove the duplicates from this list (e.g. 44 appears more than once), output only the unique numbers in the list. The purpose of this exercise is to improve your algorithmic design skills and to practice looping techniques, therefore you should not use other language features (e.g. the set data structure) to "bypass" the purpose of this exercise.
Sample output:
>>> [42, 43, 3, 28, 44, 32, 33, 8, 20, 2, 16, 23, 17, 9, 46, 26, 21, 14, 14, 40, 36, 36, 13, 2, 41, 41, 12, 18, 1, 28, 29, 16, 7, 16, 6, 25, 29, 6, 38, 21, 3, 33, 10, 6, 0, 14, 37, 8, 8, 42, 27, 34, 32, 44, 46, 33, 4, 19, 10, 4, 30, 21, 47, 31, 12, 46, 22, 47, 45, 33, 6, 45, 32, 21, 16, 32, 46, 36, 16, 43, 46, 8, 6, 23, 9, 33, 1, 11, 37, 39, 24, 9, 36, 20, 8, 17, 36, 40, 17, 21]
>>> [38, 23, 11, 2, 36, 24, 13, 45, 32, 22, 1, 37, 18, 16, 0, 20, 39, 33, 21, 27, 10, 29, 30, 34, 4, 46, 25, 19, 17, 12, 43, 7, 31, 8, 6, 28, 47, 44, 14, 41, 42, 3, 26, 9, 40]
This learning activity asks you to write a program.
Attachment:- Remove duplicates in a list.rar