add exercises

This commit is contained in:
Andreas Zweili 2021-10-31 12:05:11 +01:00
parent 2a74cdff13
commit 7967af4eb5
11 changed files with 355 additions and 0 deletions

8
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,8 @@
{
"python.pythonPath": "python/venv/bin/python3",
"python.terminal.activateEnvInCurrentTerminal": true,
"python.testing.pytestArgs": ["python"],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.linting.enabled": true
}

View File

@ -0,0 +1,37 @@
{
"blurb": "Calculate the Hamming difference between two DNA strands.",
"authors": [
"betegelse"
],
"contributors": [
"behrtam",
"cmccandless",
"Dog",
"gabriel376",
"GascaK",
"guaneec",
"iandexter",
"ikhadykin",
"kytrinyx",
"N-Parsons",
"parthsharma2",
"pheanex",
"pywkm",
"sjakobi",
"tqa236",
"yawpitch"
],
"files": {
"solution": [
"hamming.py"
],
"test": [
"hamming_test.py"
],
"example": [
".meta/example.py"
]
},
"source": "The Calculating Point Mutations problem at Rosalind",
"source_url": "http://rosalind.info/problems/hamm/"
}

View File

@ -0,0 +1 @@
{"track":"python","exercise":"hamming","id":"804bff8f6b524ed7baa7eaab7cbefa2d","url":"https://exercism.org/tracks/python/exercises/hamming","handle":"Nebucatnetzer","is_requester":true,"auto_approve":false}

55
python/hamming/HELP.md Normal file
View File

@ -0,0 +1,55 @@
# Help
## Running the tests
To run the included *tests*, run the test file using the `pytest` module, replacing `{exercise_name}`:
```bash
$ python3 -m pytest {exercise_name}_test.py
```
Many IDE's and code editors have built-in support for using Pytest to run tests; check them out [here](https://github.com/exercism/python/blob/main/docs/TOOLS.md#editors-and-ides).
For more information about running tests using `pytest`, checkout our [Python testing guide](https://github.com/exercism/python/blob/main/docs/TESTS.md#pytest).
### Common pytest options
- `-v` : enable verbose output.
- `-x` : stop running tests on first failure.
- `--ff` : run failures from previous test before running other test cases.
For other options, see `python3 -m pytest -h`.
## Submitting your solution
You can submit your solution using the `exercism submit hamming.py` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Python track's documentation](https://exercism.org/docs/tracks/python)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
Below are some resources for getting help if you run into trouble:
- [The PSF](https://www.python.org) hosts Python downloads, documentation, and community resources.
- [Python Community on Discord](https://pythondiscord.com/) is a very helpful and active community.
- [#python on Libera.chat](https://www.python.org/community/irc/) this is where the cored developers for the language hang out and get work done.
- [Exercism on Gitter](https://gitter.im/exercism/home) join the Python room for Python-related questions or problems.
- [/r/learnpython/](https://www.reddit.com/r/learnpython/) is a subreddit designed for Python learners.
- [Python Community Forums](https://discuss.python.org/)
- [Pythontutor](http://pythontutor.com/) for stepping through small code snippets visually.
Additionally, [StackOverflow](http://stackoverflow.com/questions/tagged/python) is a good spot to search for your problem/question to see if it has been answered already.
If not - you can always [ask](https://stackoverflow.com/help/how-to-ask) or [answer](https://stackoverflow.com/help/how-to-answer) someone else's question.

57
python/hamming/README.md Normal file
View File

@ -0,0 +1,57 @@
# Hamming
Welcome to Hamming on Exercism's Python Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Calculate the Hamming Distance between two DNA strands.
Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!
When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance".
We read DNA using the letters C,A,G and T. Two strands might look like this:
GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^ ^ ^ ^^
They have 7 differences, and therefore the Hamming Distance is 7.
The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)
The Hamming distance is only defined for sequences of equal length, so
an attempt to calculate it between sequences of different lengths should
not work. The general handling of this situation (e.g., raising an
exception vs returning a special value) may differ between languages.
## Source
### Created by
- @betegelse
### Contributed to by
- @behrtam
- @cmccandless
- @Dog
- @gabriel376
- @GascaK
- @guaneec
- @iandexter
- @ikhadykin
- @kytrinyx
- @N-Parsons
- @parthsharma2
- @pheanex
- @pywkm
- @sjakobi
- @tqa236
- @yawpitch
### Based on
The Calculating Point Mutations problem at Rosalind - http://rosalind.info/problems/hamm/

View File

@ -0,0 +1,6 @@
def distance(strand_a, strand_b):
strand_a_length = strand_a.length()
strand_b_length = strand_b.length()
print(strand_a_length)
pass

View File

@ -0,0 +1,48 @@
import unittest
from hamming import (
distance,
)
# Tests adapted from `problem-specifications//canonical-data.json`
class HammingTest(unittest.TestCase):
def test_empty_strands(self):
self.assertEqual(distance("", ""), 0)
def test_single_letter_identical_strands(self):
self.assertEqual(distance("A", "A"), 0)
def test_single_letter_different_strands(self):
self.assertEqual(distance("G", "T"), 1)
def test_long_identical_strands(self):
self.assertEqual(distance("GGACTGAAATCTG", "GGACTGAAATCTG"), 0)
def test_long_different_strands(self):
self.assertEqual(distance("GGACGGATTCTG", "AGGACGGATTCT"), 9)
def test_disallow_first_strand_longer(self):
with self.assertRaisesWithMessage(ValueError):
distance("AATG", "AAA")
def test_disallow_second_strand_longer(self):
with self.assertRaisesWithMessage(ValueError):
distance("ATA", "AGTG")
def test_disallow_left_empty_strand(self):
with self.assertRaisesWithMessage(ValueError):
distance("", "G")
def test_disallow_right_empty_strand(self):
with self.assertRaisesWithMessage(ValueError):
distance("G", "")
# Utility functions
def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1 @@
{"track":"python","exercise":"raindrops","id":"3ba0d6af0de74bdca60f2eb1bdd9781b","url":"https://exercism.io/my/solutions/3ba0d6af0de74bdca60f2eb1bdd9781b","handle":"Nebucatnetzer","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,63 @@
# Raindrops
Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation).
The rules of `raindrops` are that if a given number:
- has 3 as a factor, add 'Pling' to the result.
- has 5 as a factor, add 'Plang' to the result.
- has 7 as a factor, add 'Plong' to the result.
- _does not_ have any of 3, 5, or 7 as a factor, the result should be the digits of the number.
## Examples
- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
- 34 is not factored by 3, 5, or 7, so the result would be "34".
## Exception messages
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you should write:
```python
raise Exception("Meaningful message indicating the source of the error")
```
## Running the tests
To run the tests, run `pytest raindrops_test.py`
Alternatively, you can tell Python to run the pytest module:
`python -m pytest raindrops_test.py`
### Common `pytest` options
- `-v` : enable verbose output
- `-x` : stop running tests on first failure
- `--ff` : run failures from previous test before running other test cases
For other options, see `python -m pytest -h`
## Submitting Exercises
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/raindrops` directory.
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
For more detailed information about running tests, code style and linting,
please see [Running the Tests](http://exercism.io/tracks/python/tests).
## Source
A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. [https://en.wikipedia.org/wiki/Fizz_buzz](https://en.wikipedia.org/wiki/Fizz_buzz)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

View File

@ -0,0 +1,11 @@
def convert(number):
sounds = []
if (number % 3 == 0):
sounds.append("Pling")
if (number % 5 == 0):
sounds.append("Plang")
if (number % 7 == 0):
sounds.append("Plong")
if sounds:
return "".join(sounds)
return str(number)

View File

@ -0,0 +1,68 @@
import unittest
from raindrops import convert
# Tests adapted from `problem-specifications//canonical-data.json`
class RaindropsTest(unittest.TestCase):
def test_the_sound_for_1_is_1(self):
self.assertEqual(convert(1), "1")
def test_the_sound_for_3_is_pling(self):
self.assertEqual(convert(3), "Pling")
def test_the_sound_for_5_is_plang(self):
self.assertEqual(convert(5), "Plang")
def test_the_sound_for_7_is_plong(self):
self.assertEqual(convert(7), "Plong")
def test_the_sound_for_6_is_pling_as_it_has_a_factor_3(self):
self.assertEqual(convert(6), "Pling")
def test_2_to_the_power_3_does_not_make_a_raindrop_sound_as_3_is_the_exponent_not_the_base(
self
):
self.assertEqual(convert(8), "8")
def test_the_sound_for_9_is_pling_as_it_has_a_factor_3(self):
self.assertEqual(convert(9), "Pling")
def test_the_sound_for_10_is_plang_as_it_has_a_factor_5(self):
self.assertEqual(convert(10), "Plang")
def test_the_sound_for_14_is_plong_as_it_has_a_factor_of_7(self):
self.assertEqual(convert(14), "Plong")
def test_the_sound_for_15_is_pling_plang_as_it_has_factors_3_and_5(self):
self.assertEqual(convert(15), "PlingPlang")
def test_the_sound_for_21_is_pling_plong_as_it_has_factors_3_and_7(self):
self.assertEqual(convert(21), "PlingPlong")
def test_the_sound_for_25_is_plang_as_it_has_a_factor_5(self):
self.assertEqual(convert(25), "Plang")
def test_the_sound_for_27_is_pling_as_it_has_a_factor_3(self):
self.assertEqual(convert(27), "Pling")
def test_the_sound_for_35_is_plang_plong_as_it_has_factors_5_and_7(self):
self.assertEqual(convert(35), "PlangPlong")
def test_the_sound_for_49_is_plong_as_it_has_a_factor_7(self):
self.assertEqual(convert(49), "Plong")
def test_the_sound_for_52_is_52(self):
self.assertEqual(convert(52), "52")
def test_the_sound_for_105_is_pling_plang_plong_as_it_has_factors_3_5_and_7(self):
self.assertEqual(convert(105), "PlingPlangPlong")
def test_the_sound_for_3125_is_plang_as_it_has_a_factor_5(self):
self.assertEqual(convert(3125), "Plang")
if __name__ == "__main__":
unittest.main()