Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python:::: 1. Write a function called remove_duplicates() that takes a list a

ID: 3793816 • Letter: I

Question

In Python::::

1. Write a function called remove_duplicates() that takes a list and removes any extra occurrences of values that appear multiple times in the list. I don't care what order the values in the new list have. So for example, if you have the definition L1 = ["a", "b", "a", "c", "c", "c"] and you run remove_duplicates(L1), then depending on how you implement this function, L1 could end up as ["a", "b", "c"], or as ["b", "a", "c"]. Either is acceptable.

2. Define a function called filter_duplicates() that takes a list and returns a copy of the list with all duplicate values removed. As with the previous problem, I don't care what order the results come in. For example, filter_duplicates( ["a", "b", "a", "c", "c", "c"] )should return either ["a", "b", "c"] or ["b", "a", "c"].

3. Write a function called is_anagram() that takes two strings and returns True if the strings are anagrams of each other. The function should not be case-sensitive. So is_anagram("Listen","Silent") should return True, and is_anagram("foobar","baffor") should return False.

Hint 1: Make sure you understand what an anagram is before starting this problem. All of the letters in the first word must be used in the second word, and they must be used the same number of times. So "spats" and "past" are not anagrams. And it has to go both ways, so "stat" and "tasty" are not anagrams.

Hint 2: There are lots of ways of solving this problem, and you can use whatever method you like. But one good method involves using the string method .count().

4. Write a function called anagrams_in() that takes a string and a list of strings and returns a list of strings from the list that are anagrams of the given string. So anagrams_in("stop", ["tots", "post", "pop", "tops"]) would return ["post","tops"] and So anagrams_in("foobar",["one","two","three","four"]) would return []. Your function should use the is_anagram() function you just wrote.

Explanation / Answer

========remove_duplicates()==============

def remove_duplicates(list):
   setlist = set(list)
   templist = list
   for i in range(0,len(list)):
       list.pop()
   for x in setlist:
       list.append(x)

========= filter_duplicates()====================

def filter_duplicates(list):
   setlist = set(list)
   list = []
   for x in setlist:
       list.append(x)
   return list

======= is_anagram()===================

def is_anagram(str1,str2):
   list1=[]
   for c in str1:
       list1.append(c)
   list2 = []
   for c in str2:
       list2.append(c)
   list1.sort();
   list2.sort();
   if len(list1) != len(list2):
       return False
   else:
       for i in range(0,len(list2)):
           if(list1[i] != list2[i]):
               return False
   return True

========anagrams_in()====================

def anagrams_in(str,list):
   new_list = []
   for str1 in list:
       if(is_anagram(str,str1)):
           new_list.append(str1)
   return new_list

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote