Skip to content

Commit a57212f

Browse files
Merge pull request #65 from amitkrishna/master
Added Exercism Solutions
2 parents 890af98 + 99aee10 commit a57212f

File tree

24 files changed

+870
-0
lines changed

24 files changed

+870
-0
lines changed

Exercism/acronym/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Acronym
2+
3+
Convert a phrase to its acronym.
4+
5+
Techies love their TLA (Three Letter Acronyms)!
6+
7+
Help generate some jargon by writing a program that converts a long name
8+
like Portable Network Graphics to its acronym (PNG).
9+
10+
## Exception messages
11+
12+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
13+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
14+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
15+
a message.
16+
17+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
18+
`raise Exception`, you shold write:
19+
20+
```python
21+
raise Exception("Meaningful message indicating the source of the error")
22+
```
23+
24+
25+
## Submitting Exercises
26+
27+
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
28+
29+
For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/python/bob/bob.py`.
30+
31+
For more detailed information about running tests, code style and linting,
32+
please see the [help page](http://exercism.io/languages/python).
33+
34+
## Source
35+
36+
Julien Vanier [https://github.com/monkbroc](https://github.com/monkbroc)
37+
38+
## Submitting Incomplete Solutions
39+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Exercism/acronym/acronym.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def abbreviate(words):
2+
special_seps = ["-"]
3+
for sep in special_seps:
4+
words = words.replace(sep, ' ')
5+
return ''.join(word[0].upper() for word in words.split())

Exercism/all-your-base/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# All Your Base
2+
3+
Convert a number, represented as a sequence of digits in one base, to any other base.
4+
5+
Implement general base conversion. Given a number in base **a**,
6+
represented as a sequence of digits, convert it to base **b**.
7+
8+
## Note
9+
10+
- Try to implement the conversion yourself.
11+
Do not use something else to perform the conversion for you.
12+
13+
## About [Positional Notation](https://en.wikipedia.org/wiki/Positional_notation)
14+
15+
In positional notation, a number in base **b** can be understood as a linear
16+
combination of powers of **b**.
17+
18+
The number 42, *in base 10*, means:
19+
20+
(4 * 10^1) + (2 * 10^0)
21+
22+
The number 101010, *in base 2*, means:
23+
24+
(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)
25+
26+
The number 1120, *in base 3*, means:
27+
28+
(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)
29+
30+
I think you got the idea!
31+
32+
*Yes. Those three numbers above are exactly the same. Congratulations!*
33+
34+
## Exception messages
35+
36+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
37+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
38+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
39+
a message.
40+
41+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
42+
`raise Exception`, you should write:
43+
44+
```python
45+
raise Exception("Meaningful message indicating the source of the error")
46+
```
47+
48+
## Running the tests
49+
50+
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
51+
52+
- Python 2.7: `py.test all_your_base_test.py`
53+
- Python 3.3+: `pytest all_your_base_test.py`
54+
55+
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
56+
`python -m pytest all_your_base_test.py`
57+
58+
### Common `pytest` options
59+
60+
- `-v` : enable verbose output
61+
- `-x` : stop running tests on first failure
62+
- `--ff` : run failures from previous test before running other test cases
63+
64+
For other options, see `python -m pytest -h`
65+
66+
## Submitting Exercises
67+
68+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/all-your-base` directory.
69+
70+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
71+
72+
For more detailed information about running tests, code style and linting,
73+
please see the [help page](http://exercism.io/languages/python).
74+
75+
## Submitting Incomplete Solutions
76+
77+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def rebase(input, digits, output_base):
2+
if input < 2 or output_base < 2:
3+
raise ValueError("invalid base")
4+
if any(d >= input or d < 0 for d in digits):
5+
raise ValueError("invalid digit")
6+
value = 0
7+
for i, digit in enumerate(digits):
8+
value += digit * input ** (len(digits) - i - 1)
9+
if not value:
10+
return []
11+
rebased = []
12+
while True:
13+
div, mod = divmod(value, output_base)
14+
rebased.append(mod)
15+
if div == 0:
16+
break
17+
value = div
18+
return list(reversed(rebased))

Exercism/armstrong-numbers/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Armstrong Numbers
2+
3+
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits.
4+
5+
For example:
6+
7+
- 9 is an Armstrong number, because `9 = 9^1 = 9`
8+
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 2`
9+
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
10+
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
11+
12+
Write some code to determine whether a number is an Armstrong number.
13+
14+
## Submitting Exercises
15+
16+
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
17+
18+
For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/python/bob/bob.py`.
19+
20+
For more detailed information about running tests, code style and linting,
21+
please see the [help page](http://exercism.io/languages/python).
22+
23+
## Source
24+
25+
Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)
26+
27+
## Submitting Incomplete Solutions
28+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def is_armstrong(number):
2+
return sum(int(ch) ** len(str(number)) for ch in str(number)) == number

Exercism/hello-world/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Hello World
2+
3+
The classical introductory exercise. Just say "Hello, World!".
4+
5+
["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
6+
the traditional first program for beginning programming in a new language
7+
or environment.
8+
9+
The objectives are simple:
10+
11+
- Write a function that returns the string "Hello, World!".
12+
- Run the test suite and make sure that it succeeds.
13+
- Submit your solution and check it at the website.
14+
15+
If everything goes well, you will be ready to fetch your first real exercise.
16+
17+
18+
## Exception messages
19+
20+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
21+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
22+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
23+
a message.
24+
25+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
26+
`raise Exception`, you should write:
27+
28+
```python
29+
raise Exception("Meaningful message indicating the source of the error")
30+
```
31+
32+
## Running the tests
33+
34+
To run the tests, run `pytest hello_world_test.py`
35+
36+
Alternatively, you can tell Python to run the pytest module:
37+
`python -m pytest hello_world_test.py`
38+
39+
### Common `pytest` options
40+
41+
- `-v` : enable verbose output
42+
- `-x` : stop running tests on first failure
43+
- `--ff` : run failures from previous test before running other test cases
44+
45+
For other options, see `python -m pytest -h`
46+
47+
## Submitting Exercises
48+
49+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/hello-world` directory.
50+
51+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
52+
53+
For more detailed information about running tests, code style and linting,
54+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
55+
56+
## Source
57+
58+
This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)
59+
60+
## Submitting Incomplete Solutions
61+
62+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Exercism/hello-world/hello_world.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def hello():
2+
3+
return 'Hello, World!'

Exercism/isogram/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Isogram
2+
3+
Determine if a word or phrase is an isogram.
4+
5+
An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.
6+
7+
Examples of isograms:
8+
9+
- lumberjacks
10+
- background
11+
- downstream
12+
- six-year-old
13+
14+
The word *isograms*, however, is not an isogram, because the s repeats.
15+
16+
17+
## Exception messages
18+
19+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
20+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
21+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
22+
a message.
23+
24+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
25+
`raise Exception`, you should write:
26+
27+
```python
28+
raise Exception("Meaningful message indicating the source of the error")
29+
```
30+
31+
## Running the tests
32+
33+
To run the tests, run `pytest isogram_test.py`
34+
35+
Alternatively, you can tell Python to run the pytest module:
36+
`python -m pytest isogram_test.py`
37+
38+
### Common `pytest` options
39+
40+
- `-v` : enable verbose output
41+
- `-x` : stop running tests on first failure
42+
- `--ff` : run failures from previous test before running other test cases
43+
44+
For other options, see `python -m pytest -h`
45+
46+
## Submitting Exercises
47+
48+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/isogram` directory.
49+
50+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
51+
52+
For more detailed information about running tests, code style and linting,
53+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
54+
55+
## Source
56+
57+
Wikipedia [https://en.wikipedia.org/wiki/Isogram](https://en.wikipedia.org/wiki/Isogram)
58+
59+
## Submitting Incomplete Solutions
60+
61+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Exercism/isogram/isogram.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def is_isogram(string):
2+
string = string.upper()
3+
for letter in string:
4+
if(letter.isalpha() and string.count(letter) > 1):
5+
return False
6+
return True
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Palindrome Products
2+
3+
Detect palindrome products in a given range.
4+
5+
A palindromic number is a number that remains the same when its digits are
6+
reversed. For example, `121` is a palindromic number but `112` is not.
7+
8+
Given a range of numbers, find the largest and smallest palindromes which
9+
are products of numbers within that range.
10+
11+
Your solution should return the largest and smallest palindromes, along with the
12+
factors of each within the range. If the largest or smallest palindrome has more
13+
than one pair of factors within the range, then return all the pairs.
14+
15+
## Example 1
16+
17+
Given the range `[1, 9]` (both inclusive)...
18+
19+
And given the list of all possible products within this range:
20+
`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]`
21+
22+
The palindrome products are all single digit numbers (in this case):
23+
`[1, 2, 3, 4, 5, 6, 7, 8, 9]`
24+
25+
The smallest palindrome product is `1`. Its factors are `(1, 1)`.
26+
The largest palindrome product is `9`. Its factors are `(1, 9)` and `(3, 3)`.
27+
28+
## Example 2
29+
30+
Given the range `[10, 99]` (both inclusive)...
31+
32+
The smallest palindrome product is `121`. Its factors are `(11, 11)`.
33+
The largest palindrome product is `9009`. Its factors are `(91, 99)`.
34+
35+
## Exception messages
36+
37+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
38+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
39+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
40+
a message.
41+
42+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
43+
`raise Exception`, you should write:
44+
45+
```python
46+
raise Exception("Meaningful message indicating the source of the error")
47+
```
48+
49+
## Running the tests
50+
51+
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
52+
53+
- Python 2.7: `py.test palindrome_products_test.py`
54+
- Python 3.3+: `pytest palindrome_products_test.py`
55+
56+
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
57+
`python -m pytest palindrome_products_test.py`
58+
59+
### Common `pytest` options
60+
61+
- `-v` : enable verbose output
62+
- `-x` : stop running tests on first failure
63+
- `--ff` : run failures from previous test before running other test cases
64+
65+
For other options, see `python -m pytest -h`
66+
67+
## Submitting Exercises
68+
69+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/palindrome-products` directory.
70+
71+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
72+
73+
For more detailed information about running tests, code style and linting,
74+
please see the [help page](http://exercism.io/languages/python).
75+
76+
## Source
77+
78+
Problem 4 at Project Euler [http://projecteuler.net/problem=4](http://projecteuler.net/problem=4)
79+
80+
## Submitting Incomplete Solutions
81+
82+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

0 commit comments

Comments
 (0)