Duck and Chicken EggsA farmers takes chicken and duck eggs in ‘n’ baskets. In two baskets he has got duck eggs and rest of the bags he has chicken eggs. Number of eggs in each basket is distinct. He tells that number of chicken eggs is twice as that of number of duck eggs. Given the name of the ‘n’ baskets and number of eggs in them, write a code to print the name of baskets with duck eggs and name of basket with chicken eggs. If there can be more than one combination in which duck eggs can be there, consider the first basket name which comes first in the input orderInput FormatFirst line contains the number of baskets, nNext ‘n’ lines contain the name of the baskets and the number of eggs in themOutput FormatPrint name of baskets with duck eggs separated by a space in the first linePrint name of baskets with chicken eggs separated by a space in the next linePrint the name of the baskets in the order of their input and there is no space at the end of the linesExample1Input5B1 5B2 12B3 14B4 23B5 6OutputB3 B5B1 B2 B4
Question
Duck and Chicken EggsA farmers takes chicken and duck eggs in ‘n’ baskets. In two baskets he has got duck eggs and rest of the bags he has chicken eggs. Number of eggs in each basket is distinct. He tells that number of chicken eggs is twice as that of number of duck eggs. Given the name of the ‘n’ baskets and number of eggs in them, write a code to print the name of baskets with duck eggs and name of basket with chicken eggs. If there can be more than one combination in which duck eggs can be there, consider the first basket name which comes first in the input orderInput FormatFirst line contains the number of baskets, nNext ‘n’ lines contain the name of the baskets and the number of eggs in themOutput FormatPrint name of baskets with duck eggs separated by a space in the first linePrint name of baskets with chicken eggs separated by a space in the next linePrint the name of the baskets in the order of their input and there is no space at the end of the linesExample1Input5B1 5B2 12B3 14B4 23B5 6OutputB3 B5B1 B2 B4
Solution
To solve this problem, we need to iterate through the given baskets and check the number of eggs in each basket. We will keep track of the first basket with duck eggs and the first basket with chicken eggs.
Here is the step-by-step solution:
-
Read the number of baskets, n, from the input.
-
Create two empty lists, one for baskets with duck eggs and one for baskets with chicken eggs.
-
Iterate n times using a for loop, reading the name of each basket and the number of eggs in them.
-
Inside the loop, check if the number of eggs in the current basket is twice the number of eggs in the previous basket. If it is, add the current basket to the list of baskets with chicken eggs. Otherwise, add it to the list of baskets with duck eggs.
-
After the loop, print the names of the baskets with duck eggs separated by a space.
-
Print the names of the baskets with chicken eggs separated by a space.
-
Finally, print the names of all the baskets in the order of their input.
Here is the code in Python:
n = int(input())
duck_eggs = []
chicken_eggs = []
for _ in range(n):
basket_name, egg_count = input().split()
egg_count = int(egg_count)
if len(duck_eggs) == 0 or egg_count != 2 * duck_eggs[-1][1]:
duck_eggs.append((basket_name, egg_count))
else:
chicken_eggs.append((basket_name, egg_count))
for basket in duck_eggs:
print(basket[0], end=' ')
print()
for basket in chicken_eggs:
print(basket[0], end=' ')
print()
for basket in duck_eggs + chicken_eggs:
print(basket[0], end=' ')
print()
This code will give the expected output for the given example.
Similar Questions
The Gray family just bought 6 crates of eggs, and each crate had 12 eggs. The family already had 9 eggs in their refrigerator. How many eggs do they have now?
Greta is making fruit baskets. She has 64 apples and 96 oranges. Greta wants to make all the fruit baskets identical without having any pieces of fruit left over. What is the greatest number of fruit baskets Greta can make?
Maggie is making fruit baskets. She has 36 apples and 27 oranges. Maggie wants to make all the fruit baskets identical without having any pieces of fruit left over. What is the greatest number of fruit baskets Maggie can make?
There are N cows living in a cowshed, they all are standing in a line numbered form 1 to N in ascending order. The farmer is in funny mood so he made his mind to do something different, he decides to clean K cows from front and K cows from last and so on. A cow whose name is bessy trying to figure out last turn to get clean will belong to which number of cow, she needs your help to find this.Input Format :Input contains two lines , first line of input contains t - Number of test cases.Next t lines will contains two integers N and K - Number of cows and number of cows farmer is selecting from front and end.Output Format :Print the position of cow for every test case in a new line who will get the last turn.Example:Input:29 325 7Output:615Constraints:1. 1 <= T <= 102. 1 <= K<= N <= 10^5
Mai is arranging 12 cans of food in a row on a shelf. She has 5 cans of peas, 1 can of corn, and 6 cans of beets. In how many distinct orders can the cans be arranged if two cans of the same food are considered identical (not distinct)?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.