Sunday 19 August 2018

KERALA PEOPLE NEED YOUR HELP


The people of Kerala have been devastated by the continuous rains and the excessive floods that have affected the region. There have been continuous rains in majority of Kerala which lead to flooding and destruction of life and property.

Water Caused Destruction


People have been hurt both physically and mentally as they lost their houses, property, lives and families to the excess water that has lodged almost every district in the state. According to an official report, in the past 10 days, about 300 people have lost their lives with a destruction of property amounting to $3 billion.

The main purpose of this article is to request people to support people of Kerala by donating as little as 20-30 Rs. This money will help feed the needy and get them proper treatment and care.


  
Ways to donate :
  • The primary option to donate is, directly to the state by giving your money to the Kerala Chief Minister Disaster Relief Fund (CMDRF). It has been started by the government to request funds for the revival of the state.
  • The other option will be to send money directly using TEZ or any other UPI service. The government has setup a disaster relief fund with the UPI as keralacmdrf@sbi 
    • Go to your UPI payment App
    • Type in the payment address
    • Pour your heart out in the amount section
    • Pay for betterment of peoples' lives
  •  Paying via Ketto. Ketto is a fund-raiser organization, where people can ask for croud funding for their causes and ideas. There is a fund-raiser going on for the Kerala Floods, just visit the link and donate whatever amount you feel like (The minimum donation amount is 300Rs)
  • Finally if you do not seem to be able to spare even 50Rs, just visit those little Advertisements on this page, anything that this post generates will be sent to the Disaster Relief Fund.

May God Protect The People And Help Them Overcome This Calamity 

Friday 10 August 2018

Sum Of Array Elements Using Recursion

QUESTION DESCRIPTION :

Given an array of integer elements, find sum of array elements using recursion.

TEST CASE 1

INPUT

5
1 2 3 4 5

OUTPUT

15


TEST CASE 2

INPUT

4
4 4 4 4

OUTPUT

16



EXPLANATION :

The question requires you to input a set of numbers and then find their sum using the recursive addition.
The number of integers is given in the first line say "n", then n space seperated integers follow.

ALGORITHM :

For this code, we will input the "n" value, which gives the numbers of integers to be added. Then initialize the array to n. Then input the n values in the array using a loop. Then instead of using a loop to traverse through the array we will simulate it using recursion.

Steps:

  1. Input the number of integers that have to be added (The value of n)
  2. Initialize an array to size n (arr)
  3. Loop till n and input all n integers into the array
  4. Call the add() function and pass the array and n to it.
    (Set the return type of function to be int)

    Inside the add() function:
    1. If there are more values available, i.e. n>0, return the current arr[n] value and call the add function again with n-1 as the argument.
      (This is the recursive base case)
    2. Otherwise return 0
      (This is the exit case)
  5. Print the result of the add() function (don't need to save the result)
  6. Exit
P.S. It is a bit difficult to explain recursive algorithms and recursive statements. It is recommended to read the code and try to understand. Ask doubts in the comments.

P.P.S. :Here are a few links to basic recursion tutorials
SOLUTION : (Please use this as reference only)

Link to Code : Sum Of Array Elements Using Recursion

(Please Try To Refrain From Copying The Code)

Wednesday 8 August 2018

Pool

QUESTION DESCRIPTION :

You are planning to go for swimming classes. You would prefer to enroll in the centre which has the swimming pool of a greater area. In the first center that you visit, the swimming pool is circular shape (radius- r). In the next center you visit, the swimming pool is of square shape (size-S). Write a program that will help you to make the choice of the swimming pool.

Input Format: Input consists of 2 integers. The first one corresponds to the Radius of the circle, and the second one is the Side of the square

TEST CASE 1

INPUT
 
20
5

OUTPUT

I prefer centre 1


TEST CASE 2

INPUT

5
20

OUTPUT

I prefer centre 2


EXPLANATION :

The question requires you to input the radius of a circle, side of a square and then compare their areas, the one with the bigger area is the choice of the person. 

ALGORITHM :

Although this code is very easy we can add a space optimization by directly using the result of the calculation.

Steps:
  1. Input the Radius(R) and Side(S) of the circle and square respectively.
  2. Calculate the area of Circle (π*r*r) > Area1
  3. Calculate the area of the square (s*s) > Area2
  4. Compare the areas:
    1. If Area1< Area2 print "I prefer centre 2"
    2. If Area1> Area2 print "I prefer centre 1"
  5. Exit
P.S. Optimizations such as using less variables are mostly not relevant in the 21st Century scenario as the RAM add HDD sizes are large but such optimizations are recommended wherever possible in order to become a better programmer.

SOLUTION : (Please use this as reference only)



Link to Code : Pool

(Please Try To Refrain From Copying The Code)

Monday 6 August 2018

Sum Of Palindromic Numbers

QUESTION DESCRIPTION :

A number is called palindromic if its decimal representation is a palindrome. You are given a range, described by a pair of integers L and R. Find the sum of all the palindromic numbers lying in the range [L,R], inclusive of both the extremes.

Input Format :

The first line contains an integer T, denoting the number of test cases.
The description of T test cases follow:
  • Each line of test cases contains 2 numbers L and R denoting the lower and upper bound of the numbers for which you have to calculate the sum.
Output Format :

For each line of input, print a single integer which is equal to the sum of the palindromic numbers.

TEST CASE 1

INPUT

2
1 10
123 150

OUTPUT

sum is 45
sum is 272

EXPLANATION :

The question asks the programmer to send the sum of all the numbers that are palindromic in nature. So in order to solve this question we will check every number for the palindromic property and add it to the resulting sum if the number is a palindrome.

ALGORITHM :

Although there are quite a few algorithms that can be used to solve this, we will be using a simple one. Instead of checking each half of the number for similarity, we will be reversing the number and then checking if it is equal to the given number. If yes, then the number will be added to the resulting sum.

Steps:
  1. Take the input T for the number of test cases.
  2. Loop T times
    1. For each test case take L an R as input
  3. Start a loop from L till R and check palindrome constaint (done in the isPal function)
    1. For each number of the loop, calculate the reverse of it.
      • Reverse calculation:
        1. Take number and mod (%) it by 10 this gives you the first ones digit
        2. Multiply rev (Intially 0) by 10 and add the above digit to it
        3. Divide the original number by 10 to remove the previous digit.
        4. Goto step 3.1.1 until there is atleast a digit left in the number
    2. Check if the reverse is equal to original number
      • If yes : add to the sum (final result) 
  4. Redo Step 3.1 for all numbers in L and R
  5. Print the result and move to the next line
  6. Goto Step 3
  7. Exit 
Take care of the L and R constraints as they both have to be included in the calculation of the result.

SOLUTION : (Please use this as reference only)


Link to Code : Sum Of Palindromic Numbers
(Please Try To Refrain From Copying The Code)






Saturday 4 August 2018

Discount

QUESTION DESCRIPTION


Mr. Phileas Fogg is a very perfect man. He saves money even on petty things. One day he heard a discount offer announced in a mall. He wants to purchase lot of items to save his money. The discount is given only when atleast two items are bought. Since each item has different discount prices, he finds it difficult to check the amount he has saved. So he approaches you to device an automated discount calculator to make him easy while billing.

I/O Constraints :  The input consists of two floating point values denoting price of item1 and item2. The third value denotes the discount value in %. The output consists of three floating point values denoting the total amount, discounted price and the amount saved.

Note: Correct the floating point decimals to 2 places.

TEST CASE 1

INPUT

20.50
45.40
10

OUTPUT

Total amount:$65.90
Discounted amount:$59.31
Saved amount:$6.59 

EXPLANATION :

The question simply requests you to accept 3 values (float) and perform trivial operations on them. This is a basic percentage question where you ought to find the discount value of all the items bought by the customer.

Note: The note at the end of the question simply wants the answer correct upto 2 decimal places i.e. 2.567 will result in 2.57 as the answer, this can be easily done using the printf function to print the values


ALGORITHM :

This is a simple algorithm:

Steps :
  •  Take 3 numbers as input (3 float numbers a,b,c)
  •  Print the addition of the first two numbers after the Total amount value
  •  Then calculate the discount value by multiplying the sum by discount/100 as the discount is in percentage.
  • Subtract the discount from the sum and print it as the Discounted amount 
  • Print the discount value as the Saved amount.
  • Exit


Note: Please mind the spaces, the : and the $ symbol

Note 2: Check this link  to understand how printf flags work. To print at most 2 decimal values use the %0.2f flag, here % stands for flag identifier, 0.2 states at max 2 decimal values while the rest will be rounded off and f is the flag for printing the value of float.


SOLUTION :  (Please use this as reference only)




Link to Code : Discount
(Please Try To Refrain From Copying The Code)

Thursday 2 August 2018

Ascii Name

QUESTION DESCRIPTION


Write a program which reads your name from the keyboard and outputs a list of ASCII codes, which represent your name

TEST CASE 1

INPUT

SRMUNIVERSITY

OUTPUT

The ASCII values of the string are:
83 82 77 85 78 86 69 82 83 73 84 89

EXPLANATION :

This is a simple question expecting you to convert the characters to their respective ASCII representation. That can be easily done by parsing each character to their respective integer representation.


ALGORITHM :

This is a simple algorithm:

Steps :
  • Create a char array to receive the string as input
  • Take the string as input
  • Print the first mandatory line "The ASCII values of the string are:
  • Use a for loop till you find the '\0' character (Why \0)
  • For each character in the string:
    •  Convert it to its integer version and print it with a Space " ".
  • When the '\0' character is found Exit the code.

Remember the spacing in the output and the end of line


SOLUTION :  (Please use this as reference only)


Link to Code : Ascii Name
(Please Try To Refrain From Copying The Code)

Adding More Content :)

Adding All eLab Support 


The Blog initially started as a blog for solutions to PDD problems, but we have been looking to extend the support to all eLab platforms available in the College.

Although the questions listing will be random, the blog will now contain questions to the following ELABs too:


  •  Data Structures
  •  Java
  •  Algorithms
  •  Python
Soon more questions will be added concerning the above topics.

#NOTE:
Copying and pasting of code :

Although it is suggested to use the platform just to understand the question and try answering it yourself, we are also adding support to copy and paste a working piece of code. A link to a saved file containing the complete code will also be present in the problem solution section.

Missing Questions, Requesting Solutions:

If you do not find a question or wish to ask for a solution to a problem outside eLab, you can leave a reply in comments, either with a link to the question or upload the question statement to some cloud storage service and send the link here.

Keep Coding,
Keep Visiting :)

 

Tuesday 16 January 2018

Lucy

QUESTION DESCRIPTION

Lucy is celebrating her 15th birthday. Her father promised her that he will buy her a new computer on her birthday if she solves the question asked by him. He asks Lucy to find whether the year on which she had born is leap year or not. Help her to solve this puzzle so that she celebrates her birthday happily. If her birth year is 2016 and it is a leap year display 2016 is a leap year.? Else display 2016 is not a leap year and check with other leap year conditions 


TEST CASE 1


INPUT
 
1900
 
OUTPUT 
 
 1900 is not a leap year
 
 
TEST CASE 2


INPUT
 
2016
 
OUTPUT 
 
2016 is a leap year 


EXPLANATION :

The code requires you to calculate if the given number (Lucy's birth year) is a leap year or not.
(What is a leap year : Leap_year) 

ALGORITHM :
 
The main part of the algo is to determine whether a year is a leap year or not.
 
Steps :
  • Input the value (Lucy's Birth Year)
  • Check if the year is leap year or not
  • Print appropriate result
Checking Leap Year :

There are 2 basic conditions for checking a leap year :

  • If a number is divisible by 4,100 and 400 its a leap year
  • If a number is not divisible by 100 but it is divisible by 4 then it is a leap year
Check the result of the above two and print the results accordingly

 
SOLUTION : (Please use this as reference only)




Monday 15 January 2018

IO 6

QUESTION DESCRIPTION

Write a program by using all relational operators. 

Input and Output Format:

Refer sample input and output for formatting specification. All float values are displayed correct to 2 decimal places. All text in bold corresponds to input and the rest corresponds to output.

TEST CASE 1

INPUT

30 40

OUTPUT

Value of 30>40 is:0 
Value of 30>=40 is:0 
Value of 30<40 is:1 
Value of 30<=40 is:1

EXPLANATION :

The code requires you to accept 2 numbers from the user and check them for ">" , "<" , "<=" , ">=" and print '1' or '0' for "true" or "false" respectively according to the output format.


ALGORITHM :

This is a simple algorithm, as the result of the conditional statements (>,<,>=,<=) is an integer ("0" for "false" and "1" for "true"), we directly print the result of the conditional statements.

Steps :
  • Accept the values of first and the second number
  • Print the results of conditional statements in this order :
    • Greater than
    • Greater than equal to
    • Less than
    • Less than equal to

Remember the spacing in the output and the end of line


SOLUTION :  (Please use this as reference only)