Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Monday, May 5, 2008

Day 6 - Question : Breaking Strings

You are given a string containing a string Date of Birth in DD/MM/YYYY format. Eg "15/08/1947". Write a function that takes the string as an input and returns a vector containing 3 elements:
v[0] = Day as an integer
v[1] = Month as an integer
v[2] = Year as an integer

eg:
INPUT : "15/08/1947"
Output: [15, 8, 1947]

Saturday, May 3, 2008

Day 5 Solution

There is nothing special about this problem. No holistic views, no boundary conditions. You guys know "Sorting" right! This problem is to make you use it.

The Algorithm
1. Sort the array from 0 to n-1
2. Return the n-2th element

---

So you can do your regular sorting thing and get the result.

I would like to use this opportunity to introduce you to some more of STL.

Check the program



There is a legacy trick of finding the size of an array in that problem.
Noticed how we did -
sizeof(heights)/sizeof(int)

to get the number of elements in the array!

Then, while converting that array into a vector, we need to give the range of the array. By range I mean, a pointer (or an iterator) from the beginning of the array to the end of the array. Now 'heights' will give us the pointer to the beginning of the array and 'heights + n' to the end.

But will 'heights + n' always give us the pointer to the end of the array?
It will in our case, but it wont for other arrays of other types.
Can you tell why?

Wednesday, April 30, 2008

Day 3 - Programming problem: Shortest height

Question: You are given an array height[] of positive integers containing the heights of the children in a class. Write a function that takes height[] as an input and gives the shortest height as the output.

For example -
Input - [23,29,20,22,23,22,19,25,21,26,23,24,18,30]
Output - 18

Request: Pls take the quiz the right side bar to give feedback on the easiness/difficulty level of the questions. Thanks

Monday, April 28, 2008

Solution to Group 1 problem

I'll be writing in C++ to start with, will graduate to Java or Ruby if need be.

Group 1.

View the problem statement here.


Click the image to view enlarged version.

Its a good practice to use functions to do the calculation part and use the main() only for calling relevant functions and I/O.



Just click on the images for clarity.

Request: Please take the quiz or leave your comment to help me make this site more useful. Thanks

Todays Question: Group 3

Que 1.3> Write a program that finds the sum of the digits of a number, using recursion.

Todays Question: Group 2

Que 1.2> Write a program to find the sum of the digits in a number.

Todays Question: Group 1

Que 1.1> Write a program to add 2 numbers.

So you wanna code huh?

The best way to be a good coder is to write code and learn from other coders better than you.

I will post 1 problem every single day for people who have just started programming. I will post the solution in the night, the same day.

Based on user feedback, I will make the problems tougher or easier. So, do take part in the quiz.