Python. Solving problems for processing integers




Python. Solving tasks for processing integers

This topic provides examples of solving the most common tasks associated with processing numbers. The study of the topic assumes a basic knowledge of the basics of the Python language, namely, the use of input/output tools, loops, lists, functions, and the like.


Contents


Search other resources:

1. Select all digits of an integer and present them as a list

 

# Input number
n = input("n = ")

# Select all digits of a number and present them as a list

# Resulting list
L = []

# The cycle of forming the list
while n>0:
    number = n%10   # get the last number
    L = [number] + L # add a number to the list
    n = n/10         # remove the last digit from the number

print("L = ", L)

Program execution result

n = 83470230
L = [8, 3, 4, 7, 0, 2, 3, 0]

 

2. Determine the specified digit in the number
2.1. Position is indicated on the left

The example defines the specified digit to the left of the specified number.

# Task. Select a digit in a number by its position

# 1. Input number
n = input("n = ")

# 2. Input position (starts at 0)
pos = input("pos = ")

# 3. Create a list of digits of a number
L = [] # resulting list

# The cycle of forming the list L
while n>0:
    L = [n%10] + L   # get the last digit and add the digit to the list
    n = n/10         # remove the last digit from the number

# 4. Get a number
num = L[pos]

# 5. Print the result
print("num = ", num)
print("L = ", L)

The result of the program

n = 239487
pos = 4
num = 8)
L = [2, 3, 9, 4, 8, 7]

 

2.2. The position is set to the right

If the position number is given to the right (starting from 0), then in the previous example 2.1 the line

...

# 4. Get a number
num = L[pos]

...

should be replaced by a line

# 4. Get digit by offset from end
num = L[len(L) - pos - 1]

 

3. Calculate the sum of the digits of an integer

 

# Task. Calculate the sum of the digits of a number

# 1. Input number
n = input("n = ")

# 2. Calculate the sum
summ = 0 # Calculated sum

# Loop for extracting digits from a number and calculating the sum
while n>0:
    summ = summ + n%10 # get the last digit and add the digit to the sum
    n = n/10         # remove the last digit from the number

# 3. Print the sum
print("sum = ", summ)

The result of the program

n = 12345
sum = 15

 

4. Invert integer (1234 => 4321)

Invert the number means that you need to get a new number, which is read as the original number from end to beginning.

# Task. Invert number

import math

# 1. Input number
n = input("n = ")

# 2. Creating a new inverted number
# 2.1. First you need to calculate the number of digits in the number
k = 0
n2 = n
while n2>0:
    k = k+1   # increase the counter of the number of digits by 1
    n2 = n2/10 # Remove digit

# Loop for extracting digits from number n and creating an inverted number
while n>0:
    # get the last number
    last = n%10

    # decrease the order of number
    k = k-1

    # multiply the resulting figure by 10 ^ k
    # and add it to a new number
    n2 = n2 + int(last*math.pow(10, k)) # n2 - new number

    # remove the last digit from the number
    n = n/10

# 3. Print the inverted number
print("n2 = ", n2)

The result of the program

n = 23456
n2 = 65432

 

5. Function GetNumCount(). Determine the number of digits in a number

Implement the GetNumCount() function that determines the number of digits in the number. The function receives the initial number as a parameter.

# Task. Determine the number of digits in a number

import math

# GetNumCount() function - determine the number of digits in a number
def GetNumCount(n):
    k = 0 # k - result

    while n>0:
        k = k+1
        n = n/10

    return k

# Demonstration of the function
# Input number
n = input("n = ")

# Function call
k = GetNumCount(n)
print("k = ", k)

Program result

n = 22768746
k = 8

 

6. Function GetMaxNum(). Determine the maximum digit in the number

In a given integer n (n> 0), you need to determine the maximum digit. For example, if there is a number 2883345, then the maximum digit will be 8.

# Task. Determine the maximum digit in the number

import math

# GetMaxNum() function - determine the maximum digit in a number
def GetMaxNum(n):
    # get the last digit of a number
    Max = n%10

    while n>0:
        n = n/10 # decrease the number
        if Max < n%10: # compare the last digit with the maximum
            Max = n%10

    return Max

# Demonstration of the function
# Input number
n = input("n = ")

# Function call
num = GetMaxNum(n)
print("num = ", num)

Program result

n = 4556821
num = 8

 

7. Function GetMinNum(). Determine the minimum digit in a number

The problem is solved in the same way as in the previous example.

# Task. Determine the minimum digit in a number

import math

# GetMinNum() function - determine the minimum digit in a number
def GetMinNum(n):
    # get the last digit of a number
    Min = n%10

    while n>0:
        n = n/10 # reduce the number
        if Min > n%10: # compare the last digit with the minimum
            Min = n%10

    return Min

# Demonstration of the function

# Input number
n = input("n = ")

# Function call
num = GetMinNum(n)
print("num = ", num)

Program result

n = 28302324
num = 0

 


Related topics