SlideShare a Scribd company logo
1 of 2
Download to read offline
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.pdfRITU1ARORA
 
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 .pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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 .pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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 .pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 
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.pdfRITU1ARORA
 

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

male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscapingDr. M. Kumaresan Hort.
 

Recently uploaded (20)

male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 

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)