Question

(Place your answer to this problem in the “count_largest.py” file) Complete the count_largest() function, which takes...

(Place your answer to this problem in the “count_largest.py” file)
Complete the count_largest() function, which takes a single positive (non-zero) integer as its argument. This function identifies the largest digit in the argument/parameter and returns an integer value that represents the total number of occurrences of that digit. For example, the largest digit in 4234 is 4, and it appears twice.
Thereareseveralwaystosolvethisproblem,mostofwhichinvolvePythonfunctionsorconceptsthatwehaven’tcoveredin class yet, but it’s definitely possible to solve it using only what we know right now. In particular, we can use a while loop and the modulo (%) operator to process the input value from right to left, removing one digit at a time for examination

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

ANSWER :

def count_largest(num):
    if (num < 0):
        num = num * -1
    val = num
    maxVal = -1
    count = 1
    while(val != 0):
        rem = val % 10
        if(rem == maxVal):
            count += 1
        else:
            if (rem > maxVal):
                maxVal = rem
                count = 1
        val = val // 10
    return count


#Testing
print(count_largest(4234))

Add a comment
Know the answer?
Add Answer to:
(Place your answer to this problem in the “count_largest.py” file) Complete the count_largest() function, which takes...
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
  • 1.Write a function called high_point that takes a list of integers and returns True if there...

    1.Write a function called high_point that takes a list of integers and returns True if there exists a value in the list that is bigger than the values immediately before and after it in the list. Your function must return False if no such value exists. The values in the beginning and the end of the list cannot be high points. (20 points) Test Cases: print(high_point([2,5,8,9,7,9])) True print(high_point([2,5,6,6,3])) False print(high_point([2,5,8,21,22])) False 2. Write a while loop to repeatedly ask for...

  • Problem 1 (Factorial with loops) Complete the function factorial_loop(num) that takes an integer num and returns...

    Problem 1 (Factorial with loops) Complete the function factorial_loop(num) that takes an integer num and returns the value of num!, i.e., num! = num * (num-1) * (num-2) * ... * 1. This function must use a loop (iteration) to compute the value of num!. (in python)

  • USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and...

    USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and returns a single Boolean value representing whether the given argument is prime (True) or not (False). After writing the function, test it by using a loop to print out all the prime numbers from 1-100. To check your results, the prime numbers from 1-100 are: 2, 3, 5, 7, 11. 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,...

  • Must use the Scheme language to solve the problem. Thanks! Write a function deep-mult which takes...

    Must use the Scheme language to solve the problem. Thanks! Write a function deep-mult which takes a list as a parameter and computes the product of all numbers which appear anywhere in the list or in sublists. We will follow the usual mathematical convention that a product of no numbers is 1 (the multiplicative identity). You may find it useful to know that Scheme has a function number? which returns #t if its argument is numeric atom and #f otherwise....

  • This code should be in C please!! Your file should meet the following criteria: • Function...

    This code should be in C please!! Your file should meet the following criteria: • Function prototype and implementation for a calculateDivisors function that takes in an integer parameter, calculates the sum of its divisors, and returns that sum (as an integer value) • A structure that represents what is represented on each line of output, e.g., o Line number o Sum of the divisors for that line number o Character array containing “Perfect”, “Deficient”, or “Abundant” • Pointer declared...

  • C++ Project Overview The 18th season of the reality series Hell's Kitchen began airing on Sep 28th, 2018 on Fox. Suppose you are working for the boss, Chef Gordon Ramsay, your job is to create an...

    C++ Project Overview The 18th season of the reality series Hell's Kitchen began airing on Sep 28th, 2018 on Fox. Suppose you are working for the boss, Chef Gordon Ramsay, your job is to create an efficient system that generates a menu. Since in Hell's Kitchen, menu changes every day, your system should easily add items to the menu. Therefore, dynamically allocated arrays would be an excellent solution. The Dish Class (Header File) You need to create a struct called...

  • In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand....

    In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand. Do not write "#include" and do not write whole "main()" function. Write only the minimal necessary code to accomplish the required task.                                                                                                           Save your answer file with the name: "FinalA.txt" 1) (2 pts) Given this loop                                                                                              int cnt = 1;     do {     cnt += 3;     } while (cnt < 25);     cout << cnt; It runs ________ times    ...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

  • Please have the function written in python, thank you! Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5.classes.py: class P...

    Please have the function written in python, thank you! Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5.classes.py: class Pharmacy: det init (selt, inventory, unit_prices) self.inventory -inventory self.unit_pricesunit_prices class Prescription: definit_(self, patient, drug_name, quantity): self.patient patient self.drug_name - drug_name self.quantity -quantity You will be asked to write several functions that work with the Prescription and Pharmacy classes. To complete this assignment you may write any helper functions you like inside...

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