From db55c1eb3df86b936906e15786258bb62c0fed3d Mon Sep 17 00:00:00 2001 From: Rudraksh Tank <24ucs052@lnmiit.ac.in> Date: Fri, 1 Aug 2025 19:03:08 +0530 Subject: [PATCH 1/3] Add new algorithm (is_K_power_of_N) to bit manipulation --- bit_manipulation/is_K_power_of_N.py | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 bit_manipulation/is_K_power_of_N.py diff --git a/bit_manipulation/is_K_power_of_N.py b/bit_manipulation/is_K_power_of_N.py new file mode 100644 index 000000000000..bb26b3bfccbd --- /dev/null +++ b/bit_manipulation/is_K_power_of_N.py @@ -0,0 +1,52 @@ +def is_k_power_of_n(n: int, k: int) -> bool: + """ + Given two positive integers, n and k, this function determines if k + is a power of n. A number k is a power of n if k = n^x for some + non-negative integer x. + + >>> is_k_power_of_n(2, 8) + True + >>> is_k_power_of_n(3, 10) + False + >>> is_k_power_of_n(5, 5) + True + >>> is_k_power_of_n(10, 1000) + True + >>> 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(0, 16) + 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, 'a') + 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() +``` From e4e486b8afa65f75f5fb92519586036a90826796 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 1 Aug 2025 13:35:23 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/is_K_power_of_N.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/is_K_power_of_N.py b/bit_manipulation/is_K_power_of_N.py index bb26b3bfccbd..b28cbfc6a55d 100644 --- a/bit_manipulation/is_K_power_of_N.py +++ b/bit_manipulation/is_K_power_of_N.py @@ -36,10 +36,10 @@ def is_k_power_of_n(n: int, k: int) -> bool: 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 From b0413e5e232e62be7c6a5ebd6472b5558ff7f177 Mon Sep 17 00:00:00 2001 From: Rudraksh Tank <24ucs052@lnmiit.ac.in> Date: Fri, 1 Aug 2025 19:15:50 +0530 Subject: [PATCH 3/3] Add new algorithm (is_K_power_of_N) --- bit_manipulation/is_K_power_of_N.py | 44 +++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/bit_manipulation/is_K_power_of_N.py b/bit_manipulation/is_K_power_of_N.py index b28cbfc6a55d..17be843f94dc 100644 --- a/bit_manipulation/is_K_power_of_N.py +++ b/bit_manipulation/is_K_power_of_N.py @@ -1,24 +1,51 @@ +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: """ - Given two positive integers, n and k, this function determines if k - is a power of n. A number k is a power of n if k = n^x for some - non-negative integer x. + 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, 10) - False + >>> is_k_power_of_n(3, 9) + True >>> is_k_power_of_n(5, 5) True - >>> is_k_power_of_n(10, 1000) + >>> 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(0, 16) + >>> is_k_power_of_n(-2, 8) Traceback (most recent call last): ... ValueError: Both n and k must be positive integers @@ -26,14 +53,13 @@ def is_k_power_of_n(n: int, k: int) -> bool: Traceback (most recent call last): ... ValueError: Both n and k must be positive integers - >>> is_k_power_of_n(2, 'a') + >>> 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")