Problem Set
5
Given three words, write an algorithm and the
subsequent Python code to identify the following letters:
1.
Letters common to all the three words
2.
Letters in first two words but not in
third word
3.
Letters in first word but not in
second and third word
4.
Letters in all the three words
For example, if the words are apple, camel, element
then letters in common to all the three words - i, e
Letters in first two words but not in third word –
a
Letters in first word but not in second and third
word - p
Letters in all the three words – a, p, p, l, e, c,
m, n, t
Hint: Use
sets in Python. While reading input, use rstrip() function to remove extra
spaces
Input Format
First line contains word1
Second line contains word2
Third line contains word3
Output Format
List of letters common to all the three words in
lexicographical order
List of letters common to first two words but not
in third word in lexicographical order
List of letters in first word but not in second or
third word in lexicographical order
List of all letters in the three words in
lexicographical order
Input:
word_1,
word_2, word_3
Processing:
common_all
= sorted(set(word_1)&set(word_2)&set(word_3))
common_12
= sorted((set(word_1)&set(word_2))-set(word_3))
common_1
= sorted(set(word_1)-set(word_2)-set(word_3))
all_let
= sorted(set(word_1+word_2+word_3))
Output:
Display
the list of letters common in all the three words in lexicographical order
Display
the list of letters common to the first word but not in the second word in
lexicographical order
Display
the list of letters common in the first word but not in the second and third
word in lexicographical order
Algorithm:
Step1.
Get the 3 words and remove all the whitespace characters at the end of each
string
Step2.
assign common_all as the intersection of all the letters found in the three
words using intersection between the three words
Step3.
assign common_12 as the intersection of all the letters found in word_1 and
word_2 but not in word_3
Step4.
assign common_1 as the letters that are found in only word_1 and not in word_2
and word_3
Step5.
assign all_let as all the letters found in the three words and only one
occurrence of each is to recorded.
Step6.
display common_all, common_12, common_1, all_let in different lines in
lexicographical order
Step7.
end
Program:
word_1 = input().rstrip()
word_2 = input().rstrip()
word_3 = input().rstrip()
common_all = sorted(set(word_1)&set(word_2)&set(word_3))
common_12 = sorted((set(word_1)&set(word_2)) - set(word_3))
common_1 = sorted(set(word_1) - set(word_2) - set(word_3))
all_let = sorted(set(word_1 + word_2 + word_3))
print(common_all, common_12, common_1, all_let, sep='\n')
word_1 = input().rstrip()
word_2 = input().rstrip()
word_3 = input().rstrip()
common_all = sorted(set(word_1)&set(word_2)&set(word_3))
common_12 = sorted((set(word_1)&set(word_2)) - set(word_3))
common_1 = sorted(set(word_1) - set(word_2) - set(word_3))
all_let = sorted(set(word_1 + word_2 + word_3))
print(common_all, common_12, common_1, all_let, sep='\n')
Plz upload the flowcharts
ReplyDeleteWe don't need flowcharts for PPS5 that is what are teacher told us...if many people need it i would try to upload...else you would have to do it on your own...sorry
Delete