Count how many vowels and consonants occur in a string: Difference between revisions
Content deleted Content added
julia example |
Python example |
||
Line 172: | Line 172: | ||
contains 22 vowels (5 distinct), 31 consonants (13 distinct), and 16 other |
contains 22 vowels (5 distinct), 31 consonants (13 distinct), and 16 other |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
{{tr|Julia}} |
|||
<lang python>def isvowel(c): |
|||
""" true if c is an English vowel (ignore y) """ |
|||
return c in ['a', 'e', 'i', 'o', 'u', 'A', 'E', "I", 'O', 'U'] |
|||
def isletter(c): |
|||
""" true if in English standard alphabet """ |
|||
return 'a' <= c <= 'z' or 'A' <= c <= 'Z' |
|||
def isconsonant(c): |
|||
""" true if an English consonant """ |
|||
return not isvowel(c) and isletter(c) |
|||
def vccounts(s): |
|||
""" case insensitive vowel counts, total and unique """ |
|||
a = list(s.lower()) |
|||
au = set(a) |
|||
return sum([isvowel(c) for c in a]), sum([isconsonant(c) for c in a]), \ |
|||
sum([isvowel(c) for c in au]), sum([isconsonant(c) for c in au]) |
|||
def testvccount(): |
|||
teststrings = [ |
|||
"Forever Python programming language", |
|||
"Now is the time for all good men to come to the aid of their country."] |
|||
for s in teststrings: |
|||
vcnt, ccnt, vu, cu = vccounts(s) |
|||
print(f"String: {s}\n Vowels: {vcnt} (distinct {vu})\n Consonants: {ccnt} (distinct {cu})\n") |
|||
testvccount() |
|||
</lang>{{out}}<pre> |
|||
String: Forever Python programming language |
|||
Vowels: 11 (distinct 5) |
|||
Consonants: 21 (distinct 11) |
|||
String: Now is the time for all good men to come to the aid of their country. |
|||
Vowels: 22 (distinct 5) |
|||
Consonants: 31 (distinct 13) |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |