Day 18 - Python

Problem Statement: Kani Tray Arrangement

In the Tamil New Year celebration, one of the significant customs is the arrangement of the "Kani" tray. The Kani tray typically contains an assortment of fruits and jewels, arranged in a specific order. You are tasked with creating a program to arrange the Kani tray based on the provided fruits and jewels.

Write a Python program that takes input for the fruits and jewels in the Kani tray and arranges them alternately. The fruits should be sorted alphabetically by their names, and the jewels should be sorted by their values in descending order. The final arrangement should contain fruits and jewels in the order: fruit, jewel, fruit, jewel, and so on.

Input:

The first line of input contains an integer N (1 ≤ N ≤ 10^3), representing the number of fruits in the Kani tray.

Each of the next N lines contains a string fruit and an integer quantity (1 ≤ quantity ≤ 10^3), separated by a space, representing the name and quantity of each fruit.

The next line contains an integer M (1 ≤ M ≤ 10^3), representing the number of jewels in the Kani tray.

Each of the next M lines contains a string jewel and an integer value (1 ≤ value ≤ 10^4), separated by a space, representing the name and value of each jewel.

Output:

Output the arranged items in the Kani tray. Each item should be printed on a separate line, where fruits and jewels are alternately arranged.

Example:

Input:

4

Apple 5

Orange 3

Banana 4

Mango 7

3

GoldNecklace 1000

PearlEarring 5000

RubyBangle 2000

Output:

('Apple', 5)

('PearlEarring', 5000)

('Banana', 4)

('RubyBangle', 2000)

('Mango', 7)

('GoldNecklace', 1000)

('Orange', 3)


SOLUTION:

def arrange_kani_tray(fruits, jewels):

    arranged_kani = []

    sorted_fruits = sorted(fruits.items(), key=lambda x: x[0])

    sorted_jewels = sorted(jewels.items(), key=lambda x: x[1], reverse=True)


    total_items = len(sorted_fruits) + len(sorted_jewels)

    for i in range(total_items):

        if i % 2 == 0 and sorted_fruits:

            arranged_kani.append(sorted_fruits.pop(0))

        elif sorted_jewels:

            arranged_kani.append(sorted_jewels.pop(0))

    return arranged_kani


fruits = {}

num_fruits = int(input())

for _ in range(num_fruits):

    fruit, quantity = input().split()

    fruits[fruit] = int(quantity)


jewels = {}

num_jewels = int(input())

for _ in range(num_jewels):

    jewel, value = input().split()

    jewels[jewel] = int(value)


arranged_kani = arrange_kani_tray(fruits, jewels)

for item in arranged_kani:

    print(item)


Insights:

  • The arrange_kani_tray function sorts the fruits alphabetically by their names and the jewels by their values in descending order. It then alternately arranges fruits and jewels in the Kani tray.
  • Fruits are sorted alphabetically by their names using the sorted function with a custom key function lambda x: x[0], which sorts based on the fruit names.
  • Jewels are sorted by their values in descending order using the sorted function with the reverse=True parameter and a custom key function lambda x: x[1], which sorts based on the jewel values.
  • The total number of items in the Kani tray is calculated by summing the lengths of the sorted fruits and jewels lists.
  • The for loop iterates over the total number of items, and if the index is even and there are still sorted fruits remaining, it appends the next fruit to the arranged Kani tray. Otherwise, it appends the next jewel.
  • The code checks if there are still items remaining in the sorted fruits and jewels lists before attempting to pop an item from them to avoid errors.
  • The code takes input for the number of fruits, their names, and quantities, as well as the number of jewels, their names, and values, using the input function. The quantities of fruits and values of jewels are converted to integers using the int function after splitting the input strings.
  • Fruits and jewels are stored in dictionaries, with the fruit names as keys and their quantities as values, and the jewel names as keys and their values as values.
  • Finally, the arranged items in the Kani tray are printed out one by one using a loop, with each item printed on a separate line.
May this Tamil New Year bring you abundant blessings and opportunities for growth. As you embark on your coding journey, may your knowledge bloom like flowers and bear fruits of success.

Enjoy Coding! :)

இனிய தமிழ் புத்தாண்டு நல்வாழ்த்துகள்! 


Comments

  1. Hi Mam, I enjoyed reading about the Tamil New Year celebration and I noticed that it was actually quite similar to the Malayalam New Year. Our celebration also includes the Kani and all the colorful fruits and vegetables as well as jewelry and money. I am happy that I got to read about the celebration at the top of this post and I also like how you connected it with Python. This post is my most favorite (along with the Birthday post) among the posts I have read through so far. Happy coding! :)

    ReplyDelete
    Replies
    1. Wow so sweet of you!!! Let's explore more in Python with advanced concepts linked with our day to day activities and special moments!! HAPPY CODING!! 💛💛

      Delete

Post a Comment