Here are some problems that require Python basic knowledge about Lists, Strings and operators.
Digits Multiplication
You are given a positive integer. Your function should calculate the product of the digits excluding any zeroes. For example: The number given is 123405. The result will be 1*2*3*4*5=120 (don’t forget to exclude zeroes).
Input: A positive integer.
Output: The product of the digits as an integer.
def muliply(number): product = 1 while number > 0: lastDigit = number % 10 if lastDigit != 0: product *= lastDigit number = number // 10 return product
Index Power
You are given an array with positive numbers and a number N. You should find the N-th power of the element in the array with the index N. If N is outside of the array, then return -1. Don’t forget that the first element has the index 0.
Let’s look at a few examples:
– array = [1, 2, 3, 4] and N = 2, then the result is 32 == 9;
– array = [1, 2, 3] and N = 3, but N is outside of the array, so the result is -1.
Input: Two arguments. An array as a list of integers and a number as a integer.
Output: The result as an integer.
def index_power(array, n): if n < len(array): return array[n]**n else: return -1
Number Base
You are given a positive number as a string along with the radix for it. Your function should convert it into decimal form. The radix is less than 37 and greater than 1. The task uses digits and the letters A-Z for the strings.
Watch out for cases when the number cannot be converted. For example: “1A” cannot be converted with radix 9. For these cases your function should return -1.
Input: Two arguments. A number as string and a radix as an integer.
Output: The converted number as an integer.
def number(str_number, radix):
result = 0
for char in str_number:
number = 0
if char.isdigit():
number = ord(char) - ord('0')
else:
number = ord(char) - ord('A') + 10
if number < radix:
result = radix * result + number
else:
return -1
return result
