Skip to content

Instantly share code, notes, and snippets.

@dleacock
Last active August 29, 2015 14:22
Show Gist options
  • Save dleacock/210b815f99900a37b0f8 to your computer and use it in GitHub Desktop.
Save dleacock/210b815f99900a37b0f8 to your computer and use it in GitHub Desktop.
Swapping Elements
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
/// Recieves input in the form of 22 16 1 44 72 42 92 87 59 93 87 41 : 3-12, 0-1, 5-12
/// The numbers after the colon on instructions. i.e swap elements in places 3 and 12,then 0 and 1 and so on.
/// Program takes in file for instruction. Parses string into a number array and instructions
/// Parses the instructions and loops through swapping elements then loops through and prints array.
/// I keep everything as as a string until I need to learn its number to know which element to access
template <typename T>
T convertToInt(const string &text)
{
stringstream ss_(text);
T result;
return ss_ >> result ? result : 0;
}
/// Inputs a string and parses it into two arrays, number array and the instruction
void parseString(vector<string> &numArray, vector<string> &instructArray, const string &text)
{
size_t positionColon = text.find_first_of(":", 0);
string array_ = text.substr(0, positionColon);
string instructions_ = text.substr(positionColon+1, string::npos);
size_t prevPos = 0, pos;
/// Parse through each space and grab the number
while((pos = array_.find_first_of(" ", prevPos)) != string::npos )
{
if( pos > prevPos)
numArray.push_back(array_.substr(prevPos, pos-prevPos));
prevPos = pos + 1;
}
if ( prevPos < array_.length() )
numArray.push_back(array_.substr(prevPos, string::npos));
// Repeat search now on instruction array
prevPos = 0;
while( (pos = instructions_.find_first_of(",", prevPos)) != string::npos )
{
if( pos > prevPos )
instructArray.push_back(instructions_.substr(prevPos, pos-prevPos));
prevPos = pos + 1;
}
if(prevPos < instructions_.length())
instructArray.push_back(instructions_.substr(prevPos, string::npos));
}
/// Grab instruction string, first instruction found after the space. second instruction found
/// after the dash. Convert these to int's as we need the number and then store as ints
void parseInstructions(const string &instruction, int &a, int &b)
{
size_t firstPos, secondPos;
firstPos = instruction.find_first_of(" ", 0);
secondPos = instruction.find_first_of("-", 0);
a = convertToInt<int>(instruction.substr(firstPos, secondPos));
b = convertToInt<int>(instruction.substr(secondPos+1, string::npos));
}
int main(int argc, char *argv[])
{
fstream inputFile(argv[1]);
string line;
vector<string> numArray;
vector<string> instructArray;
int a;
int b;
string temp;
while(getline(inputFile, line))
{
/// Parse entire line to create a number array and an instruction array
parseString(numArray, instructArray, line);
/// Cycle through each instruction string and extract the element positions in the swap
/// Then follow those instructions in swapping
for(int i = 0; i < instructArray.size(); ++i)
{
parseInstructions(instructArray[i], a, b);
temp = numArray[a];
numArray[a] = numArray[b];
numArray[b] = temp;
}
/// Print post-swapped number array
for(int i = 0; i <numArray.size(); ++i)
{
cout << numArray[i] << " ";
}
cout << endl;
numArray.clear();
instructArray.clear();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment