The base, or radix, is the number of unique digits, including the digit zero, used to represent numbers.
from https://en.wikipedia.org/wiki/Radix
Normally, this is the digits 0-9 and this is conventionally followed by letters (a -> 10, up to z -> 36). This is the system employed for this kumite; however other symbols may be used.
Numpy offers a base_repr function, but this is not part of basic python. As numpy is an external module, it may not always be available. The aim is to produce a function in native python that gives equivalent functioinality. A further challenge is to avoid use of the math module (or the bin, hex, oct functions for that matter).
The function to_base
is intended to provide the same functionality. The string uses uppercase, because numpy.base_repr
does the same.
The function to_base
may be tested by:
- Comparing its output to that of
numpy.base_repr
- Converting the output of
to_base
back to the original number using Python's nativeint()
function.
def to_base (n, base):
"""
A simple function to replicate the functionality of numpy's base_repr
using native python. Returns upper case.
"""
assert base >= 2, "Minimum base 2"
assert base <= 36, "Maximum base 36"
assert type(base) == int, "Base must be an integer"
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digits = []
while n:
digits.append(chars[n%base])
n //= base
digits.reverse()
return ''.join(digits)
import codewars_test as test
import numpy as np
import secrets
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
n = 300
base = 16
test.assert_equals(to_base(n,base), np.base_repr(n,base)) # Equivalent output
test.assert_equals(int(to_base(n,base),base), n) # Converts back using python's native int() function.
for i in range(100):
@test.it("test case")
def test_case():
n = secrets.choice(range(10**7))
base = secrets.choice(range(2,37))
test.assert_equals(to_base(n,base), np.base_repr(n,base)) # Equivalent output
test.assert_equals(int(to_base(n,base),base), n) # Converts back using python's native int() function.
Some info about Armstrong numbers
from https://oeis.org/A005188
Armstrong (or pluperfect, or Plus Perfect, or narcissistic) numbers:
m-digit positive numbers equal to sum of the m-th powers of their digits.
===
For purposes of this kumite (at least this iteration), base 10 will be used.
Other bases may be available. Oh wait, they are.
===
Narcissistic number
From Wikipedia, the free encyclopedia
(Redirected from Armstrong number)
https://en.wikipedia.org/wiki/Narcissistic_number
In number theory, a narcissistic number (also known as a pluperfect
digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong)
or a plus perfect number)[6] in a given number base b is a number that is
the sum of its own digits each raised to the power of the number of digits.
#This works for base-10 only. Other bases might require using text-type objects.
def is_Armstrong(n):
m = n # Make a copy of n for use obtaining digits.
#Bearing in mind that n must be kept available.
digits = [] #Store digits here
#Get digits
while (m):
digits.append(m%10)
m //= 10
#Get count of digits
x = len(digits)
#Compare original value to sum of digit ^ count
return n == sum(d ** x for d in digits)
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
test.assert_equals(is_Armstrong(153), True)
test.assert_equals(is_Armstrong(43), False)
test.assert_equals(is_Armstrong(802), False)
#Borrowed from a recent Kata for format reference
"""
test.assert_equals(my_automaton.read_commands(["1"]), True)
test.assert_equals(my_automaton.read_commands(["1", "0", "0", "1"]), True)
"""