Kotlin coding test course, Card Sorting

Hello! In this Kotlin coding test course, we will cover an interesting problem called “Card Sorting.” This will be an opportunity to enhance logical thinking and understanding of various data structures while solving algorithmic problems.

Problem Description

The problem is to sort the given cards. Each card consists of the following information:

  • Value: The number on the card (e.g., 1, 2, 3)
  • Color: The color of the card (e.g., Clubs, Diamonds, Hearts, Spades)

Please write a program to sort the given cards by size and color. The size of the card should be sorted in ascending order based on the numbers, and if the numbers are the same, it should be sorted by color. The order of colors is Clubs < Diamonds < Hearts < Spades.

Input Format

The input is provided as an array consisting of several cards. Each card is represented as an element in the array in the Pair(value, color) format. For example, it can be in the form of [Pair(3, 'Hearts'), Pair(2, 'Spades'), Pair(3, 'Diamonds'), Pair(1, 'Clubs'), Pair(2, 'Hearts')].

Output Format

The output is the sorted array of cards. The information of each card should be expressed in the Pair(value, color) format.

Process of Solving the Problem

1. Understanding the Problem

The key point of the problem is to sort the cards. Two criteria are needed to perform the sorting:

  • Value: The number on the card
  • Color: Processed according to the defined order of card types

2. How to Sort the Data?

To define the sorting criteria, it is necessary to map the priorities of colors to numbers. For example, the mapping can be done as follows:

  • Clubs: 1
  • Diamonds: 2
  • Hearts: 3
  • Spades: 4

This allows for easy creation of sorting conditions based on color.

3. Implementing in Kotlin

Now, let’s write the code based on the explanation above. Below is an example implementation of a card sorting program using Kotlin:

data class Card(val value: Int, val color: String)

fun main() {
    val colors = mapOf(
        "Clubs" to 1,
        "Diamonds" to 2,
        "Hearts" to 3,
        "Spades" to 4
    )
    
    val cards = listOf(
        Card(3, "Hearts"),
        Card(2, "Spades"),
        Card(3, "Diamonds"),
        Card(1, "Clubs"),
        Card(2, "Hearts")
    )
    
    val sortedCards = cards.sortedWith(compareBy({ it.value }, { colors[it.color] }))
    
    println("Sorted card list:")
    for (card in sortedCards) {
        println("Value: ${card.value}, Color: ${card.color}")
    }
}

4. Explanation of the Code

Let me explain each component of the code above:

  • data class Card: Defines a data class that includes the value and color of the card.
  • colors map: Creates a map that maps the card colors to integers. This value is referenced during sorting.
  • cards list: Creates a list of the given card list.
  • sortedWith: Sorts the card list by value and color. The compareBy function allows for sorting by multiple criteria.

5. Checking the Results

When you run the program, the output will be as follows:

Sorted card list:
Value: 1, Color: Clubs
Value: 2, Color: Hearts
Value: 2, Color: Spades
Value: 3, Color: Diamonds
Value: 3, Color: Hearts

Conclusion

In this course, we dealt with the card sorting problem, exploring the problem-solving process using sorting algorithms and implementing it in Kotlin code. Understanding various data structures and algorithms is crucial in coding tests, so continued practice will be beneficial. We will cover more interesting problems in the next course. Thank you!