Kembali ke Katalog
Back to Catalog
Solved
+15 pts
PYTHON
Python Basics & Strings
Easy
Python 01. Hitung Huruf Vokal dalam Kalimat Python 01. Count Vowels in a String
Hitung jumlah total huruf vokal (a, e, i, o, u) dari sebuah teks tanpa membedakan huruf besar/kecil. Count total occurrences of vowels (a, e, i, o, u) in a string case-insensitively.
Contoh Logika Serupa: Menghitung Karakter Angka dalam String Similar Pattern Example: Count Digits in String
Referensi KodeMenggunakan looping atau list comprehension untuk menyaring karakter tertentu: Filtering characters using Python list comprehension & generator expressions:
def count_digits(text: str) -> int:
# Menghitung karakter angka (0-9)
return sum(1 for char in text if char.isdigit())
Langkah & Spesifikasi Tugas Step-by-Step Requirements
1
Deklarasikan fungsi
def count_vowels(text: str) -> int:.
2
Ubah seluruh teks menjadi huruf kecil dengan method
text.lower().
3
Periksa setiap karakter dan hitung hanya yang termasuk huruf vokal (
a, e, i, o, u).
4
Kembalikan jumlah total kemunculan huruf vokal berupa integer (
int).
1
Define function
def count_vowels(text: str) -> int:.
2
Convert string to lowercase using
text.lower().
3
Iterate through characters and count vowels:
a, e, i, o, u.
4
Return total count as an integer.
Contoh Kasus & Ketentuan Example Cases & Rules
- Input:
"Bagas Praditya"→ Vokal: a, a, a, i, a → Return:5 - Input:
"Python Programming"→ Vokal: o, o, a, i → Return:4 - Input:
"rhythm"→ Vokal: 0 → Return:0
- Input:
"Bagas Praditya"-> Return:5 - Input:
"Python Programming"-> Return:4 - Input:
"rhythm"-> Return:0
Target Output (Expected) Expected Output
5
Petunjuk Pengerjaan:
- Clue 1: Gunakan `text.lower()` untuk mengabaikan perbedaan huruf kapital.
- Clue 2: Manfaatkan list comprehension Python yang elegan: `sum(1 for c in text.lower() if c in "aeiou")`.
def count_vowels(text: str) -> int:
vowels = set('aeiou')
return sum(1 for char in text.lower() if char in vowels)
solution.py
✨ Kode dirapikan!
Test Case #
Target:
Output:
Pesan:
Login Diperlukan
Masuk dengan akun Google untuk mengeksekusi kodingan & query sandbox di server secara interaktif.