Ask Question

In this problem you will write a function that takes in a single integer (positive, negative, or zero) and produces an array of all of its individual digits along with a boolean which flags if the number is negative (often called the sign bit). The list of digits should begin with the lowest place and increase to the highest place. Main Function:

Input variables: • a scalar representing the integer to be studied

Output variables: • a vector of each digit in the order from lowest place to highest place

• a scalar boolean which is 1 if the number is negative and 0 otherwise A possible sample case is: " [digits, sign] = LXX_QYY (1234) digits = 4 3 2 1 sign = 0 " [digits, sign] = LXX_QYY (-9) digits = sign %3D

+3
Answers (1)
  1. 2 March, 04:11
    0
    The solution code is written in Python 3.

    def LXX_QYY (num) : num_list = [] if (num > 0) : num_str = str (num) lastIndex = len (num_str) - 1 for i in range (0, len (num_str)) : num_list. append (int (num_str[lastIndex - i])) return ([0, num_list]) else: num = - (num) num_str = str (num) lastIndex = len (num_str) - 1 for i in range (0, len (num_str)) : num_list. append (int (num_str[lastIndex - i])) return ([1, num_list]) [digits, sign] = LXX_QYY (1234) print (digits) print (sign) [digits, sign] = LXX_QYY (-5678) print (digits) print (sign)

    Explanation:

    Firstly, let's create a function LXX_QYY () that take one input number, num. (Line 1 - 20)

    Since one of the output is a vector of digit, we create a new list, num_list to hold the vector of digit.

    Prior to generating the vector of digit, we need to handle two possible types of number, which are positive and negative. Define an if-else statement (Line 3 & 11). If the number is positive, program will go through Line 4 - 10, else Line 12 - 20. One different between this two parts of codes is that, if the number is negative, it is necessary to multiply the number with - 1 to convert it to positive.

    To capture the individual digit of the input number, we need to convert the number into string (Line 4) so that we can traverse through each of the digit using for loop (Line 7).

    Besides, we also need to ensure the digit captured in order from lowest place to highest place. Therefore, we need to extract the digit one after another from the last index (Line 5) and then add it into the num_list (Line 8).

    Next, we can return the num_list along with the sign 0 (if the number is positive) or 1 (if the number is negative) as a list.

    At last, we can test the function with two sample cases (Line 22 & 26) and they shall give the output as:

    0

    [4, 3, 2, 1]

    1

    [8, 7, 6, 5]
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “In this problem you will write a function that takes in a single integer (positive, negative, or zero) and produces an array of all of its ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers