Featured Post

SQL Interview Success: Unlocking the Top 5 Frequently Asked Queries

Image
 Here are the five top commonly asked SQL queries in the interviews. These you can expect in Data Analyst, or, Data Engineer interviews. Top SQL Queries for Interviews 01. Joins The commonly asked question pertains to providing two tables, determining the number of rows that will return on various join types, and the resultant. Table1 -------- id ---- 1 1 2 3 Table2 -------- id ---- 1 3 1 NULL Output ------- Inner join --------------- 5 rows will return The result will be: =============== 1  1 1   1 1   1 1    1 3    3 02. Substring and Concat Here, we need to write an SQL query to make the upper case of the first letter and the small case of the remaining letter. Table1 ------ ename ===== raJu venKat kRIshna Solution: ========== SELECT CONCAT(UPPER(SUBSTRING(name, 1, 1)), LOWER(SUBSTRING(name, 2))) AS capitalized_name FROM Table1; 03. Case statement SQL Query ========= SELECT Code1, Code2,      CASE         WHEN Code1 = 'A' AND Code2 = 'AA' THEN "A" | "A

How to Count Vowels in Input Quickly

Here you'll know how python counts characters in the input. This is an interview question asked recently.

How to Count Vowels in Input Quickly


How to Count Vowels in Input String


Below is the Interview Question

Vowels = ('aeiou')

You need to check any vowels are present in the input string. Vowels means 'a', 'e', 'i', 'o', 'u'. The below function checks each character of input and compares it with the Vowels.

3 Steps to Count Vowels in Input


You can achieve this in three steps.

1. Create a function called 'my_vowel' (the name up to you)
2. Use Set & Intersection Method
3. Run the function

1. Created 'my_vowel' Function


#!usr/bin/python
#This function finds vowel counts
#welcome to my function
""" Optional description """
def my_vowel(a):
    vowels=set('aeiou')
    a_1= vowels.intersection(set(a))
# initialized the dictionary
    found = {'a' : 0, 'e' : 0, 'i' : 0, 'o' :, 'u' : 0}
    for indx in a_1:
        if indx in vowels:
            found[indx] += 1
     for k, v sorted(found.items():
        print(k,v)






I used vim editor to create a .py module with the name vowel.py. Below is the actual code.


Logic to count vowels in input


2. Used Set & Intersection Methods

- Set method converts the input string as a set (you aware about this in mathes)

- Intersect method extracts each character and compares it with a Dictionary (that I'have already defined).

- I used the IF condition to work this logic correctly and validate each character.


3. Result

I have imported the .py module to Python3. Then, I have supplied an input string. It is then validated and given the counts.

The result says, in the given string the vowels are like this:
a -> 0
e -> 1
i -> 0
o -> 1
u -> 0

using function counting of vowels


Bottom-line

  • As expected in the result we got counts for matching vowels.
  • It's very tricky and asked in most of the interviews
  • Test your function with various strings and improve your knowledge.

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

Explained Ideal Structure of Python Class

How to Check Kafka Available Brokers