Question

Write a python code function that selects top ten max numbers from a sequence of n integers, test it, and determine the Big-O

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

# Solution for 1

The simplest approach would be:

--> Sorting the elements in descending order in O(nLogn)
--> Print the first ten numbers of the sorted array O(k).

CODE:

def max_ten(arr):

    ''' Function code for Ten max elements in an array '''

    # Sort the given array arr in reverse order.

    arr.sort(reverse=True)

    # return the first 10 largest elements

    return arr[:10]


# Testing

arr1 = [1, 23, 12, 9, 30, 2, 50, 2, 56, 89, 10]

arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

arr3 = [90, 80, 70, 60, 50, 40, 30, 20, 10, 5, 4, 3, 2, 1]


print(max_ten(arr1))

print(max_ten(arr2))

print(max_ten(arr3))

assert(max_ten(arr1) == [89, 56, 50, 30, 23, 12, 10, 9, 2, 2])

assert(max_ten(arr2) == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1])

assert(max_ten(arr3) == [90, 80, 70, 60, 50, 40, 30, 20, 10, 5])

# Time complexity: O(nlogn)

# Solution for 2

def harmonic_recursive(n):

    """

    Function that return Nth Harmonic number using recursion

    """

    # H1 = 1

    if n == 1:

        return 1

    else:

        # Hn = H1 + H2 + H3 ... +

        # the result can be rounded using the round function

        return 1.0/n + harmonic_recursive(n-1)


# Testing

print(harmonic_recursive(4))

assert(harmonic_recursive(4) == 2.083333333333333)

assert(harmonic_recursive(20) == 3.597739657143682)

assert(harmonic_recursive(8) == 2.7178571428571425)

Add a comment
Know the answer?
Add Answer to:
Write a python code function that selects top ten max numbers from a sequence of n...
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
  • Consider the following Python function: def find_max (L): max = 0 for x in L: if...

    Consider the following Python function: def find_max (L): max = 0 for x in L: if x > max: max = x return max Suppose list L has n elements. In asymptotic notation, determine the best case running time as function of n In asymptotic notation, determine the worst case running time as function of n Now, assume L is sorted. Give an algorithm that takes asymptotically less time than the above algorithm, but performs the same function. Prove that...

  • In python Programming Exercise 1 Write a function that will have a list as an input,...

    In python Programming Exercise 1 Write a function that will have a list as an input, the task of the function is to check if all the elements in the list are unique,( i.e. no repetition of any value has occurred in the list), then the function returns true otherwise it returns false. Your program should include a main method that call the method to test it. Algorithm Analysis For the function you implemented in part 1, please calculate T(n),...

  • Give python code for this algorithm in two ways. 1 Method has to be recursive, Method...

    Give python code for this algorithm in two ways. 1 Method has to be recursive, Method 2 has to use list comprehensions or list traversal methods. You have to write python function that will be in this form ArithmeticSequence(0,2,10){} where 0 is the first number in the sequence, the next number goes up by 2, and stopping at the last value in sequence before 10, so 10 is the max of this sequence. it has to pass a test like...

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the...

    Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the linear recurrence equation Fn F-1 F-2 where F F2 1 and by convention Fo 0. For example, the first 8 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21. (a) For a given n, compute the nth Fibonnaci number using a for loop (b) For a given n, compute the nth Fibonnaci number using a while loop Print the 15th Fibonacci number...

  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

  • F# ONLY SOMEONE SOLVED IN PYTHON JUST NOW. I NEED F SHARP PROGRAMMING THANKS A more...

    F# ONLY SOMEONE SOLVED IN PYTHON JUST NOW. I NEED F SHARP PROGRAMMING THANKS A more efficient version of the function for computing Tetranacci numbers can be defined by following three steps: Implement a function which takes a list of integers and adds the sum of the top four elements to the head of the list (e.g., in F#, 1::1::1::1::[] should become 4::1::1::1::1::[]). Implement a recursive function which accepts an integer n as input (again, assume n >= 0), and...

  • Write a code using loop and flag function in python jupitior notebook: Ask the user for...

    Write a code using loop and flag function in python jupitior notebook: Ask the user for the number of items they bought. Also ask the price of each item they bought and quantity purchased. Display the total amount they owe. Ask the user for a number n. Calculate 1+2+3+….+n. Ask the user for a number n. Calculate 1*2*3*…*n. Ask the user for number of rows (r) and columns (c). Print * for r rows and c columns. Ask the user...

  • 1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci...

    1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci number. By definition Fibonnaci(0) is 1, Fibonnaci(1) is 1, Fibonnaci(2) is 2, Fibonnaci(3) is 3, Fibonnaci(4) is 5, and so on. Your function may only use a constant amount of memory (i.e. no auxiliary array). Argue that the running time of the function is Θ(n), i.e. the function is linear in n. 2. (10 points) Order the following functions by growth rate: N, \N,...

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