The Fibonacci Series is a numeric series starting with the integers 0 and 1. In this series, the next integer is determined by summing the previous two. This gives us:
0, 1, 1, 2, 3, 5, 8, 13, ...
Note: When asking for the nth number in the series, presume starting at zero.
fibonacci(0) == 0fibonacci(1) == 1fibonacci(2) == 1, etc.The Lucas Numbers are a related series of integers that start with the values 2 and 1 rather than 0 and 1. The resulting series looks like this:
2, 1, 3, 4, 7, 11, 18, 29, ...
math-series.math-series.series.py.test_series.py to your repository. Use Test-Driven Development (TDD) practices. Write tests first, then implement code. Make small changes with many cycles of Red-Green-Refactor.fibonacci. The function should have one parameter n. Return the nth value in the Fibonacci series. You may implement the function using recursion or iteration. If you are feeling particularly frisky, do both as separate functions. Ensure that your function(s) have well-formed docstrings.series.py module, add a new function lucas that returns the nth value in the Lucas numbers. Again, you may use recursion or iteration, or both. Ensure that your function has a well-formed docstring.sum_series with one required parameter and two optional parameters. The required parameter will determine which element in the series to print. The two optional parameters will have default values of 0 and 1 and will determine the first two values for the series to be produced. Calling this function with no optional parameters will produce numbers from the Fibonacci series. Calling it with the optional arguments 2 and 1 will produce values from the Lucas numbers. Other values for the optional parameters will produce other series. Ensure that your function has a well-formed docstring.series.py and test_series.py modules to your repository and commit frequently while working on your implementation. Include good commit messages that explain concisely both what you are doing and why.mkdir example-lab
cd example-lab
touch README.md
python3 -m venv .venv
NOTE: Replace python3 with a more specific version as needed.
Mac/Linux:
source .venv/bin/activate
Windows:
source .venv/Scripts/activate
mkdir example_lab
touch example_lab/example_script.py
Note the underscore vs hyphen
For example:
pip install favorite-library
pip freeze > requirements.txt
Should result in this file tree:
└── example-lab
├── README.md
├── requirements.txt
└── example_lab
└── example_script.py
Many labs will require automated testing. If your lab requires it, then install pytest or pytest-watch.
pip install pytest # or pytest-watch
pip freeze > requirements.txt
touch tests/__init__.py # (Note: 2 underscores on both sides.)
touch tests/test_example.py
Should result in a file tree like this:
└── example-lab
├── README.md
├── requirements.txt
├── example_lab
│ └── example_script.py
└── tests
├── __init__.py
└── test_example.py
Your project’s README.md should match the structure of the template README.
git init
touch .gitignore
Add .venv folder to .gitignore
git add .
git commit -m "first commit"
Create an EMPTY repository example-lab on Github. DO NOT initialize with README, license, or gitignore.
Those will be added soon.
The next screen will have a “Quick Setup” section with a URL available to copy. Copy it ;)
git remote add origin the_url_you_copied_that_ends_with_git
git push -u origin main
Now everything is wired up between the local machine and Github.
Submit a link to the README.md from your assignment branch in Canvas.
Any commits made to the submission branch will be updated in the PR.
In the event of assignment resubmission, submit the submission branch PR on canvas.
This step is optional early in the course. The instructor will inform you when it is required.
Setup “Github Actions” so that your code can be properly tested in Github as you make new pushes to your branches and pull requests to master.
Include the following YAML code:
name: Run Python Tests
on:
push:
branches:
- main
paths:
- "python/**"
pull_request:
branches:
- main
paths:
- "python/**"
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
working-directory: ./python
- name: Test with pytest
run: pytest -vv
working-directory: ./python
NOTE: This challenge is whiteboard only. Write out code as part of your whiteboard process, but don’t worry about creating external program files.
Write a function called reverseArray which takes an array as an argument. Without utilizing any of the built-in methods available to your language, return an array with elements in reversed order.
| Input | Output |
|---|---|
| [1, 2, 3, 4, 5, 6] | [6, 5, 4, 3, 2, 1] |
| [89, 2354, 3546, 23, 10, -923, 823, -12] | [-12, 823, -923, 10, 23, 3546, 2354, 89] |
| [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, …] | [199, 197, 193, 191, 181, … 7, 5, 3, 2] |
Once you’ve achieved a working solution, implement the same feature with a different methodology. (Hint: what different techniques do you have when working with arrays? Recursion, loops, indexes, modifying the array input directly…) In other words, use a different algorithm & pseudocode to solve the same problem. Then compare approaches for efficiency, readability, flexibility, etc.
In your open pull request, comment with the following checklist of tasks:
Read all of the following instructions carefully.
data-structures-and-algorithms.array-insert-shift.README.md file.README at the root of the repository - with a link to this challenge’s README file.NOTE: This challenge is whiteboard only. Write out code as part of your whiteboard process, but don’t worry about creating external program files.
Write a function called insertShiftArray which takes in an array and a value to be added. Without utilizing any of the built-in methods available to your language, return an array with the new value added at the middle index.
| Input | Output |
|---|---|
| [2,4,6,-8], 5 | [2,4,5,6,-8] |
| [42,8,15,23,42], 16 | [42,8,15,16,23,42] |
Bookmark and Review
Statement on why this topic matter as it relates to what I’m studying in this module:
Embracing test-driven development builds a solid foundation for understanding Python by reinforcing good coding practices, the conditional execution structure enhances script versatility, and recursion demonstrates a fundamental problem-solving technique
What are the key principles of Test-Driven Development (TDD) in Python, and how do they contribute to the overall quality of code?
Write Tests First:
Write Minimal Code:
Refactor Code:
Explain the purpose of the if __name__ == '__main__': statement in Python scripts. What are some use cases for including this conditional in your code?
if __name__ == '__main__': statement in Python is used to determine whether the Python script is being run as the main program or if it is being imported as a module into another script.Describe the concept of recursion in Python.
example
python
Copy code
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
What is the difference between Python modules and packages? Explain how to create, import, and use them in your Python programs.
Module: a file containing Python definitions and statements. It allows organizing code logically into reusable files. - Create:
````# module.py
def greet(name):
print(f"Hello, {name}!")```
- Import:
```
# main.py
import module
module.greet(“John”) ```
Packages: a way of organizing related modules into a single directory hierarchy. It includes a special file called __init__.py
mypackage/ ├── init.py ├── module1.py └── module2.py
- Import:
# main.py
from mypackage import module1
module1.function()
```
I got really confused reading the different types of recursion like tail, implicit, etc so I need to know more about that
One of the most effective tools in adult learning is reflection. By writing coherent summaries of lessons learned, we cement that learning and deepen our understanding of a subject. It also helps us to measure our progress.
Write a brief reflection on your learning today, or use the prompt below to get started.
In other words, your life experience in learning the things you did before learning code will make this new experience more meaningful for you, and reflection on all of your experiences is essential in maximizing what you can gain from your study at Code Fellows. How do you think your prior life and professional experience will help you in this new endeavor?