Python Program to Convert Octal to Hexadecimal

Program to Convert Octal to Hexadecimal

In the previous article, we have discussed Python Program to Convert Decimal to Hexadecimal
Given an octal number and the task is to get the respective hexadecimal number.

Examples:

Example1:

Input:

Given Octal number = 24

Output:

The Hexadecimal value of the given octal number { 24 } is: 
14

Example2:

Input:

Given Octal number = 72

Output:

The Hexadecimal value of the given octal number { 72 } is: 
3A

Program to Convert Octal to Hexadecimal in Python

Below are the ways to convert the given octal number into hexadecimal :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the octal number as static input and store it in a variable.
  • Take a variable say ‘k‘ and initialize its value with 0.
  • Take a list of length 50 and initialize all the values to zero using the multiplication operator.
  • Store it in another variable.
  • Take another variable say ‘deciml_num‘ and initialize its value with 0.
  • Take another variable say ‘tempry‘ and initialize its value with 0.
  • Loop till the given octal number is not equal to zero using the while loop.
  • Inside the loop, calculate the value of the given octal number modulus 10 and store it in another variable ‘a’.
  • Multiply ‘a’ with the 8 raised to the power tempry using the pow() function and store it in another variable ‘b’.
  • Add ‘b’ with the deciml_num and store it in the same variable deciml_num.
  • Increment the value of tempry by 1 and store it in the same variable tempry.
  • Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Calculate the value of deciml_num modulus 16 to get the remainder and store it in a variable.
  • Check if the above remainder value is less than 10 using the if conditional statement.
  • If the statement is true, then get the character with the ASCII value remainder+48 using the if conditional statement and store it in a variable
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Else if the statement is false, get the character with the ASCII value remainder+55.
  • Store it in another variable.
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Divide the decimal number by 16 and store it in the same variable deciml_num.
  • Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
  • Print the element present at the iterator of the hexadecimal list to get the hexadecimal value of the given octal number.
  • The Exit of the Program.

Below is the implementation:

# Give the octal number as static input and store it in a variable.
gvn_octl_num = 24
# Take a variable say 'k' and initialize its value with 0.
k = 0
# Take a list of length 50 and initialize all the values to zero using the multiplication
# operator.
# Store it in another variable.
Hexa_deciml = ['0']*50
# Take another variable say 'deciml_num' and initialize its value with 0.
deciml_num = 0
# Take another variable say 'tempry' and initialize its value with 0.
tempry = 0
print(
    "The Hexadecimal value of the given octal number {", gvn_octl_num, "} is: ")
# Loop till the given octal number is not equal to zero using the while loop.
while gvn_octl_num != 0:
  # Inside the loop, calculate the value of the given octal number modulus 10 and store
    # it in another variable 'a'.
    a = (gvn_octl_num % 10)
 # Multiply 'a' with the 8 raised to the power tempry using the pow() function and store
# it in another variable 'b'.
    b = pow(8, tempry)*a
  # Add 'b' with the deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num+b
 # Increment the value of tempry by 1 and store it in the same variable tempry.
    tempry += 1
  # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
    gvn_octl_num = gvn_octl_num // 10
# Loop till the decimal number is not equal to zero using the while loop.
while deciml_num != 0:
  # Calculate the value of deciml_num modulus 16 to get the remainder and store it in a
  # variable.
    remindr = deciml_num % 16
 # Check if the above remainder value is less than 10 using the if conditional statement.
    if remindr < 10:
        # If the statement is true, then get the character with the ASCII value remainder+48
        # using the if conditional statement and store it in a variable
        chrvalue = chr(remindr+48)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
 # Increment the value of k by 1 and store it in the same variable k.
        k += 1
    else:
     # Else if the statement is false, get the character with the ASCII value remainder+55.
        # Store it in another variable.
        chrvalue = chr(remindr+55)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
  # Increment the value of k by 1 and store it in the same variable k.
        k += 1
  # Divide the decimal number by 16 and store it in the same variable deciml_num.
    deciml_num //= 16
# Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
for itr in range(k-1, -1, -1):
  # Print the element present at the iterator of the hexadecimal list to get the
    # hexadecimal value of the given octal number.
    print(Hexa_deciml[itr], end='')

Output:

The Hexadecimal value of the given octal number { 24 } is: 
14

Method #2: Using While loop (User Input)

Approach:

  • Give the octal number as user input using the int(input()) function input and store it in a variable.
  • Take a variable say ‘k‘ and initialize its value with 0.
  • Take a list of length 50 and initialize all the values to zero using the multiplication operator.
  • Store it in another variable.
  • Take another variable say ‘deciml_num‘ and initialize its value with 0.
  • Take another variable say ‘tempry‘ and initialize its value with 0.
  • Loop till the given octal number is not equal to zero using the while loop.
  • Inside the loop, calculate the value of the given octal number modulus 10 and store it in another variable ‘a’.
  • Multiply ‘a’ with the 8 raised to the power tempry using the pow() function and store it in another variable ‘b’.
  • Add ‘b’ with the deciml_num and store it in the same variable deciml_num.
  • Increment the value of tempry by 1 and store it in the same variable tempry.
  • Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Calculate the value of deciml_num modulus 16 to get the remainder and store it in a variable.
  • Check if the above remainder value is less than 10 using the if conditional statement.
  • If the statement is true, then get the character with the ASCII value remainder+48 using the if conditional statement and store it in a variable
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Else if the statement is false, get the character with the ASCII value remainder+55.
  • Store it in another variable.
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Divide the decimal number by 16 and store it in the same variable deciml_num.
  • Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
  • Print the element present at the iterator of the hexadecimal list to get the hexadecimal value of the given octal number.
  • The Exit of the Program.

Below is the implementation:

# Give the octal number as user input using the int(input()) function input and
# store it in a variable.
gvn_octl_num = int(input("Enter some random number = "))
# Take a variable say 'k' and initialize its value with 0.
k = 0
# Take a list of length 50 and initialize all the values to zero using the multiplication
# operator.
# Store it in another variable.
Hexa_deciml = ['0']*50
# Take another variable say 'deciml_num' and initialize its value with 0.
deciml_num = 0
# Take another variable say 'tempry' and initialize its value with 0.
tempry = 0
print(
    "The Hexadecimal value of the given octal number {", gvn_octl_num, "} is: ")
# Loop till the given octal number is not equal to zero using the while loop.
while gvn_octl_num != 0:
  # Inside the loop, calculate the value of the given octal number modulus 10 and store
    # it in another variable 'a'.
    a = (gvn_octl_num % 10)
 # Multiply 'a' with the 8 raised to the power tempry using the pow() function and store
# it in another variable 'b'.
    b = pow(8, tempry)*a
  # Add 'b' with the deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num+b
 # Increment the value of tempry by 1 and store it in the same variable tempry.
    tempry += 1
  # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
    gvn_octl_num = gvn_octl_num // 10
# Loop till the decimal number is not equal to zero using the while loop.
while deciml_num != 0:
  # Calculate the value of deciml_num modulus 16 to get the remainder and store it in a
  # variable.
    remindr = deciml_num % 16
 # Check if the above remainder value is less than 10 using the if conditional statement.
    if remindr < 10:
        # If the statement is true, then get the character with the ASCII value remainder+48
        # using the if conditional statement and store it in a variable
        chrvalue = chr(remindr+48)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
 # Increment the value of k by 1 and store it in the same variable k.
        k += 1
    else:
     # Else if the statement is false, get the character with the ASCII value remainder+55.
        # Store it in another variable.
        chrvalue = chr(remindr+55)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
  # Increment the value of k by 1 and store it in the same variable k.
        k += 1
  # Divide the decimal number by 16 and store it in the same variable deciml_num.
    deciml_num //= 16
# Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
for itr in range(k-1, -1, -1):
  # Print the element present at the iterator of the hexadecimal list to get the
    # hexadecimal value of the given octal number.
    print(Hexa_deciml[itr], end='')

Output:

Enter some random number = 72
The Hexadecimal value of the given octal number { 72 } is: 
3A

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find the Maximum Element in the Matrix

Program to Find the Maximum Element in the Matrix

In the previous article, we have discussed Python Program to find the Sum of Series 1^2+2^2+3^2…+N^2
Given a matrix, the task is to find the maximum element in the given Matrix in Python

What is a matrix:

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

Examples:

Example1:

Input:

Given Matrix
2   9   1
11 4   5 
9   2   3
1   2   3

Output:

The maximum element of the given matrix [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]] is :
11

Example2:

Input:

Given Matrix 
1   7   5
25 4   6

Output:

The maximum element of the given matrix [[1, 7, 5], [25, 4, 6]] is :
25

Program to Find the Maximum Element in the Matrix in Python

Below are the ways to find the maximum element in the given Matrix in Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the matrix as static input and store it in a variable.
  • Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
  • Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
  • Take a variable mtrxmax which stores the maximum element of the given matrix and initialize its value to the first element of the given matrix.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
  • Check if the gvnmatrix[n][m] value (where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop) is greater than mtrxmax using the if conditional statement.
  • If it is true then modify the mtrxmax value with gvnmatrix[n][m] value.
  • Print the mtrxmax value which is the maximum element of the given Matrix
  • The Exit of the Program.

Below is the implementation:

# Give the matrix as static input and store it in a variable.
mtrx = [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]]
# Calculate the number of rows of the given matrix by
# calculating the length of the nested list using the len() function
# and store it in a variable mtrxrows.
mtrxrows = len(mtrx)
# Calculate the number of columns of the given matrix by
# calculating the length of the first list in the nested list
# using the len() function and store it in a variable mtrxcols.
mtrxcols = len(mtrx[0])
# Take a variable mtrxmax which stores the maximum element of the given matrix
# and initialize its value to the first element of the given matrix.
mtrxmax = mtrx[0][0]
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of rows using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Check if the gvnmatrix[n][m] value (where n is the iterator value of the
        # parent For loop and m is the iterator value of the inner For loop)
        # is greater than mtrxmax using the if conditional statement.
        if(mtrx[n][m] > mtrxmax):
            # If it is true then modify the mtrxmax value with gvnmatrix[n][m] value.
            mtrxmax = mtrx[n][m]
# Print the mtrxmax value which is the maximum element of the given Matrix
print('The maximum element of the given matrix', mtrx, 'is :')
print(mtrxmax)

Output:

The maximum element of the given matrix [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]] is :
11

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
  • Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
  • Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
  • Loop till the given number of rows using the For loop
  • Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
  • Add the above row elements list to gvnmatrix using the append() function.
  • Take a variable mtrxmax which stores the maximum element of the given matrix and initialize its value to the first element of the given matrix.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
  • Check if the gvnmatrix[n][m] value (where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop) is greater than mtrxmax using the if conditional statement.
  • If it is true then modify the mtrxmax value with gvnmatrix[n][m] value.
  • Print the mtrxmax value which is the maximum element of the given Matrix
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows of the matrix as user input using the int(input()) function
# and store it in a variable.
mtrxrows = int(input('Enter some random number of rows of the matrix = '))
# Give the number of columns of the matrix as user input using the int(input()) function
# and store it in another variable.
mtrxcols = int(input('Enter some random number of columns of the matrix = '))
# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
mtrx = []
# Loop till the given number of rows using the For loop
for n in range(mtrxrows):
    # Inside the For loop, Give all the row elements of the given Matrix as a list using
    # the list(),map(),int(),split() functions and store it in a variable.
    l = list(map(int, input(
        'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))
    # Add the above row elements list to gvnmatrix using the append() function.

    mtrx.append(l)
# Take a variable mtrxmax which stores the maximum element of the given matrix
# and initialize its value to the first element of the given matrix.
mtrxmax = mtrx[0][0]
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
    # Inside the For loop, Iterate till the given number of rows using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Check if the gvnmatrix[n][m] value (where n is the iterator value of the
        # parent For loop and m is the iterator value of the inner For loop)
        # is greater than mtrxmax using the if conditional statement.
        if(mtrx[n][m] > mtrxmax):
            # If it is true then modify the mtrxmax value with gvnmatrix[n][m] value.
            mtrxmax = mtrx[n][m]
# Print the mtrxmax value which is the maximum element of the given Matrix
print('The maximum element of the given matrix', mtrx, 'is :')
print(mtrxmax)

Output:

Enter some random number of rows of the matrix = 2
Enter some random number of columns of the matrix = 3
Enter {3} elements of row {1} separated by spaces = 1 7 5
Enter {3} elements of row {2} separated by spaces = 25 4 6
The maximum element of the given matrix [[1, 7, 5], [25, 4, 6]] is :
25

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find Sum of Series (1+(1+2)+(1+2+3)+…till N)

Program to Find Sum of Series (1+(1+2)+(1+2+3)+...till N)

In the previous article, we have discussed Python Program to Swap Upper Diagonal Elements with Lower Diagonal Elements of Matrix.

Given a number N and the task is to find the sum of the given series (1+(1+2)+(1+2+3)+…till N) for the given number in Python.

Examples:

Example1:

Input:

Given Number = 25

Output:

The total sum of the series till the given number { 25 } =  2925

Example2:

Input:

Given Number = 18

Output:

The total sum of the series till the given number { 18 } =  1140

Program to Find Sum of Series (1+(1+2)+(1+2+3)+…till N) in Python

Below are the ways to find the sum of the given series (1+(1+2)+(1+2+3)+…till N) for the given number in Python

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, iterate from 1 to the value of k using the for loop.
  • Add the iterator value to the above-initialized resltsum and store it in the same variable resltsum.
  • Increment the value of k by 1 outside the for loop.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 25
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
  # Inside the loop, iterate from 1 to the value of k using the for loop.
    for itr in range(1, k+1):
      # Add the iterator value to the above-initialized resltsum and store it in the same
        # variable resltsum.
        resltsum += itr
  # Increment the value of k by 1 outside the for loop.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 25 } =  2925

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, iterate from 1 to the value of k using the for loop.
  • Add the iterator value to the above-initialized resltsum and store it in the same variable resltsum.
  • Increment the value of k by 1 outside the for loop.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
  # Inside the loop, iterate from 1 to the value of k using the for loop.
    for itr in range(1, k+1):
      # Add the iterator value to the above-initialized resltsum and store it in the same
        # variable resltsum.
        resltsum += itr
  # Increment the value of k by 1 outside the for loop.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some Random Number = 18
The total sum of the series till the given number { 18 } = 1140

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.

 

Program to Reverse a String using a Stack Data Structure in C++ and Python

Program to Reverse a String using a Stack Data Structure in C++ and Python

Strings:

A string data type is used in most computer languages for data values that are made up of ordered sequences of characters, such as “hello world.” A string can include any visible or unseen series of characters, and characters can be repeated. The length of a string is the number of characters in it, and “hello world” has length 11 – made up of 10 letters and 1 space. The maximum length of a string is usually restricted. There is also the concept of an empty string, which includes no characters and has a length of zero.

A string can be both a constant and a variable. If it is a constant, it is commonly expressed as a string of characters surrounded by single or double quotes.

Stack:

Stacking objects means putting them on top of one another in the English language. This data structure allocates memory in the same manner.

Data structures are essential for organizing storage in computers so that humans can access and edit data efficiently. Stacks were among the first data structures to be defined in computer science. In layman’s terms, a stack is a linear accumulation of items. It is a collection of objects that provides fast last-in, first-out (LIFO) insertion and deletion semantics. It is a modern computer programming and CPU architecture array or list structure of function calls and parameters. Elements in a stack are added or withdrawn from the top of the stack in a “last in, first out” order, similar to a stack of dishes at a restaurant.

Unlike lists or arrays, the objects in the stack do not allow for random access.

Given a string the task is to reverse the given string using stack data structure in C++ and python.

Examples:

Example1:

Input:

given string ="hellothisisBTechGeeks"

Output:

Printing the given string before reversing : hellothisisBTechGeeks
Printing the given string after reversing : skeeGhceTBsisihtolleh

Example2:

Input:

given string ="skyisbluieIFC"

Output:

Printing the given string before reversing : skyisbluieIFC
Printing the given string after reversing : CFIeiulbsiyks

Example3:

Input:

given string="cirusfinklestein123"

Output:

Printing the given string before reversing : cirusfinklestein123
Printing the given string after reversing : 321nietselknifsuric

Program to Reverse a String using a Stack Data Structure in C++ and Python

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

1)Reversing the string using stack<> function in C++

It is critical to concentrate on the algorithm in order to develop better code. This could be the initial step in selecting a problem; one must consider the optimal algorithm before moving on to the implementation and coding.

The following steps may be useful:

  • Scan the given string or given string as static input
  • The first step would be to start with an empty stack.
  • In C++, we use stack<type>, where type is the data type of the stack (like integer, character, string, etc).
  • Then, using the push function, add the characters from the string one by one to the stack, so that the last character in the string is at the top.
  • Consider a for loop that runs a certain string length number of times.
  • Using the pop() function, pop the character and replace the popped characters in the given string.
  • Print the string

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
// function which reverses the given string using stack
void revString(string& givenstr)
{
    // Taking a empty stack of character type
    stack<char> st;

    // Traversing the given string using for loop
    for (char character : givenstr) {
        // adding each element of the given string to stack
        st.push(character);
    }

    // popping all elements from the stack and
    // replacing the elements of string with the popped
    // element
    for (int i = 0; i < givenstr.length(); i++) {
        // intializing the ith character of string with the
        // top element of the stack
        givenstr[i] = st.top();
        // popping the top element from the stack using
        // pop() function
        st.pop();
    }
}

int main()
{
    // given string
    string givenstr = "hellothisisBTechGeeks";
    // printing the string before reversing elements
    cout << "Printing the given string before reversing : "
         << givenstr << endl;
    revString(givenstr);
    cout << "Printing the given string after reversing : "
         << givenstr << endl;

    return 0;
}

Output:

Printing the given string before reversing : hellothisisBTechGeeks
Printing the given string after reversing : skeeGhceTBsisihtolleh

All the characters present in the string get reversed using the above approach(including space characters).

2)Reversing the string using deque in Python

We use deque to copy all elements from the givenstring(which performs the same operation as a stack in this case)

We use the join method to join all the characters of the given string after reversing.

Below is the implementation:

from collections import deque
# given string
givenstr = "HellothisisBTechGeeks"
# Printing the given string before reversing
print("Printing the given string before reversing : ")
print(givenstr)
# creating the stack from the given string
st = deque(givenstr)

# pop all characters from the stack and join them back into a string
givenstr = ''.join(st.pop() for _ in range(len(givenstr)))

# Printing the given string after reversing
print("Printing the given string after reversing : ")
print(givenstr)

Output:

Printing the given string before reversing : 
HellothisisBTechGeeks
Printing the given string after reversing : 
skeeGhceTBsisihtolleH

Related Programs:

Python program to convert seconds into day, hours, minutes and seconds

Different Ways to convert seconds into Hours and Minutes

In this post, we will look at various Python techniques for converting seconds to hours and minutes.

Date and Time

Python, as a multipurpose language, can be used for a variety of reasons. Python includes a number of packages that help us with data manipulation tasks.

To achieve the same result in timestamp conversion, i.e. conversion of seconds to hours or minutes, numerous strategies might be considered.

Examples:

Input:

timeSeconds=30000

Output:

converting given time in seconds 30000 = 8 hrs 20 minutes

Different Ways to convert seconds into Hours and Minutes in Python

There are several ways to convert seconds into hours and minutes in python some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1: Using mathematical formula

We will write a generic function which converts the given time into hours and minutes

First, we convert the supplied seconds value to the 24 hour format.

timeseconds = timeseconds % (24*3600)

Furthermore, because one hour is equal to 3600 seconds and one minute is equal to 60 seconds, we use the below mathematical logic to convert seconds to hours and minutes.

timehour = timeseconds/3600

timeminutes = timeseconds//60

Below is the implementation:

def convertSec(timeseconds):
    secondValue = timeseconds % (24 * 3600)
    timeHour = secondValue // 3600
    secondValue %= 3600
    timeMinutes = secondValue // 60
    secondValue %= 60
    print("converting given time in seconds",
          timeseconds, "=", timeHour, "hrs", timeMinutes, "minutes")


# given time in seconds
timeseconds = 30000
# passing given time in seconds to convertSec function to convert it to minutes and hours
convertSec(timeseconds)

Output:

converting given time in seconds 30000 = 8 hrs 20 minutes

Method #2: Using time module in python

The Python time module includes time. By giving the format codes to the strftime() function, you can display the timestamp as a string in a certain format.

The function time.gmtime() is used to convert the value supplied to it into seconds. Furthermore, the time.strftime() function converts the value supplied from time.gmtime() into hours and minutes using the format codes provided.

Below is the implementation:

import time
givenseconds = 30000
#converting given time in seconds to hours and minutes
temp = time.gmtime(givenseconds)
resultantTime = time.strftime("%H:%M:%S", temp)
#printing the given time in seconds to hours and minutes
print("converting given time in seconds", givenseconds,
      "=", resultantTime)

Output:

converting given time in seconds 30000 = 08:20:00

Method #3: Using datetime module in Python

The Python datetime module has a number of built-in functions for manipulating dates and times. The date and time. The timedelta() function manipulates and represents data in the correct time format.

Below is the implementation:

import datetime
# given time in seconds
givenSeconds = 30000
#converting given time in seconds to hours and minutes
resultantTime = datetime.timedelta(seconds=givenSeconds)
# printing the given time in seconds to hours and minutes
print("converting given time in seconds", givenSeconds,
      "=", resultantTime)

Output:

converting given time in seconds 30000 = 8:20:00

Method #4:Using divmod function

You may get the result quickly with only two mathematical operations by using the divmod() function, which performs a single division to generate both the quotient and the remainder.

This is similar to method 1

Below is the implementation:

# given time in seconds
timeSeconds = 30000
# storing it in another variable
givenSeconds = timeSeconds
# converting it into minutes and seconds
timeMinutes, timeSeconds = divmod(timeSeconds, 60)
# converting it into hours and minutes
timeHour, timeMinutes = divmod(timeMinutes, 60)
# printing the given time in seconds to hours and minutes
print("converting given time in seconds", givenSeconds,
      "=", timeHour, "hrs", timeMinutes, "minutes")

Output:

converting given time in seconds 30000 = 8 hrs 20 minutes

Related Programs:

Python Program to Find Lexicographic Rank of a Given String

Program to Find Lexicographic Rank of a Given String

In the previous article, we have discussed Python Program to Check if a String Contains at least One Number

Given a string and the task is to find the Lexicographic Rank of a given string in python.

Examples:

Example1:

Input:

Given String = "btechgeeks"

Output:

The given String's { btechgeeks } Lexicographic Rank =  328009

Example2:

Input:

Given String = "hello"

Output:

The given String's { hello } Lexicographic Rank =  25

Program to Find Lexicographic Rank of a Given String in Python

Below are the ways to find the Lexicographic Rank of a given string in python.

Method #1: Using For Loop (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the string as static input and store it in a variable.
  • Take a variable to say lexicogrpc_rank and initialize its value to 1.
  • Calculate the length of the given string using the len() function and decrease it by 1.
  • Store it in another variable say str_lengt.
  • Loop from 0 to the above length using the for loop.
  • Inside the loop, take a variable to say cnt and initialize its value to 0.
  • Loop from the itr+1 to the above str_lengt (where itr is the iterator value of the parent for loop) using the for loop.
  • Inside the loop, check if the character present at the iterator value of the parent for loop is greater than the character present at the iterator value of the inner for loop using the if conditional statement.
  • If the statement is true, then increment the value of the above-initialized cnt by 1.
  • Calculate the factorial of (str_lengt – itr) using the math.factorial() function and store it in another variable. (where itr is the iterator value of the parent for loop)
  • Multiply the above result with cnt and add the result to the lexicogrpc_rank.
  • Store it in the same variable lexicogrpc_rank.
  • Print the lexicogrpc_rank to get the Lexicographic Rank of a given string.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the string as static input and store it in a variable.
gven_str = "btechgeeks"
# Take a variable to say lexicogrpc_rank and initialize its value to 1.
lexicogrpc_rank = 1
# Calculate the length of the given string using the len() function and decrease it by 1.
# Store it in another variable say str_lengt.
str_lengt = len(gven_str)-1
# Loop from 0 to the above length using the for loop.
for itr in range(0, str_lengt):
    # Inside the loop, take a variable to say cnt and initialize its value to 0.
    cnt = 0
    # Loop from the itr+1 to the above str_lengt (where itr is the iterator value of the parent
    # for loop) using the for loop.
    for k in range(itr+1, str_lengt+1):
      # Inside the loop, check if the character present at the iterator value of the parent for
      # loop is greater than the character present at the iterator value of the inner for
      # loop using the if conditional statement.
        if gven_str[itr] > gven_str[k]:
         # If the statement is true, then increment the value of the above-initialized cnt by 1.

            cnt += 1
    # Calculate the factorial of (str_lengt - itr) using the math.factorial() function and
    # store it in another variable. (where itr is the iterator value of the parent for loop)
    f = math.factorial(str_lengt-itr)
    # Multiply the above result with cnt and add the result to the lexicogrpc_rank.
    # Store it in the same variable lexicogrpc_rank.
    lexicogrpc_rank += cnt*f
# Print the lexicogrpc_rank to get the Lexicographic Rank of a given string.
print("The given String's {", gven_str,
      "} Lexicographic Rank = ", lexicogrpc_rank)

Output:

The given String's { btechgeeks } Lexicographic Rank =  328009

Method #2: Using For loop (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the string as user input using the input() function and store it in a variable.
  • Take a variable to say lexicogrpc_rank and initialize its value to 1.
  • Calculate the length of the given string using the len() function and decrease it by 1.
  • Store it in another variable say str_lengt.
  • Loop from 0 to the above length using the for loop.
  • Inside the loop, take a variable to say cnt and initialize its value to 0.
  • Loop from the itr+1 to the above str_lengt (where itr is the iterator value of the parent for loop) using the for loop.
  • Inside the loop, check if the character present at the iterator value of the parent for loop is greater than the character present at the iterator value of the inner for loop using the if conditional statement.
  • If the statement is true, then increment the value of the above-initialized cnt by 1.
  • Calculate the factorial of (str_lengt – itr) using the math.factorial() function and store it in another variable. (where itr is the iterator value of the parent for loop)
  • Multiply the above result with cnt and add the result to the lexicogrpc_rank.
  • Store it in the same variable lexicogrpc_rank.
  • Print the lexicogrpc_rank to get the Lexicographic Rank of a given string.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the string as user input using the input() function and 
# store it in a variable.
gven_str = input("Enter some random String = ")
# Take a variable to say lexicogrpc_rank and initialize its value to 1.
lexicogrpc_rank = 1
# Calculate the length of the given string using the len() function and decrease it by 1.
# Store it in another variable say str_lengt.
str_lengt = len(gven_str)-1
# Loop from 0 to the above length using the for loop.
for itr in range(0, str_lengt):
    # Inside the loop, take a variable to say cnt and initialize its value to 0.
    cnt = 0
    # Loop from the itr+1 to the above str_lengt (where itr is the iterator value of the parent
    # for loop) using the for loop.
    for k in range(itr+1, str_lengt+1):
      # Inside the loop, check if the character present at the iterator value of the parent for
      # loop is greater than the character present at the iterator value of the inner for
      # loop using the if conditional statement.
        if gven_str[itr] > gven_str[k]:
         # If the statement is true, then increment the value of the above-initialized cnt by 1.

            cnt += 1
    # Calculate the factorial of (str_lengt - itr) using the math.factorial() function and
    # store it in another variable. (where itr is the iterator value of the parent for loop)
    f = math.factorial(str_lengt-itr)
    # Multiply the above result with cnt and add the result to the lexicogrpc_rank.
    # Store it in the same variable lexicogrpc_rank.
    lexicogrpc_rank += cnt*f
# Print the lexicogrpc_rank to get the Lexicographic Rank of a given string.
print("The given String's {", gven_str,
      "} Lexicographic Rank = ", lexicogrpc_rank)

Output:

Enter some random String = hello
The given String's { hello } Lexicographic Rank = 25

Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.

Python Program to Find the Shortest Word in a String

Program to Find the Shortest Word in a String

In the previous article, we have discussed Program to Print First 50 Natural Numbers Using Recursion

Given a string and the task is to find the shortest word in a given string.

Examples:

Example1:

Input:

Given String = "Hello this is BTechGeeks online Platform"

Output:

Printing the shortest length word :
is

Example2:

Input:

Given String = "good morning btechgeeks"

Output:

Printing the shortest length word :
good

Program to Find the Shortest Word in a String in Python

Below are the ways to find the shortest word in a given string:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Convert the given string into a list of words using the list() and split() functions and store it in a variable.
  • Assume that the first element is the one with the shortest word.
  • The lengths of the words in the list are then compared with the first element using a for loop and an if statement.
  • In a temporary variable, save the name of the term with the shortest length.
  • Print the word that has the shortest length.
  • The Exit of program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str="Hello this is BTechGeeks online Platform "
# Convert the given string into a list of words using the list(),split() functions and
#store it in a variable.
listWords = list(gvn_str.split())
# taking the first element length as min length word
minLength = len(listWords[0])
# Initializing a temp variable which stores the first word
word = listWords[0]
# Traversing the words list
for i in listWords:
    if(len(i) < minLength):
        minLength = len(i)
        word = i
print("Printing the shortest length word :")
print(word)

Output:

Printing the shortest length word :
is

Method #2: Using For loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Convert the given string into a list of words using the list() and split() functions and store it in a variable.
  • Assume that the first element is the one with the shortest word.
  • The lengths of the words in the list are then compared with the first element using a for loop and an if statement.
  • In a temporary variable, save the name of the term with the shortest length.
  • Print the word that has the shortest length.
  • The Exit of program.

Below is the implementation:

# Give the string as user input using the input() function 
# and store it in a variable.
gvn_str= input("Enter some random String = ")
# Convert the given string into a list of words using the list(),split() functions and
#store it in a variable.
listWords = list(gvn_str.split())
# taking the first element length as min length word
minLength = len(listWords[0])
# Initializing a temp variable which stores the first word
word = listWords[0]
# Traversing the words list
for i in listWords:
    if(len(i) < minLength):
        minLength = len(i)
        word = i
print("Printing the shortest length word :")
print(word)

Output:

Enter some random String = good morning btechgeeks
Printing the shortest length word :
good

The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.

Python Program to Print First 50 Natural Numbers Using Recursion

Program to Print First 50 Natural Numbers Using Recursion

In the previous article, we have discussed Python Program to find the Normal and Trace of a Matrix

The task is to print the first 50 natural numbers using recursion.

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Natural Numbers:

Natural numbers are a subset of the number system that includes all positive integers from one to infinity. Natural numbers, which do not include zero or negative numbers, are also known as counting numbers. They are a subset of real numbers that include only positive integers and exclude zero, fractions, decimals, and negative numbers.

Program to Print First 50 Natural Numbers Using Recursion in Python

Below are the ways to print the first 50 natural numbers using recursion.

Method : Using Recursion

Approach:

  • Take a variable say gvn_numb and initialize its value to 1 and store it in a variable.
  • Pass the given number as an argument to the NaturlNumbr function.
  • Create a recursive function to say NaturlNumbr which takes the given number as an argument and returns the first 50 natural numbers.
  • Check if the given number is less than or equal to 50 using the if conditional statement.
  • If the statement is true, print the given number separated by spaces.
  • Pass the given number +1 as an argument to the NaturlNumbr function.{Recursive Logic}
  • Print the first 50 natural numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say NaturlNumbr which takes the given number as an
# argument and returns the first 50 natural numbers.


def NaturlNumbr(gvn_numb):
    # Check if the given number is less than or equal to 50 using the if conditional
    # statement.
    if(gvn_numb <= 50):
      # If the statement is true, print the given number separated by spaces.
        print(gvn_numb, end=" ")
      # Pass the given number +1 as an argument to the NaturlNumbr function.{Recursive Logic}
        NaturlNumbr(gvn_numb + 1)


# Take a variable say gvn_numb and initialize its value to 1 and store it in a variable.
# Print the first 50 natural numbers using recursion.
gvn_numb = 1
# Pass the given number as an argument to the NaturlNumbr function.
print("The first 50 natural numbers are as follows:")
NaturlNumbr(gvn_numb)

Output:

The first 50 natural numbers are as follows:
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

Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.

Python Program to Multiply Two Numbers Using Recursion

Program to Multiply Two Numbers Using Recursion

In the previous article, we have discussed Python Program to Find Subtraction of Two Numbers using Recursion

Given two numbers and the task is to find the multiplication of the given two numbers using recursion.

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Examples:

Example1:

Input:

Given First Number      =  3
Given Second Number =  5

Output:

The Multiplication of { 3 * 5 } using recursion = 15

Example2:

Input:

Given First Number      = 6
Given Second Number = 9

Output:

The Multiplication of { 6 * 9 } using recursion = 54

Program to Multiply Two Numbers Using Recursion in Python

Below are the ways to find the multiplication of the given two numbers using recursion :

Method #1: Using Recursion (Static Input)

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given two numbers as the arguments to recur_mult function.
  • Create a recursive function to say recur_mult which takes the two numbers as arguments and returns the multiplication of the given two numbers using recursion.
  • Check if the first number is less than the second number using the if conditional statement.
  • If the statement is true, then return the second number, first number as arguments to the recur_mult.{Recursive logic}.
  • Check if the second number is not equal to 0 using the elif conditional statement.
  • If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb – 1) {Recursive Logic}.
  • Else return 0.
  • Print the multiplication of the given two numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say recur_mult which takes the two numbers as arguments
# and returns the multiplication of the given two numbers using recursion.


def recur_mult(fst_numb, secnd_numb):
  # Check if the first number is less than the second number using the if conditional
  # statement.
    if fst_numb < secnd_numb:
        # If the statement is true, then return the second number, first number as arguments
        # to the recur_mult.{Recursive logic}.
        return recur_mult(secnd_numb, fst_numb)
    # Check if the second number is not equal to 0 using the elif conditional statement.
    elif secnd_numb != 0:
        # If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb - 1)
        # {Recursive Logic}.

        return fst_numb + recur_mult(fst_numb, secnd_numb - 1)
    else:
        # Else return 0.
        return 0


# Give the first number as static input and store it in a variable.
fst_numb = 5
# Give the second number as static input and store it in another variable.
secnd_numb = 3
# Pass the given two numbers as the arguments to recur_mult function.
# Print the multiplication of given two numbers using recursion.
print("The Multiplication of {", fst_numb, "*", secnd_numb,
      "} using recursion =", recur_mult(fst_numb, secnd_numb))

Output:

The Multiplication of { 5 * 3 } using recursion = 15

Method #2: Using Recursion (User Input)

Approach:

  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers as the arguments to recur_mult function.
  • Create a recursive function to say recur_mult which takes the two numbers as arguments and returns the multiplication of the given two numbers using recursion.
  • Check if the first number is less than the second number using the if conditional statement.
  • If the statement is true, then return the second number, first number as arguments to the recur_mult.{Recursive logic}.
  • Check if the second number is not equal to 0 using the elif conditional statement.
  • If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb – 1) {Recursive Logic}.
  • Else return 0.
  • Print the multiplication of the given two numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say recur_mult which takes the two numbers as arguments
# and returns the multiplication of the given two numbers using recursion.


def recur_mult(fst_numb, secnd_numb):
  # Check if the first number is less than the second number using the if conditional
  # statement.
    if fst_numb < secnd_numb:
        # If the statement is true, then return the second number, first number as arguments
        # to the recur_mult.{Recursive logic}.
        return recur_mult(secnd_numb, fst_numb)
    # Check if the second number is not equal to 0 using the elif conditional statement.
    elif secnd_numb != 0:
        # If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb - 1)
        # {Recursive Logic}.

        return fst_numb + recur_mult(fst_numb, secnd_numb - 1)
    else:
        # Else return 0.
        return 0


# Give the first number as user input using the int(input()) function and 
# store it in a variable.
fst_numb = int(input("Enter some random number = "))
#Give the second number as user input using the int(input()) function and 
# store it in another variable.
secnd_numb = int(input("Enter some random number = "))
# Pass the given two numbers as the arguments to recur_mult function.
# Print the multiplication of given two numbers using recursion.
print("The Multiplication of {", fst_numb, "*", secnd_numb,
      "} using recursion =", recur_mult(fst_numb, secnd_numb))

Output:

Enter some random number = 7
Enter some random number = 0
The Multiplication of { 7 * 0 } using recursion = 0

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.

Python Program to Find the First Small Letter in a Given String

Program to Find the First Small Letter in a Given String

In the previous article, we have discussed Python Program to Find the First Capital Letter in a Given String

Given a string and the task is to find the first small letter in a given string in python.

Examples:

Example1:

Input:

Given String = "GOOD morning Btechgeeks"

Output:

The given String's first small letter =  m

Example2:

Input:

Given String =  "hello this is btechgeeks"

Output:

The given String's first small letter =  h

Program to Find the First Small Letter in a Given String in Python

Below are the ways to find the first small letter in a given string in python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a new empty string and store it in another variable.
  • Calculate the length of the given string using the len() function and store it in another variable.
  • Loop till the length of the given string using the for loop.
  • Check if the character which is present at the iterator value of the given string is greater than or equal to ‘a’ and less than or equal to ‘z’ using the if conditional statement.
  • If the statement is true, then add the character which is present at the iterator value of the given string to the above-initialized new string.
  • Break the statement.
  • If the statement is false, then continue.
  • Print the given string’s first small letter.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gven_str = "GOOD morning Btechgeeks"
# Take a new empty string and store it in another variable.
fstsmall_lettr = ' '
# Calculate the length of the given string using the len() function and store it in
# another variable.
str_lengt = len(gven_str)
# Loop till the length of the given string using the for loop.
for itr in range(str_lengt):
   # Check if the character which is present at the iterator value of the given string
   # is greater than or equal to 'a' and less than or equal to 'z' using the if
   # conditional statement.
    if gven_str[itr] >= 'a' and gven_str[itr] <= 'z':
       # If the statement is true, then add the character which is present at the iterator
       # value of the given string to the above-initialized new string.
        fstsmall_lettr = gven_str[itr]
    # Break the statement.
        break
    else:
      # If the statement is false, then continue.
        continue
# Print the given string's first small letter.
print("The given String's first small letter = ", fstsmall_lettr)

Output:

The given String's first small letter =  m

Method #2: Using For loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a new empty string and store it in another variable.
  • Calculate the length of the given string using the len() function and store it in another variable.
  • Loop till the length of the given string using the for loop.
  • Check if the character which is present at the iterator value of the given string is greater than or equal to ‘a’ and less than or equal to ‘z’ using the if conditional statement.
  • If the statement is true, then add the character which is present at the iterator value of the given string to the above-initialized new string.
  • Break the statement.
  • If the statement is false, then continue.
  • Print the given string’s first small letter.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and 
# store it in a variable.
gven_str = input("Enter some Random String = ")
# Take a new empty string and store it in another variable.
fstsmall_lettr = ' '
# Calculate the length of the given string using the len() function and store it in
# another variable.
str_lengt = len(gven_str)
# Loop till the length of the given string using the for loop.
for itr in range(str_lengt):
   # Check if the character which is present at the iterator value of the given string
   # is greater than or equal to 'a' and less than or equal to 'z' using the if
   # conditional statement.
    if gven_str[itr] >= 'a' and gven_str[itr] <= 'z':
       # If the statement is true, then add the character which is present at the iterator
       # value of the given string to the above-initialized new string.
        fstsmall_lettr = gven_str[itr]
    # Break the statement.
        break
    else:
      # If the statement is false, then continue.
        continue
# Print the given string's first small letter.
print("The given String's first small letter = ", fstsmall_lettr)

Output:

Enter some Random String = hello this is btechgeeks
The given String's first small letter = h

If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.