0% found this document useful (0 votes)
8 views2 pages

Untitled 93

Uploaded by

yah7368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Untitled 93

Uploaded by

yah7368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import math

def golomb_encode(n, m):


"""Encodes an integer n using Golomb coding with divisor m."""
q = n // m # Quotient
r = n % m # Remainder

# Unary code for quotient


unary_code = '1' * q + '0'

# Binary code for remainder


b = [Link](math.log2(m))
if (1 << b) - m > r:
b -= 1
binary_code = format(r, f'0{b}b')

return unary_code + binary_code

# Example usage
m = 5 # Divisor for Golomb coding
number_to_encode = 21

encoded = golomb_encode(number_to_encode, m)

print(f"Original number: {number_to_encode}")


print(f"Encoded: {encoded}")

Original number: 21
Encoded: 1111001

import math

def golomb_encode(n, m):


"""Encodes an integer n using Golomb coding with divisor m."""
q = n // m # Quotient
r = n % m # Remainder

# Unary code for quotient


unary_code = '1' * q + '0'

# Binary code for remainder


b = [Link](math.log2(m))
if (1 << b) - m >= r:
b -= 1
binary_code = format(r, f'0{b}b')

return unary_code + binary_code


# Example usage with a larger dataset
m = 4 # Divisor for Golomb coding
numbers_to_encode = [10, 15, 23, 34, 45, 56, 78, 100, 150, 200] #
Larger set of numbers

# Encoding each number in the dataset


encoded_data = [golomb_encode(number, m) for number in
numbers_to_encode]

# Print results
print("Original numbers:", numbers_to_encode)
print("Encoded data:", encoded_data)

Original numbers: [10, 15, 23, 34, 45, 56, 78, 100, 150, 200]
Encoded data: ['11010', '111011', '11111011', '11111111010',
'11111111111001', '1111111111111100', '1111111111111111111010',
'111111111111111111111111100',
'1111111111111111111111111111111111111010',
'1111111111111111111111111111111111111111111111111100']

You might also like