Iterations

File Name: iterations.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# python module.py data_types/numeric/integer/iterations.py
# python -m data_types.numeric/integer data_types/numeric/integer/iterations.py

import data_types.commonUtils as utils

utils.print_h1('Integer Iterations')

utils.print_blockquote([
  'Types are short, long, int32, int64',
  'Binary, Octal, and Hexadecimal Integers',
  'Underscores in Numeric Literals (Python 3.6+) ex: large_num = 1_000_000'
])

salary = 9000
increment = 500

print('\n >>>> Iterate using for loop with type casting')
print(' >>>> Follow this approach on demand and Its not recommended')
for text in str(salary):
    print(text)

# print('\n >>>> Iterate using while loop')
# def iterateInteger(integer_number):
#     stack = []
#     while integer_number > 0:
#         digit = integer_number % 10
#         stack.append(digit)
#         integer_number //= 10

#     while stack:
#         print(stack.pop())

# iterateInteger(salary)

# def iterateInteger(integer_number):
#     stack = []
#     while integer_number > 0:
#         digit = integer_number % 10
#         stack.append(digit)
#         integer_number //= 10

#     while stack:
#         print(stack.pop())

def find_digit_position(number, digit_to_find):
    original_number = number
    position = 1
    while number >= 10:
        if number % 10 == digit_to_find:
            return len(str(original_number)) - position + 1
        number //= 10
        position += 1
    # Check the last digit
    return 1 if number == digit_to_find else -1

# Test the function with the number 9000 and digit 9
find_digit_position_number = 8778890942
find_number = 0
print(f'Position of {find_number} from {find_digit_position_number} is ', find_digit_position(find_digit_position_number, find_number))