Skip to content

Commit

Permalink
added hexadecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
celiknedim committed Feb 7, 2024
1 parent bac8487 commit a4b6089
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Binary file modified __pycache__/math_functions.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/test_all.cpython-312-pytest-8.0.0.pyc
Binary file not shown.
31 changes: 30 additions & 1 deletion math_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def fib(n):
return n
return fib(n-1) + fib(n-2)

######################

def is_prime(N):

Expand All @@ -21,6 +22,8 @@ def is_prime(N):
if divise == 0:
return True

#####################

# Nombres premiers jumeaux : Identifiez et affichez les paires de nombres
# premiers jumeaux (deux nombres premiers dont la différence est de 2)
# jusqu'à un certain nombre N ?
Expand All @@ -29,4 +32,30 @@ def is_prime(N):
# twins = []
# for num in range(2, n):
# if is_prime(num) and is_prime(num + 2):



######################


# Conversion hexadécimale-décimale :
# Écrivez un programme qui prend un nombre hexadécimal en entrée
# (sous forme de chaîne de caractères) et le convertit en décimal


# def hex_to_dec(n):
# d = int(n, 16)
# return d



def hex_to_decimal(n):
d = 0
for digit in n:
if '0' <= digit <= '9':
d = d * 16 + (ord(digit) - ord('0'))
elif 'A' <= digit <= 'F':
d = d * 16 + (ord(digit) - ord('A') + 10)
else:
print("invalid")
return None
return d
16 changes: 16 additions & 0 deletions test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ def test_prime():
assert math_functions.is_prime(100000007) == True


def test_hex():
assert math_functions.hex_to_dec('A') == 10
assert math_functions.hex_to_dec('F') == 15
assert math_functions.hex_to_dec('10') == 16
assert math_functions.hex_to_dec('2B') == 43
assert math_functions.hex_to_dec('B2') == 178



def test_hex():
assert math_functions.hex_to_decimal('A') == 10
assert math_functions.hex_to_decimal('F') == 15
assert math_functions.hex_to_decimal('10') == 16
assert math_functions.hex_to_decimal('2B') == 43
assert math_functions.hex_to_decimal('B2') == 178
assert math_functions.hex_to_decimal('3A2') == 930

0 comments on commit a4b6089

Please sign in to comment.