Program to check old and new version numbering are correct or not in Python

PythonServer Side ProgrammingProgramming

Suppose we have a strings older and another string newer. These two are representing software package versions in the format "major.minor.patch", we have to check whether the newer version is actually newer than the older one.

So, if the input is like older = "7.2.2", newer = "7.3.1", then the output will be True

To solve this, we will follow these steps −

  • older := a list of major, minor, patch code of older
  • newer:= a list of major, minor, patch code of newer
  • for i in range the size of list older, do
  • := older[i], n := newer[i]
    • if n > o, then
      • return True
    • otherwise when n < o, then
      • return False
  • return False

Let us see the following implementation to get better understanding −


 Live Demo

class Solution:
   def solve(self, older, newer):
      older = older.split('.')
      for o, n in zip(older, newer):
         if int(n)>int(o):
            return True
         elif int(n)<int(o):
            return False
      return False
ob = Solution()
older = "7.2.2"
newer = "7.3.1"
print(ob.solve(older, newer))


"7.2.2", "7.3.1"


Published on 06-Oct-2020 06:57:56