Question
(Python 3.5 , short and simple codes will suffice) 1. Assume the availability of a function called printStars. The function receives an integer value as
(Python 3.5 , short and simple codes will suffice)
1. Assume the availability of a function called printStars. The function receives an integer value as an argument. If the argument is positive, the function prints (to standard output) the given number of asterisks. Thus, if printStars(8) is called, ******** (8 asterisks) will be printed.
Assume further that the variable starCount has been declared and initialized to contain a positive integer value.
Write some code that prints starCount asterisks to standard output by:
first printing a single asterisk and no other characters then calls printStars to print the remaining asterisks.
Hint: Make sure to use the "end" option for print characters without the default newline, e.g. print("hello", end="")
__________________________________________________________________________________________________________
2. Assume the availability of a function called printStars. The function receives an integer value as an argument. If the argument is positive, the function prints (to standard output) the given number of asterisks. Thus, if printStars(8) is called, ******** (8 asterisks) will be printed. Assume further that the variable starCount has been declared and initialized with an integer value, possibly negative or zero. Write some code that does nothing if starCount is not positive but that otherwise prints starCount asterisks to standard ouput by:
first printing a single asterisk (and no other characters) then calls printStars to print the remaining asterisks
__________________________________________________________________________________________________________
3. Write a function called printStars. The function receives a parameter containing an integer value. If the parameter is positive, the funciton prints (to standard output) the given number of asterisks. Otherwise the function does nothing. The function does not return a value. Thus, if printStars(8) is called, ******** (8 asterisks) will be printed. The function must not use a loop of any kind (for, while, do-while) to accomplish its job. Instead, it should examine its parameter, returning if the parameters value is not positive. If the parameter is positive, it prints a single asterisk (and no other characters), then crecursively calls itself to print the remaining asterisks
___________________________________________________________________________________________________________________________
4. Assume the availability of a function named printStars that can be passed a parameter containing a non-negative integer value. The function prints out the given number of asterisks. Write a function named printTriangle that receives a parameter that holds a non-negative integer value and prints a triangle of asterisks as follows: first a line of n asterisks, followed by a line of n-1 askterisks, and then a line of n-2 asterisks, and so on. For example, if the function received 5, it would print: ***** **** *** ** * The function must not use a loop of any kind (for, while, do-while) to accomplish its job. The function should invoke printStars to accomplish the task of printing a single line.
____________________________________________________________________________________________________________________
5. Assume the availability of a function called fact. The function receives an argument containing an integer value and returns an integer value. The function should return the factorial of the argument. That is, if the argument is one or zero, the function should return 1. Otherwise, it should return the product of all the integers from 1 to the argument. So the value of fact(4) is 1*2*3*4 and the value of fact(10) is 1*2*3*4*5*6*7*8*9*10. Additionally, assume that the variable k has been initialized with a positive integer value. Write a statement that assigns the value of fact(k) to a variable x. The solution must include multiplying the return value of fact by k.
____________________________________________________________________________________________________________________
6. Write a definition of a function called product. The function receives two parameters containing integer values. You may assume that neither parameter is negative. The function returns the product of the parameters. So, product(3,5) returns 15. The function must not use a loop of any kind (for, while, do-while).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started