Question

In: Computer Science

In Python please! Given an unsorted list (ls) of values (you do not know the datatype)....

In Python please!

Given an unsorted list (ls) of values (you do not know the datatype). Only two values appear in the list but they appear many times and are not sorted. Without using any significant additional space (i.e. you cannot copy the list) sort the elements in linear time going through the list only once. For example:

Given the list [‘a’, ‘a’, ‘b’, ‘a’, ‘a’, ‘b’, ‘b’, ‘a’] after the function, the list is [‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘b’, ‘b’, ‘b’] Given the list [1, 0, 1, 0, 1, 1] after the function, the list is [0,0,1,1,1,1]

Solutions

Expert Solution

Python Code:


#function to sort list in 1 traverse:
def sort(arr, size): 
    # Initializing left and right index 
    left, right = 0, size-1
    #taking 1 value in x to distinguish between 2 values in list
    x=arr[0]
      
    while left < right: 
        # Increment left index while we see 0 at left 
        while arr[left] == x and left < right: 
            left += 1
  
        # Decrement right index while we see 1 at right 
        while arr[right] != x and left < right: 
            right -= 1
  
        # If left is less than right, then there is no x at left 
        # and x at right. Exchange arr[left] and arr[right] 
        if left < right: 
            temp=arr[left]
            arr[left] = arr[right]
            arr[right] = temp
            left += 1
            right -= 1
  
    return arr 

#list arr initialized
arr = ['a','a','b','a','a','b','a','b','a','b']
#length of list arr
arr_size = len(arr) 

#fuction call and print returned value
print(sort(arr, arr_size)) 

OUTPUT:


Related Solutions

Write a program in python such that There exists a list of emails List Ls =...
Write a program in python such that There exists a list of emails List Ls = ['[email protected]','[email protected]','[email protected]','[email protected]',[email protected]'] Count the number of emails IDS which ends in "ac.in" Write proper program with proper function accepting the argument list of emails and function should print the number of email IDs and also the email IDS ending with ac.in output 2 [email protected] [email protected] ================================= i am trying like this but getting confused len [True for x in Ls if x.endswith('.ac.in')] please write complete...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Create a Python program that will take an unsorted list of 1000 integers and sort them...
Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.
List all of the values of the sine function that you know. Remember that values of...
List all of the values of the sine function that you know. Remember that values of sin(x) repeat every 2π radians, so your answer should include infinitely many values.
I know this qu is posted but I have not got the answer BY unsorted list...
I know this qu is posted but I have not got the answer BY unsorted list using STLl!! 1.(70) An organization that your little cousin belongs to is selling low-fat cookies. If your cousin's class sells more cookies than any other class, the teacher has promised to take the whole class on a picnic. Of course, your cousin volunteered you to keep track of all the sales and determine the winner. Each class has an identification number. Each sales slip...
assume n = 2^100. consider the following problem. given an unsorted list of size n you...
assume n = 2^100. consider the following problem. given an unsorted list of size n you can choose to sort it or not first. after this step, you get m queries q1, q2, ... qm. one by one of the form "is qi in the list?" Describe whether you would sort and what algorithm you would use if 1.) m= 10, and 2) m = 1000
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
Implement a function that returns the maximum number in a given unsorted linked list. For example,...
Implement a function that returns the maximum number in a given unsorted linked list. For example, there is a linked list 3->5->1->10->9. The printMax() function in max.c should return the maximum number in the linked list, namely 10 in the example. 1. Implement max.c with the completed printMax() function. 2. Provide an explanation for your solution #include <stdio.h> typedef struct node { int value; struct node *next; } node; int printMax(node *first) { // Your implementation return 0; } int...
(If you know how to do sales forecasting please) (Still learning) (Pretend values as well, just...
(If you know how to do sales forecasting please) (Still learning) (Pretend values as well, just creating a make believe short forecast) *For a fake beer company that is completely healthy and adds electrolytes* 1) Forecasting Methods: What method or methods are you using to help forecast demand for your product or service? Why did you choose this method(s)? Be sure to show proof of your forecasting efforts. 2) Initial Forecast: Given what forecasting work you have done, create an...
What do you know about programming in Python? What is the difference between Python and Java?
What do you know about programming in Python? What is the difference between Python and Java? What does the term Open Source mean? Name four examples of Open Source software. What is the IDEL programming environment? How does IDEL relate to Python? How do you spread a long statement over multiple lines in Python? How do you use the loop-index? How will knowing and understanding Python impact what you do in your profession and/or personal experiences?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT