Exponents || Python || challenge

Exponents || Python || challenge

In this challenge, we will be using nested loops in order to raise a list of numbers to the power of a list of other numbers. What this means is that for every number in the first list, we will raise that number to the power of every number in the second list. If you provide the first list with 2 elements and the second list with 3 numbers, then there will be 6 final answers. Let’s look at the steps we need:

  1. Define the function to accept two lists of numbers, bases and powers
  2. Create a new list that will contain our answers
  3. Create a loop that iterates through every base in bases
  4. Within that loop, create another loop that iterates through every power in power
  5. Within that nested loop, append the result of the current base raised to the current power.
  6. After all iterations of both loops are complete, return the list of answers

Challenge code:

Create a function named exponents() that takes two lists as parameters named bases and powers. Return a new list containing every number in bases raised to every number in powers.

For example, consider the following code.

exponents([2, 3, 4], [1, 2, 3])        

the result would be the list [2, 4, 8, 3, 9, 27, 4, 16, 64]. It would first add two to the first. Then two to the second. Then two to the third, and so on.

The solution:

No alt text provided for this image

To view or add a comment, sign in

More articles by Ahmed Khaleel

  • Even Keys || Python

    Even Keys || Python

    Even Keys Next, we are going to do something similar, but we are going to use the keys in order to retrieve the values.…

  • Sum Values || Python

    Sum Values || Python

    Sum Values For the first code challenge, we are going to look at only the values in a dictionary. This function should…

  • Intersect || explained || SQL

    Intersect || explained || SQL

    Intersect: As you think about major world cities and their corresponding country, you may ask which countries also have…

  • Sorting Numbers || javascript

    Sorting Numbers || javascript

    Sorting Numbers let's take our numbers array and put the elements out of order (1, 2, 3, ..

  • Splitting Strings || python || challenge

    Splitting Strings || python || challenge

    Data : Your boss at the Poetry organization sent over a bunch of author names that he wants you to prepare for…

  • Inserting an element in the first position || javascript

    Inserting an element in the first position || javascript

    Inserting an element in the first position: Suppose we need to add a new element to the array (number -1) and would…

    2 Comments
  • Fibonacci sequence || Array || javascript

    Fibonacci sequence || Array || javascript

    Let's say that we want to find out the first 20 numbers of the Fibonacci sequence. The first two numbers of the…

  • Meal Maker || javascript || challenge

    Meal Maker || javascript || challenge

    Meal Maker A restaurant has hired you to create a function for their website that allows them to set a meal and price…

  • Python Code Challenges

    Python Code Challenges

    Copeland’s Corporate Company has finalized what they want their username and temporary password creation to be and have…

  • Larger Sum || Python || challenge

    Larger Sum || Python || challenge

    We are going to start our advanced challenge problems by calculating which list of two inputs has the larger sum. We…

Insights from the community

Others also viewed

Explore topics