Horje
Python program to find the smallest number in a file

Given a  text file, write a Python program to find the smallest number in the given text file.

Examples:

Input: gfg.txt
Output: 9

Explanation: 
Contents of gfg.txt: I live at 624 Hyderabad.
My mobile number is 52367. My favourite number is 9.
Numbers present in the text file are 9,624,52367
Minimum number is 9.

Approach:

  • Create a file object using the open function and pass the filename as parameter.
  • Read the contents in the file using readlines() function.
  • For each line in the file, Using regex find all integers present in that line.
  • Create a result variable to store the final answer.
  • If a number in a line is less than the current result then update the result.
  • Close the file object.
  • Return the result.

Below is the implementation of the above approach.

Python3

# Function to find smallest number in a text file.
import math
import re
 
 
def findMinimumElement(fileName):
 
    # Create a file object using open
    # function and pass filename as parameter.
    file = open(fileName, 'r')
 
    # Split into lines.
    lines_in_file = file.readlines()
 
    # Creating a result
    # variable to store final answer.
    result = math.inf
    for line in lines_in_file:
 
        # For each line in the file,
        # Using regex find all integers
        # present in that line.
        nums = list(map(int, re.findall('[0-9]+', line)))
        for i in nums:
 
            # If a number in a line is less
            # than the current result then
            # update the result.
            result = min(result, i)
 
    file.close()
    return result
 
 
# Creating sample text file for testing
with open("gfg.txt", "w") as file:
    g
    file.write(
        "I live at 624 Hyderabad. \
        My mobile number is 52367. \
        My favourite number is 9.")
 
print('Smallest number in the file is:',
      findMinimumElement('gfg.txt'))

Output:

Smallest number in the file is: 9

Complexity analysis:

Time complexity: O(n)
Auxiliary Space: O(n)

Method : Using sort() method

Python3

# Function to find smallest number in a text file.
import math
import re
def findMinimumElement(fileName):
 
    # Create a file object using open
    # function and pass filename as parameter.
    file = open(fileName, 'r')
 
    # Split into lines.
    lines_in_file = file.readlines()
 
    # Creating a result
    # variable to store final answer.
    result = math.inf
    for line in lines_in_file:
 
        # For each line in the file,
        # Using regex find all integers
        # present in that line.
        nums = list(map(int, re.findall('[0-9]+', line)))
        nums.sort()
        result=nums[0]
    file.close()
    return result
 
 
# Creating sample text file for testing
with open("gfg.txt", "w") as file:
    file.write(
        "I live at 624 Hyderabad. \
        My mobile number is 52367. \
        My favourite number is 9.")
 
print('Smallest number in the file is:',findMinimumElement('gfg.txt'))

Output

Smallest number in the file is: 9

Complexity analysis:

N is the number of words.

Time complexity: O(n)
Auxiliary Space: O(n)




Reffered: https://www.geeksforgeeks.org


Python

Related
Why do we pass __name__ to the Flask class? Why do we pass __name__ to the Flask class?
Change case of all characters in a .txt file using Python Change case of all characters in a .txt file using Python
How to install Python packages with requirements.txt How to install Python packages with requirements.txt
Difference between Numpy array and Numpy matrix Difference between Numpy array and Numpy matrix
Count the occurrence of a certain item in an ndarray - Numpy Count the occurrence of a certain item in an ndarray - Numpy

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13