DEV Community

Guru prasanna
Guru prasanna

Posted on

Python day-28 Dictionary, Frequency of character using nested loops

Dictionary-{}

--> Dictionaries are used to store data values in key:value pairs.
--> A dictionary is a collection which is ordered, changeable and do not allow duplicates.
-->In dictionary each element can be accessed by their keys, not through indexing.
-->If dictionary does not contain the key then the output will be 'KeyError'.

Example:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

student = {"name":"raja", "class":5}

print(thisdict)
print(student)
Enter fullscreen mode Exit fullscreen mode

Output:

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
{'name': 'raja', 'class': 5}
Enter fullscreen mode Exit fullscreen mode

Exercises:Find characters in a string using Nested loops
1. Finding frequency of each letter in a string

s = 'guruprasanna'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
        print(key, count)
    j+=1
Enter fullscreen mode Exit fullscreen mode

Output:

g 1
u 2
r 2
p 1
a 3
s 1
n 2
Enter fullscreen mode Exit fullscreen mode

*2. Letters appeared Only Once *

s = 'guruprasanna'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count == 1 and key!='*':
        print(key, count)
    j+=1
Enter fullscreen mode Exit fullscreen mode

Output:

g 1
p 1
s 1
Enter fullscreen mode Exit fullscreen mode

3. Most frequent letter

s = 'guruprasanna'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count != 1 and key!='*':
        print(key, count)
    j+=1
Enter fullscreen mode Exit fullscreen mode

Output:

u 2
r 2
a 3
n 2
Enter fullscreen mode Exit fullscreen mode

4. First Non-repeated letter

s = 'guruprasanna'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count == 1 and key!='*':
        print(key, count)
        break
    j+=1
Enter fullscreen mode Exit fullscreen mode

Output:

g 1
Enter fullscreen mode Exit fullscreen mode

5. First repeated letter

s = 'guruprasanna'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count != 1 and key!='*':
        print(key, count)
        break
    j+=1
Enter fullscreen mode Exit fullscreen mode

6. Last non-repeated letter

last = ' '
last_count = 0 
s = 'guruprasanna'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count == 1 and key!='*':
            last = key
            last_count = count
        #print(key, count)
    j+=1

print(last, last_count)
Enter fullscreen mode Exit fullscreen mode

Output:

s 1
Enter fullscreen mode Exit fullscreen mode

7. Last repeated letter

last = ' '
last_count = 0 
s = 'guruprasanna'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count != 1 and key!='*':
            last = key
            last_count = count
        #print(key, count)
    j+=1

print(last, last_count)
Enter fullscreen mode Exit fullscreen mode

Output:

n 2
Enter fullscreen mode Exit fullscreen mode

8. Most Frequent letter

s = 'guruprasanna'
name = list(s)
j = 0 
last = ' '
last_count = 0 

while j<len(name):
    key = name[j]
    count = 1
    i = j+1
    if key != '*':
        while i<len(name):
            if key == name[i]:
                name[i] = '*'
                count+=1
            i+=1
    if count != 1 and key!='*':
        if count>last_count:
            last = key
            last_count = count
    j+=1

print(last, last_count)
Enter fullscreen mode Exit fullscreen mode

9. Frequency of Vowels (a,e,i,o,u)

vowels = ['a','e','i','o','u']
last = ' '
last_count = 0 
s = 'guruprasanna'
name = list(s)
j = 0 
while j<len(name):
    key = name[j]
    if key in vowels:
        count = 1
        i = j+1
        if key != '*':
            while i<len(name):
                if key == name[i]:
                    name[i] = '*'
                    count+=1
                i+=1
        if key!='*':
            print(key, count)
    j+=1
Enter fullscreen mode Exit fullscreen mode

Output:

u 2
a 3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)