Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
152 views
ubuntu2204
Kernel: SageMath 10.0

Description

You must complete the following two functions. Your inputs are the digits of left/right hand side of the operators. We have discussed the solution during practice. You can assume that the inputs are correct, i.e. you should not check for errors. The digits are in little endian order, i.e. 123 in base 10 is [3, 2, 1]. You can only work with the digits and must not calculate the numbers from the digits.

You solution (Part 1):

def add(lhs_digits: list[Integer], rhs_digits: list[Integer], base: Integer) -> list[Integer]: # Initializing an empty list to store the result result = [] # Initializing a variable "var" to 0 to store # and keep track of the carry value during the addition var = 0 # Here we are trying to find the maximum of the length # from the given two separate inputs or numbers. # We have to make sure that we take the maximum length # between lhs_digits and rhs_digits to ensure they have the # same length for addition to prevent error in case of unequal length. max_length = max(len(lhs_digits), len(rhs_digits)) # In this step, we are padding the input lists with zeros # at the end to make them of equal length lhs_digits = lhs_digits + [0] * (max_length - len(lhs_digits)) rhs_digits = rhs_digits + [0] * (max_length - len(rhs_digits)) # We are iterating through each position in the digits list # starting from the least significant or the rightmost digit for i in range(max_length): # Calculating the sum of the digits at the current # positions in both lists along with the carry value digit_sum = lhs_digits[i] + rhs_digits[i] + var # Now we are calculating the new carry value for the next # iteration by dividing the digit sum by the specified base. var = digit_sum // base # Here we are calculating the digit to be placed at the # current position in the results by taking the remainder # of the digit sum when divided by the base. digit = digit_sum % base # Appending the calculated digit to the result list result.append(digit) # If there is a carry value left after the loop, we ar adding it to the result if var > 0: result.append(var) # Returning the final result as a list of digits return result

Check your solution

assert add([0], [1], 10) == [1] assert add([0], [1, 1], 10) == [1, 1] assert add([0, 1], [1], 10) == [1, 1] assert add([2, 2], [3, 9], 10) == [5, 1, 1]

Your solution (Part 2):

def mul(lhs_digits: list[Integer], rhs_digits: list[Integer], base: Integer) -> list[Integer]: # Initializing a result list filled with zeroes # to store the product of lhs_digits and rhs_digits. # The length of this list is set to accommodate the maximum # possible number of digits in the product. result = [0] * (len(lhs_digits) + len(rhs_digits)) # Looping over each digit in the lhs_digits for i in range(len(lhs_digits)): # Initializing a carry variable to 0, which will be used # to keep track of carry values during multiplication. carry = 0 # Looping over each digit in the rhs_digits for j in range(len(rhs_digits)): # Calculating the product of the current digits in the lhs_digits and rhs_digits # along with the current value in the result list and the carry. mul_result = lhs_digits[i] * rhs_digits[j] + result[i + j] + carry # Updating the carry for the next iteration # by dividing mul_result by the specified base and # assigns the quotient to carry. carry = mul_result // base # Updating the result at the corresponding position # with the remainder of mul_result when divided by the base. result[i + j] = mul_result % base # If there is a carry remaining after the inner loop, add # it to the result at the position corresponding to the end of rhs_digits. if carry > 0: result[i + len(rhs_digits)] += carry # Removing trailing zeros from the result by iterating from # right to left until a non-zero digit is found. # It ensures that the result does not contain unnecessary leading zeros. while len(result) > 1 and result[-1] == 0: result.pop() # Returning the final result as a list of digits. return result

Check your solution

assert mul([0], [1], 10) == [0] assert mul([1], [0], 10) == [0] assert mul([0, 1], [1], 10) == [0, 1] assert mul([1], [0, 1], 10) == [0, 1] assert mul([5], [6], 10) == [0, 3] assert mul([9, 9], [9, 9], 10) == [1, 0, 8, 9]