Skip to content

Instantly share code, notes, and snippets.

@rosdyana
Created June 21, 2018 15:17
Show Gist options
  • Save rosdyana/3ac358dfe2ea107252558fc62652ee17 to your computer and use it in GitHub Desktop.
Save rosdyana/3ac358dfe2ea107252558fc62652ee17 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string>
#include <stdexcept>
#include <iostream>
class Palindrome{
public:
static bool isPalindrome(const std::string &word){
int stringLength=word.length();
for(int i=0; i<stringLength; i++){
if(tolower(word[i])!=tolower(word[stringLength-i-1])){
//converting chars in string to lower case for comparison
//cout<< "wordArray["<<i<<"] != wordArray["<<stringLength-i-1<<"]"<<endl; //debug
return false;
}
}
return true;
}
};
#include <iostream>
#include <string>
#include <stdexcept>
class Path
{
public:
Path(std::string path)
{
currentPath = path;
pastPath = path;
}
std::string getPath() const
{
return currentPath;
}
void cd(std::string newPath)
{
if (newPath == ""){
return;
}
int newPathLength = newPath.length();
if (newPathLength == 1 && (isspace(newPath[0]) || newPath[0] == '/')){
currentPath = "/";
}
else if (newPathLength > 0 && newPath[0] != '~'){
int i = 0;
while(i < newPathLength){
int pathLength = currentPath.length();
if(newPath[i] == '.' && newPath[i+1] == '.'){
pastPath=currentPath;
if (pathLength > 2){
currentPath.erase(pathLength-2, 2);
i = i+3;
}
else{
currentPath = "/";
i = i+3;
}
}
else if (newPath[i] == '/'){
pastPath=currentPath;
currentPath.clear();
int j = 0;
while (j < newPathLength){
if(newPath[j] == '.' ){
break;
}
currentPath += newPath[j];
j++;
}
if (currentPath[j-1] == '/'){
currentPath.erase(j-1,1);
}
i = i+j;
}
else if (newPath[i] == '.'){
if (newPath[i+1] == '/'){
i = i+2;
}
else{
i = i+1;
}
}
else if(isspace(newPath[i])){
i++;
}
else if(newPath[i] == '-'){
swap(pastPath, currentPath);
i++;
}
else{
pastPath = currentPath;
if (pathLength > 1){
currentPath = currentPath+"/"+newPath[i];
}
else{
currentPath = currentPath+newPath[i];
}
i = i+2;
}
}
}
else{
currentPath = "/";
}
}
private:
std::string pastPath;
std::string currentPath;
};
#ifndef RunTests
int main()
{
Path path("/a/b/c/d");
path.cd("../../x");
std::cout << path.getPath();
}
#endif
#include <stdexcept>
#include <iostream>
#include <vector>
#include <utility>
#include <unordered_map>
class TwoSum
{
public:
static std::pair<int, int> findTwoSum(const std::vector<int>& list, int sum)
{
std::unordered_map<int, int> match;
for (int i=0; i < list.size(); i++){
auto checkIt = match.find(sum - list[i]);
if (checkIt != match.end()){
return std::make_pair(i, checkIt->second);
}
match[list[i]] = i;
}
return std::make_pair(-1, -1);
}
};
#ifndef RunTests
int main()
{
std::vector<int> list;
list.push_back(1);
list.push_back(3);
list.push_back(5);
list.push_back(7);
list.push_back(9);
std::pair<int, int> indices = TwoSum::findTwoSum(list, 12);
std::cout << indices.first << '\n' << indices.second;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment