repo_name
stringclasses 1
value | pr_number
int64 4.12k
11.2k
| pr_title
stringlengths 9
107
| pr_description
stringlengths 107
5.48k
| author
stringlengths 4
18
| date_created
timestamp[ns, tz=UTC] | date_merged
timestamp[ns, tz=UTC] | previous_commit
stringlengths 40
40
| pr_commit
stringlengths 40
40
| query
stringlengths 118
5.52k
| before_content
stringlengths 0
7.93M
| after_content
stringlengths 0
7.93M
| label
int64 -1
1
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
TheAlgorithms/Python | 7,417 | Remove references to depreciated QasmSimulator | ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| tianyizheng02 | 2022-10-19T03:31:43Z | 2022-10-19T20:12:44Z | 50da472ddcdc2d79d1ad325ec05cda3558802fda | 2859d4bf3aa96737a4715c65d4a9051d9c62d24d | Remove references to depreciated QasmSimulator. ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| """ Luhn Algorithm """
from __future__ import annotations
def is_luhn(string: str) -> bool:
"""
Perform Luhn validation on an input string
Algorithm:
* Double every other digit starting from 2nd last digit.
* Subtract 9 if number is greater than 9.
* Sum the numbers
*
>>> test_cases = (79927398710, 79927398711, 79927398712, 79927398713,
... 79927398714, 79927398715, 79927398716, 79927398717, 79927398718,
... 79927398719)
>>> [is_luhn(str(test_case)) for test_case in test_cases]
[False, False, False, True, False, False, False, False, False, False]
"""
check_digit: int
_vector: list[str] = list(string)
__vector, check_digit = _vector[:-1], int(_vector[-1])
vector: list[int] = [int(digit) for digit in __vector]
vector.reverse()
for i, digit in enumerate(vector):
if i & 1 == 0:
doubled: int = digit * 2
if doubled > 9:
doubled -= 9
check_digit += doubled
else:
check_digit += digit
return check_digit % 10 == 0
if __name__ == "__main__":
import doctest
doctest.testmod()
assert is_luhn("79927398713")
assert not is_luhn("79927398714")
| """ Luhn Algorithm """
from __future__ import annotations
def is_luhn(string: str) -> bool:
"""
Perform Luhn validation on an input string
Algorithm:
* Double every other digit starting from 2nd last digit.
* Subtract 9 if number is greater than 9.
* Sum the numbers
*
>>> test_cases = (79927398710, 79927398711, 79927398712, 79927398713,
... 79927398714, 79927398715, 79927398716, 79927398717, 79927398718,
... 79927398719)
>>> [is_luhn(str(test_case)) for test_case in test_cases]
[False, False, False, True, False, False, False, False, False, False]
"""
check_digit: int
_vector: list[str] = list(string)
__vector, check_digit = _vector[:-1], int(_vector[-1])
vector: list[int] = [int(digit) for digit in __vector]
vector.reverse()
for i, digit in enumerate(vector):
if i & 1 == 0:
doubled: int = digit * 2
if doubled > 9:
doubled -= 9
check_digit += doubled
else:
check_digit += digit
return check_digit % 10 == 0
if __name__ == "__main__":
import doctest
doctest.testmod()
assert is_luhn("79927398713")
assert not is_luhn("79927398714")
| -1 |
TheAlgorithms/Python | 7,417 | Remove references to depreciated QasmSimulator | ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| tianyizheng02 | 2022-10-19T03:31:43Z | 2022-10-19T20:12:44Z | 50da472ddcdc2d79d1ad325ec05cda3558802fda | 2859d4bf3aa96737a4715c65d4a9051d9c62d24d | Remove references to depreciated QasmSimulator. ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| # A naive recursive implementation of 0-1 Knapsack Problem
This overview is taken from:
https://en.wikipedia.org/wiki/Knapsack_problem
---
## Overview
The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. The problem often arises in resource allocation where the decision makers have to choose from a set of non-divisible projects or tasks under a fixed budget or time constraint, respectively.
The knapsack problem has been studied for more than a century, with early works dating as far back as 1897 The name "knapsack problem" dates back to the early works of mathematician Tobias Dantzig (1884–1956), and refers to the commonplace problem of packing the most valuable or useful items without overloading the luggage.
---
## Documentation
This module uses docstrings to enable the use of Python's in-built `help(...)` function.
For instance, try `help(Vector)`, `help(unit_basis_vector)`, and `help(CLASSNAME.METHODNAME)`.
---
## Usage
Import the module `knapsack.py` from the **.** directory into your project.
---
## Tests
`.` contains Python unit tests which can be run with `python3 -m unittest -v`.
| # A naive recursive implementation of 0-1 Knapsack Problem
This overview is taken from:
https://en.wikipedia.org/wiki/Knapsack_problem
---
## Overview
The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. The problem often arises in resource allocation where the decision makers have to choose from a set of non-divisible projects or tasks under a fixed budget or time constraint, respectively.
The knapsack problem has been studied for more than a century, with early works dating as far back as 1897 The name "knapsack problem" dates back to the early works of mathematician Tobias Dantzig (1884–1956), and refers to the commonplace problem of packing the most valuable or useful items without overloading the luggage.
---
## Documentation
This module uses docstrings to enable the use of Python's in-built `help(...)` function.
For instance, try `help(Vector)`, `help(unit_basis_vector)`, and `help(CLASSNAME.METHODNAME)`.
---
## Usage
Import the module `knapsack.py` from the **.** directory into your project.
---
## Tests
`.` contains Python unit tests which can be run with `python3 -m unittest -v`.
| -1 |
TheAlgorithms/Python | 7,417 | Remove references to depreciated QasmSimulator | ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| tianyizheng02 | 2022-10-19T03:31:43Z | 2022-10-19T20:12:44Z | 50da472ddcdc2d79d1ad325ec05cda3558802fda | 2859d4bf3aa96737a4715c65d4a9051d9c62d24d | Remove references to depreciated QasmSimulator. ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| """
Project Euler Problem 686: https://projecteuler.net/problem=686
2^7 = 128 is the first power of two whose leading digits are "12".
The next power of two whose leading digits are "12" is 2^80.
Define p(L,n) to be the nth-smallest value of j such that
the base 10 representation of 2^j begins with the digits of L.
So p(12, 1) = 7 and p(12, 2) = 80.
You are given that p(123, 45) = 12710.
Find p(123, 678910).
"""
import math
def log_difference(number: int) -> float:
"""
This function returns the decimal value of a number multiplied with log(2)
Since the problem is on powers of two, finding the powers of two with
large exponents is time consuming. Hence we use log to reduce compute time.
We can find out that the first power of 2 with starting digits 123 is 90.
Computing 2^90 is time consuming.
Hence we find log(2^90) = 90*log(2) = 27.092699609758302
But we require only the decimal part to determine whether the power starts with 123.
So we just return the decimal part of the log product.
Therefore we return 0.092699609758302
>>> log_difference(90)
0.092699609758302
>>> log_difference(379)
0.090368356648852
"""
log_number = math.log(2, 10) * number
difference = round((log_number - int(log_number)), 15)
return difference
def solution(number: int = 678910) -> int:
"""
This function calculates the power of two which is nth (n = number)
smallest value of power of 2
such that the starting digits of the 2^power is 123.
For example the powers of 2 for which starting digits is 123 are:
90, 379, 575, 864, 1060, 1545, 1741, 2030, 2226, 2515 and so on.
90 is the first power of 2 whose starting digits are 123,
379 is second power of 2 whose starting digits are 123,
and so on.
So if number = 10, then solution returns 2515 as we observe from above series.
We will define a lowerbound and upperbound.
lowerbound = log(1.23), upperbound = log(1.24)
because we need to find the powers that yield 123 as starting digits.
log(1.23) = 0.08990511143939792, log(1,24) = 0.09342168516223506.
We use 1.23 and not 12.3 or 123, because log(1.23) yields only decimal value
which is less than 1.
log(12.3) will be same decimal value but 1 added to it
which is log(12.3) = 1.093421685162235.
We observe that decimal value remains same no matter 1.23 or 12.3
Since we use the function log_difference(),
which returns the value that is only decimal part, using 1.23 is logical.
If we see, 90*log(2) = 27.092699609758302,
decimal part = 0.092699609758302, which is inside the range of lowerbound
and upperbound.
If we compute the difference between all the powers which lead to 123
starting digits is as follows:
379 - 90 = 289
575 - 379 = 196
864 - 575 = 289
1060 - 864 = 196
We see a pattern here. The difference is either 196 or 289 = 196 + 93.
Hence to optimize the algorithm we will increment by 196 or 93 depending upon the
log_difference() value.
Let's take for example 90.
Since 90 is the first power leading to staring digits as 123,
we will increment iterator by 196.
Because the difference between any two powers leading to 123
as staring digits is greater than or equal to 196.
After incrementing by 196 we get 286.
log_difference(286) = 0.09457875989861 which is greater than upperbound.
The next power is 379, and we need to add 93 to get there.
The iterator will now become 379,
which is the next power leading to 123 as starting digits.
Let's take 1060. We increment by 196, we get 1256.
log_difference(1256) = 0.09367455396034,
Which is greater than upperbound hence we increment by 93. Now iterator is 1349.
log_difference(1349) = 0.08946415071057 which is less than lowerbound.
The next power is 1545 and we need to add 196 to get 1545.
Conditions are as follows:
1) If we find a power whose log_difference() is in the range of
lower and upperbound, we will increment by 196.
which implies that the power is a number which will lead to 123 as starting digits.
2) If we find a power, whose log_difference() is greater than or equal upperbound,
we will increment by 93.
3) if log_difference() < lowerbound, we increment by 196.
Reference to the above logic:
https://math.stackexchange.com/questions/4093970/powers-of-2-starting-with-123-does-a-pattern-exist
>>> solution(1000)
284168
>>> solution(56000)
15924915
>>> solution(678910)
193060223
"""
power_iterator = 90
position = 0
lower_limit = math.log(1.23, 10)
upper_limit = math.log(1.24, 10)
previous_power = 0
while position < number:
difference = log_difference(power_iterator)
if difference >= upper_limit:
power_iterator += 93
elif difference < lower_limit:
power_iterator += 196
else:
previous_power = power_iterator
power_iterator += 196
position += 1
return previous_power
if __name__ == "__main__":
import doctest
doctest.testmod()
print(f"{solution() = }")
| """
Project Euler Problem 686: https://projecteuler.net/problem=686
2^7 = 128 is the first power of two whose leading digits are "12".
The next power of two whose leading digits are "12" is 2^80.
Define p(L,n) to be the nth-smallest value of j such that
the base 10 representation of 2^j begins with the digits of L.
So p(12, 1) = 7 and p(12, 2) = 80.
You are given that p(123, 45) = 12710.
Find p(123, 678910).
"""
import math
def log_difference(number: int) -> float:
"""
This function returns the decimal value of a number multiplied with log(2)
Since the problem is on powers of two, finding the powers of two with
large exponents is time consuming. Hence we use log to reduce compute time.
We can find out that the first power of 2 with starting digits 123 is 90.
Computing 2^90 is time consuming.
Hence we find log(2^90) = 90*log(2) = 27.092699609758302
But we require only the decimal part to determine whether the power starts with 123.
So we just return the decimal part of the log product.
Therefore we return 0.092699609758302
>>> log_difference(90)
0.092699609758302
>>> log_difference(379)
0.090368356648852
"""
log_number = math.log(2, 10) * number
difference = round((log_number - int(log_number)), 15)
return difference
def solution(number: int = 678910) -> int:
"""
This function calculates the power of two which is nth (n = number)
smallest value of power of 2
such that the starting digits of the 2^power is 123.
For example the powers of 2 for which starting digits is 123 are:
90, 379, 575, 864, 1060, 1545, 1741, 2030, 2226, 2515 and so on.
90 is the first power of 2 whose starting digits are 123,
379 is second power of 2 whose starting digits are 123,
and so on.
So if number = 10, then solution returns 2515 as we observe from above series.
We will define a lowerbound and upperbound.
lowerbound = log(1.23), upperbound = log(1.24)
because we need to find the powers that yield 123 as starting digits.
log(1.23) = 0.08990511143939792, log(1,24) = 0.09342168516223506.
We use 1.23 and not 12.3 or 123, because log(1.23) yields only decimal value
which is less than 1.
log(12.3) will be same decimal value but 1 added to it
which is log(12.3) = 1.093421685162235.
We observe that decimal value remains same no matter 1.23 or 12.3
Since we use the function log_difference(),
which returns the value that is only decimal part, using 1.23 is logical.
If we see, 90*log(2) = 27.092699609758302,
decimal part = 0.092699609758302, which is inside the range of lowerbound
and upperbound.
If we compute the difference between all the powers which lead to 123
starting digits is as follows:
379 - 90 = 289
575 - 379 = 196
864 - 575 = 289
1060 - 864 = 196
We see a pattern here. The difference is either 196 or 289 = 196 + 93.
Hence to optimize the algorithm we will increment by 196 or 93 depending upon the
log_difference() value.
Let's take for example 90.
Since 90 is the first power leading to staring digits as 123,
we will increment iterator by 196.
Because the difference between any two powers leading to 123
as staring digits is greater than or equal to 196.
After incrementing by 196 we get 286.
log_difference(286) = 0.09457875989861 which is greater than upperbound.
The next power is 379, and we need to add 93 to get there.
The iterator will now become 379,
which is the next power leading to 123 as starting digits.
Let's take 1060. We increment by 196, we get 1256.
log_difference(1256) = 0.09367455396034,
Which is greater than upperbound hence we increment by 93. Now iterator is 1349.
log_difference(1349) = 0.08946415071057 which is less than lowerbound.
The next power is 1545 and we need to add 196 to get 1545.
Conditions are as follows:
1) If we find a power whose log_difference() is in the range of
lower and upperbound, we will increment by 196.
which implies that the power is a number which will lead to 123 as starting digits.
2) If we find a power, whose log_difference() is greater than or equal upperbound,
we will increment by 93.
3) if log_difference() < lowerbound, we increment by 196.
Reference to the above logic:
https://math.stackexchange.com/questions/4093970/powers-of-2-starting-with-123-does-a-pattern-exist
>>> solution(1000)
284168
>>> solution(56000)
15924915
>>> solution(678910)
193060223
"""
power_iterator = 90
position = 0
lower_limit = math.log(1.23, 10)
upper_limit = math.log(1.24, 10)
previous_power = 0
while position < number:
difference = log_difference(power_iterator)
if difference >= upper_limit:
power_iterator += 93
elif difference < lower_limit:
power_iterator += 196
else:
previous_power = power_iterator
power_iterator += 196
position += 1
return previous_power
if __name__ == "__main__":
import doctest
doctest.testmod()
print(f"{solution() = }")
| -1 |
TheAlgorithms/Python | 7,417 | Remove references to depreciated QasmSimulator | ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| tianyizheng02 | 2022-10-19T03:31:43Z | 2022-10-19T20:12:44Z | 50da472ddcdc2d79d1ad325ec05cda3558802fda | 2859d4bf3aa96737a4715c65d4a9051d9c62d24d | Remove references to depreciated QasmSimulator. ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| def hamming_distance(string1: str, string2: str) -> int:
"""Calculate the Hamming distance between two equal length strings
In information theory, the Hamming distance between two strings of equal
length is the number of positions at which the corresponding symbols are
different. https://en.wikipedia.org/wiki/Hamming_distance
Args:
string1 (str): Sequence 1
string2 (str): Sequence 2
Returns:
int: Hamming distance
>>> hamming_distance("python", "python")
0
>>> hamming_distance("karolin", "kathrin")
3
>>> hamming_distance("00000", "11111")
5
>>> hamming_distance("karolin", "kath")
Traceback (most recent call last):
...
ValueError: String lengths must match!
"""
if len(string1) != len(string2):
raise ValueError("String lengths must match!")
count = 0
for char1, char2 in zip(string1, string2):
if char1 != char2:
count += 1
return count
if __name__ == "__main__":
import doctest
doctest.testmod()
| def hamming_distance(string1: str, string2: str) -> int:
"""Calculate the Hamming distance between two equal length strings
In information theory, the Hamming distance between two strings of equal
length is the number of positions at which the corresponding symbols are
different. https://en.wikipedia.org/wiki/Hamming_distance
Args:
string1 (str): Sequence 1
string2 (str): Sequence 2
Returns:
int: Hamming distance
>>> hamming_distance("python", "python")
0
>>> hamming_distance("karolin", "kathrin")
3
>>> hamming_distance("00000", "11111")
5
>>> hamming_distance("karolin", "kath")
Traceback (most recent call last):
...
ValueError: String lengths must match!
"""
if len(string1) != len(string2):
raise ValueError("String lengths must match!")
count = 0
for char1, char2 in zip(string1, string2):
if char1 != char2:
count += 1
return count
if __name__ == "__main__":
import doctest
doctest.testmod()
| -1 |
TheAlgorithms/Python | 7,417 | Remove references to depreciated QasmSimulator | ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| tianyizheng02 | 2022-10-19T03:31:43Z | 2022-10-19T20:12:44Z | 50da472ddcdc2d79d1ad325ec05cda3558802fda | 2859d4bf3aa96737a4715c65d4a9051d9c62d24d | Remove references to depreciated QasmSimulator. ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| """
Implementation of median filter algorithm
"""
from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey
from numpy import divide, int8, multiply, ravel, sort, zeros_like
def median_filter(gray_img, mask=3):
"""
:param gray_img: gray image
:param mask: mask size
:return: image with median filter
"""
# set image borders
bd = int(mask / 2)
# copy image size
median_img = zeros_like(gray_img)
for i in range(bd, gray_img.shape[0] - bd):
for j in range(bd, gray_img.shape[1] - bd):
# get mask according with mask
kernel = ravel(gray_img[i - bd : i + bd + 1, j - bd : j + bd + 1])
# calculate mask median
median = sort(kernel)[int8(divide((multiply(mask, mask)), 2) + 1)]
median_img[i, j] = median
return median_img
if __name__ == "__main__":
# read original image
img = imread("../image_data/lena.jpg")
# turn image in gray scale value
gray = cvtColor(img, COLOR_BGR2GRAY)
# get values with two different mask size
median3x3 = median_filter(gray, 3)
median5x5 = median_filter(gray, 5)
# show result images
imshow("median filter with 3x3 mask", median3x3)
imshow("median filter with 5x5 mask", median5x5)
waitKey(0)
| """
Implementation of median filter algorithm
"""
from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey
from numpy import divide, int8, multiply, ravel, sort, zeros_like
def median_filter(gray_img, mask=3):
"""
:param gray_img: gray image
:param mask: mask size
:return: image with median filter
"""
# set image borders
bd = int(mask / 2)
# copy image size
median_img = zeros_like(gray_img)
for i in range(bd, gray_img.shape[0] - bd):
for j in range(bd, gray_img.shape[1] - bd):
# get mask according with mask
kernel = ravel(gray_img[i - bd : i + bd + 1, j - bd : j + bd + 1])
# calculate mask median
median = sort(kernel)[int8(divide((multiply(mask, mask)), 2) + 1)]
median_img[i, j] = median
return median_img
if __name__ == "__main__":
# read original image
img = imread("../image_data/lena.jpg")
# turn image in gray scale value
gray = cvtColor(img, COLOR_BGR2GRAY)
# get values with two different mask size
median3x3 = median_filter(gray, 3)
median5x5 = median_filter(gray, 5)
# show result images
imshow("median filter with 3x3 mask", median3x3)
imshow("median filter with 5x5 mask", median5x5)
waitKey(0)
| -1 |
TheAlgorithms/Python | 7,417 | Remove references to depreciated QasmSimulator | ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| tianyizheng02 | 2022-10-19T03:31:43Z | 2022-10-19T20:12:44Z | 50da472ddcdc2d79d1ad325ec05cda3558802fda | 2859d4bf3aa96737a4715c65d4a9051d9c62d24d | Remove references to depreciated QasmSimulator. ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| -1 |
||
TheAlgorithms/Python | 7,417 | Remove references to depreciated QasmSimulator | ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
| tianyizheng02 | 2022-10-19T03:31:43Z | 2022-10-19T20:12:44Z | 50da472ddcdc2d79d1ad325ec05cda3558802fda | 2859d4bf3aa96737a4715c65d4a9051d9c62d24d | Remove references to depreciated QasmSimulator. ### Describe your change:
Replaced instances of `qiskit.Aer.get_backend("qasm_simulator")` in `quantum/` with `q.Aer.get_backend("aer_simulator")`, as the former is depreciated and raises warnings (Qiskit's [documentation](https://qiskit.org/documentation/apidoc/aer_provider.html) says that `QasmSimulator` is legacy).
This PR edits multiple code files because they all raise the same warning and are mentioned in the same GitHub issue.
Fixes #7308
* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?
### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
|