Skip to content

Commit

Permalink
Added barcode_validator.py (TheAlgorithms#6771)
Browse files Browse the repository at this point in the history
* Update README.md

Added Google Cirq references

* Create barcode_validator.py

Barcode/EAN validator

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update barcode_validator.py

Included docstring and updated variables to snake_case

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update barcode_validator.py

Included docset and updated bugs

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update barcode_validator.py

Implemented the changes asked in review.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update barcode_validator.py

Updated with f-string format

* Update barcode_validator.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2022
1 parent 07e991d commit 1aa7bd9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions quantum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ Started at https://github.com/TheAlgorithms/Python/issues/1831
* Google: https://research.google/teams/applied-science/quantum
* IBM: https://qiskit.org and https://github.com/Qiskit
* Rigetti: https://rigetti.com and https://github.com/rigetti
* Zapata: https://www.zapatacomputing.com and https://github.com/zapatacomputing

## IBM Qiskit
- Start using by installing `pip install qiskit`, refer the [docs](https://qiskit.org/documentation/install.html) for more info.
- Tutorials & References
- https://github.com/Qiskit/qiskit-tutorials
- https://quantum-computing.ibm.com/docs/iql/first-circuit
- https://medium.com/qiskit/how-to-program-a-quantum-computer-982a9329ed02

## Google Cirq
- Start using by installing `python -m pip install cirq`, refer the [docs](https://quantumai.google/cirq/start/install) for more info.
- Tutorials & references
- https://github.com/quantumlib/cirq
- https://quantumai.google/cirq/experiments
- https://tanishabassan.medium.com/quantum-programming-with-google-cirq-3209805279bc
88 changes: 88 additions & 0 deletions strings/barcode_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
https://en.wikipedia.org/wiki/Check_digit#Algorithms
"""


def get_check_digit(barcode: int) -> int:
"""
Returns the last digit of barcode by excluding the last digit first
and then computing to reach the actual last digit from the remaining
12 digits.
>>> get_check_digit(8718452538119)
9
>>> get_check_digit(87184523)
5
>>> get_check_digit(87193425381086)
9
>>> [get_check_digit(x) for x in range(0, 100, 10)]
[0, 7, 4, 1, 8, 5, 2, 9, 6, 3]
"""
barcode //= 10 # exclude the last digit
checker = False
s = 0

# extract and check each digit
while barcode != 0:
mult = 1 if checker else 3
s += mult * (barcode % 10)
barcode //= 10
checker = not checker

return (10 - (s % 10)) % 10


def is_valid(barcode: int) -> bool:
"""
Checks for length of barcode and last-digit
Returns boolean value of validity of barcode
>>> is_valid(8718452538119)
True
>>> is_valid(87184525)
False
>>> is_valid(87193425381089)
False
>>> is_valid(0)
False
>>> is_valid(dwefgiweuf)
Traceback (most recent call last):
...
NameError: name 'dwefgiweuf' is not defined
"""
return len(str(barcode)) == 13 and get_check_digit(barcode) == barcode % 10


def get_barcode(barcode: str) -> int:
"""
Returns the barcode as an integer
>>> get_barcode("8718452538119")
8718452538119
>>> get_barcode("dwefgiweuf")
Traceback (most recent call last):
...
ValueError: Barcode 'dwefgiweuf' has alphabetic characters.
"""
if str(barcode).isalpha():
raise ValueError(f"Barcode '{barcode}' has alphabetic characters.")
elif int(barcode) < 0:
raise ValueError("The entered barcode has a negative value. Try again.")
else:
return int(barcode)


if __name__ == "__main__":
import doctest

doctest.testmod()
"""
Enter a barcode.
"""
barcode = get_barcode(input("Barcode: ").strip())

if is_valid(barcode):
print(f"'{barcode}' is a valid Barcode")
else:
print(f"'{barcode}' is NOT is valid Barcode.")

0 comments on commit 1aa7bd9

Please sign in to comment.