# Examples of solving tasks for processing numbers

### Contents

Search other resources:

##### 1.1. Position numbering from left to right (=>)

The following GetPosMaxNum() function returns the position of the first maximum digit of a number. The position is numbered from 1. The position number is determined in the left-to-right direction.

```# GetPosMaxNum() function - find the position of the maximum digit of an integer
# View the position from left to right
# For example: 283412 => 2 position (maximum = 8).

# The function receives a parameter n (n> 0)
def GetPosMaxNum(n):
# 1. Checking n for correctness
if (n<=0):
return -1

# 2. Generate a list of digits of a number
L = []
while n>0:
L = [n%10] + L
n = n//10

# 3. Find the maximum in the list and the position of this maximum
# 3.1. Set the first element of the list
Max = L
PosMax = 0

# 3.2. The cycle of maximum search
i = 1
while i<len(L):
if Max<L[i]:
Max = L[i]
PosMax = i
i = i+1

return PosMax+1 # numbering from 1

# Demonstration of the GetPosMaxNum() function
n = int(input("n = "))
PosMax = GetPosMaxNum(n)
print("PosMax = ", PosMax)```

The result of the program

```n = 111111171111
PosMax = 8```

### ⇑

##### 1.2. Right-to-left implementation (<=)

To get the position of the maximum value from right to left, you need to change the line in the while() loop in the GetPosMaxNum() function

`L = [n%10] + L`

to the next line

`L = L + [n%10]`

### ⇑

##### 1.3. Calculating the position of the minimum digit of an integer

To calculate the position of the minimum digit of an integer in any direction, you need in the example of clause 9.1 in the comparison line

```...
if Max<L[i]:
...```

sign <(less) replace with sign > (greater). It would also be logical to replace the variable names in the function:

• Max => Min;
• PosMax => PosMin.

You can also replace the function name, for example GetPosMinNum().

### ⇑

##### 2. Function SumOddNum(). Calculate the sum of unpaired digits of a number

The example shows the SumOddNum() function, which calculates the sum of the odd digits of a number. For example, for the number 12397544 the result will be

`1 + 3 + 9 + 7 + 5 = 25`

The input parameter of the function is the original number. To check a digit for odd parity, use the condition

`n%2 == 1`

The demo text is as follows

```# Task. Determine the sum of the unpaired digits of the number.

# Function that determines the sum of unpaired digits of a number
def SumOddNum(n):
summ = 0 # required sum

while n>0:
if (n%2) == 1: # checking if a number is odd
summ = summ + n%10
n = n//10

# Return the result
return summ

# Demonstration of using the SumOddNum() function
n = input("n = ") # Input number
summ = SumOddNum(n) # Invoke function, calculation
print("Sum = ", summ)```

The result of the program

```n = 100000333
Sum = 10```

### ⇑

##### 3. Function GetNumMore7(). Calculate the number of digits greater than 7

In the example below, a function is implemented that, in a given number, determines the number of digits greater than 7. For example, for the number 123485379, the result will be 2 (digits 8 and 9 are greater than 7).

```# A function that calculates the number of digits greater than 7
def GetNumMore7(n):
k = 0 # the result is the number of digits

while n>0:
if (n%10) > 7: # checking if a digit is greater than 7
k = k+1 # if the digit is greater than 7, then increase k
n = n//10

# Return the result
return k

# Demonstration of using the GetNumMore7() function
n = input("n = ") # Input number
count = GetNumMore7(n) # Invoke function, calculation
print("count = ", count)```

Program result

```n = 8674493609
count = 3```

### ⇑

##### 4. Function GetMaxPairNum(). Calculate the maximum even digit of a number

The example implements a function that calculates the maximum even digit of the number n (n>0). For example, for the number 2811169, the correct result would be 8.

If there are no even digits in them, the function returns -1.

To find the maximum in the function, the following actions are performed:

• a list L is formed from the digits of the number;
• the standard function max() is called for finding the maximum in the list L.
```# A function that calculates the maximum even digit of a number
def GetMaxPairNum(n):
# Generate a list of odd digits of a number
L = []

while n>0:
if (n%2) == 0:     # check the last digit for parity
L = L + [n%10] # if the digit is even, then add it to the list
n = n//10 # reduce the number

# Return the result
if L == []:
return -1
else:
return max(L) # use the standard function max()

# Demonstration of using the GetNumMore7() function
n = input("n = ") # Input number
Max = GetMaxPairNum(n) # Invoke function, calculation
print("Max = ", Max)```

The result of the program

```n = 262320113
Max =  6```

### ⇑

##### 5.1. Direction of position determination from left to right (=>)

The example implements the GetPosMinOddNum() function, which determines the position of the minimum odd digit of the number n, which is an input parameter. The position number starts with 1 and is determined from left to right (=>).

For example, for the number 22524872, the correct answer would be 3 (the minimum odd digit 5 is placed in the 3rd position).

If there are no odd digits in the number, the function returns -1.

In order to simplify the solution of the problem, the following main actions are performed in the function:

• a list L is formed from odd digits of a number;
• if the list L is not empty (L! = []), then the minimum value Min in the list L is determined by calling the standard min() function;
• the Min value is used to calculate the MinPos position of the first occurrence of Min in the list L from left to right.

```# A function that returns the position (number) of the lowest odd digit of a number.
# If there are no odd digits in the number, -1 is returned.
# Position number is viewed from left to right (=>)
def GetPosMinOddNum(n):
# 1. Generate a list of digits of a number
L = []
while n>0:
L = [n%10] + L # from left to right
n = n//10

# 2. Finding the minimum among odd digits
# 2.1. Initial assignments
fOdd = False # First, assume that there are no odd digits in the number

# 2.2. Minimum position search loop
i = 0
while i<len(L):
# Digit processing L[i]
if L[i]%2 == 1:
if fOdd == False:
# If the first digit is odd, then just save the minimum
Min = L[i]
MinPos = i
fOdd = True
else:
# If not the first odd digit, then implement the comparison
if Min>L[i]:
Min = L[i]
MinPos = i

i = i+1

if fOdd: return MinPos+1
else: return -1

# Demonstration of using the GetPosMinOddNum() function
n = input("n = ") # Input number
Pos = GetPosMinOddNum(n) # Invoke function, calculation
print("Pos = ", Pos)```

The result of the program

```n = 22213320
Pos = 4```

### ⇑

##### 5.2.1. Replacing a line

To determine the position number in the direction from right to left, in the previous example, in the while() loop of forming the list L, the line

`L = [n%10] + L # Left-to-right`

replace with line

`L = L + [n%10] # right-to-left`

In this case, the list of numbers is formed in reverse order.

### ⇑

##### 5.2.2. Using code without a list

For the case of right-to-left position numbering (<=), it is good to implement the function without first generating the list, as shown below.

```# A function that returns the position (number) of the lowest odd digit of a number.
# If there are no odd digits in the number, -1 is returned.
# Position number is considered from right to left (<=)
def GetPosMinOddNum(n):
# First, accept that there are no odd digits in the number
fOdd = False
MinPos = -1
pos = 0

while n>0:
pos = pos + 1 # current position
d = n%10     # get a digit from the end

if d%2 == 1: # is t odd?
if fOdd == False:
# if this is the first digit
fOdd = True
MinPos = pos
Min = d
else:
# if the digit is not the first, then implement the comparison
if Min>d:
MinPos = pos
Min = d
n = n//10

return MinPos

# Demonstration of using the GetPosMinOddNum() function
n = input("n = ") # Input number
Pos = GetPosMinOddNum(n) # Invoke function, calculation
print("Pos = ", Pos)```

The result of the program

```n = 23481234550
Pos = 7```