コード例 #1
0
ファイル: arith.py プロジェクト: ibrahimmedhar/Keuzevak-IADIP
def sum(a, b):
    r = 0                                     # Result
    c = 0                                     # Carry

    length_a = arith_tools.get_number_length(a)     # Get length of number a
    length_b = arith_tools.get_number_length(b)     # Get length of number b
    max_length = max(length_a, length_b)            # Calculate maximum a if a > b, else b
    
    # Implement algorithm

    return r
コード例 #2
0
ファイル: arith.py プロジェクト: ibrahimmedhar/Keuzevak-IADIP
def sub(a, b):
    r = 0                                           # Result
    c = 0                                           # Borrow

    s = 1                                           # Sign is positive
    if b > a:
        pass # Implement algorithm, replace pass by your own code

    length_a = arith_tools.get_number_length(a)     # Get length of number a
    length_b = arith_tools.get_number_length(b)     # Get length of number b
    max_length = max(length_a, length_b)            # Calculate maximum a if a > b, else b
    
    # Implement algorithm

    return r * s                                    # Note the sign
コード例 #3
0
def sub(a, b):
    r = 0  # Result
    c = 0  # Borrow

    s = 1  # Sign is positive

    answer_list = []
    # Create a list of the given digits
    a_list = [int(i) for i in str(a)]
    b_list = [int(i) for i in str(b)]

    if b > a:
        s = -1
        op1 = b_list
        op2 = a_list
    else:
        op1 = a_list
        op2 = b_list

    op1.reverse()
    op2.reverse()

    length_a = arith_tools.get_number_length(a)  # Get length of number a
    length_b = arith_tools.get_number_length(b)  # Get length of number b
    max_length = max(length_a,
                     length_b)  # Calculate maximum a if a > b, else b

    # Implement algorithm
    for x in range(max_length):
        try:
            r = op1[x] - op2[x] - c
        except:
            r = op1[x] - c

        c = 0

        if r < 0:
            r += 10
            c += 1
        answer_list.append(r)

    answer_list.reverse()
    r = ''

    for x in answer_list:
        r = r + str(x)

    return int(r) * s  # Note the sign
コード例 #4
0
def sum(a, b):
    r = 0  # Result
    c = 0  # Carry
    result = []

    length_a = arith_tools.get_number_length(a)  # Get length of number a
    length_b = arith_tools.get_number_length(b)  # Get length of number b
    max_length = max(length_a,
                     length_b)  # Calculate maximum a if a > b, else b

    a1 = str(a)
    b1 = str(b)
    a_list = list(a1)
    b_list = list(b1)
    a_list.reverse()
    b_list.reverse()

    while max_length > 0:
        if len(a_list) > 0:
            sum1 = a_list.pop(0)
        else:
            sum1 = 0
        if len(b_list) > 0:
            sum2 = b_list.pop(0)
        else:
            sum2 = 0

        current_step = c + int(sum1) + int(sum2)

        if current_step >= 10:
            c = 1
            current_step = current_step - 10
            max_length = max_length - 1
            result.extend(str(current_step))
        else:
            c = 0
            max_length = max_length - 1
            result.extend(str(current_step))
    if c == 1:
        result.extend(str('1'))
    result.reverse()
    result_int = list(map(int, result))
    try:
        r = int("".join(map(str, result_int)))
    except ValueError:
        pass
    return r
コード例 #5
0
def sub(a, b):
    r = 0  # Result
    c = 0  # Borrow
    a = int(a)
    b = int(b)
    s = 1  # Sign is positive
    if b > a:
        s = -1

    length_a = arith_tools.get_number_length(a)  # Get length of number a
    length_b = arith_tools.get_number_length(b)  # Get length of number b
    max_length = max(length_a,
                     length_b)  # Calculate maximum a if a > b, else b

    # Implement algorithm
    list_a = [int(i) for i in str(a)]
    list_b = [int(i) for i in str(b)]
    if arith_tools.longest(length_a, length_b) == length_a:
        while len(list_b) != len(list_a):
            list_b.insert(0, 0)
    else:
        while len(list_a) != len(list_b):
            list_a.insert(0, 0)
    list_r = []
    for i in range(max_length):
        minus_i = (i * (-1)) - 1
        if s == 1:
            digit = list_a[minus_i] - list_b[minus_i] - c
        elif s == -1:
            digit = list_b[minus_i] - list_a[minus_i] - c
        if digit < 0 and i != max_length - 1:
            digit += 10
            c = 1
        else:
            c = 0
        list_r.append(digit)
    list_r = list_r[::-1]
    r = int(arith_tools.list_to_str(list_r))
    return r * s  # Note the sign
コード例 #6
0
def sum(a, b):
    r = 0  # Result
    c = 0  # Carry
    a = int(a)
    b = int(b)
    length_a = arith_tools.get_number_length(a)  # Get length of number a
    length_b = arith_tools.get_number_length(b)  # Get length of number b
    max_length = max(length_a,
                     length_b)  # Calculate maximum a if a > b, else b

    if a < 0 and b > 0:
        r = sub(a * (-1), b)
        r *= -1
    elif b < 0:
        r = sub(a, b * (-1))
    else:
        # Implement algorithm
        list_a = [int(i) for i in str(a)]
        list_b = [int(i) for i in str(b)]
        if arith_tools.longest(length_a, length_b) == length_a:
            while len(list_b) != len(list_a):
                list_b.insert(0, 0)
        else:
            while len(list_a) != len(list_b):
                list_a.insert(0, 0)
        list_r = []
        for i in range(max_length):
            minus_i = (i * (-1)) - 1
            tempR = c + list_a[minus_i] + list_b[minus_i]
            list_r.append(tempR % 10)
            c = tempR // 10
        if c > 0:
            list_r.append(c)
        list_r = list_r[::-1]
        r = arith_tools.list_to_str(list_r)
    return r
コード例 #7
0
def sub(a, b):
    r = 0  # Result
    c = 0  # Borrow
    result = []

    s = 1  # Sign is positive
    if b > a:
        s = -1
    elif a > b:
        s = 1

    length_a = arith_tools.get_number_length(a)  # Get length of number a
    length_b = arith_tools.get_number_length(b)  # Get length of number b
    max_length = max(length_a,
                     length_b)  # Calculate maximum a if a > b, else b

    a1 = str(a)
    b1 = str(b)
    a_list = list(a1)
    b_list = list(b1)
    a_list.reverse()
    b_list.reverse()
    if s == 1:
        while max_length > 0:
            if len(a_list) > 0:
                sub1 = int(a_list.pop(0))
            else:
                sub1 = 0
            if len(b_list) > 0:
                sub2 = int(b_list.pop(0))
            else:
                sub2 = 0

            if c == 1:
                sub1 = sub1 - 1

            if sub2 > sub1:
                sub1 = sub1 + 10
                current_step = sub1 - sub2
                c = 1
                max_length = max_length - 1
                result.extend(str(current_step))
            elif sub2 <= sub1:
                current_step = sub1 - sub2
                c = 0
                result.extend(str(current_step))
                max_length = max_length - 1
    elif s == -1:
        while max_length > 0:
            if len(a_list) > 0:
                sub2 = int(a_list.pop(0))
            else:
                sub2 = 0
            if len(b_list) > 0:
                sub1 = int(b_list.pop(0))
            else:
                sub1 = 0
            if c == 1:
                sub1 = sub1 - 1

            if sub2 > sub1:
                sub1 = sub1 + 10
                current_step = sub1 - sub2
                c = 1
                max_length = max_length - 1
                result.extend(str(current_step))
            elif sub2 <= sub1:
                current_step = sub1 - sub2
                c = 0
                result.extend(str(current_step))
                max_length = max_length - 1

    result.reverse()
    result_int = list(map(int, result))
    r = int("".join(map(str, result_int)))

    return r * s  # Note the sign
コード例 #8
0
def sum(a, b):
    r = 0  # Result
    c = 0  # Carry

    length_a = arith_tools.get_number_length(a)  # Get length of number a
    length_b = arith_tools.get_number_length(b)  # Get length of number b
    max_length = max(length_a,
                     length_b)  # Calculate maximum a if a > b, else b

    # Implement algorithm
    answer_list = []
    # Create a list from the given digits
    a_list = [int(i) for i in str(a)]
    b_list = [int(i) for i in str(b)]

    # check which operand is bigger and use is as the first one
    if a > b:
        op1 = a_list
        op2 = b_list
    else:
        op1 = b_list
        op2 = a_list

    # reverse the lists to start from right to left
    op1.reverse()
    op2.reverse()

    # Loop to the length of the largest given number + 1 (for the potential carry that has to be added in front)
    for x in range(max_length + 1):
        if c > 0:
            # Try to add all the number else try to add to the main number else just add the remaining Carry
            try:
                r = op1[x] + op2[x] + c
            except:
                try:
                    r = op1[x] + c
                except:
                    r = c
            # If there is no carry just add the two operands togheter else just add the main remaining operand else break out of the loop
        else:
            try:
                r = op1[x] + op2[x]
            except:
                try:
                    r = op1[x]
                except:
                    break

        # after using the carry, set it back to 0 for next upcoming use
        c = 0

        # if r is larger than 10 add 1 to carry and remove 10 from r
        if r >= 10:
            r -= 10
            c += 1
        # then add the r to the answer_list
        answer_list.append(r)
    # Reverse the answer_list(rember we reversed it in the beginning so now we reverse it back to have the true number position)
    answer_list.reverse()
    # Change the r variable into a empty string
    r = ''

    # loop through the final answe_list and create a string from it
    for x in answer_list:
        r = r + str(x)
    # return the created string as an integer
    return int(r)