Programming
Programming
Instructions to candidates:
1. Read the problem statement, examples and other details carefully and implement the
solution
2. Code submitted with compilation errors may not get evaluated
3. DO NOT write code that result in infinite loops / infinite recursive calls
NOTE: NOT adhering to the above instructions may lead to reduction in score.
Problem Statement:
Write a Python program which takes a list of single digit integers as input parameter. List may
contain values ranging from 1 to 9. The function returns an integer as per logic below:
1. Using the individual elements of the list, find all the possible 3 digits number
combinations.
Note: For a single combination, element at same index position should not be considered
more than once
Example: Input list: [1, 2, 1]
Possible three digit combination: 121, 112, 211, 211, 112 and 121.
Note that 222, 122 and 221 are not considered because in these cases digit 2
occurring at index position 2 is appearing at least twice. In valid combinations
listed above, the digit 1 can repeat twice as in the original input list itself the
digit 1 is repeated twice. But 111 is not possible combination as 1 is not repeated
thrice in the list
2. From all the combinations obtained from step 1, consider only the unique combinations.
For the above example: Input list: [1, 2, 1]
Possible three digit unique combinations obtained from step 1:
121, 112 and 211.
Note: from the three digit number combinations listed above,
duplicates have been removed
3. Find the maximum number among the three digit combinations listed above. Append it
with the number of three digit combinations obtained in step 2.
For the above example: Input list: [1, 2, 1]
Possible unique three digit combination obtained from step 2: 121,
112 and 211.
The maximum number among the above is 211. Append it with the
number of three digit combinations. I.e. 3. If you append it to 211,
it will become 2113
4. Return the generated number in step 3
Example:
Input List: [1, 2, 1, 4]
Output: 42112
Possible unique three digit combinations: 121, 124, 112, 114, 142, 141, 211, 214, 241, 412, 411
and 421
Maximum Number among the combination: 421
Number of unique combinations: 12
Hence the output will be 42112.
Assumptions:
1. Input list would contain at least three elements and maximum of five elements.
2. Input list would not contain the digit 0 (zero)
Note: No need to validate the assumptions
Sample Input and Output:
Problem Statement: Ferrari rental services rent cars and want to automate the process of their
bill calculation. Write a Python program to implement the class diagrams given below.
Class Diagram:
Notes:
Do not include any extra instance variables in the given classes
Case sensitive comparison is required to be done wherever applicable and not
mentioned
Value of no_of_kms member variable is assumed to be positive. It is not validated
Read notes and examples for better understanding of the logic
Customer Class:
This class has static dictionary named as member_cust_details which contains following
entries {'1001P':2050, '1051R':5345, '1072P':6896, '2019R':9100, '2913R':4500,
'2931P':3234}
Example 1: If the rental_amount is Rs. 20,000/- and the cust_id is ‘2019R’, the customer
gets upgraded and the discount amount given to him is Rs. 2000/-
Example 2: If the rental_amount is Rs. 15,000/- and the cust_id is ‘2119R’, the calculated
discount amount is -1
identify_per_day_rent (car_type):
This method accepts car_type which is a string as an input parameter and returns the
corresponding per_day_rent which is an integer value
It searches for the car_type in car_types list
If match not found, it initializes rent_per_day to -1
Otherwise, it initializes per_day_rent to the corresponding value in per_day_rent list
It returns rent_per_day
For example, per_day_rent of car type “Sedan” is Rs. 5,000/- and per_day_rent of ‘XUV’ is -1
CarRental Class:
calculate_final_amount():
This method calculates and returns final_amount
It invokes identify_per_day_rent() method of CarDetails class to obtain per_day_rent
It invokes identify_journey_days() method which sets journey_days and returns
excess_kms
If per_day_rent is -1 or excess_kms is negative or journey_days is negative, set
final_amount to -1
Otherwise, it calculates the final_amount as discussed below :-
o calculates rental amount for all the journey_days as product of the
journey_days and per_day_rent
o calculates excess_kms_amount using excess_kms as discussed below:-
For every excess kilometer travelled, it charges Rs. 12/-
o calculates rental_amount as the sum of rental amount for all the journey_days
and excess_kms_amount
o It invokes calculate_discount() method by passing rental_amount as input
parameter and obtains discount amount
o If discount amount is -1, set final_amount to -1
o Otherwise,
It calculates final_amount by subtracting discount amount from
rental_amount
It updates member_cust_details of Customer class by adding
final_amount to the value of corresponding cust_id
It returns final_amount
Write a function which takes input queue and input stack as input parameters and returns an
output queue. The output queue contains same elements as that of input queue but in an order
which is determined by the elements of input stack.
For every element of the input stack, input queue is reordered based on the below rule-
If the element in the stack is 1, then the first element in the queue (element at front) will
become last element (element at rear).
If the element in the stack is 2, then the last element (element at rear) in the queue will
become the first element (element in front).
After reordering the input queue based on the elements in the stack, if the reordered
queue and input queue are same, then based on the top element of the input stack
reordering has to be done once again.
Example:
Input_queue (frontrear): A, B, C
Input_stack (topbottom): 2, 1
Output_queue(frontrear): C, A, B
Step 1: For the first element in the input stack that is 2, the last element in the input queue that
is ‘C’ is moved to the front, so the input queue is now reordered as (front->rear) C, A, B.
Step 2: For the second element in the input stack that is 1, the first element in the reordered
input queue as per step 1 that is ‘C’ is moved to the rear of the queue, the input queue is now
reordered as (frontrear) A, B, C
Step 3: Now the reordered queue and the input queue are same and the top of the input stack is
2, the last element in the input queue that is “C” is moved to the front , so the input queue is
now reordered as (frontrear) C, A, B.
Step 4: Final elements in the output queue after step 3 are (frontrear) C, A, B
Note: For first element in input stack, original input queue is reordered first, then for each
element in input stack the previous reordered queue is referenced to reorder again
Assumption:
Input queue and input stack will always contain at least one element each
Input queue will always contain single character strings
The elements in the input stack can be either 1 or 2
Note: No need to validate assumptions
Sample Input and Output: