Question

In Python - For these functions do not use built functions such as hex() and bin()...

In Python - For these functions do not use built functions such as hex() and bin() to solve the problem intToBinary ( int ) : String • takes a int as parameter and returns 16 bit string representation of that integer in binary. • Your function only needs to work for integers in the range 0 - 65535 • Example: intToBinary(213) – returns “0000000011010101”

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def intToBinary(num):
    result = ""
    #Repeat the loop till n becomes negative
    while (num > 0):
        #Finding the reminder
        rem = num % 2
        #Appending reminder to the result
        result += str(rem)
        #Making num to its half
        num = num // 2
    #Reverse the value of result
    result = result[::-1]
    l = 16-len(result)
    result = "0"*(l) + result
    #Printing final binary value to screen
    return result

#Testing
print(intToBinary(213))

0000000011010101

Add a comment
Know the answer?
Add Answer to:
In Python - For these functions do not use built functions such as hex() and bin()...
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
  • In Python, For these functions do not use built functions such as hex() and bin() to...

    In Python, For these functions do not use built functions such as hex() and bin() to solve the problem. binaryToInt ( String ) : int • Takes a string of 16 0s and 1s and returns an integer corresponding integer in the range 0-65535

  • Can someone solve this practice exam question, python 3 5. (+15) Write a function bin 2dec...

    Can someone solve this practice exam question, python 3 5. (+15) Write a function bin 2dec ( ) that takes a string representation of any length binary number and returns the decimal equivalent without using any type conversions. Remember that the binary representation ofa number has a power of 2 in each position, starting with 2° in the right most position and increasing by 1 as it moves left (e.g. 222120) loina dec (nun det >>> bin2dec('110') 6 >>>bin2dec('11111111' )...

  • In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of...

    In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of lists and functions with multiple inputs and multiple outputs. Your task is to implement the separate_and_sort function. This function takes two input parameters: 1. A list containing strings and integers in any order. 2. An optional integer parameter size which controls how many items in the list the function will process. If the length of the list is smaller than the value of size,...

  • Using Python In the decimal system (base 10), a natural number is represented as a sequence...

    Using Python In the decimal system (base 10), a natural number is represented as a sequence dndn?1 . . . d0 of (decimal) digits, each of which is in the range 0..9. The value of the number is d0 ×100 +d1 ×101 +···+ dn ×10n. Similarly, in the binary system (base 2), a natural number is represented as a sequence bnbn?1 · · · b0 of (binary) digits, each of which is 0 or 1. The value of the number...

  • Write a function in Python called addressesthat will take an address value as parameter and print...

    Write a function in Python called addressesthat will take an address value as parameter and print it as a binary number, a hex number, and an integer decimal number on three separate lines. Include your code and evidence that it works in your homework submission. [Hint: The Python interpreter has a number of functions and types built intothe language that are always available. A few of these might be useful ;) Google: Python built-in functions.] You can use whichever Python...

  • in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all...

    in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all the items in the tuple are integers(2), and returns False otherwise. For example, contains_only_integers (3, 5, 17, 257, 65537) ), contains_only_integers( (-1,) ), and contains_only_integers( ) should all return True, but contains_only_integers (2.0,4.0)) and contains_only_integers (8, 4, "2", 1)) should both return false. Your function should use a while loop to do this calculation. 121 Hint: the is instance() built-in function provides the most...

  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

  • Python help! Any help is appreciated, thank you! Fill in the missing function, monthString(), in the...

    Python help! Any help is appreciated, thank you! Fill in the missing function, monthString(), in the program. The function should take number between 1 and 12 as a parameter and returns the corresponding month as a string. For example, if the parameter is 1, your function should return "January". If the parameter is 2, your function should return out "February", etc. def monthString(monthNum): """ Takes as input a number, monthNum, and returns the corresponding month name as a string. Example:...

  • PLEASE HELP! python code Problem 4. Define the function pythagorian_coprimes (n=100) that prints all pairs of...

    PLEASE HELP! python code Problem 4. Define the function pythagorian_coprimes (n=100) that prints all pairs of positive integer numbers (a, b) such that c = a + b is also a whole number and 1 <c<n. Include only those triples that are co-prime (do not have any common divisors other than 1). For example, (3, 4, 5) is okay but (30, 40, 50) should be skipped. Help: As a starting example, examine the function pythagorian_triples that yields all triples. Modify...

  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

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