Geometric Progression Sequence Generator (Challenge)

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In mathematics, a geometric progression is a sequence of numbers in which each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. In this challenge, you will create a function that initializes a list containing the numbers in the specified range where start and end are inclusive and the ratio between two terms is step.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/for_loops -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/list_comprehensions -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/lists -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/tuples -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/function_definition -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/default_arguments -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/importing_modules -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/using_packages -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/standard_libraries -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/math_random -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} python/build_in_functions -.-> lab-13115{{"`Geometric Progression Sequence Generator (Challenge)`"}} end

Geometric Progression Challenge

Problem

Write a function called geometric_progression that takes in three parameters:

  • end: an integer representing the end of the range (inclusive)
  • start: an optional integer representing the start of the range (inclusive), with a default value of 1
  • step: an optional integer representing the common ratio between two terms, with a default value of 2

The function should return a list containing the numbers in the specified range where the ratio between two terms is step. The list should start with start and end with end.

If step equals 1, the function should return an error.

You should use range(), math.log(), and math.floor() and a list comprehension to create a list of the appropriate length, applying the step for each element.

Example

geometric_progression(256) ## [1, 2, 4, 8, 16, 32, 64, 128, 256]
geometric_progression(256, 3) ## [3, 6, 12, 24, 48, 96, 192]
geometric_progression(256, 1, 4) ## [1, 4, 16, 64, 256]

Summary

In this challenge, you have learned how to create a function that initializes a list containing the numbers in the specified range where the ratio between two terms is step. You have used range(), math.log(), and math.floor() and a list comprehension to create a list of the appropriate length, applying the step for each element.

Other Python Tutorials you may like