Write A Function IntegerPower (base, Exponent) That Returns ✓ Solved

Write a function integerPower (base, exponent) that returns

Task 1: Write a function integerPower(base, exponent) that returns the value of base raised to the exponent. For example, integerPower(3, 4) equals 3 3 3 * 3. Assume that the exponent is a positive, nonzero integer and the base is an integer. The function should use a loop to control the calculation without utilizing any math library functions.

Task 2: Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 100; b) -1 ≤ n ≤ 1; c) -3 ≤ n ≤ 11. For each of the following sets of integers, write a single statement that will print a random number from the set: a) 2, 4, 6, 8, 10; b) 3, 5, 7, 9, 11; c) 6, 10, 14, 18, 22.

Task 3: Write a function that displays a solid square of asterisks with the side specified in the integer parameter side. For example, if side is 4, the function displays a square of asterisks.

Task 4: An integer is considered prime if it is divisible only by 1 and itself. For instance, 2, 3, 5, and 7 are prime, while 4, 6, 8, and 9 are not. Write a function that determines if a number is prime.

Task 5: A triangle made of blocks has the topmost row with 1 block, the next row down with 2 blocks, and so on. Write a recursive function (without loops or multiplication) that returns the total number of blocks in such a triangle with a given number of rows. Examples include: triangle(0) → 0, triangle(1) → 1, triangle(2) → 3.

Paper For Above Instructions

In the realm of programming, the ability to implement functions that execute defined tasks is crucial. This assignment tackles several fundamental programming tasks that require the use of functions and control structures. The following sections detail the solutions for each of the tasks outlined above, employing Python as the coding language.

Task 1: Integer Power Function

The first task requires creating a function named integerPower that computes the power of a base raised to an exponent. The implementation uses a for loop to multiply the base by itself exponent times. Here’s how this can be structured:

def integerPower(base, exponent):

result = 1

for _ in range(exponent):

result *= base

return result

Example usage:

print(integerPower(3, 4)) # Output: 81

This function initializes a result variable to 1 and utilizes a loop that iteratively multiplies result by base for the number of times specified by the exponent.

Task 2: Random Integer Assignment

For the second task, random integers must be generated within specified ranges and selected from predefined sets of integers. Python's random module provides the necessary functions to achieve this. Below are the statements for this task:

import random

a) Assign random integer in the range 1 ≤ n ≤ 100

n1 = random.randint(1, 100)

b) Assign random integer in the range -1 ≤ n ≤ 1

n2 = random.randint(-1, 1)

c) Assign random integer in the range -3 ≤ n ≤ 11

n3 = random.randint(-3, 11)

Random selections from specified sets

random_set_a = random.choice([2, 4, 6, 8, 10])

random_set_b = random.choice([3, 5, 7, 9, 11])

random_set_c = random.choice([6, 10, 14, 18, 22])

print(n1, n2, n3)

print(random_set_a, random_set_b, random_set_c)

Task 3: Solid Square of Asterisks Function

The third task involves crafting a function that outputs a solid square of asterisks based on the parameter side. The following implementation displays the necessary square:

def displaySquare(side):

for _ in range(side):

print(' ' side)

Example usage:

displaySquare(4)

This function generates a square by printing a row of asterisks for the number of rows equal to the side length, thus forming a square shape.

Task 4: Prime Number Checker Function

The fourth task is to determine if a number is prime. This is done by checking divisibility from 2 up to the square root of the number. If it is divisible by any of these, it’s not prime:

def isPrime(num):

if num

return False

for i in range(2, int(num**0.5) + 1):

if num % i == 0:

return False

return True

Example usage:

print(isPrime(7)) # Output: True

print(isPrime(10)) # Output: False

Task 5: Recursive Triangle Block Function

The fifth and final task requires creating a recursive function to calculate the number of blocks in a triangle. This function avoids loops and multiplication:

def triangle(rows):

if rows == 0:

return 0

return rows + triangle(rows - 1)

Example usage:

print(triangle(3)) # Output: 6

This function adds the current row number to the total of the previous rows until it reaches zero.

Conclusion

In summary, the tasks outlined cover essential programming concepts such as function creation, random number generation, conditional checks for primality, iterative and recursive structures, as well as output formatting. Mastering these concepts is pivotal for anyone aspiring to excel in programming.

References

  • Beazley, D. M. (2013). Python Essential Reference. Addison-Wesley.
  • Downey, A. (2015). Think Python: How to Think Like a Computer Scientist. Green Tea Press.
  • Lutz, M. (2013). Learning Python. O'Reilly Media.
  • McKinney, W. (2018). Python for Data Analysis. O'Reilly Media.
  • Mitchell, R. (2015). Python Programming for Beginners: An Introduction to the Python Language. CreateSpace Independent Publishing Platform.
  • Oliphant, T. E. (2007). Python for Scientific Computing. Computing in Science & Engineering, 9(3), 10-20.
  • Severance, C. (2016). Python for Everybody: Exploring Data in Python 3. CreateSpace Independent Publishing Platform.
  • Sweigart, A. (2019). Automate the Boring Stuff with Python. No Starch Press.
  • Van Rossum, G. & Drake, F. L. (2011). Python 3 Reference. Python Software Foundation.
  • Zelle, J. (2010). Python Programming: An Introduction to Computer Science. Franklin, Beedle & Associates Inc.