This is a Python challenge. Create pull requests (PR's) to this repository to solve it. Upon PR submission, the GitHub action robots will check your code and report back how well you did. You can then add more commits to your PR until all tests come back green, which means you win!
The exercises are meant to test your knowledge of some important features of the Python programming language. When it's not immediately obvious to you how to solve an exercise using only a few lines of code, it is likely you can learn a new Python trick by checking the links below the exercise.
- Fork this repository
- Create a new branch (name it whatever you like)
- Add a new file
gizmo.py
to the repository - Create a pull-request from your new branch to the master branch of this repository
When gizmo.py
exists, there should be gizmo
python module that you can import.
For example:
>>> import gizmo
Learn how to fork, branch and submit a pull request
Learn how to create a Python module
Add a hello()
function to the gizmo
module. The function should take two parameters: name
and country
. The country
parameter is optional and should default to 'Finland'
. When called, the function uses the print()
function to write the text Hello {name}, how are things in {country}?
to the screen, where {name}
and {country}
should be the name and country given as parameters.
For example:
>>> import gizmo
>>> gizmo.hello('Gizmo', 'Germany')
Hello Gizmo, how are things in Germany?
>>> gizmo.hello('Gizmo')
Hello Gizmo, how are things in Finland?
Learn how to define functions in Python
Add a spell()
function to the gizmo
module that uses the print()
function
to spell out the word given as a parameter to the function, with dots between
the letters. Use a for
-loop to implement this (no split
/join
).
For example:
>>> import gizmo
>>> gizmo.spell('hello')
h.e.l.l.o
Learn how to write a for loop in Python
Reading a bunch of files with only small differences in their filenames is a
very common occurrence in any research code. Add a function relative_path()
to the gizmo
module that returns a list of files, including their relative
path, following the following pattern:
'./subjects/mock_recording_{subject_identifier}.rec'
where {subject_identifier}
is any string. Subject identifiers will be passed
to the function as a list of strings.
For example:
>>> import gizmo
>>> subject_identifiers = ['subject1', 'subject2']
>>> names = gizmo.relative_path(subject_identifiers)
>>> print(names)
['./subjects/mock_recording_subject1.rec', './subjects/mock_recording_subject2.rec']
Get started with string formatting
If you want to go into more detail you can also check the python documentation here.
Add a class called Gizmo
to your gizmo.py
module.
For example:
>>> from gizmo import Gizmo
>>> g = Gizmo()
Learn how to create a Python class
Modify your class such that when you create a new instance of Gizmo
, you can give it a name.
The name is a string.
Afterwards, the name should be available as the .name
attribute.
For example:
>>> from gizmo import Gizmo
>>> g = Gizmo('Ariel')
>>> g.name
'Ariel'
Modify your class such that it has a .speak()
method. Calling this method
will make the Gizmo
object print its name using the print()
function.
For example:
>>> from gizmo import Gizmo
>>> g = Gizmo('Ariel')
>>> g.speak()
Ariel
Learn how to add methods to your class
Add a function generate_fibonacci_sequence(n)
to your gizmo
module that will be a generator that yield
s the first n
numbers of the Fibonacci sequence.
For example:
>>> import gizmo
>>> fib = gizmo.generate_fibonacci_sequence(5)
>>> while(True):
>>> print(next(fib))
0
1
1
2
3
StopIteration
Add a docstring to the generate_fibonacci_sequence
function. Use the "numpydoc" style
for this documentation. The documentation should contain:
- Short summary: a one-line description of what the function does.
- Extended summary: A longer description with more details about what the function does.
- Parameters: a list of all parameters that the function takes. For each parameter, describe:
- The parameter name
- Its expected type (int, bool, str, array, ...)
- What the parameter does
- Yields: a list of all values yielded by the generator. For each return value, describe:
- The yielded value's name
- Its type (int, bool, str, array, ...)
- What the yielded value means
Learn about docstrings
Learn about the numpydoc documentation style