GOOD OR BAD
Q. A word is called as a good word if all the letters
of the word are distinct. That is, all the letters of the word are different
from each other letter. Else, the word is called as a bad word.
Write an algorithm and the
subsequent Python code to check if the given word is good or bad.: e.g. START,
GOOD, BETTER are bad: WRONG is good! Make the comparison to be case
insensitive.
Input format:
A word
Output format:
Print ‘Good’ if all letters of
the word are distinct and print ‘bad’ otherwise
Input:
The
Word (word)
Processing:
length = len(word)
count=0
for i in range(0,word):
for j in range(0,length):
if word[i]==word[j]:
count+=1
Output:
Display
whether the word is good or bad
Program:
word = input('')
word = word.lower()
word = word.lower()
length = len(word)
count=0
for i in range(0,length):
for j in range(0,length):
if word[i]==word[j]:
count+=1
if count==len(word):
print('Good')
else:
print('Bad')
Algorithm:
Step1.Read
the word.
Step2.Initialize
i, j and count as 0
Step3.Repeat
till i is less than length
Step3.1
Repeat till j is less than length
Step3.1.1
if the letter at the ith position of word is equal to that of jth position of the word then increase count by 1
Step4.
If count is equal to length display Good else display Bad
Step5.
End
Flowchart:
To edit this flowchart click on this
this code was not executed corrrectly as showing private hidden cases are failed
ReplyDeletei added a line...i.e. word = word.lower() which i forgot but yeah this should solve the problem....as this makes the uppercase as well as the lower case letters to be in lowercase and hence H and h would be considered as the same letters...and the code should now run perfectly....actually i had to develop a logic based on the topics that the teacher teaches in the class...my original code always contains extra concepts which have not been taught and hence sometimes i miss these subtle points...thank you for pointing out the mistake.
Deletewhere can i find more of such codes?
ReplyDeleteYou can find many more questions and solutions for these problems on this blog.
DeleteThank you for visiting the blog