- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 sort all vowels at beginning then the consonants, are in sorted order in Python
Suppose we have a lowercase alphabet string s, we have to find a string with all the vowels of s in sorted sequence followed by all consonants of s in sorted sequence.
So, if the input is like "helloworld", then the output will be "eoodhlllrw", as vowels are "eo" And consonants are in sorted order "dhlllrw"
To solve this, we will follow these steps −
- k := blank string, t := blank string
- for each character c in s, do
- if c is a vowel, then
- k := k concatenate c
- otherwise,
- t := t concatenate c
- if c is a vowel, then
- return (k after sort and concatenate t after sorting)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, s): vowels = 'aeiou' k = '' t = '' for c in s: if c in vowels : k = k + c else : t = t + c k = ''.join(sorted(k)) t = ''.join(sorted(t)) return k + t ob = Solution() print(ob.solve("helloworld"))
Input
"helloworld"
Output
eoodhlllrw
Advertisements