lab7-kgashok.py
Table of Contents
Lab 7: Removing all the duplicate elements in a list
[TOC]
Problem statement
Write a python program that removes all the duplicate elements in a list.
Solution Key
CloudCoder Exercise
TBD
Pre-Lab Questions
If a list contains
[1, 2, 3, 'python', 3, 4, 'python1', 'python', 5]
, what would the list look like after all the duplicates are removed?If a list contains
[5, 5, 5, 5, 5, 6, [1, 2, 6], [1, 2, 6]]
, what would the list contain after all the duplicates are removed?What is the method available to remove an element from a list?
How will you get a list of all the
keys
that are present in a dictionary (which basically contains a key:value pair)?How will get a list of all the
values
that are present in a dictionary?What is the python code for building a dictionary out of 3 tuples? For e.g.
("jan", 31) ("feb", 28) and ("mar", 31) `
What is the value of
L
after you run the code below?
Post-Lab Questions
Rewrite the code to use only
lists
and not adictionary
. Or vice versa. Provide analysis to whether the eventual output isstable
orunstable
.You are not allowed to use another data structure to remove duplicates. That is, remove the duplicates in-place. Is this possible?
How will you find the common elements that exist in two different lists?
Write a program to calculate the product of all the integers that are available in a list. For e.g. if
alist = [2, 3, 5, 8]
, thenproduct(alist) = 240
Find the
gcd
of two integers using the prime factorization method. Review video at http://j.mp/whatIsPrime and http://j.mp/gcdPrime. Use the function you developed inLab 5
to complete this. Here's an outline for the function that you will need to develop:
Bonus 1
You are asked to remove all the duplicate occurrences of only one specific element in the list. Write a function for this.
Count the number of number of distinct characters that occur more than once in the string. Read the problem statement and solve on CC - http://j.mp/countDuplicates
Write a program that will analyze whether a string is an isogram, pangram or a partial perfect pangram. Also if the string contains duplicates, count the number of duplicate characters that are there in the string.
And how about checking whether two words are their respective anagrams? And if not, how many changes are needed to make them?
Bonus 2
There is probably a one-line Pythonic solution for almost every complicated problem out there. That is also the case for this lab exercise. Can you figure it out?
There is even a shorter one liner if you use another powerful Python data type. Can you figure it out?
Between 1 and 2, which solution is more preferable? And why?
Interview grade
Calculate the gcd
for a list of integers using the prime factorization method. The method is well described in the video at http://j.mp/gcdPrime - use lists as part of the solution.
Related Links
https://goo.gl/v7ephq http://j.mp/removeDuplicates
Predict the output of Snippet1
Predict the output of Snippet2
Analyze Snippet1 vs Snippet2
Both
mylist
andu
are identical lists. True or False?At the end of execution of their respective codes,
mylist
fromSnippet1
andu
fromSnippet2
will always be of the same size, as long as they both start off by being equal. True or False?At the end of execution of their respective codes,
mylist
andu
will have the same contents, in the same order. True or False?
Last updated