Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 16, 2016
0 parents commit 81a84f7
Show file tree
Hide file tree
Showing 108 changed files with 14,542 additions and 0 deletions.
91 changes: 91 additions & 0 deletions 001_PalindromeNumber.c
@@ -0,0 +1,91 @@
// Coding Interviews: Questions, Analysis & Solutions
// Harry He

#include <stdio.h>
#include <string.h>

// ==================== Solution 1 ====================

int IsPalindrome(const char* const string)
{
int palindrome = 1;
if(string != NULL)
{
int length = strlen(string);
int half = length >> 1;

int i;
for(i = 0; i < half; ++ i)
{
if(string[i] != string[length - 1 - i])
{
palindrome = 0;
break;
}
}
}

return palindrome;
}

/* It returns 1 when number is palindrome, otherwise returns 0. */
#define NUMBER_LENGTH 20
int IsPalindrome_solution1(unsigned int number)
{
char string[NUMBER_LENGTH];
sprintf(string, "%d", number);

return IsPalindrome(string);
}

// ==================== Solution 2 ====================

/* It returns 1 when number is palindrome, otherwise returns 0. */
int IsPalindrome_solution2(unsigned int number)
{
int reversed = 0;
int copy = number;

while(number != 0)
{
reversed = reversed * 10 + number % 10;
number /= 10;
}

return (reversed == copy) ? 1 : 0;
}

// ==================== Test Code ====================

void Test(char* testName, int number, int expected)
{
if(testName != NULL)
printf("%s begins: ", testName);

if(IsPalindrome_solution1(number) == expected)
printf("solution 1 passed; ");
else
printf("solution 1 FAILED; ");

if(IsPalindrome_solution2(number) == expected)
printf("solution 2 passed.\n");
else
printf("solution 2 FAILED.\n");
}

int main(int argc, char* argv[])
{
Test("Test1", 5, 1);
Test("Test2", 33, 1);
Test("Test3", 242, 1);
Test("Test4", 2332, 1);

Test("Test5", 0, 1);

Test("Test6", 32, 0);
Test("Test7", 233, 0);
Test("Test8", 2331, 0);
Test("Test9", 2322, 0);

return 0;
}
135 changes: 135 additions & 0 deletions 002_AssignmentOperator.cpp
@@ -0,0 +1,135 @@
// Coding Interviews: Questions, Analysis & Solutions
// Harry He

#include <stdio.h>
#include <string>

class CMyString
{
public:
CMyString(char* pData = NULL);
CMyString(const CMyString& str);
~CMyString(void);

CMyString& operator = (const CMyString& str);

void Print();

private:
char* m_pData;
};

CMyString::CMyString(char *pData)
{
if(pData == NULL)
{
m_pData = new char[1];
m_pData[0] = '\0';
}
else
{
int length = strlen(pData);
m_pData = new char[length + 1];
strcpy(m_pData, pData);
}
}

CMyString::CMyString(const CMyString &str)
{
int length = strlen(str.m_pData);
m_pData = new char[length + 1];
strcpy(m_pData, str.m_pData);
}

CMyString::~CMyString()
{
delete[] m_pData;
}

CMyString& CMyString::operator = (const CMyString& str)
{
if(this == &str)
return *this;

delete []m_pData;
m_pData = NULL;

m_pData = new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);

return *this;
}

// ==================== Test Code ====================

void CMyString::Print()
{
printf("%s", m_pData);
}

void Test1()
{
printf("Test1 begins:\n");

char* text = "Hello world";

CMyString str1(text);
CMyString str2;
str2 = str1;

printf("The expected result is: %s.\n", text);

printf("The actual result is: ");
str2.Print();
printf(".\n\n");
}

// Assign to self
void Test2()
{
printf("Test2 begins:\n");

char* text = "Hello world";

CMyString str1(text);
str1 = str1;

printf("The expected result is: %s.\n", text);

printf("The actual result is: ");
str1.Print();
printf(".\n\n");
}

// Continuous Assignments
void Test3()
{
printf("Test3 begins:\n");

char* text = "Hello world";

CMyString str1(text);
CMyString str2, str3;
str3 = str2 = str1;

printf("The expected result is: %s.\n", text);

printf("The actual result is: ");
str2.Print();
printf(".\n");

printf("The expected result is: %s.\n", text);

printf("The actual result is: ");
str3.Print();
printf(".\n\n");
}

int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();

return 0;
}
141 changes: 141 additions & 0 deletions 003_Singleton.cs
@@ -0,0 +1,141 @@
// Coding Interviews: Questions, Analysis & Solutions
// Harry He

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _003_Singleton
{
public sealed class Singleton1
{
private Singleton1()
{
}

private static Singleton1 instance = null;
public static Singleton1 Instance
{
get
{
if (instance == null)
instance = new Singleton1();

return instance;
}
}
}

public sealed class Singleton2
{
private Singleton2()
{
}

private static readonly object syncObj = new object();

private static Singleton2 instance = null;
public static Singleton2 Instance
{
get
{
lock (syncObj)
{
if (instance == null)
instance = new Singleton2();
}

return instance;
}
}
}

public sealed class Singleton3
{
private Singleton3()
{
}

private static object syncObj = new object();

private static Singleton3 instance = null;
public static Singleton3 Instance
{
get
{
if (instance == null)
{
lock (syncObj)
{
if (instance == null)
instance = new Singleton3();
}
}

return instance;
}
}
}

public sealed class Singleton4
{
private Singleton4()
{
Console.WriteLine("An instance of Singleton4 is created");
}

public static void Print()
{
Console.WriteLine("Singleton4 Print");
}

private static Singleton4 instance = new Singleton4();
public static Singleton4 Instance
{
get
{
return instance;
}
}
}

public sealed class Singleton5
{
Singleton5()
{
Console.WriteLine("An instance of Singleton5 is created");
}

public static void Print()
{
Console.WriteLine("Singleton5 Print");
}

public static Singleton5 Instance
{
get
{
return Nested.instance;
}
}

class Nested
{
static Nested()
{
}

internal static readonly Singleton5 instance = new Singleton5();
}
}

class Program
{
static void Main(string[] args)
{
Singleton4.Print();
Singleton5.Print();
}
}
}

0 comments on commit 81a84f7

Please sign in to comment.