Skip to content

Add new algorithm (is_K_power_of_N) to bit manipulation #12871

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

Closed
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
78 changes: 78 additions & 0 deletions bit_manipulation/is_K_power_of_N.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
The error message `Syntax Error @ 1:1. tokenizer error: '`' is not a valid character in this position\` indicates that the Python parser is encountering an invalid character right at the beginning of the file.

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

bit_manipulation/is_K_power_of_N.py:1:89: E501 Line too long (209 > 88)

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:76: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:70: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:50: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:40: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:27: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:19: SyntaxError: Got unexpected token `

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:11: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:5: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

bit_manipulation/is_K_power_of_N.py:1:1: N999 Invalid module name: 'is_K_power_of_N'

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

bit_manipulation/is_K_power_of_N.py:1:89: E501 Line too long (209 > 88)

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:76: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:70: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:50: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:40: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:27: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:19: SyntaxError: Got unexpected token `

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:11: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

bit_manipulation/is_K_power_of_N.py:1:5: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

bit_manipulation/is_K_power_of_N.py:1:1: N999 Invalid module name: 'is_K_power_of_N'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: bit_manipulation/is_K_power_of_N.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 1:1.
tokenizer error: '`' is not a valid character in this position

The error message `Syntax Error @ 1:1. tokenizer error: '`' is not a valid character in this position\` indicates that the Python parser is encountering an invalid character right at the beginning of the file.
^


The character causing the issue is the backtick symbol (`` ` ``) which seems to have been included at the very start of the code block. This is not a valid character to start a Python statement.

Here is the corrected code. It is identical to the previous version, but without the leading backtick, making it syntactically correct.

```python
"""
Author : Your Name
Date : August 1, 2025

Task:
Given two positive integers, n and k, determine if k is a power of n.

Implementation notes: Use a loop to repeatedly divide k by n.
For a number k to be a power of n, it must be possible to reduce k
to 1 by repeatedly dividing by n without any remainder.
For example, 8 is a power of 2 because:
8 / 2 = 4
4 / 2 = 2
2 / 2 = 1
The final result is 1.
"""


def is_k_power_of_n(n: int, k: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: bit_manipulation/is_K_power_of_N.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 1:1.
tokenizer error: '`' is not a valid character in this position

def is_k_power_of_n(n: int, k: int) -> bool:
^

"""
Return True if k is a power of n or False otherwise.

>>> is_k_power_of_n(2, 8)
True
>>> is_k_power_of_n(3, 9)
True
>>> is_k_power_of_n(5, 5)
True
>>> is_k_power_of_n(2, 1)
True
>>> is_k_power_of_n(3, 10)
False
>>> is_k_power_of_n(2, 6)
False
>>> is_k_power_of_n(4, 2)
False
>>> is_k_power_of_n(1, 1)
True
>>> is_k_power_of_n(1, 5)
False
>>> is_k_power_of_n(-2, 8)
Traceback (most recent call last):
...
ValueError: Both n and k must be positive integers
>>> is_k_power_of_n(2, -8)
Traceback (most recent call last):
...
ValueError: Both n and k must be positive integers
>>> is_k_power_of_n(2.5, 8)
Traceback (most recent call last):
...
TypeError: n and k must be integers
"""
if not isinstance(n, int) or not isinstance(k, int):
raise TypeError("n and k must be integers")
if n <= 0 or k <= 0:
raise ValueError("Both n and k must be positive integers")

if n == 1:
return k == 1

while k % n == 0:
k //= n
return k == 1


if __name__ == "__main__":
import doctest

doctest.testmod()
```
Loading