Question

In: Computer Science

USING PYTHON PROGRAM ONLY (cannot use list, max, or index function because have not learned about...

USING PYTHON PROGRAM ONLY (cannot use list, max, or index function because have not learned about it in class yet)

Part C builds on parts A and B, so just need the final code from part C.

USING PYTHON PROGRAM

Part 4a: Addition Table

Write a program that prompts the user for two integers that are greater than or equal to zero. Ensure that the first integer is less than the second integer. Next, write a program that generates an "addition table" using these numbers that computes the sums of all possible values - use the output below as a guide:

Lowest number: -5
Lowest number must be 0 or greater
Lowest number: 0
Highest number: 0
Highest number must be larger than lowest number!
Highest number: 5

 +     0   1   2   3   4   5
----------------------------
0  |   0   1   2   3   4   5
1  |   1   2   3   4   5   6
2  |   2   3   4   5   6   7
3  |   3   4   5   6   7   8
4  |   4   5   6   7   8   9
5  |   5   6   7   8   9  10

Here are some hints to get you started:

  • Don't worry about formatting your table until you have a basic version printing out correctly.
  • Start by generating the first line of output (the '+' sign and the column headings)
  • Next, generate the left most column (the row headings)
  • Big hint: Python prints from top to bottom, left to right. Before moving onto the next row, what do you have to do? Do you need another loop here?
  • When you're ready to format your table you need to ensure that no matter what there will always be room for all of your numbers. Hint: you might need to count the size of your largest possible value and use that as part of the "format" function. For example, notice how the spacing in the following tables always works out no matter the size of the values being printed:
    Lowest number: 9999
    Highest number: 10004
    
       +      9999  10000  10001  10002  10003  10004
    -------------------------------------------------
    9999  |  19998  19999  20000  20001  20002  20003
    10000 |  19999  20000  20001  20002  20003  20004
    10001 |  20000  20001  20002  20003  20004  20005
    10002 |  20001  20002  20003  20004  20005  20006
    10003 |  20002  20003  20004  20005  20006  20007
    10004 |  20003  20004  20005  20006  20007  20008
    Lowest number: 9999999
    Highest number: 10000004
    
        +        9999999  10000000  10000001  10000002  10000003  10000004
    ----------------------------------------------------------------------
    9999999  |  19999998  19999999  20000000  20000001  20000002  20000003
    10000000 |  19999999  20000000  20000001  20000002  20000003  20000004
    10000001 |  20000000  20000001  20000002  20000003  20000004  20000005
    10000002 |  20000001  20000002  20000003  20000004  20000005  20000006
    10000003 |  20000002  20000003  20000004  20000005  20000006  20000007
    10000004 |  20000003  20000004  20000005  20000006  20000007  20000008

Part 4b: Addition Table

Next, add in a feature that asks the user if they want to identify 'Prime' numbers in their table. If the user elects to show prime numbers you can print a lowercase 'p' character after each prime number. Ensure that your table displays correctly, as described above.

Lowest number: 0
Highest number: 10
Would you like to identify Prime numbers in your table? (y/n): pikachu
Invalid command, try again
Would you like to identify Prime numbers in your table? (y/n): y
 +     0   1   2   3   4   5   6   7   8   9  10
------------------------------------------------
0  |   0   1  2p  3p   4  5p   6  7p   8   9  10
1  |   1  2p  3p   4  5p   6  7p   8   9  10 11p
2  |  2p  3p   4  5p   6  7p   8   9  10 11p  12
3  |  3p   4  5p   6  7p   8   9  10 11p  12 13p
4  |   4  5p   6  7p   8   9  10 11p  12 13p  14
5  |  5p   6  7p   8   9  10 11p  12 13p  14  15
6  |   6  7p   8   9  10 11p  12 13p  14  15  16
7  |  7p   8   9  10 11p  12 13p  14  15  16 17p
8  |   8   9  10 11p  12 13p  14  15  16 17p  18
9  |   9  10 11p  12 13p  14  15  16 17p  18 19p
10 |  10 11p  12 13p  14  15  16 17p  18 19p  20

Part 4c: Addition Table

Expand your program to support ALL of the arithmetic operators (+, -, *, /, // and %) - prompt the user for an operator to use and then display the desired table. Validate your data accordingly. Ensure that your tables print out using the expected formatting. Note that negative numbers are not considered Prime for the purpose of this part of the assignment.

Solutions

Expert Solution

Code for above program :-

def add(low,high,p):
    string = '+  '
    for i in range(low,high+1):
        string = string + " " + str(i)
    print(string)
    print("-"*((high-low)*4))
    for i in range(low,high+1):
        string = str(i)+" "+"|"
        num = i
        for j in range(low,high+1):
            temp = num + j
            if p == 1:
                if isPrime(temp):
                    temp = str(temp) + "p"
                else:
                    temp = str(temp)
            else:
                temp = str(temp)
            string = string+" "+ temp
        print(string)


def isPrime(n):
    if (n <= 1):
        return False
    if (n <= 3):
        return True
    if (n % 2 == 0 or n % 3 == 0):
        return False
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i = i + 6
    return True

def subtract(low,high,p):
    string = '-  '
    for i in range(low,high+1):
        string = string + " " + str(i)
    print(string)
    print("-"*((high-low)*4))
    for i in range(low,high+1):
        string = str(i)+" "+"|"
        num = i
        for j in range(low,high+1):
            temp = num - j
            if p == 1:
                if isPrime(temp):
                    temp = str(temp) + "p"
                else:
                    temp = str(temp)
            else:
                temp = str(temp)
            string = string+" "+str(temp)
        print(string)

def product(low,high,p):
    string = '*  '
    for i in range(low,high+1):
        string = string + " " + str(i)
    print(string)
    print("-"*((high-low)*4))
    for i in range(low,high+1):
        string = str(i)+" "+"|"
        num = i
        for j in range(low,high+1):
            temp = num * j
            if p == 1:
                if isPrime(temp):
                    temp = str(temp) + "p"
                else:
                    temp = str(temp)
            else:
                temp = str(temp)
            string = string+" "+str(temp)
        print(string)

def division(low,high,p):
    string = '/  '
    for i in range(low,high+1):
        string = string + " " + str(i)
    print(string)
    print("-" * ((high - low) * 4))
    string = str(0) + " " + "|"
    for i in range(low,high+1):
        string = string +" "+ "-"
    print(string)
    for i in range(low+1,high+1):
        string = str(i)+" "+"|"
        num = i
        for j in range(low,high+1):
            temp = j/num
            temp = round(temp, 2)
            if p == 1:
                if float(temp).is_integer() and isPrime(temp):
                    temp = str(temp) + "p"
                else:
                    temp = str(temp)
            else:
                temp = str(temp)
            string = string+" "+ str(temp)
        print(string)

def floor(low,high,p):
    string = '//  '
    for i in range(low,high+1):
        string = string + " " + str(i)
    print(string)
    print("-"*((high-low)*4))
    string = str(0) + " " + "|"
    for i in range(low, high + 1):
        string = string + " " + "-"
    print(string)
    for i in range(low+1,high+1):
        string = str(i)+" "+"|"
        num = i
        for j in range(low,high+1):
            temp = j//num
            if p == 1:
                if isPrime(temp):
                    temp = str(temp) + "p"
                else:
                    temp = str(temp)
            else:
                temp = str(temp)
            string = string+" "+str(temp)
        print(string)

def mod(low,high,p):
    string = '%  '
    for i in range(low,high+1):
        string = string + " " + str(i)
    print(string)
    print("-"*((high-low)*4))
    string = str(0) + " " + "|"
    for i in range(low, high + 1):
        string = string + " " + "-"
    print(string)
    for i in range(low+1,high+1):
        string = str(i)+" "+"|"
        num = i
        for j in range(low,high+1):
            temp = j%num
            if p == 1:
                if isPrime(temp):
                    temp = str(temp) + "p"
                else:
                    temp = str(temp)
            else:
                temp = str(temp)
            string = string+" "+str(temp)
        print(string)

low = int(input("Enter Lowest Number: "))
while low<0:
    low = int(input("Lowest number must be 0 or greater: "))
high = int(input("Enter Highest Number: "))
while high<low:
    high = int(input("Highest number must be greater than lowest number: "))
operation = input("Enter +,-,*,/,//,% to perform operation: ")
p = 0
if operation=="+":
    add(low,high,p)
    pr = ''
    while pr.lower()!="y" or pr.lower()!='n':
        pr = input("Would you like to know the prime in the above table(y/n): ")
        if pr.lower() =="y":
            p = 1
            add(low,high,p)
            break
        elif pr.lower()=='n':
            break
        else:
            print("Invalid input Try again .")
elif operation=="-":
    subtract(low,high,p)
    pr = ''
    while pr.lower() != "y" or pr.lower() != 'n':
        pr = input("Would you like to know the prime in the above table(y/n): ")
        if pr.lower() == "y":
            p = 1
            subtract(low, high, p)
            break
        elif pr.lower() == 'n':
            break
        else:
            print("Invalid input Try again .")
elif operation=="*":
    product(low,high,p)
    pr = ''
    while pr.lower() != "y" or pr.lower() != 'n':
        pr = input("Would you like to know the prime in the above table(y/n): ")
        if pr.lower() == "y":
            p = 1
            product(low, high, p)
            break
        elif pr.lower() == 'n':
            break
        else:
            print("Invalid input Try again .")
elif operation=="/":
    division(low,high,p)
    pr = ''
    while pr.lower() != "y" or pr.lower() != 'n':
        pr = input("Would you like to know the prime in the above table(y/n): ")
        if pr.lower() == "y":
            p = 1
            division(low, high, p)
            break
        elif pr.lower() == 'n':
            break
        else:
            print("Invalid input Try again .")
elif operation=="//":
    floor(low,high,p)
    pr = ''
    while pr.lower() != "y" or pr.lower() != 'n':
        pr = input("Would you like to know the prime in the above table(y/n): ")
        if pr.lower() == "y":
            p = 1
            floor(low, high, p)
            break
        elif pr.lower() == 'n':
            break
        else:
            print("Invalid input Try again .")
elif operation=="%":
    mod(low,high,p)
    pr = ''
    while pr.lower() != "y" or pr.lower() != 'n':
        pr = input("Would you like to know the prime in the above table(y/n): ")
        if pr.lower() == "y":
            p = 1
            mod(low, high, p)
            break
        elif pr.lower() == 'n':
            break
        else:
            print("Invalid input Try again .")


Screenshot for above program :-


Related Solutions

1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
which statements are true about Python functions? a)Different functions cannot use same function name b)a function...
which statements are true about Python functions? a)Different functions cannot use same function name b)a function always returns some value c)different function cannot use the same variable names d) function must use the same parameter names as the corresponding variables in the caller what benefits does structuring a program through defining functions bring? a) there is a possibility of reducing the number of variables and/or objects that must be managed at any cost at any one point b)the program is...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot use for or while loops to solve the problems as well as not using global variables. 1. Create a function that takes a positive integer and returns it with neighboring digits removed. Do not convert the integer to a list. Ex. Input = [5555537777721] Output = [53721]
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT