Programming Homework Help

Programming Homework Help. Concord University Programming & Basic Arithmetic Functions Lab Report

directions provided!

# these are the basic arithmetic functions you will be using for this challenge

# function:   add
# input:      two integers/floats
# processing: adds the two supplied values
# output:     returns the sum (integer/float)
def add(a,b):
    return a+b

# function:   sub
# input:      two integers/floats
# processing: subtracts the two supplied values
# output:     returns the difference (integer/float)
def sub(a,b):
    return a-b

# function:   mult
# input:      two integers/floats
# processing: multiplies the two supplied values
# output:     returns the product (integer/float)
def mult(a,b):
    return a*b

# function:   sqrt
# input:      one integer/float
# processing: computes the square root of the supplied value
# output:     returns the square root (float)
def sqrt(a):
    return a**0.5

# function:   square
# input:      one integer/float
# processing: raises the supplied integer/float to the 2nd power
# output:     returns the square (integer/float)
def square(a):
    return a**2

# these are the two points you will be using

# point 1
x1 = 0
y1 = 0

# point 2
x2 = 100
y2 = 100

# compute the distance between the two points above using the distance formula.
# you may ONLY use the functions above to do this - no math operators are allowed!  
# your calculation must also be done on a single line.
distance = ______________________

print (distance) # answer should be 141.4213562373095

distance formula: 

 Part 2
Write two functions called <strong>'maximum'</strong> and <strong>'minimum'</strong> - these function should accept two arguments and <strong>return</strong> the larger/smaller of the two supplied values. For the purpose of this program you can always assume that the arguments being supplied are numeric.. Your program should work perfectly with the following code:
a = 5
b = 10
c = 15
d = 20

ans1 = maximum(a,b)
ans2 = maximum(a,c)
ans3 = maximum(a,d)
print (ans1,ans2,ans3) # 10 15 20

ans4 = minimum(d,c)
ans5 = minimum(d,b)
ans6 = minimum(d,a)
print (ans4,ans5,ans6) # 15 10 5

ans7 = maximum( maximum(a,b), maximum(c,d) )
print ("The biggest is:", ans7)

Challenge 3

# write a function called '<strong>simple_sort_version1'</strong> that accepts two values.  you can assume
# that your two values will always be the same data type (all ints, all floats or all strings).
# sort these two values in ascending order and <strong>return</strong> them in that order. 
# you may use any function that you've written so far in this assignment if you'd like to (maximum, minimum, etc)

# your function should work perfectly with the following lines of code
a,b = simple_sort_version1(10,20)
print (a,b) # 10 20

a,b = simple_sort_version1(20,10)
print (a,b) # 10 20

a,b = simple_sort_version1(30,30)
print (a,b) # 30 30

Challenge 4

# next, write a new function called '<strong>simple_sort_version2</strong>' that accepts three values.  you can assume
# that your three values will always be the same data type (all ints, all floats or all strings).
# sort these values in ascending order and <strong>return</strong> them. 
# you may use any function that you've written so far in this assignment if you'd like to (simple_sort_version1, maximum, minimum, etc)

# your function should work perfectly with the following lines of code
a,b,c = simple_sort_version2(10,20,30)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(10,30,20)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(30,20,10)
print (a,b,c) # 10 20 30

a,b,c = simple_sort_version2(30,20,20)
print (a,b,c) # 20 20 30
 
  

Part 2a

For this step you are given three functions – go ahead and copy these functions into a new file called “digitalclock.py”:

# function:   horizontal_line
# input:      a width value (integer) and a single character (string)
# processing: generates a single horizontal line of the desired size
# output:     returns the generated pattern (string)
def horizontal_line(width,char):
    return width*char + "n"

# function:   vertical_line
# input:      a shift value and a height value (both integers)  and a single character (string)
# processing: generates a single vertical line of the desired height.  the line is
#             offset from the left side of the screen using the shift value
# output:     returns the generated pattern (string)
def vertical_line(shift,height,char):
    pattern = ""
    for i in range(height):
        pattern += shift*" " + char + "n"
    return pattern

# function:   two_vertical_lines
# input:      a width value and a height value (both integers)  and a single character (string)
# processing: generates two vertical lines.  the first line is along the left side of
#             the screen. the second line is offset using the "width" value supplied
# output:     returns the generated pattern (string)
def two_vertical_lines(width,height,char):
    pattern = ""
    for i in range(height):
        pattern += char + " "*(width-2) + char + "n"
    return pattern

Next, create a file called ‘LastName_FirstName_Assign6_part2.py’ – make sure this file is in the same folder as your newly created ‘digitalclock.py’ file. Import your module then run the following code – you should be able to see a series of graphical patterns that match the output below:

import digitalclock

print ("Horizontal line, width = 5:")
temp = digitalclock.horizontal_line(5, '*')
print (temp)
print ()

print ("Horizontal line, width = 10:")
temp = digitalclock.horizontal_line(10, '+')
print (temp)
print ()

print ("Horizontal line, width = 15:")
temp = digitalclock.horizontal_line(15, 'z')
print (temp)
print ()

print ("Vertical Line, shift=0; height=3:")
temp = digitalclock.vertical_line(0, 3, '!')
print (temp)
print ()

print ("Vertical Line, shift=3; height=3:")
temp = digitalclock.vertical_line(3, 3, '&')
print (temp)
print ()

print ("Vertical Line, shift=6; height=5:")
temp = digitalclock.vertical_line(6, 5, '$')
print (temp)
print ()

print ("Two Vertical Lines, width=3; height=3:")
temp = digitalclock.two_vertical_lines(3, 3, '^')
print (temp)
print ()

print ("Two Vertical Lines, width=4; height=5:")
temp = digitalclock.two_vertical_lines(4, 5, '@')

print (temp)
print ()

print ("Two Vertical Lines, width=5; height=2:")
temp = digitalclock.two_vertical_lines(5, 2, '#')
print (temp)
print ()

Expected Output:

Horizontal line, width = 5:
*****

Horizontal line, width = 10:
++++++++++

Horizontal line, width = 15:
zzzzzzzzzzzzzzz

Vertical Line, shift=0; height=3:
!
!
!
Vertical Line, shift=3; height=3:
   &
   &
   &
Vertical Line, shift=6; height=5:
      $
      $
      $
      $
      $
Two Vertical Lines, width=3; height=3:
^ ^
^ ^
^ ^
Two Vertical Lines, width=4; height=5:
@  @
@  @
@  @
@  @
@  @
Two Vertical Lines, width=5; height=2:
#   #
#   #
   
   Part 2b
As you can see, you have three "primitive" functions for generating simple shapes (horizontal lines, vertical lines and parallel vertical lines). Your next task is to write 10 new functions that generate the digits 0-9 using your three line functions. These functions should be stored in your 'digitalclock.py' module. The goal here is to render the digits as they would appear on a digital display:
Each function should accept a "width" argument to control how wide the number should be as well as a single character. You can assume numbers will always be printed with a height of 5. For example, here is the function for the number 1:
# function:   number_1
# input:      a width value (integer) and a single character (string)
# processing: generates the number 1 as it would appear on a digital display
#             using the supplied width value
# output:     returns the generated pattern (string)
def number_1(width, character):
    pattern = vertical_line(width-1, 5, character)
    return patternAnd here's a sample program that calls the function a few times (test this in your main program, not in your module):
print ("Number 1, width=5: ")
temp = digitalclock.number_1(5, '*')
print(temp)
print()

print ("Number 1, width=10: ")
temp = digitalclock.number_1(10, '*')
print(temp)
print()

print ("Number 1, width=2: ")
temp = digitalclock.number_1(2, '*')
print(temp)
print()And here's the expected output:
Number 1, width=5: 
    *
    *
    *
    *
    *
    

Number 1, width=10: 
         *
         *
         *
         *
         *
         

Number 1, width=2: 
 *
 *
 *
 *
 *
 
 Here's a sample program that prints all of the numbers (0-9).
temp = digitalclock.number_0(5, '*')
print(temp)
print()

temp = digitalclock.number_1(5, '*')
print(temp)
print()

temp = digitalclock.number_2(5, '*')
print(temp)
print()

temp = digitalclock.number_3(5, '*')
print(temp)
print()

temp = digitalclock.number_4(5, '*')
print(temp)
print()

temp = digitalclock.number_5(5, '*')
print(temp)
print()

temp = digitalclock.number_6(5, '*')
print(temp)
print()

temp = digitalclock.number_7(5, '*')
print(temp)
print()

temp = digitalclock.number_8(5, '*')
print(temp)
print()

temp = digitalclock.number_9(5, '*')
print(temp)
print()And here's the expected output:
*****
*   *
*   *
*   *
*****
    *
    *
    *
    *
    *
*****
    *
*****
*
*****
*****
    *
*****
    *
*****
*   *
*   *
*****
    *
    *
*****
*
*****
    *
*****
*****
*
*****
*   *
*****
*****
    *
    *
    *
    *
*****
*   *
*****
*   *
*****
*****
*   *
*****
    *
    *
    
    Part 2c
Write a function called 'print_number' that prints out any desired number to the screen. This function should also be placed in your 'digitalclock.py' module. Here's the IPO for this function:
# function:   print_number
# input:      a number to print (integer), a width value (integer) and a single character (string)
# processing: prints the desired number to the screen using the supplied width value
# output:     does not return anythingAnd here's some sample code that you can use to test your function:
digitalclock.print_number(0, 5, '*')
digitalclock.print_number(1, 6, '*')
digitalclock.print_number(2, 7, '*')
digitalclock.print_number(3, 8, '*')
digitalclock.print_number(4, 9, '*')
digitalclock.print_number(5, 10, '*')
digitalclock.print_number(6, 11, '*')
digitalclock.print_number(7, 12, '*')
digitalclock.print_number(8, 13, '*')
digitalclock.print_number(9, 14, '*')And here's the expected output:
*****
*   *
*   *
*   *
*****

     *
     *
     *
     *
     *

*******
      *
*******
*
*******

********
       *
********
       *
********

*       *
*       *
*********
        *
        *

**********
*
**********
         *
**********

***********
*
***********
*         *
***********

************
           *
           *
           *
           *

*************
*           *
*************
*           *
*************

**************
*            *
**************
             *
             *
 Part 2d
Write two new functions that simulate the addition and subtraction operators. Each of these functions should accept a width value as an argument (integer) and a single character (string) -- the function should then return the generated pattern. You can assume the operators will always be 5 units high. Again, these functions should be placed in your 'digitalclock.py' module. Here's some sample code:
temp = digitalclock.plus(5, '*')
print(temp)
print()

temp = digitalclock.minus(5, '*')
print(temp)Which will generate ...
  *
  *
*****
  *
  *
*****
Note that your "plus" sign may look odd if it is rendered using an even size value - for example:
# rendered using a width of 6
  *
  *
******
  *
  *
To fix this you should double up the vertical line in the center for even sizes, like this:
# rendered using a width of 6
  **
  **
******
  **
  **

Part 2e
Write a function called "check_answer" which will determine if a given addition or subtraction problem was solved correctly. This function should be inside of your "digitalclock.py" module. Here's the IPO notation for the function:
# function:   check_answer
# input:      two numbers (number1 & number2, both integers); an answer (an integer)
#             and an operator (+ or -, expressed as a String)
# processing: determines if the supplied expression is correct.  for example, if the operator
#             is "+", number1 = 1, number2 = 2 and answer = 3 then the expression is correct
#             (1 + 2 = 3).
# output:     returns True if the expression is correct, False if it is not correct
Here's a sample program that you can use to test your function:
answer1 = digitalclock.check_answer(1, 2, 3, "+")
print (answer1)
answer2 = digitalclock.check_answer(1, 2, -1, "-")
print (answer2)
answer3 = digitalclock.check_answer(9, 5, 3, "+")
print (answer3)
answer4 = digitalclock.check_answer(8, 2, 4, "-")
print (answer4)And here's the expected output:
True
True
False
FalsePart 2f
Finally, put everything together and write a program that lets the user practice a series of random addition and subtraction problems. Begin by asking the user for a number of problems (only accept positive values) and a size for their numbers (only accept numbers between 5 and 10). Also prompt them for a single character to be used to generate their patterns - only accept single character strings (i.e. 'a' is OK, but 'apple' is not). The generate a series of random addition and subtraction problems - display the numbers to the user with your digital display functions. Then prompt the user for an answer and check the answer using your check_answer function. Your program should also keep track of how many correct questions the user answered during their game. Here's a sample running of the program:
How many problems would you like to attempt? -5
Invalid number, try again

How many problems would you like to attempt? 5
How wide do you want your digits to be? 5-10: 3
Invalid width, try again

How wide do you want your digits to be? 5-10: 5

What character would you like to use? foo
String too long, try again
What character would you like to use? *

Here we go!

What is .....

*****
    *
*****
    *
*****

  *
  *
*****
  *
  *

    *
    *
    *
    *
    *

= 4
Correct!

What is .....

*****
    *
*****
*
*****
*****
*****
    *
    *
    *
    *

= -5
Correct!

What is .....

    *
    *
    *
    *
    *
*****
*****
*
*****
    *
*****

= 0
Sorry, that's not correct.

What is .....

*****
    *
*****
*
*****

  *
  *
*****
  *
  *

    *
    *
    *
    *
    *

= 3
Correct!

What is .....

*****
    *
*****
*
*****

  *
  *
*****
  *
  *

*****
    *
*****
*
*****

= 4
Correct!

You got 4 out of 5 correct!
 Part 3: Extra Credit
You can add any of the following features to your game for extra credit. These are optional features and are not required to receive full credit on the assignment!:
Add multiplication problems to the game. You will have to update your check_answer function as well as add a new operator function to display the multiplication sign. Note that the visual representation of your multiplication sign does not need to be "perfect" - try and come up with a function that somewhat looks like a "X" or "*" character.
Add division problems to the game. You will have to update your check_answer function as well as add a new operator function to display the division sign. For division problems you need to ensure that the result of the problem you present is a whole number. For example, the following would be valid division problems for this game:2 / 2 = 1
4 / 2 = 2
9 / 3 = 3However, the following would NOT be valid since the answers are not whole numbers:5 / 2 = 2.5
9 / 8 = 1.125
8 / 3 = 2.6666666 (repeating)Ensure that the division problems you supply to your players always yield a whole number result. You may need to generate a few different numbers in order to do this (hint: while loop!).
Add in a "drill" mode to your game. If this mode is activated by the user they will be re-prompted to solve a problem that they got incorrect. Points are turned off in "drill" mode since the user can attempt a problem multiple times. Here's an example:How many problems would you like to attempt? 2
How wide do you want your digits to be? 5-10: 5
Would you like to activate 'drill' mode? yes or no: yes

What is .....

*****
*   *
*   *
*   *
*****

  *
  *
*****
  *
  *

*****
    *
*****
    *
*****

= 1
Sorry, that's not correct.
= 2
Sorry, that's not correct.
= 3
Correct!

What is .....

*****
*   *
*****
*   *
*****

  *
  *
*****
  *
  *

*****
*
*****
    *
*****

= 13
Correct!

Thanks for playing!
Keep track of statistics by problem type. For example, at the end of your program you could display a display like the following that summarizes the performance of the player:Total addition problems presented:  5
Correct addition problems: 4 (80.0%)

Total subtraction problems presented: 4
Correct subtraction problems: 3 (75.0%)

No multiplication problems presented

Total division problems presented: 1
Correct division problems: 0 (0.0%)If you implemented "drill" mode you should modify your statistics display to include information about how many "extra" attempts it took so solve those problems. For example:Total addition problems presented:  5
# of extra attempts needed: 0 (perfect!)

Total subtraction problems presented: 4
# of extra attempts needed: 1

No multiplication problems presented

Total division problems presented: 1
# of extra attempts needed: 5

Programming Homework Help