Monday, August 15, 2016

String Permutations in Python

"""This program prints the number of permutations of a given input. The number of permutation of a given input of length n is n! The algorithm is as follows 1. If the input length is less than or equal to 1, just return 2. For each element in the input, until the end of input is reached 3. Get the sub elements in the input except the current element that's being processed 4. Recursively traverse the sub elements 5. Append the recursive sublements to the current element 6. Return the list. """ def permutate(seq):#check the length of the input. If its less than or equal to 1, return the input as 1! is 1     if len(seq) <= 1:         return [seq]     else:         #Create a temporary list to hold the values         temp = []         #for each element in the given input, traverse until end of the the string         for k in range(len(seq)):             #get the subset of all the elements except the element
#that's being processed             #string slicing works this way             #get the elements until the current element +
#elements after the current element             part = seq[:k] + seq[k+1:]             #recursively traverse for the subset of elements             for m in permutate(part):                #append recursive element to the current element                 temp.append(seq[k] + m)         #return the final output after all the possible recursions are complete         return temp #Main Program #Prompt the user to enter input. Save it in user_input variable user_input = input("Enter the string of characters or numbers for permutations: ") #Call the permutate function and store the output into a list permutations = permutate(user_input) #traverse the list and print the permutations for perm in permutations:     print(perm)

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

       }

}