SlideShare a Scribd company logo
RAREST Method
Write a method rarest that accepts a map whose keys are strings and whose values are integers as
a parameter and returns the integer value that occurs the fewest times in the map. If there is a tie,
return the smaller integer value. If the map is empty, throw an exception.
For example, suppose the map contains mappings from students' names (strings) to their ages
(integers). Your method would return the least frequently occurring age. Consider a map variable
m containing the following key/value pairs:
Three people are age 20 (Jeff, Kasey, and Kim), two people are age 22 (Alyssa and Stef), and
four people are age 25 (Char, Dan, Mogran, and Ryan). So a call of rarest(m) returns 22 because
only two people are that age.
If there is a tie (two or more rarest ages that occur the same number of times), return the
youngest age among them. For example, if we added another pair of Kelly=22 to the map above,
there would now be a tie of three people of age 20 (Jeff, Kasey, Kim) and three people of age 22
(Alyssa, Kelly, Stef). So a call of rarest(m) would now return 20 because 20 is the smaller of the
rarest values.
Solution
Rarest Method:
Algorithm:
Input: Key value pairs as a dictionary
Expected output: Least frequent value from key value pairs
We see that '22' has occurred twice, and '25' four times. Now we have to find it using out
algorithm:
Now, we have the following values in 'min_age' and 'frequency' variables:
min_age = 22
frequency = 2
If we add kelly to the initial key value pair, we will get the min_age as 20 and frequency as 3 as
expected.
The following python code can be referred to understand the algorithm:
def rarest(d):
age_count = {}
for key,value in d.items():
if value in age_count:
age_count[value]+=1
else:
age_count[value]=1
min_age=0
freq=0
for key,value in age_count.items():
if freq==0:
freq=value
min_age=key
elif freq>value:
freq=value
min_age=key
elif freq==value:
if min_age>key:
min_age=key
return min_age,freq,frd
d={'Alyssa':22, 'Char':25, 'Dan':25, 'Jeff':20, 'Kasey':20, 'Kim':20, 'Mogran':25,
'Ryan':25, 'Stef':22}
r = rarest(d)
print(r)

More Related Content

More from RITU1ARORA

Compare and contrast the physical characteristic of protozoans, yeas.pdf
Compare and contrast the physical characteristic of protozoans, yeas.pdfCompare and contrast the physical characteristic of protozoans, yeas.pdf
Compare and contrast the physical characteristic of protozoans, yeas.pdf
RITU1ARORA
 
Which statement correctly saves the number of items in the variable .pdf
Which statement correctly saves the number of items in the variable .pdfWhich statement correctly saves the number of items in the variable .pdf
Which statement correctly saves the number of items in the variable .pdf
RITU1ARORA
 
What does the fossil record of Basilosaurid whales tell us about wha.pdf
What does the fossil record of Basilosaurid whales tell us about wha.pdfWhat does the fossil record of Basilosaurid whales tell us about wha.pdf
What does the fossil record of Basilosaurid whales tell us about wha.pdf
RITU1ARORA
 
Three decision makers have assessed utilities for the following deci.pdf
Three decision makers have assessed utilities for the following deci.pdfThree decision makers have assessed utilities for the following deci.pdf
Three decision makers have assessed utilities for the following deci.pdf
RITU1ARORA
 
Think about our economy. Irrespective of economic conditions, we con.pdf
Think about our economy. Irrespective of economic conditions, we con.pdfThink about our economy. Irrespective of economic conditions, we con.pdf
Think about our economy. Irrespective of economic conditions, we con.pdf
RITU1ARORA
 
The hardenability curves of several iron alloys are shown in the fig.pdf
The hardenability curves of several iron alloys are shown in the fig.pdfThe hardenability curves of several iron alloys are shown in the fig.pdf
The hardenability curves of several iron alloys are shown in the fig.pdf
RITU1ARORA
 
The de Broglie relationships are both valid for bloch electrons. Tur.pdf
The de Broglie relationships are both valid for bloch electrons. Tur.pdfThe de Broglie relationships are both valid for bloch electrons. Tur.pdf
The de Broglie relationships are both valid for bloch electrons. Tur.pdf
RITU1ARORA
 
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdfTextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
RITU1ARORA
 
Based on taxonomical classification, to which of the following organi.pdf
Based on taxonomical classification, to which of the following organi.pdfBased on taxonomical classification, to which of the following organi.pdf
Based on taxonomical classification, to which of the following organi.pdf
RITU1ARORA
 
Answer choices are Internalized Principle, Societal Expectations, S.pdf
Answer choices are Internalized Principle, Societal Expectations, S.pdfAnswer choices are Internalized Principle, Societal Expectations, S.pdf
Answer choices are Internalized Principle, Societal Expectations, S.pdf
RITU1ARORA
 
An environment would be considered harsh to an organism if it would.pdf
An environment would be considered harsh to an organism if  it would.pdfAn environment would be considered harsh to an organism if  it would.pdf
An environment would be considered harsh to an organism if it would.pdf
RITU1ARORA
 
A committee of three people is to be randomly selected from a group .pdf
A committee of three people is to be randomly selected from a group .pdfA committee of three people is to be randomly selected from a group .pdf
A committee of three people is to be randomly selected from a group .pdf
RITU1ARORA
 
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdfA man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
RITU1ARORA
 
5) What is the dependent variable measured in the BOD test (Explain .pdf
5) What is the dependent variable measured in the BOD test (Explain .pdf5) What is the dependent variable measured in the BOD test (Explain .pdf
5) What is the dependent variable measured in the BOD test (Explain .pdf
RITU1ARORA
 
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
RITU1ARORA
 
1. Which of the following is a characteristic of specific to corpora.pdf
1. Which of the following is a characteristic of specific to corpora.pdf1. Which of the following is a characteristic of specific to corpora.pdf
1. Which of the following is a characteristic of specific to corpora.pdf
RITU1ARORA
 
Nosocomial infectionsare acquired at various social events.are a.pdf
Nosocomial infectionsare acquired at various social events.are a.pdfNosocomial infectionsare acquired at various social events.are a.pdf
Nosocomial infectionsare acquired at various social events.are a.pdf
RITU1ARORA
 
Mitochondria were most likely acquire earlier than plastids in the e.pdf
Mitochondria were most likely acquire earlier than plastids in the e.pdfMitochondria were most likely acquire earlier than plastids in the e.pdf
Mitochondria were most likely acquire earlier than plastids in the e.pdf
RITU1ARORA
 
1. Martin would say that in a society with ample protection for whis.pdf
1. Martin would say that in a society with ample protection for whis.pdf1. Martin would say that in a society with ample protection for whis.pdf
1. Martin would say that in a society with ample protection for whis.pdf
RITU1ARORA
 
MULTIPLE CHOICE CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
MULTIPLE CHOICE  CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdfMULTIPLE CHOICE  CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
MULTIPLE CHOICE CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
RITU1ARORA
 

More from RITU1ARORA (20)

Compare and contrast the physical characteristic of protozoans, yeas.pdf
Compare and contrast the physical characteristic of protozoans, yeas.pdfCompare and contrast the physical characteristic of protozoans, yeas.pdf
Compare and contrast the physical characteristic of protozoans, yeas.pdf
 
Which statement correctly saves the number of items in the variable .pdf
Which statement correctly saves the number of items in the variable .pdfWhich statement correctly saves the number of items in the variable .pdf
Which statement correctly saves the number of items in the variable .pdf
 
What does the fossil record of Basilosaurid whales tell us about wha.pdf
What does the fossil record of Basilosaurid whales tell us about wha.pdfWhat does the fossil record of Basilosaurid whales tell us about wha.pdf
What does the fossil record of Basilosaurid whales tell us about wha.pdf
 
Three decision makers have assessed utilities for the following deci.pdf
Three decision makers have assessed utilities for the following deci.pdfThree decision makers have assessed utilities for the following deci.pdf
Three decision makers have assessed utilities for the following deci.pdf
 
Think about our economy. Irrespective of economic conditions, we con.pdf
Think about our economy. Irrespective of economic conditions, we con.pdfThink about our economy. Irrespective of economic conditions, we con.pdf
Think about our economy. Irrespective of economic conditions, we con.pdf
 
The hardenability curves of several iron alloys are shown in the fig.pdf
The hardenability curves of several iron alloys are shown in the fig.pdfThe hardenability curves of several iron alloys are shown in the fig.pdf
The hardenability curves of several iron alloys are shown in the fig.pdf
 
The de Broglie relationships are both valid for bloch electrons. Tur.pdf
The de Broglie relationships are both valid for bloch electrons. Tur.pdfThe de Broglie relationships are both valid for bloch electrons. Tur.pdf
The de Broglie relationships are both valid for bloch electrons. Tur.pdf
 
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdfTextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
 
Based on taxonomical classification, to which of the following organi.pdf
Based on taxonomical classification, to which of the following organi.pdfBased on taxonomical classification, to which of the following organi.pdf
Based on taxonomical classification, to which of the following organi.pdf
 
Answer choices are Internalized Principle, Societal Expectations, S.pdf
Answer choices are Internalized Principle, Societal Expectations, S.pdfAnswer choices are Internalized Principle, Societal Expectations, S.pdf
Answer choices are Internalized Principle, Societal Expectations, S.pdf
 
An environment would be considered harsh to an organism if it would.pdf
An environment would be considered harsh to an organism if  it would.pdfAn environment would be considered harsh to an organism if  it would.pdf
An environment would be considered harsh to an organism if it would.pdf
 
A committee of three people is to be randomly selected from a group .pdf
A committee of three people is to be randomly selected from a group .pdfA committee of three people is to be randomly selected from a group .pdf
A committee of three people is to be randomly selected from a group .pdf
 
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdfA man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
 
5) What is the dependent variable measured in the BOD test (Explain .pdf
5) What is the dependent variable measured in the BOD test (Explain .pdf5) What is the dependent variable measured in the BOD test (Explain .pdf
5) What is the dependent variable measured in the BOD test (Explain .pdf
 
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
 
1. Which of the following is a characteristic of specific to corpora.pdf
1. Which of the following is a characteristic of specific to corpora.pdf1. Which of the following is a characteristic of specific to corpora.pdf
1. Which of the following is a characteristic of specific to corpora.pdf
 
Nosocomial infectionsare acquired at various social events.are a.pdf
Nosocomial infectionsare acquired at various social events.are a.pdfNosocomial infectionsare acquired at various social events.are a.pdf
Nosocomial infectionsare acquired at various social events.are a.pdf
 
Mitochondria were most likely acquire earlier than plastids in the e.pdf
Mitochondria were most likely acquire earlier than plastids in the e.pdfMitochondria were most likely acquire earlier than plastids in the e.pdf
Mitochondria were most likely acquire earlier than plastids in the e.pdf
 
1. Martin would say that in a society with ample protection for whis.pdf
1. Martin would say that in a society with ample protection for whis.pdf1. Martin would say that in a society with ample protection for whis.pdf
1. Martin would say that in a society with ample protection for whis.pdf
 
MULTIPLE CHOICE CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
MULTIPLE CHOICE  CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdfMULTIPLE CHOICE  CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
MULTIPLE CHOICE CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
 

Recently uploaded

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 

Recently uploaded (20)

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 

RAREST MethodWrite a method rarest that accepts a map whose keys a.pdf

  • 1. RAREST Method Write a method rarest that accepts a map whose keys are strings and whose values are integers as a parameter and returns the integer value that occurs the fewest times in the map. If there is a tie, return the smaller integer value. If the map is empty, throw an exception. For example, suppose the map contains mappings from students' names (strings) to their ages (integers). Your method would return the least frequently occurring age. Consider a map variable m containing the following key/value pairs: Three people are age 20 (Jeff, Kasey, and Kim), two people are age 22 (Alyssa and Stef), and four people are age 25 (Char, Dan, Mogran, and Ryan). So a call of rarest(m) returns 22 because only two people are that age. If there is a tie (two or more rarest ages that occur the same number of times), return the youngest age among them. For example, if we added another pair of Kelly=22 to the map above, there would now be a tie of three people of age 20 (Jeff, Kasey, Kim) and three people of age 22 (Alyssa, Kelly, Stef). So a call of rarest(m) would now return 20 because 20 is the smaller of the rarest values. Solution Rarest Method: Algorithm: Input: Key value pairs as a dictionary Expected output: Least frequent value from key value pairs We see that '22' has occurred twice, and '25' four times. Now we have to find it using out algorithm: Now, we have the following values in 'min_age' and 'frequency' variables: min_age = 22 frequency = 2 If we add kelly to the initial key value pair, we will get the min_age as 20 and frequency as 3 as expected. The following python code can be referred to understand the algorithm: def rarest(d): age_count = {} for key,value in d.items():
  • 2. if value in age_count: age_count[value]+=1 else: age_count[value]=1 min_age=0 freq=0 for key,value in age_count.items(): if freq==0: freq=value min_age=key elif freq>value: freq=value min_age=key elif freq==value: if min_age>key: min_age=key return min_age,freq,frd d={'Alyssa':22, 'Char':25, 'Dan':25, 'Jeff':20, 'Kasey':20, 'Kim':20, 'Mogran':25, 'Ryan':25, 'Stef':22} r = rarest(d) print(r)