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

Write a Python function that takes in two lists and calculates whether they are

ID: 3853520 • Letter: W

Question

Write a Python function that takes in two lists and calculates whether they are permutations of each other. The lists can contain both integers and strings. We define a permutation as follows: the lists have the same number of elements list elements appear the same number of times in both lists If the lists are not permutations of each other, the function returns False. If they are permutations of each other, the function returns a tuple consisting of the following elements: the element occurring the most times how many times that element occurs the type of the element that occurs the most times If both lists are empty return the tuple (None, None, None). If more than one element occurs the most number of times, you can return any of them. def is_list_permutation(Ll, L2): L1 and L2: lists containing integers and strings Returns False if L1 and L2 are not permutations of each other. If they are permutations of each other, returns a tuple of 3 items in this order: the element occurring most, how many times it occurs, and its type # Your code here

Explanation / Answer

Code:
#!/usr/bin/python

def is_list_permutation(L1, L2):

# computing size of lists
len_L1 = len(L1)
len_L2 = len(L2)

# if both lists are empty, return None
if len_L1 == 0 and len_L2 == 0:
return (None, None, None)

freq_L1 = {}
freq_L2 = {}
# only if lists have same number of elements we will proceed here   
if len_L1 == len_L2:
for i in range(0, len_L1):
# calculating the frequency of each element in both lists
if L1[i] in freq_L1:
freq_L1[L1[i]] += 1
else:
freq_L1[L1[i]] = 1
if L2[i] in freq_L2:
freq_L2[L2[i]] += 1
else:
freq_L2[L2[i]] = 1
max = 0
max_element = ''
# checking if element in list L1 exists list L2
for each_elem in freq_L1:
if each_elem in freq_L2:
# checking if length of element in list L1 and list L2 is same
if freq_L1[each_elem] == freq_L2[each_elem]:
# checking if element size is max among the list of elements found in both lists L1 and L2
if freq_L1[each_elem] > max:
max = freq_L1[each_elem]
max_element = each_elem
# indicates some element which is in list L1 and list L2 but not have the same element occurred number of times in both lists
else:
return (False, False, False)

# indicates the lists do not have same number of elements   
else:
return (False, False, False)

return (max_element, max, type(max_element))
  

if __name__=='__main__':
l1 = (1,2,3,4,'abc')
l2 = (2,3,1,5,6)
list_out = []
list_out = is_list_permutation(l1, l2)
print list_out

l1 = ('a', 'bcde', 'a', 1, 2)
l2 = (1, 2, 'a', 'bcde', 'a')
list_out = is_list_permutation(l1, l2)
print list_out

l1 = (1,5,1,2,3,4)
l2 = (1,1,2,5,3,4)
list_out = is_list_permutation(l1, l2)
print list_out

l1 = ()
l2 = ()
list_out = is_list_permutation(l1, l2)
print list_out

l1 = (1,1,2)
l2 = (2,1,2)
list_out = is_list_permutation(l1, l2)
print list_out


Execution and output:

Unix Terminal> python permutation.py
(1, 1, <type 'int'>)
('a', 2, <type 'str'>)
(1, 2, <type 'int'>)
(None, None, None)
(False, False, False)

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