SlideShare a Scribd company logo
1 of 1
Download to read offline
Write a Kotlin program with a checkSorting() generic function. The checkSorting()
function accepts two arguments: an input list and a function that determines the
comparison condition. The function should be able to accept any list belonging to the
Number supertype. The user should be able to specify through the function argument the
condition using which a list will be considered sorted. For example, a list (1,2,3,4,5) is
considered sorted if we are using a comparison condition where each (n-1)th element
should be less than each nth element, otherwise it is considered unsorted. The function
checkSorting() returns True of a list is sorted, otherwise it returns false.
Please note that checkSorting should be written as a pure function, therefore, it should be
recursive and use an internal helper function. Test your program by at least two calls to this
function from main using any two distinct number subtypes of your choice.
Hint: To compare elements of an array, you will need to write two extension functions. An
extension function allows you to add helpful behavior for a type or instance. To declare an
extension function, prefix its name with the type of the data to which you would like to add
the behavior. The following is an example of an extension function show() that we would like
to apply to an Int to show the Ints value.
fun Int.show(): String = "The value of this Int is $this"
1.show()
res1: kotlin.String = The value of this Int is 1
The two extension function you will require are tail() and head(). tail() uses the drop method
for lists which returns a list containing all elements except the first n elements. head() uses
the first method for lists which returns the first element on the list. For more information,
please refer to the following links:
Extension functions: http://developer.android.com
drop(): https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/drop.html
first(): https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html

More Related Content

Similar to Write a Kotlin program with a checkSorting generic functio.pdf

C++ Please make sure that your answer is fully submitted .pdf
C++ Please make sure that your answer is fully submitted .pdfC++ Please make sure that your answer is fully submitted .pdf
C++ Please make sure that your answer is fully submitted .pdf
aashkaahm
 
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdfComplete a C++ class implementation for a linked-list of sorted (asc.pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
shahidqamar17
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
info114
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
jane3dyson92312
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
festockton
 
Linked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdfLinked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdf
adityacomputers001
 

Similar to Write a Kotlin program with a checkSorting generic functio.pdf (20)

Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
C++ Please make sure that your answer is fully submitted .pdf
C++ Please make sure that your answer is fully submitted .pdfC++ Please make sure that your answer is fully submitted .pdf
C++ Please make sure that your answer is fully submitted .pdf
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
Functions
FunctionsFunctions
Functions
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
PE1 Module 3.ppt
PE1 Module 3.pptPE1 Module 3.ppt
PE1 Module 3.ppt
 
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdfComplete a C++ class implementation for a linked-list of sorted (asc.pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
 
Parametricity
ParametricityParametricity
Parametricity
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Vizwik Coding Manual
Vizwik Coding ManualVizwik Coding Manual
Vizwik Coding Manual
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Linked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdfLinked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdf
 

More from vivekemailid

More from vivekemailid (13)

Write a Python Monster class Monster should have the follow.pdf
Write a Python Monster class Monster should have the follow.pdfWrite a Python Monster class Monster should have the follow.pdf
Write a Python Monster class Monster should have the follow.pdf
 
Write a program in Java that will display a letter from fiv.pdf
Write a program in Java that will display a letter  from fiv.pdfWrite a program in Java that will display a letter  from fiv.pdf
Write a program in Java that will display a letter from fiv.pdf
 
Write a program that contains the following functions The i.pdf
Write a program that contains the following functions The i.pdfWrite a program that contains the following functions The i.pdf
Write a program that contains the following functions The i.pdf
 
Write a program named FavoriteWordsjava that prompts user t.pdf
Write a program named FavoriteWordsjava that prompts user t.pdfWrite a program named FavoriteWordsjava that prompts user t.pdf
Write a program named FavoriteWordsjava that prompts user t.pdf
 
Write a program in Python that implements Newtons method to.pdf
Write a program in Python that implements Newtons method to.pdfWrite a program in Python that implements Newtons method to.pdf
Write a program in Python that implements Newtons method to.pdf
 
Write a program called diagnosispy that simulates an expe.pdf
Write a program called diagnosispy that simulates an expe.pdfWrite a program called diagnosispy that simulates an expe.pdf
Write a program called diagnosispy that simulates an expe.pdf
 
Write a program that prints the message Hello my name is .pdf
Write a program that prints the message Hello my name is .pdfWrite a program that prints the message Hello my name is .pdf
Write a program that prints the message Hello my name is .pdf
 
Write a method called recAdd that will add the integer ele.pdf
Write a method called recAdd that will add the integer ele.pdfWrite a method called recAdd that will add the integer ele.pdf
Write a method called recAdd that will add the integer ele.pdf
 
Write a Python function named validInput that displays to .pdf
Write a Python function named validInput that displays to .pdfWrite a Python function named validInput that displays to .pdf
Write a Python function named validInput that displays to .pdf
 
Write a program defines a Person class that has private inst.pdf
Write a program defines a Person class that has private inst.pdfWrite a program defines a Person class that has private inst.pdf
Write a program defines a Person class that has private inst.pdf
 
Write a main method that will request the user to enter Stri.pdf
Write a main method that will request the user to enter Stri.pdfWrite a main method that will request the user to enter Stri.pdf
Write a main method that will request the user to enter Stri.pdf
 
Write a Java program using while loop or do while and swi.pdf
Write a Java program using while  loop or do while and swi.pdfWrite a Java program using while  loop or do while and swi.pdf
Write a Java program using while loop or do while and swi.pdf
 
Write a Java program to accept 10 integers from the user an.pdf
Write a Java program to accept 10 integers from the user an.pdfWrite a Java program to accept 10 integers from the user an.pdf
Write a Java program to accept 10 integers from the user an.pdf
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Recently uploaded (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 

Write a Kotlin program with a checkSorting generic functio.pdf

  • 1. Write a Kotlin program with a checkSorting() generic function. The checkSorting() function accepts two arguments: an input list and a function that determines the comparison condition. The function should be able to accept any list belonging to the Number supertype. The user should be able to specify through the function argument the condition using which a list will be considered sorted. For example, a list (1,2,3,4,5) is considered sorted if we are using a comparison condition where each (n-1)th element should be less than each nth element, otherwise it is considered unsorted. The function checkSorting() returns True of a list is sorted, otherwise it returns false. Please note that checkSorting should be written as a pure function, therefore, it should be recursive and use an internal helper function. Test your program by at least two calls to this function from main using any two distinct number subtypes of your choice. Hint: To compare elements of an array, you will need to write two extension functions. An extension function allows you to add helpful behavior for a type or instance. To declare an extension function, prefix its name with the type of the data to which you would like to add the behavior. The following is an example of an extension function show() that we would like to apply to an Int to show the Ints value. fun Int.show(): String = "The value of this Int is $this" 1.show() res1: kotlin.String = The value of this Int is 1 The two extension function you will require are tail() and head(). tail() uses the drop method for lists which returns a list containing all elements except the first n elements. head() uses the first method for lists which returns the first element on the list. For more information, please refer to the following links: Extension functions: http://developer.android.com drop(): https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/drop.html first(): https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html