Đếm số lượng nguyên âm trong một chuỗi
Chương trình để đếm số nguyên âm mà chúng tôi sẽ kiểm tra xem có bao nhiêu nguyên âm có trong một chuỗi nhất định. Có năm nguyên âm bằng từ vựng tiếng Anh, chúng là - ’a,’ e, ’i,’ o, ‘u.
Ví dụ: trong chuỗi prepinsta thì trong trường hợp đó, VOWESL là 3 [A, E, I]
Thuật toán
- Bước 1:- Bắt đầu.
- Bước 2:- Lấy đầu vào của người dùng.
- Bước 3:- Khởi tạo biến số đếm.
- Bước 4:- Lặp qua chuỗi để tìm số nguyên âm.
- Bước 5:- Kiểm tra xem bảng chữ cái của chuỗi nằm dưới nhóm nguyên âm.
- Bước 6:- Nếu tăng thực sự số 1.
- Bước 7:- Số lượng in.
- Bước 8:- Kết thúc.
Chương trình Python để đếm số nguyên âm trong một chuỗi
Chạy
#take user input String = input['Enter the string :'] count = 0 #to check for less conditions #keep string in lowercase String = String.lower[] for i in String: if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u': #if True count+=1 #check if any vowel found if count == 0: print['No vowels found'] else: print['Total vowels are :' + str[count]]
Output: Enter the string :PrepInsta Total vowels are :3
Độ phức tạp về thời gian của mã trên trong o [n] như trong mã chúng ta đang lặp lại trên The time complexity of above code in O[n] as in the code we are looping over the sting once
Phương pháp 2
Mục tiêu: Tìm số nguyên âm trong chuỗi Python Find number of vowels in string python
- Trước tiên hãy lấy đầu vào chuỗi trong một biến chuỗi và tạo một hàm số lượng [str, str.length []]
- Nếu giá trị của n đó là [chiều dài của chuỗi] là 1 thì chúng ta sẽ trả về. [Đây là trường hợp cơ sở]
- Nếu kích thước của chuỗi không phải là 1 thì CountVowels [str, n-1] + isVowel [str [n-1]]
- Đây là isvowel [str [n-1]] sẽ được gọi nếu ký tự là nguyên âm thì điều này sẽ trả về 1 nếu không 0
Mục tiêu: Cho một chuỗi bảng chữ cái ‘s. Đếm số lượng nguyên âm trong đó. Given an alphabetical string ‘s’. count the number of vowels in it.
Run
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]
Total numbers of Vowel = 3
NoteRecurs gọi hàm nên ở đây độ phức tạp của mã là o [n] recursively calling the function so here the time complexity of the code is O[n]
Đoạn giới thiệu khóa học chính
Biểu ngữ liên quan
Nhận prepinsta Prime & có được quyền truy cập vào tất cả hơn 200 khóa học được cung cấp bởi prepinsta trong một đăng ký
Output: Enter the string :PrepInsta Total vowels are :31
5 ['e', 'e', 'o', 'e', 'e']1
Output: Enter the string :PrepInsta Total vowels are :33
5 ['e', 'e', 'o', 'e', 'e']3
5 ['e', 'e', 'o', 'e', 'e']4
5 ['e', 'e', 'o', 'e', 'e']5
Examples:
In a simple way Input : Geeks for Geeks Output : 5 ['e', 'e', 'o', 'e', 'e'] This is in a different way Input : Geeks for Geeks Output : {'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}
Output:
Enter the string :PrepInsta
Total vowels are :3
15
['e', 'e', 'o', 'e', 'e']
6 def
3
Làm thế nào để bạn tìm thấy số lượng nguyên âm?
Python3
def
Output: Enter the string :PrepInsta Total vowels are :30
Output: Enter the string :PrepInsta Total vowels are :31
Output: Enter the string :PrepInsta Total vowels are :32
Output: Enter the string :PrepInsta Total vowels are :33
Output: Enter the string :PrepInsta Total vowels are :34
Output: Enter the string :PrepInsta Total vowels are :35
Output: Enter the string :PrepInsta Total vowels are :36
Output: Enter the string :PrepInsta Total vowels are :37
Output: Enter the string :PrepInsta Total vowels are :38__
Output: Enter the string :PrepInsta Total vowels are :31
Run4
Run5
Run6
Run7
Output: Enter the string :PrepInsta Total vowels are :31
Run4
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]0
Output: Enter the string :PrepInsta Total vowels are :38
Output: Enter the string :PrepInsta Total vowels are :33
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]3
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]4
Output: Enter the string :PrepInsta Total vowels are :33
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]6
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]7
Output:
5 ['e', 'e', 'o', 'e', 'e']
Đếm nguyên âm: Từ điển cách
Điều này cũng thực hiện cùng một nhiệm vụ nhưng theo một cách khác. Trong phương pháp này, chúng tôi tạo thành một từ điển với các nguyên âm và tăng chúng khi gặp nguyên âm. Trong phương pháp này, chúng tôi sử dụng phương thức Fold Case để bỏ qua các trường hợp, theo đó chúng tôi tạo thành một từ điển nguyên âm với khóa làm nguyên âm. Đây là một cách tốt hơn và hiệu quả để kiểm tra và tìm số lượng của mỗi nguyên âm có trong một chuỗi. & Nbsp; & nbsp;
Python3
def
Output: Enter the string :PrepInsta Total vowels are :30
Output: Enter the string :PrepInsta Total vowels are :31
Output: Enter the string :PrepInsta Total vowels are :38
Output: Enter the string :PrepInsta Total vowels are :33
Total numbers of Vowel = 33
Output: Enter the string :PrepInsta Total vowels are :31
Total numbers of Vowel = 35
Output: Enter the string :PrepInsta Total vowels are :33
Total numbers of Vowel = 37
Total numbers of Vowel = 38
Total numbers of Vowel = 39
Output: Enter the string :PrepInsta Total vowels are :31
Output: Enter the string :PrepInsta Total vowels are :35
In a simple way Input : Geeks for Geeks Output : 5 ['e', 'e', 'o', 'e', 'e'] This is in a different way Input : Geeks for Geeks Output : {'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}2
Output: Enter the string :PrepInsta Total vowels are :37
In a simple way Input : Geeks for Geeks Output : 5 ['e', 'e', 'o', 'e', 'e'] This is in a different way Input : Geeks for Geeks Output : {'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}4
In a simple way Input : Geeks for Geeks Output : 5 ['e', 'e', 'o', 'e', 'e'] This is in a different way Input : Geeks for Geeks Output : {'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}5
Output: Enter the string :PrepInsta Total vowels are :39
In a simple way Input : Geeks for Geeks Output : 5 ['e', 'e', 'o', 'e', 'e'] This is in a different way Input : Geeks for Geeks Output : {'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}22____17
In a simple way Input : Geeks for Geeks Output : 5 ['e', 'e', 'o', 'e', 'e'] This is in a different way Input : Geeks for Geeks Output : {'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}9
5 ['e', 'e', 'o', 'e', 'e']0
5 ['e', 'e', 'o', 'e', 'e']1
5 ['e', 'e', 'o', 'e', 'e']2
Output: Enter the string :PrepInsta Total vowels are :33 ________ 64 & nbsp; & nbsp; & nbsp;
Output: Enter the string :PrepInsta Total vowels are :31
5 ['e', 'e', 'o', 'e', 'e']6
5 ['e', 'e', 'o', 'e', 'e']7
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]4
Output: Enter the string :PrepInsta Total vowels are :33
{'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}0
Output: Enter the string :PrepInsta Total vowels are :38
Output: Enter the string :PrepInsta Total vowels are :33
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]3
Run4
{'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}5
Output:
{'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}
& nbsp; Đếm nguyên âm: Regex Way & nbsp;Counting vowels: regex way
Chúng tôi cũng có thể sử dụng phương pháp này để thực hiện nhiệm vụ này. Chúng ta có thể sử dụng biểu thức chính quy để thực hiện nhiệm vụ này. Chúng tôi sử dụng phương thức re.findall [] để tìm tất cả các nguyên âm trong danh sách tạo chuỗi với chúng. Chúng tôi sử dụng Len trên danh sách để tìm tổng nguyên âm trong chuỗi. & NBSP;
Python3
{'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}6
{'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}7
def
Output: Enter the string :PrepInsta Total vowels are :30
Output: Enter the string :PrepInsta Total vowels are :31
5 ['e', 'e', 'o', 'e', 'e']1
Output: Enter the string :PrepInsta Total vowels are :33
5 ['e', 'e', 'o', 'e', 'e']3
5 ['e', 'e', 'o', 'e', 'e']4
5 ['e', 'e', 'o', 'e', 'e']5
Output: Enter the string :PrepInsta Total vowels are :31
Run4
Run5
Run6
def
0Output: Enter the string :PrepInsta Total vowels are :31
5 ['e', 'e', 'o', 'e', 'e']6
def
3# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]4
Output: Enter the string :PrepInsta Total vowels are :33
{'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}0
Output: Enter the string :PrepInsta Total vowels are :38
Output: Enter the string :PrepInsta Total vowels are :33
# Function to check the Vowel def isVowel[ch]: return ch.upper[] in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels[str, n]: if [n == 1]: return isVowel[str[n - 1]]; return [countVovels[str, n - 1] + isVowel[str[n - 1]]]; # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print["Total numbers of Vowel =",countVovels[str, len[str]]]3
Run4
{'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}5
Output:
5 ['e', 'e', 'o', 'e', 'e']