Question

USING PYTHON 3 Write a function no_pairs(L) that consumes a list of natural numbers L and...

USING PYTHON 3

Write a function no_pairs(L) that consumes a list of natural numbers L and mutates it, replacing any natural numbers which appear exactly twice in the list with -1. The no_pairs function returns None. This function should run in at worst O(n log n) time.

HINT: Solving this problem within the demanded runtime will likely require both O(n log n) sorting and O(log n) searching!

Samples: L = [254 , 955 , 198 , 590 , 368] after no_pairs ( L ) , L == [254 , 955 , 198 , 590 , 368] L = [42 , 0 , 42 , 12 , 6000 , 1] after no_pairs ( L ) , L == [ -1 , 0 , -1 , 12 , 6000 , 1] L = [4 , 4 , 4 , 2 , 1 , 2 , 1] after no_pairs ( L ) , L == [4 , 4 , 4 , -1 , -1 , -1 , -1]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of code:

output:

code:

def L_search(A,x):
low=0
high=len(A)-1
while(low<=high):
mid=(low+high)//2
if A[mid]==x:
if mid==0:
return mid
elif A[mid-1]==x:
high=mid-1
else:
return mid
elif A[mid]<x:
low=mid+1
else:
high=mid-1
return low
def R_search(A,x):
low=0
high=len(A)-1
while(low<=high):
mid=(low+high)//2
if A[mid]==x:
if mid==len(A)-1:
return mid
elif A[mid+1]==x:
low=mid+1
else:
return mid
elif A[mid]<x:
low=mid+1
else:
high=mid-1
return low
def no_pairs(A,L):
for i in range(len(L)):
left=L_search(A,L[i])
right=R_search(A,L[i])
if right==left+1:
L[i]=-1
print(L)
L=[]
print("Enter list: ",end=" ")
L = list(map(int, input().split()))
A=[]
for i in range(len(L)):
A.append(L[i])
A.sort()
print("Mutated list is: ",end=" ")
no_pairs(A,L)

#please upvote.

Add a comment
Know the answer?
Add Answer to:
USING PYTHON 3 Write a function no_pairs(L) that consumes a list of natural numbers L and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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). Using accumulative recursion, no abstract list functions. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof...

  • using Dr racket language Write a function (add-mul L). The function consumes a (listof Int) It performs a series of...

    using Dr racket language Write a function (add-mul L). The function consumes a (listof Int) It performs a series of mathematical operations. It starts with an answer of zero, then starting at the right, walks through the list L, and does the following operations: (1) If the next item in L is even, it adds the item to the answer so far. (2) If the next item on L is odd, it multiplies the answer by the item. For example,...

  • The language is python Write the function largest_edge_group(vertices) that consumes vertices, a list of list of...

    The language is python Write the function largest_edge_group(vertices) that consumes vertices, a list of list of integer containing the coordinates of the consecutive vertices of a polygon. The function largest_edge_group returns the size of the largest group of same length edges. Your function must run in O(n), where n is the length of vertices. Example largest_edge_group([[0,0],[1,1],[0,2],[-2,3],[-1,2]]) => 3 Hint Remember, it is not possible to compare Floats for strict equality, but since the coordinates are integers, the squared distance is...

  • Date: December 12th, 2019 4. Describe why you selected the worstcase runtime above. Recall what we...

    Date: December 12th, 2019 4. Describe why you selected the worstcase runtime above. Recall what we said about inversions and how the number of inversions is equivalent to the number of swaps for insertion sort. Also, recall the visualization from the notes on insertion First Name: Last Name: Hint: Given a list L = {4, 15, 104, Z. 11.2.14) where we are determining the number of inversions for 10, the inversions are underlined in red. I. INSERTION SORT 1. Given...

  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • Write a Python function that creates and returns a list of prime numbers between 2 and...

    Write a Python function that creates and returns a list of prime numbers between 2 and N, inclusive, sorted in increasing order. A prime number is a number that is divisible only by 1 and itself. This function takes in an integer and returns a list of integers.

  • ANSWER USING JAVA CODE (1)The sum of the squares of the first ten natural numbers is,...

    ANSWER USING JAVA CODE (1)The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. Find the difference between the...

  • Using Racket, write a bridgely? function that takes a list of at least 3 numbers and...

    Using Racket, write a bridgely? function that takes a list of at least 3 numbers and returns #true if it is bridgely, otherwise #false. A list of numbers is called "bridgely" if it contains at least 3 numbers, and every number in the list, except for the first and the last, is greater than both the first and the last number. Thus, these lists are bridgely: (list 1 2 3 4 5 6 5 4 3 2 1) (list 0...

  • PYTHON CODE In a program, write a function that accepts two arguments: a list, and a...

    PYTHON CODE In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. Output should look like: Number: 5 List of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List of numbers that are larger than 5: [6, 7, 8, 9, 10]

  • 6 Write a function named extract_lesser (L, v) that, from a list of numbers It finds...

    6 Write a function named extract_lesser (L, v) that, from a list of numbers It finds all of the 05 values less than v, puts them into a new list, called Less_list, and returns that new list (abai 4) def extract_lesser (L, v): Less_list = [] for ... if num <v: return Less list أدخل إجابتك 7

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT