Problem Set 4(Longest Name)


Longest Name:
Q.Write an algorithm and the subsequent Python program to store the names of your friends, count the number of friends, identify the longest name and the number of letters in the longest name. Get the names of friends till 'stop' is entered. For example, if the input sequence is Ravi, Raj, Puela, stop,  then the output is 3, Puela and 5.
When you are coding in the online judge (SkillRack), use rstrip() function to remove carriage return from the input string.
Input Format:
First line is the name of the first friend
Second line is the name of the second friend
Third line is the name of the third friend
….
stop
Output Format:
Number of friends
Friend’s name with longest length
Number of characters in that longest name

Input:
 Names of friends

Processing:
count = 0
max_length = 0
while True:
    count += 1
    temp = len(name)
    if temp > max_length:
        max_length = temp
        max_name = name

Output:
The Number of friends(count)
The Longest Name(max_name)
The maximum number of characters in longest name(max_length)
 
Program:
count = 0
max_length = 0
max_name = ''
while True:
    name = input()
    name = name.rstrip()
    if name == 'Stop':
        break
    count += 1
    temp = len(name)
    if temp > max_length:
        max_length = temp
        max_name = name

print(count,max_name,max_length,sep = '\n')


Algorithm:
Step1. Initialize count, max_length as 0 and max_name as en empty string.
Step2. Repeat till the loop is not broken.
Step2.1 Get the name from the user and strip the rightmost carriage return.
Step2.2 If the name is equal to Stop then break out of the loop
Step2.3 Increment count by 1
Step2.4 Assign temp as the length of the name
Step2.5 If temp is greater than max_length then assign max_length as temp and max_name as the name
Step3. Display count, max_name, max, max_length

Step4. End

Flowchart:
To edit this flowchart click on this

No comments:

Post a Comment

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...