Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converter-ines-cardinal #78

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
35 changes: 35 additions & 0 deletions converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import List


def dec_to_bin(n: int) -> List[int]:
"""
Converts a positive integer to binary
:param n: int, decimal integer to convert to binary
:return: Binary translation of input number, as a list of 0s and 1
"""
if n < 0:
raise ValueError("n should be positive")

bit = [0 if n % 2 == 0 else 1]
return bit if n <= 1 else dec_to_bin(n >> 1) + bit


def bin_to_dec(bits_array: List[int]) -> int:
"""
Converts a list of 0s and 1s to its corresponding decimal representation
:param bits_array: list of 0s and 1s to convert
:return: int
"""
if not inputs_is_bits(bits_array):
raise ValueError("input should contain only bits (0 or 1)")

ba = bits_array.copy()
ba.reverse()
return sum([(1 << i) * m for i, m in enumerate(ba)])


def inputs_is_bits(bits_array):
for b in bits_array:
if b not in [0, 1]:
return False
return True
9 changes: 7 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from converter import dec_to_bin, bin_to_dec


def say_hello():
print("Say Hello !")
print("Hello")


if __name__ == '__main__':
say_hello()
print(*dec_to_bin(256))
print(bin_to_dec([1, 0, 1, 0, 0, 1]))
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "example_package_YOUR_USERNAME_HERE"
version = "0.0.1"
authors = [
{ name="Example Author", email="[email protected]" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]

[project.urls]
"Homepage" = "https://github.com/pypa/sampleproject"
"Bug Tracker" = "https://github.com/pypa/sampleproject/issues"
Empty file.
2 changes: 2 additions & 0 deletions src/example_package_Ines_Cardinal/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def add_one(number):
return number + 1
29 changes: 29 additions & 0 deletions test_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from unittest import TestCase

from converter import dec_to_bin, bin_to_dec


class TestConverter(TestCase):
def test_dec_to_bin_even(self):
n = 10
expected = [1, 0, 1, 0]
self.assertListEqual(expected, dec_to_bin(n))

def test_dec_to_bin_odd(self):
n = 37
expected = [1, 0, 0, 1, 0, 1]
self.assertListEqual(expected, dec_to_bin(n))

def test_bin_to_dec_odd(self):
bits = [1, 0, 0, 1, 0, 1]
expected = 37
self.assertEqual(expected, bin_to_dec(bits))

def test_bin_to_dec_even(self):
bits = [1, 0, 1, 0]
expected = 10
self.assertEqual(expected, bin_to_dec(bits))

def test_reverse(self):
n = 135
self.assertEqual(n, bin_to_dec(dec_to_bin(n)))