Question

In: Computer Science

Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi...

Python question

Write a function

int(lofi, alofi)

that consumes two sorted lists of distinct integers lofi and alofi, and returns a sorted list that contains only elements common to both lists.

You must obey the following restrictions:

  • No recursion or abstract list functions,
  • intersect must run in O(n) where n is the combined length of the two parameters.
  • sort function is not allowed as well as list comprehensions
  • math is the only library that can be imported

Example:

 int([4, 13, 10, 8, 15], [2, 3, 6, 13, 11, 12, 8]) => [8, 13]

Solutions

Expert Solution

Python code:

#defining function

def int(lofi,alofi):

    #initializing list1 to store common elements

    list1=[]

    #looping through each element in lofi

    for i in lofi:

        #checking if each element in lofi is in alofi

        if i in alofi:

            #inserting that element into list1

            list1.append(i)

    #list for sorting

    list2=[]

    #looping through each element in list1

    for i in range(len(list1)):

        #inserting into list2, minimum element in list1

        list2.append(min(list1))

        #removing that element from list1

        list1.remove(min(list1))

    #returning list2

    return(list2)

#testing and printing

print(int([4,13,10,8,15],[2,3,6,13,11,12,8]))

Screenshot:

Output:


Related Solutions

Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
Can you answer these question using python and recursive functions Write a function that consumes two...
Can you answer these question using python and recursive functions Write a function that consumes two numbers N1 and N2 and prints out all the even numbers between those numbers, using recursion. Using any number of list literals of maximum length 2, represent the data from your Survey List. So you cannot simply make a list of 15 values. Make a comment in your code that explicitly describes how you were able to represent this. Write a recursive function that...
In Python write a loop that tests whether two lists contain the same integers in the...
In Python write a loop that tests whether two lists contain the same integers in the same order: listA = [1, 2, 3, 4] listB = [1, 2, 3, 4] equal = True for ______ in ______:    if _______ != ________ :       _______ = ________ if equal:    print(_______) else:    print(_______)
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
This is a python question. Write a function mult_table(n) that consumes a natural number n and...
This is a python question. Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Use accumulative recursion. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof (listof Nat))    Examples:...
PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of...
PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of natural numbers L and return the single digit that occurs most frequently at the end of the numbers in the list. The function returns the smallest digit in the case of a tie. Your function should run in O(n) time. Do not mutate the passed parameter. def most_ending_digit(L) ''' Returns the single digit that occurs most frequently as the last digit of numbers in...
Write a C function hwkOneA, that takes a long int x as well as two integers...
Write a C function hwkOneA, that takes a long int x as well as two integers n and m as arguments, and returns a long int. Here is the function declaration: long int hwkOneA (long int x, int n, int m); The function should swap nibble n and m of a long int x (64-bit integer). A nibble is a four-bit aggregation. For this problem, the index of the most significant nibble is 0, and the index of the least...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
Problem 1: You have two sorted lists of integers, L1 and L2. You know the lengths...
Problem 1: You have two sorted lists of integers, L1 and L2. You know the lengths of each list, L1 has length N1 and L2 has length N2. Design a linear running time complexity algorithm (ONLY PSEUDOCODE) to output a sorted list L1 ∧ L2 (the intersection of L1 and L2). Important Notes: For this problem, you don’t need to submit any implementation in Java. Only the pseudocode of your algorithm is required. Pseudocode is a simple way of writing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT