Friday, July 15, 2016

Cleanse Phone Numbers (C++)

Recently I came across a question. The question is as follows.
1. There are several phone numbers (only US phone numbers). All the US phone numbers are basically in the format xxx xxx xxxx which is area code and 7 digit phone number. The number can have spaces between area code, first three digits of phone number or may be without spaces
2. The catch is that each phone number is re-arranged in a way that first three digits of phone number comes first then area code and then the last four digits.

Example:
Phone number is 425 123 4567 or 4251234567. This is the correct representation. But its stored as 123 425 4567 or 1234254567. This needs to be fixed and represented as the correct format. C++ code to cleanse the data. This is the quick solution that I got to. There may be several ways to achieve this


// FirstProject.cpp : Defines the entry point for the console application.
// This program assumes that the phone numbers are entered in the wrong order instead of area code and phone number its xxxareacodeandlastfournumbers
//Some times people may enter space/spaces between area code and first three numbers of phone.
//This program gets all the phone numbers into a vector, removes any spaces between numbers in the string and re-arranges values in the right order

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>


using namespace std;

//function prototype
void cleansePhoneNumbers(vector<string>&inputVector);


int main()
{
       vector<string> phoneNumbers;
       string phone = "";

       while (true) //start of infinite loop to add data to vector
       {
              cout << "Enter a phone number (press enter to exit):";
              getline(cin, phone); //get the data into phone string including spaces
              if (phone == "") //if just Enter key is pressed at the prompt
              {
                     break; //loop breaks
              }
              phoneNumbers.push_back(phone); //append the current value in phone to the vector
       }
      
       cleansePhoneNumbers(phoneNumbers); //function call to cleanse phone numbers


       for (string st : phoneNumbers) //for each string at the index of the vector
       {
              cout << "Cleansed Phone Numbers: " << st << endl; //print the value at that index
       }
       return 0;
}

//function definition
void cleansePhoneNumbers(vector<string>&inputVector)
{
       string *temp; //pointer to store address of each index of the vector
       int size;
       string val;
       size = inputVector.size(); //get the size of vector and assign it to size. Length of the vector
      
       for (int i = 0; i < size; i++) //traverse through each index
       {
              temp = &inputVector[i]; //get the address of index into temp pointer. This will be used to update the value in the vector directly
              val = inputVector[i]; //get the value at that index
      
              while (val.find(" ") != val.npos) //loop to remove any spaces in the string at a given idex
              {
                     val.replace(val.find(" "), 1, "");
              }

              val = val.substr(3, 3) + val.substr(0, 3) + val.substr(6, 4); //change the value
             
              *temp = val; //update value in the temp pointer so that actual data in the vector at that index changes

       }

}


Friday, December 4, 2015

Find Median For Given Set Of Numbers

/*
   MEDIAN RULE: IF COUNT OF NUMBERS IS ODD THEN THE VALUE IS MIDDLE NUMBER: FOR EXAMPLE MEDIAN FOR 1,8,10 WILL BE 8
                IF COUNT OF NUMBER IS EVEN THEN THE VALUE IS SUM(MIDDLE NUMBER + SUCCEEDING NUMBER TO MIDDLE NUMBER)/2.0 ESSENTIALLY ITS AN AVERAGE: FOR EXAMPLE 1,2,4,8
                MEDIAN IS (2+4)/2.0 WHICH IS 3.
*/


DECLARE @ArrayForMedian    VARCHAR(MAX) = '1,2,100,23,11,16,17,21,19,33,21,24'
DECLARE @FirstMedianValue  AS FLOAT
DECLARE @SecondMedianValue AS FLOAT
DECLARE @MedianValue       AS FLOAT

DECLARE @MedianTable AS TABLE
(
   ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY
   ,Value INT NOT NULL
)

--traverse and split the string into individual numbers
;WITH ValueCTE
AS
(
   SELECT SUBSTRING(@ArrayForMedian + ',', 1, CHARINDEX(',', @ArrayForMedian +',') - 1) AS val
         ,SUBSTRING(@ArrayForMedian + ',',CHARINDEX(',', @ArrayForMedian +',') + 1, LEN(@ArrayForMedian + ',')) AS Rem

   UNION ALL

   SELECT SUBSTRING(Rem, 1, CHARINDEX(',',Rem) -1)
         ,SUBSTRING(Rem, CHARINDEX(',', Rem) + 1, LEN(Rem))
   FROM ValueCTE
   WHERE CHARINDEX(',',  Rem) <> 0
)
INSERT INTO @MedianTable
(
   Value
)
SELECT Val
FROM ValueCTE


--EVEN COUNT OF NUMBERS
IF (SELECT COUNT(*)%2
    FROM @MedianTable) = 0
BEGIN
   SELECT @FirstMedianValue = Value
   FROM @MedianTable
   WHERE ID = (SELECT COUNT(*)/2
               FROM @MedianTable)
   SELECT @SecondMedianValue = Value
   FROM @MedianTable
   WHERE ID = (SELECT (COUNT(*)/2) + 1
               FROM @MedianTable)

   SELECT @MedianValue = (@FirstMedianValue + @SecondMedianValue)/2.0
END
ELSE
--ODD COUNT OF NUMBERS
BEGIN
   SELECT @MedianValue = Value
   FROM @MedianTable
   WHERE ID = (SELECT CEILING(COUNT(*)/2.0)
               FROM @MedianTable)
END


SELECT @MedianValue

 

Tuesday, August 18, 2015

SQL 2016

SQL Server 2016 preview is out. It has lot of new features

http://www.microsoft.com/en-us/server-cloud/products/sql-server-2016/

Wednesday, March 25, 2015

Fibonacci series with T-SQL




DECLARE @intStart       INT            = 0
DECLARE @intEnd         INT            = 1000 --change this to what ever fibonacci series you want. Make sure the results fit in integer datatype. if not change them to BIGINT or DECIMAL/FLOAT
DECLARE @vchFinalOutPut VARCHAR(MAX)   = ' '
DECLARE @intResult      INT            = 0
DECLARE @intNextValue   INT            = 1

WHILE @intStart < @intEnd
BEGIN
   SET @vchFinalOutPut = @vchFinalOutPut + ',' + LTRIM(RTRIM(STR(@intStart)))
   SET @intResult = @intStart --Store the start value
   SET @intStart = @intNextValue
   SET @intNextValue = @intResult + @intNextValue --Add previous and current value
END


PRINT LTRIM(RTRIM(STUFF(@vchFinalOutPut,PATINDEX('%,%', @vchFinalOutPut),1,'')))