
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find second largest digit in a string using Python
Suppose we have an alphanumeric string s, we have to find the second largest numerical digit that appears in s, if there is no such string then return -1.
So, if the input is like s = "p84t3ho1n", then the output will be 4 as the digits are [1,3,4,8], so second largest digit is 4.
To solve this, we will follow these steps −
lst := a new set
for each let in s, do
if let is not alphabetic, then
insert let as integer in lst
if size of lst <= 1, then
return -1
return the second last element after sorting lst
Let us see the following implementation to get better understanding −
Example
def solve(s): lst = set() for let in s: if not let.isalpha(): lst.add(int(let)) if len(lst) <= 1: return -1 return sorted(list(lst))[len(lst) - 2] s = "p84t3ho1n" print(solve(s))
Input
"hello", "hlelo"
Output
True
- Related Questions & Answers
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python program to find the second largest number in a list
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Python Program to Find the Second Largest Number in a List Using Bubble Sort
- Program to find largest perimeter triangle using Python
- C program to find frequency of each digit in a string
- Python program to find largest number in a list
- C program to find the second largest and smallest numbers in an array
- Program to find super digit of a number in Python
- Python Program to Find the Largest value in a Tree using Inorder Traversal
- Find the Second Largest Element in a Linked List in C++
- Python program to find the largest number in a list
- Program to find Smallest and Largest Word in a String in C++
- Get second largest marks from a MySQL table using subquery?
Advertisements