admin管理员组

文章数量:1122846

I want to solve this task:

Implement parity bit client number validation.

Implement a ParityBitPython class with a validate_client_number(client_number: str) -> bool method to validate 10 digit client numbers that contain an even parity bit.

A bank wants to implement a method to validate their client numbers. A valid 10 digit client number must satisfy the following:

  • Contain only digits 0-9
  • Have a length of 10 digits
  • The last bit of the client number in binary form (parity bit) should make the total number of "1" in the entire number even

My approach:

class ParityBitPython:
    @staticmethod
    def validate_client_number(client_number: str) -> bool:
        if len(client_number) != 10 or not client_number.isdigit():
            return False  
    
        data_digits = client_number[:-1]  
        parity_bit = int(client_number[-1])  
    
        total_ones = sum(format(int(digit), '04b').count('1') for digit in data_digits)
       
        if total_ones % 2 == 1:
            return True
        else:
            return False

The following numbers are asked in the result.

8456894318 is even (True)
3456848879 is odd (False)
4864684516 is even (True)
9999999999 is even (True)

The first two results are correctly solved by my approach, but for the last two numbers my code returns "odd" (False) instead of the correct result "even" (True).

What am I doing wrong?

I want to solve this task:

Implement parity bit client number validation.

Implement a ParityBitPython class with a validate_client_number(client_number: str) -> bool method to validate 10 digit client numbers that contain an even parity bit.

A bank wants to implement a method to validate their client numbers. A valid 10 digit client number must satisfy the following:

  • Contain only digits 0-9
  • Have a length of 10 digits
  • The last bit of the client number in binary form (parity bit) should make the total number of "1" in the entire number even

My approach:

class ParityBitPython:
    @staticmethod
    def validate_client_number(client_number: str) -> bool:
        if len(client_number) != 10 or not client_number.isdigit():
            return False  
    
        data_digits = client_number[:-1]  
        parity_bit = int(client_number[-1])  
    
        total_ones = sum(format(int(digit), '04b').count('1') for digit in data_digits)
       
        if total_ones % 2 == 1:
            return True
        else:
            return False

The following numbers are asked in the result.

8456894318 is even (True)
3456848879 is odd (False)
4864684516 is even (True)
9999999999 is even (True)

The first two results are correctly solved by my approach, but for the last two numbers my code returns "odd" (False) instead of the correct result "even" (True).

What am I doing wrong?

Share Improve this question edited Nov 22, 2024 at 17:38 mkrieger1 22.9k7 gold badges63 silver badges79 bronze badges asked Nov 22, 2024 at 17:33 user28432840user28432840 92 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Python's int object has a convenient built-in function bit_count() which, if I understand your problem correctly, you could utilise as follows:

values = ["8456894318", "3456848879", "4864684516", "9999999999"]

def validate(value: str) -> bool:
    if len(value) == 10:
        try:
            return int(value).bit_count() % 2 == 0
        except ValueError:
            pass
    return False

for value in values:
    print(value, validate(value))

Output:

8456894318 True
3456848879 False
4864684516 True
9999999999 True

本文标签: pythonClient number validation with ParityBitStack Overflow