[go: up one dir, main page]

0% found this document useful (0 votes)
10 views26 pages

Mock 2

The document outlines solutions to four programming problems involving sets, dictionaries, and file handling in Python. Each problem includes a description, a solution, and test cases to validate the functionality. The solutions demonstrate how to manipulate data structures and perform operations like filtering and counting elements.

Uploaded by

publicbhabhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views26 pages

Mock 2

The document outlines solutions to four programming problems involving sets, dictionaries, and file handling in Python. Each problem includes a description, a solution, and test cases to validate the functionality. The solutions demonstrate how to manipulate data structures and perform operations like filtering and counting elements.

Uploaded by

publicbhabhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Mock-2 Solution

Problem-1
Question
Accept two positive integers start and end as input. Construct the following sets in Python:
(1) A is the set of of positive integers from start to end (endpoints inclusive) divisible by 3.
(2) B is the set of of positive integers from start to end (endpoints inclusive) divisible by 5.

Using Python's set notation, find the set of all integers that are:
(1) divisible by 3 or 5, and store it in the variable o1
(2) divisible by both 3 and 5, and store it in the variable o2
(3) divisible by 3 but not divisible by 5, and store it in the variable o3
(4) divisible by 5 but not divisible by 3, and store it in the variable o4

Note that each bullet corresponds to a separate set. You have to accept start and end as input
and create these four sets. You do not have to print the output to the console.

Solution

1 start, end = int(input()), int(input())


2
3 A = set()
4 for i in range(start, end + 1):
5 if i % 3 == 0:
6 A.add(i)
7
8 B = set()
9 for i in range(start, end + 1):
10 if i % 5 == 0:
11 B.add(i)
12
13 o1 = A | B
14 o2 = A & B
15 o3 = A - B
16 o4 = B - A

Suffix

1 print(sorted(o1))
2 print(sorted(o2))
3 print(sorted(o3))
4 print(sorted(o4))
Testcases

Input Output

[3, 5, 6, 9, 10]
1 []
Public
10 [3, 6, 9]
[5, 10]

[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25]


3 [15]
Public
25 [3, 6, 9, 12, 18, 21, 24]
[5, 10, 20, 25]

[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45,
48, 50]
1
Private [15, 30, 45]
50
[3, 6, 9, 12, 18, 21, 24, 27, 33, 36, 39, 42, 48]
[5, 10, 20, 25, 35, 40, 50]

[100, 102, 105, 108, 110, 111, 114, 115, 117, 120]
100 [105, 120]
Private
120 [102, 108, 111, 114, 117]
[100, 110, 115]

[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45,
48, 50, 51, 54, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84, 85, 87,
90, 93, 95, 96, 99, 100]
1
Private [15, 30, 45, 60, 75, 90]
100
[3, 6, 9, 12, 18, 21, 24, 27, 33, 36, 39, 42, 48, 51, 54, 57, 63, 66, 69, 72, 78,
81, 84, 87, 93, 96, 99]
[5, 10, 20, 25, 35, 40, 50, 55, 65, 70, 80, 85, 95, 100]

[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30]
3 [15, 30]
Private
30 [3, 6, 9, 12, 18, 21, 24, 27]
[5, 10, 20, 25]

[10]
10 []
Private
11 []
[10]
Problem-2
Question
Let words be a list of words. You are expected to create a dictionary eng_dict that contains
information about the list of words that begin with a specific letter. Mimic the "English language
dictionary" by sorting every list of words that begins with a given letter.

Convert all the words to lower case for evaluation. Think about the right choice of keys and their
corresponding values.
Accepting input from the user and printing output to the console are not required.

Prefix

1 words = eval(input())

Solution

1 freq = dict()
2 for word in words:
3 if word.lower() not in freq:
4 freq[word.lower()] = 0
5 freq[word.lower()] += 1

Suffix visible

1 for i in sorted(freq):
2 print(f'{i}:{freq[i]}')

Test cases
Input Output

a:1
day:1
Public ['Everyday', 'is', 'a', 'good', 'day'] everyday:1
good:1
is:1
Input Output

after:1
and:3
at:1
building:1
calendar:1
cane:1
class:1
considered:1
delicious:1
difficult:1
discipline:1
dismal:1
eyes:1
fire-eyed:1
freedom:1
get:1
he:2
headmaster:1
his:3
['It', 'was', 'Monday', 'morning', 'Swaminathan', 'was', in:1
'reluctant', 'to', 'open', 'his', 'eyes', 'He', 'considered', 'Monday', into:1
'specially', 'unpleasant', 'in', 'the', 'calendar', 'After', 'the', it:2
'delicious', 'freedom', 'of', 'Saturday', 'And', 'Sunday', 'it', 'was', long:1
Public 'difficult', 'to', 'get', 'into', 'the', 'Monday', 'mood', 'of', 'work', monday:3
'and', 'discipline', 'He', 'shuddered', 'at', 'the', 'very', 'thought', mood:1
'of', 'school', 'the', 'dismal', 'yellow', 'building', 'the', 'fire-eyed', morning:1
'Vedanayagam', 'his', 'class', 'teacher', 'and', 'headmaster', of:3
'with', 'his', 'thin', 'long', 'cane'] open:1
reluctant:1
saturday:1
school:1
shuddered:1
specially:1
sunday:1
swaminathan:1
teacher:1
the:6
thin:1
thought:1
to:2
unpleasant:1
vedanayagam:1
very:1
was:3
with:1
work:1
yellow:1
Input Output

a:1
Private ['a', 'an', 'the'] an:1
the:1

because:3
do:1
excuses:1
for:1
i:1
['I', 'do','not', 'like', 'using', 'because', 'because', 'because', 'is',
Private is:1
'normally', 'used', 'for', 'excuses']
like:1
normally:1
not:1
used:1
using:1

after:1
calendar:1
considered:1
delicious:1
eyes:1
freedom:1
he:1
his:1
in:1
['It', 'was', 'Monday', 'morning', 'Swaminathan', 'was', it:1
'reluctant', 'to', 'open', 'his', 'eyes', 'He', 'considered', 'Monday', monday:2
Private
'specially', 'unpleasant', 'in', 'the', 'calendar', 'After', 'the', morning:1
'delicious', 'freedom', 'of', 'Saturday'] of:1
open:1
reluctant:1
saturday:1
specially:1
swaminathan:1
the:2
to:1
unpleasant:1
was:2
Problem-3
Question
There are n companies and n candidates. Each company has selected exactly one candidate to
intern at its office during the summer of 2022. We call a selection perfect if no two companies
have selected the same candidate. An example of a perfect selection for n = 3 is given below:

selection = {
'company-1': 'candidate-3',
'company-2': 'candidate-1',
'company-3': 'candidate-2'
}

An imperfect matching would be:

selection = {
'company-1': 'candidate-2',
'company-2': 'candidate-1',
'company-3': 'candidate-2'
}

Write a function named is_perfect that accepts the dictionary selection as input. It should
return True if the selection is perfect and False otherwise.

You do not have to accept input from the user or print the output to the console. You just have to
write the function definition.

Solution
Basic solution

1 def is_perfect(selection):
2 n = len(selection)
3 the_set = set()
4 for candidate in selection.values():
5 the_set.add(candidate)
6 uniq = len(the_set)
7 return uniq == n

One-liner

1 def is_perfect(selection):
2 return len(set(selection.values())) == len(selection)

Suffix

1 print(is_perfect(eval(input().strip())))
Test cases

Input Output

{ 'company-1': 'candidate-3', 'company-2': 'candidate-1', 'company-3':


Public True
'candidate-2' }

{ 'company-1': 'candidate-2', 'company-2': 'candidate-1', 'company-3':


Public False
'candidate-2' }

{ 'company-1': 'candidate-3', 'company-2': 'candidate-1', 'company-3':


Private True
'candidate-2' , 'company-4': 'candidate-4'}

{ 'company-1': 'candidate-2', 'company-2': 'candidate-1', 'company-3':


Private False
'candidate-2' , 'company-4': 'candidate-4'}

Private { 'company-1': 'candidate-1', 'company-2': 'candidate-2'} True

Private { 'company-1': 'candidate-2', 'company-2': 'candidate-2'} False


Problem-4
Question
The file file.txt has one sentence on each line. Each sentence is made of certain number of
words. There are no punctuation marks. All words are in lower case. With this information, create
and populate the following variables:

(1) sentence_count -- int -- The number of sentences in the file.


(2) word_count -- int -- Number of words in each sentence.
(3) max_sentence -- str -- Sentence with the maximum number of words in it
(4) word_freq -- dict -- Frequency of occurrence of words in the file.
(5) unique -- list -- List of unique words used in the file.

You do not have to accept input from the user or print output to the console. However, you have
to read the file file.txt to extract the required information.

Prefix

1 f = open('file.txt', 'w')
2 while True:
3 line = input()
4 if line == '':
5 break
6 f.write(line+'\n')
7 f.close()

Solution

1 f = open('file.txt', 'r')
2 sentences = [ ]
3 line = f.readline()
4 while line:
5 words = line.strip().split(' ')
6 sentences.append(words)
7 line = f.readline()
8 sentence_count = len(sentences)
9 word_count = 0
10 max_sentence_words = 0
11 for i in sentences:
12 if len(i) > max_sentence_words:
13 max_sentence = ' '.join(i)
14 for j in i:
15 word_count += 1
16 f.close()
17 word_freq = { }
18 for sentence in sentences:
19 for word in sentence:
20 if word not in word_freq:
21 word_freq[word] = 0
22 word_freq[word] += 1
23 unique = list(word_freq.keys())
Suffix

1 print(eval(input().strip()))

Testcases
Input Output

doris enjoyed tapping


her nails on the table to
annoy everyone
they called out her name
time and again but were
met with nothing but
silence
Public after exploring the 4
abandoned building he
started to believe in
ghosts
he found a leprechaun in
his walnut shell

sentence_count

doris enjoyed tapping


her nails on the table to
annoy everyone
they called out her name
time and again but were
met with nothing but
silence
Public after exploring the 45
abandoned building he
started to believe in
ghosts
he found a leprechaun in
his walnut shell

word_count

doris enjoyed tapping


her nails on the table to
annoy everyone
they called out her name
time and again but were
met with nothing but
silence
they called out her name time and again but were
Public after exploring the
met with nothing but silence
abandoned building he
started to believe in
ghosts
he found a leprechaun in
his walnut shell

max_sentence
Input Output

doris enjoyed tapping


her nails on the table to
annoy everyone
they called out her name [('a', 1), ('abandoned', 1), ('after', 1), ('again', 1),
time and again but were ('and', 1), ('annoy', 1), ('believe', 1), ('building', 1),
met with nothing but ('but', 2), ('called', 1), ('doris', 1), ('enjoyed', 1),
silence ('everyone', 1), ('exploring', 1), ('found', 1), ('ghosts',
Public after exploring the 1), ('he', 2), ('her', 2), ('his', 1), ('in', 2), ('leprechaun',
abandoned building he 1), ('met', 1), ('nails', 1), ('name', 1), ('nothing', 1),
started to believe in ('on', 1), ('out', 1), ('shell', 1), ('silence', 1), ('started',
ghosts 1), ('table', 1), ('tapping', 1), ('the', 2), ('they', 1),
he found a leprechaun in ('time', 1), ('to', 2), ('walnut', 1), ('were', 1), ('with', 1)]
his walnut shell

sorted(word_freq.items())

doris enjoyed tapping


her nails on the table to
annoy everyone
they called out her name
time and again but were
['a', 'abandoned', 'after', 'again', 'and', 'annoy',
met with nothing but
'believe', 'building', 'but', 'called', 'doris', 'enjoyed',
silence
'everyone', 'exploring', 'found', 'ghosts', 'he', 'her',
Public after exploring the
'his', 'in', 'leprechaun', 'met', 'nails', 'name', 'nothing',
abandoned building he
'on', 'out', 'shell', 'silence', 'started', 'table', 'tapping',
started to believe in
'the', 'they', 'time', 'to', 'walnut', 'were', 'with']
ghosts
he found a leprechaun in
his walnut shell

sorted(unique)

Private he turned in the research 17


paper on friday
otherwise he would have
not passed the class
i would have gotten the
promotion but my
attendance was not good
enough
he uses onomatopoeia
as a weapon of mental
destruction
she found it strange that
people use their
cellphones to talk to one
another
the snow covered path
was no help in finding his
way out of the back
country
Input Output

i never knew what


hardship looked like until
it started raining bowling
balls
the heat crowds and
lines made disneyland
the worst place on earth
the fox in the tophat
whispered into the ear of
the rabbit
the truth is that you pay
for your lifestyle in hours
jason did not understand
why his parents would
not let him sell his little
sister at the garage sale
dan ate the clouds like
cotton candy
she had that tint of
craziness in her soul that
made her believe she
could actually make a
difference
the three year old girl
ran down the beach as
the kite flew behind her
getting up at dawn is for
the birds
the overpass went under
the highway
combines are no longer
just for farms
the bees decided to have
a mutiny against their
queen

sentence_count

Private he turned in the research 207


paper on friday
otherwise he would have
not passed the class
i would have gotten the
promotion but my
attendance was not good
enough
he uses onomatopoeia
as a weapon of mental
destruction
she found it strange that
people use their
Input Output

cellphones to talk to one


another
the snow covered path
was no help in finding his
way out of the back
country
i never knew what
hardship looked like until
it started raining bowling
balls
the heat crowds and
lines made disneyland
the worst place on earth
the fox in the tophat
whispered into the ear of
the rabbit
the truth is that you pay
for your lifestyle in hours
jason did not understand
why his parents would
not let him sell his little
sister at the garage sale
dan ate the clouds like
cotton candy
she had that tint of
craziness in her soul that
made her believe she
could actually make a
difference
the three year old girl
ran down the beach as
the kite flew behind her
getting up at dawn is for
the birds
the overpass went under
the highway
combines are no longer
just for farms
the bees decided to have
a mutiny against their
queen

word_count

Private he turned in the research jason did not understand why his parents would
paper on friday not let him sell his little sister at the garage sale
otherwise he would have
not passed the class
i would have gotten the
promotion but my
attendance was not good
Input Output

enough
he uses onomatopoeia
as a weapon of mental
destruction
she found it strange that
people use their
cellphones to talk to one
another
the snow covered path
was no help in finding his
way out of the back
country
i never knew what
hardship looked like until
it started raining bowling
balls
the heat crowds and
lines made disneyland
the worst place on earth
the fox in the tophat
whispered into the ear of
the rabbit
the truth is that you pay
for your lifestyle in hours
jason did not understand
why his parents would
not let him sell his little
sister at the garage sale
dan ate the clouds like
cotton candy
she had that tint of
craziness in her soul that
made her believe she
could actually make
difference
the three year old girl
ran down the beach as
the kite flew behind her
getting up at dawn is for
the birds
the overpass went under
the highway
combines are no longer
just for farms
the bees decided to have
a mutiny against their
queen

max_sentence

Private he turned in the research [('a', 3), ('actually', 1), ('against', 1), ('and', 1),
('another', 1), ('are', 1), ('as', 2), ('at', 2), ('ate', 1),
Input Output
('attendance', 1), ('back', 1), ('balls', 1), ('beach', 1),
paper on friday
('bees', 1), ('behind', 1), ('believe', 1), ('birds', 1),
otherwise he would have
('bowling', 1), ('but', 1), ('candy', 1), ('cellphones', 1),
not passed the class
('class', 1), ('clouds', 1), ('combines', 1), ('cotton', 1),
i would have gotten the
('could', 1), ('country', 1), ('covered', 1), ('craziness',
promotion but my
1), ('crowds', 1), ('dan', 1), ('dawn', 1), ('decided', 1),
attendance was not good
('destruction', 1), ('did', 1), ('difference', 1),
enough
('disneyland', 1), ('down', 1), ('ear', 1), ('earth', 1),
he uses onomatopoeia
('enough', 1), ('farms', 1), ('finding', 1), ('flew', 1),
as a weapon of mental
('for', 3), ('found', 1), ('fox', 1), ('friday', 1), ('garage',
destruction
1), ('getting', 1), ('girl', 1), ('good', 1), ('gotten', 1),
she found it strange that
('had', 1), ('hardship', 1), ('have', 3), ('he', 3), ('heat',
people use their
1), ('help', 1), ('her', 3), ('highway', 1), ('him', 1), ('his',
cellphones to talk to one
3), ('hours', 1), ('i', 2), ('in', 5), ('into', 1), ('is', 2), ('it', 2),
another
('jason', 1), ('just', 1), ('kite', 1), ('knew', 1), ('let', 1),
the snow covered path
('lifestyle', 1), ('like', 2), ('lines', 1), ('little', 1), ('longer',
was no help in finding his
1), ('looked', 1), ('made', 2), ('make', 1), ('mental', 1),
way out of the back
('mutiny', 1), ('my', 1), ('never', 1), ('no', 2), ('not', 4),
country
('of', 4), ('old', 1), ('on', 2), ('one', 1), ('onomatopoeia',
i never knew what
1), ('otherwise', 1), ('out', 1), ('overpass', 1), ('paper',
hardship looked like until
1), ('parents', 1), ('passed', 1), ('path', 1), ('pay', 1),
it started raining bowling
('people', 1), ('place', 1), ('promotion', 1), ('queen', 1),
balls
('rabbit', 1), ('raining', 1), ('ran', 1), ('research', 1),
the heat crowds and
('sale', 1), ('sell', 1), ('she', 3), ('sister', 1), ('snow', 1),
lines made disneyland
('soul', 1), ('started', 1), ('strange', 1), ('talk', 1), ('that',
the worst place on earth
4), ('the', 21), ('their', 2), ('three', 1), ('tint', 1), ('to', 3),
the fox in the tophat
('tophat', 1), ('truth', 1), ('turned', 1), ('under', 1),
whispered into the ear of
('understand', 1), ('until', 1), ('up', 1), ('use', 1), ('uses',
the rabbit
1), ('was', 2), ('way', 1), ('weapon', 1), ('went', 1),
the truth is that you pay
('what', 1), ('whispered', 1), ('why', 1), ('worst', 1),
for your lifestyle in hours
('would', 3), ('year', 1), ('you', 1), ('your', 1)]
jason did not understand
why his parents would
not let him sell his little
sister at the garage sale
dan ate the clouds like
cotton candy
she had that tint of
craziness in her soul that
made her believe she
could actually make a
difference
the three year old girl
ran down the beach as
the kite flew behind her
getting up at dawn is for
the birds
the overpass went under
the highway
combines are no longer
just for farms
Input Output

the bees decided to have


a mutiny against their
queen

sorted(word_freq.items())

Private he turned in the research ['a', 'actually', 'against', 'and', 'another', 'are', 'as', 'at',
paper on friday 'ate', 'attendance', 'back', 'balls', 'beach', 'bees',
otherwise he would have 'behind', 'believe', 'birds', 'bowling', 'but', 'candy',
not passed the class 'cellphones', 'class', 'clouds', 'combines', 'cotton',
i would have gotten the 'could', 'country', 'covered', 'craziness', 'crowds',
promotion but my 'dan', 'dawn', 'decided', 'destruction', 'did',
attendance was not good 'difference', 'disneyland', 'down', 'ear', 'earth',
enough 'enough', 'farms', 'finding', 'flew', 'for', 'found', 'fox',
he uses onomatopoeia 'friday', 'garage', 'getting', 'girl', 'good', 'gotten', 'had',
as a weapon of mental 'hardship', 'have', 'he', 'heat', 'help', 'her', 'highway',
destruction 'him', 'his', 'hours', 'i', 'in', 'into', 'is', 'it', 'jason', 'just',
she found it strange that 'kite', 'knew', 'let', 'lifestyle', 'like', 'lines', 'little',
people use their 'longer', 'looked', 'made', 'make', 'mental', 'mutiny',
cellphones to talk to one 'my', 'never', 'no', 'not', 'of', 'old', 'on', 'one',
another 'onomatopoeia', 'otherwise', 'out', 'overpass',
the snow covered path 'paper', 'parents', 'passed', 'path', 'pay', 'people',
was no help in finding his 'place', 'promotion', 'queen', 'rabbit', 'raining', 'ran',
way out of the back 'research', 'sale', 'sell', 'she', 'sister', 'snow', 'soul',
country 'started', 'strange', 'talk', 'that', 'the', 'their', 'three',
i never knew what 'tint', 'to', 'tophat', 'truth', 'turned', 'under',
hardship looked like until 'understand', 'until', 'up', 'use', 'uses', 'was', 'way',
it started raining bowling 'weapon', 'went', 'what', 'whispered', 'why', 'worst',
balls 'would', 'year', 'you', 'your']
the heat crowds and
lines made disneyland
the worst place on earth
the fox in the tophat
whispered into the ear of
the rabbit
the truth is that you pay
for your lifestyle in hours
jason did not understand
why his parents would
not let him sell his little
sister at the garage sale
dan ate the clouds like
cotton candy
she had that tint of
craziness in her soul that
made her believe she
could actually make a
difference
the three year old girl
ran down the beach as
the kite flew behind her
Input Output

getting up at dawn is for


the birds
the overpass went under
the highway
combines are no longer
just for farms
the bees decided to have
a mutiny against their
queen

sorted(unique)

Private there is a message for 16


you if you look up
she only paints with bold
colors she does not like
pastels
she insisted that cleaning
out your closet was the
key to good driving
jim liked driving around
town with his hazard
lights on
the toddlers endless
tantrum caused the
entire plane anxiety
kevin embraced his
ability to be at the wrong
place at the wrong time
the teenage boy was
accused of breaking his
arm simply to get christal
to sign his cast
i am confused when
people ask me what is up
and i point they groan
for oil spots on the floor
nothing beats parking a
motorbike in the lounge
tomorrow will bring
something new so leave
today as a memory
the campers waited
anxiously as the care
packages arrived at
camp
the three year old girl
ran down the beach as
the kite flew behind her
at that moment he was
not listening to music he
Input Output
was living an experience
he had decided to accept
his fate of accepting his
fate
he dreamed leaving his
law firm to open a
portable dog wash
i liked their first two
albums but changed my
mind after that charity
gig

sentence_count

Private there is a message for 201


you if you look up
she only paints with bold
colors she does not like
pastels
she insisted that cleaning
out your closet was the
key to good driving
jim liked driving around
town with his hazard
lights on
the toddlers endless
tantrum caused the
entire plane anxiety
kevin embraced his
ability to be at the wrong
place at the wrong time
the teenage boy was
accused of breaking his
arm simply to get christal
to sign his cast
i am confused when
people ask me what is up
and i point they groan
for oil spots on the floor
nothing beats parking a
motorbike in the lounge
tomorrow will bring
something new so leave
today as a memory
the campers waited
anxiously as the care
packages arrived at
camp
the three year old girl
ran down the beach as
the kite flew behind her
Input Output

at that moment he was


not listening to music he
was living an experience
he had decided to accept
his fate of accepting his
fate
he dreamed leaving his
law firm to open a
portable dog wash
i liked their first two
albums but changed my
mind after that charity
gig

word_count

Private there is a message for the teenage boy was accused of breaking his arm
you if you look up simply to get christal to sign his cast
she only paints with bold
colors she does not like
pastels
she insisted that cleaning
out your closet was the
key to good driving
jim liked driving around
town with his hazard
lights on
the toddlers endless
tantrum caused the
entire plane anxiety
kevin embraced his
ability to be at the wrong
place at the wrong time
the teenage boy was
accused of breaking his
arm simply to get christal
to sign his cast
i am confused when
people ask me what is up
and i point they groan
for oil spots on the floor
nothing beats parking a
motorbike in the lounge
tomorrow will bring
something new so leave
today as a memory
the campers waited
anxiously as the care
packages arrived at
camp
the three year old girl
Input Output

ran down the beach as


the kite flew behind her
at that moment he was
not listening to music he
was living an experience
he had decided to accept
his fate of accepting his
fate
he dreamed leaving his
law firm to open a
portable dog wash
i liked their first two
albums but changed my
mind after that charity
gig

max_sentence

Private there is a message for [('a', 4), ('ability', 1), ('accept', 1), ('accepting', 1),
you if you look up ('accused', 1), ('after', 1), ('albums', 1), ('am', 1), ('an',
she only paints with bold 1), ('and', 1), ('anxiety', 1), ('anxiously', 1), ('arm', 1),
colors she does not like ('around', 1), ('arrived', 1), ('as', 3), ('ask', 1), ('at', 4),
pastels ('be', 1), ('beach', 1), ('beats', 1), ('behind', 1), ('bold',
she insisted that cleaning 1), ('boy', 1), ('breaking', 1), ('bring', 1), ('but', 1),
out your closet was the ('camp', 1), ('campers', 1), ('care', 1), ('cast', 1),
key to good driving ('caused', 1), ('changed', 1), ('charity', 1), ('christal', 1),
jim liked driving around ('cleaning', 1), ('closet', 1), ('colors', 1), ('confused', 1),
town with his hazard ('decided', 1), ('does', 1), ('dog', 1), ('down', 1),
lights on ('dreamed', 1), ('driving', 2), ('embraced', 1),
the toddlers endless ('endless', 1), ('entire', 1), ('experience', 1), ('fate', 2),
tantrum caused the ('firm', 1), ('first', 1), ('flew', 1), ('floor', 1), ('for', 2),
entire plane anxiety ('get', 1), ('gig', 1), ('girl', 1), ('good', 1), ('groan', 1),
kevin embraced his ('had', 1), ('hazard', 1), ('he', 4), ('her', 1), ('his', 7), ('i',
ability to be at the wrong 3), ('if', 1), ('in', 1), ('insisted', 1), ('is', 2), ('jim', 1),
place at the wrong time ('kevin', 1), ('key', 1), ('kite', 1), ('law', 1), ('leave', 1),
the teenage boy was ('leaving', 1), ('lights', 1), ('like', 1), ('liked', 2),
accused of breaking his ('listening', 1), ('living', 1), ('look', 1), ('lounge', 1),
arm simply to get christal ('me', 1), ('memory', 1), ('message', 1), ('mind', 1),
to sign his cast ('moment', 1), ('motorbike', 1), ('music', 1), ('my', 1),
i am confused when ('new', 1), ('not', 2), ('nothing', 1), ('of', 2), ('oil', 1),
people ask me what is up ('old', 1), ('on', 2), ('only', 1), ('open', 1), ('out', 1),
and i point they groan ('packages', 1), ('paints', 1), ('parking', 1), ('pastels',
for oil spots on the floor 1), ('people', 1), ('place', 1), ('plane', 1), ('point', 1),
nothing beats parking a ('portable', 1), ('ran', 1), ('she', 3), ('sign', 1), ('simply',
motorbike in the lounge 1), ('so', 1), ('something', 1), ('spots', 1), ('tantrum', 1),
tomorrow will bring ('teenage', 1), ('that', 3), ('the', 13), ('their', 1), ('there',
something new so leave 1), ('they', 1), ('three', 1), ('time', 1), ('to', 7), ('today',
today as a memory 1), ('toddlers', 1), ('tomorrow', 1), ('town', 1), ('two',
the campers waited 1), ('up', 2), ('waited', 1), ('was', 4), ('wash', 1), ('what',
anxiously as the care 1), ('when', 1), ('will', 1), ('with', 2), ('wrong', 2), ('year',
packages arrived at 1), ('you', 2), ('your', 1)]
Input Output

camp
the three year old girl
ran down the beach as
the kite flew behind her
at that moment he was
not listening to music he
was living an experience
he had decided to accept
his fate of accepting his
fate
he dreamed leaving his
law firm to open a
portable dog wash
i liked their first two
albums but changed my
mind after that charity
gig

sorted(word_freq.items())

Private there is a message for ['a', 'ability', 'accept', 'accepting', 'accused', 'after',
you if you look up 'albums', 'am', 'an', 'and', 'anxiety', 'anxiously', 'arm',
she only paints with bold 'around', 'arrived', 'as', 'ask', 'at', 'be', 'beach', 'beats',
colors she does not like 'behind', 'bold', 'boy', 'breaking', 'bring', 'but', 'camp',
pastels 'campers', 'care', 'cast', 'caused', 'changed', 'charity',
she insisted that cleaning 'christal', 'cleaning', 'closet', 'colors', 'confused',
out your closet was the 'decided', 'does', 'dog', 'down', 'dreamed', 'driving',
key to good driving 'embraced', 'endless', 'entire', 'experience', 'fate',
jim liked driving around 'firm', 'first', 'flew', 'floor', 'for', 'get', 'gig', 'girl',
town with his hazard 'good', 'groan', 'had', 'hazard', 'he', 'her', 'his', 'i', 'if',
lights on 'in', 'insisted', 'is', 'jim', 'kevin', 'key', 'kite', 'law',
the toddlers endless 'leave', 'leaving', 'lights', 'like', 'liked', 'listening',
tantrum caused the 'living', 'look', 'lounge', 'me', 'memory', 'message',
entire plane anxiety 'mind', 'moment', 'motorbike', 'music', 'my', 'new',
kevin embraced his 'not', 'nothing', 'of', 'oil', 'old', 'on', 'only', 'open', 'out',
ability to be at the wrong 'packages', 'paints', 'parking', 'pastels', 'people',
place at the wrong time 'place', 'plane', 'point', 'portable', 'ran', 'she', 'sign',
the teenage boy was 'simply', 'so', 'something', 'spots', 'tantrum',
accused of breaking his 'teenage', 'that', 'the', 'their', 'there', 'they', 'three',
arm simply to get christal 'time', 'to', 'today', 'toddlers', 'tomorrow', 'town',
to sign his cast 'two', 'up', 'waited', 'was', 'wash', 'what', 'when', 'will',
i am confused when 'with', 'wrong', 'year', 'you', 'your']
people ask me what is up
and i point they groan
for oil spots on the floor
nothing beats parking a
motorbike in the lounge
tomorrow will bring
something new so leave
today as a memory
the campers waited
Input Output

anxiously as the care


packages arrived at
camp
the three year old girl
ran down the beach as
the kite flew behind her
at that moment he was
not listening to music he
was living an experience
he had decided to accept
his fate of accepting his
fate
he dreamed leaving his
law firm to open a
portable dog wash
i liked their first two
albums but changed my
mind after that charity
gig

sorted(unique)
Problem-5
Question
Create a class named Book . It should have the following attributes and methods:
Attributes
(1) name: str
(2) author: str
(3) pages: int
(4) genre: str 'Fiction'/'Nonfiction'

Methods
(1) init(self, name, author, pages, genre) : initialize the attributes with the argument values
(2) is_fiction(self) : returns True if the book is belongs to the fiction category, and False
otherwise
(3) is_nonFiction(self) : returns True if the book belongs to the non-fiction category and
False otherwise
(4) time_to_read(self) :
returns the string 5 days if the number of pages is less than 100
returns the string 20 days if the number of pages lies between 100 and 500 (endpoints
inclusive)
returns the string infinite if the number of pages is greater than 500
(5) same_author(self, book) : returns True if book is written by the same author. book is an
argument of type Book .

You do not have to accept the input from the user or print output to the console

Solution

1 class Book:
2 def __init__(self, name, author, pages, genre):
3 self.name = name
4 self.author = author
5 self.pages = pages
6 self.genre = genre
7 def is_fiction(self):
8 if self.genre == "Fiction":
9 return True
10 else:
11 return False
12 def is_nonFiction(self):
13 if self.genre == "Nonfiction":
14 return True
15 else:
16 return False
17 def time_to_read(self):
18 if self.pages < 100:
19 return "5 days"
20 if 100 <= self.pages <= 500:
21 return "20 days"
22 if self.pages > 500:
23 return "infinite"
24 def same_author(self, book):
25 if self.author == book.author:
26 return True
27 else:
28 return False

Suffix

1 # Please enter your answer above this code


2 inp1 = input().strip().split(',')
3 inp1[2] = int(inp1[2])
4 inp2 = input().strip().split(',')
5 inp2[2] = int(inp2[2])
6
7 book1 = Book(*inp1)
8 book2 = Book(*inp2)
9
10 print(book1.is_fiction(), book1.is_nonFiction(), book1.time_to_read())
11 print(book2.is_fiction(), book2.is_nonFiction(), book2.time_to_read())
12 print(book1.same_author(book2))

Testcases

Input Output

False True 20 days


Igniting Minds,Kalam,178,Nonfiction
Public False True 20 days
Wings of Fire,Kalam,180,Nonfiction
True

True False 20 days


Harry Potter-1,Rowling,309,Fiction
Private True False 20 days
Harry Potter-2,Rowling,341,Fiction
True

False True 20 days


Wings of Fire,Kalam,180,Nonfiction
Private True False 20 days
Harry Potter-2,Rowling,341,Fiction
False

True False 5 days


The Little Prince,Antoine de Saint,96,Fiction
Private True False infinite
Harry Potter-5,Rowling,870,Fiction
False

You might also like