Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch 09-02 ported from Java #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# OS generated files
.DS_Store
.DS_Store?

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# User-specific files (MonoDevelop/Xamarin Studio)
# User-specific files (MonoDevelop/Xamarin Studio/VS Code)
*.userprefs
.vscode

# Build results
[Dd]ebug/
Expand Down
15 changes: 15 additions & 0 deletions Ch 07. Object-Oriented Design/Ch 07. Object-Oriented Design.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RootNamespace>Chapter07</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ctci.Contracts\ctci.Contracts.csproj" />
<ProjectReference Include="..\ctci.Library\ctci.Library.csproj" />
</ItemGroup>
</Project>
56 changes: 56 additions & 0 deletions Ch 07. Object-Oriented Design/Q7_12_Hash_Table.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using ctci.Contracts;

namespace Chapter07
{
public class Q7_12_Hash_Table: Question
{
public override void Run()
{
Dummy bob = new Dummy("Bob", 20);
Dummy jim = new Dummy("Jim", 25);
Dummy alex = new Dummy("Alex", 30);
Dummy tim = new Dummy("Tim", 35);
Dummy maxwell = new Dummy("Maxwell", 40);
Dummy john = new Dummy("John", 45);
Dummy julie = new Dummy("Julie", 50);
Dummy christy = new Dummy("Christy", 55);
Dummy tim2 = new Dummy("Tim", 100); // This should replace the first "tim"

Dummy[] dummies = { bob, jim, alex, tim, maxwell, john, julie, christy, tim2 };

/* Test: Insert Elements. */
Hasher<String, Dummy> hash = new Hasher<String, Dummy>(3);
foreach (Dummy d in dummies)
{
Console.WriteLine(hash.Put(d.Name, d));
}

hash.PrintTable();

/* Test: Recall */
foreach (Dummy d in dummies)
{
String name = d.Name;
Dummy dummy = hash.Get(name);
if (dummy == null)
{
Console.WriteLine($"Dummy named {name}: null");
}
else
{
Console.WriteLine($"Dummy named {name}: {dummy}");
}
Dummy d2 = hash.Remove(name);
if (d2 == null)
{
Console.WriteLine($"Dummy removed named {name}: null");
}
else
{
Console.WriteLine($"Dummy removed named {name}: {d2}");
}
}
}
}
}
22 changes: 22 additions & 0 deletions Ch 07. Object-Oriented Design/Q7_12_Hash_Table/Dummy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

namespace Chapter07
{
public class Dummy
{
public string Name { get; private set; }
public int Age { get; private set; }

public Dummy(string n, int a)
{
Name = n;
Age = a;
}


public override string ToString()
{
return "(" + Name + ", " + Age + ")";
}
}
}

134 changes: 134 additions & 0 deletions Ch 07. Object-Oriented Design/Q7_12_Hash_Table/Hasher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;

namespace Chapter07
{
public class Hasher<K, V>
{
private class LinkedListNode<K, V>
{
public LinkedListNode<K, V> Next { get; set; }
public LinkedListNode<K, V> Prev { get; set; }
public K Key { get; set; }
public V Value { get; set; }

public LinkedListNode(K k, V v)
{
Key = k;
Value = v;
}

public string PrintForward()
{
string data = $"({Key},{Value})";
if (Next != null)
{
return data + "->" + Next.PrintForward();
}
else
{
return data;
}
}
}

private List<LinkedListNode<K, V>> _arr;
public Hasher(int capacity)
{
/* Create list of linked lists. */
_arr = new List<LinkedListNode<K, V>>();
for (int i = 0; i < capacity; i++)
{
_arr.Add(null);
}
}

/* Insert key and value into hash table. */
public V Put(K key, V value)
{
LinkedListNode<K, V> node = GetNodeForKey(key);
if (node != null)
{
V oldValue = node.Value;
node.Value = value; // just update the value.
return oldValue;
}

node = new LinkedListNode<K, V>(key, value);
int index = GetIndexForKey(key);
if (_arr[index] != null)
{
node.Next = _arr[index];
node.Next.Prev = node;
}
_arr[index] = node;
return default(V);
}

/* Remove node for key. */
public V Remove(K key)
{
LinkedListNode<K, V> node = GetNodeForKey(key);
if (node == null)
{
return default(V);
}

if (node.Prev != null)
{
node.Prev.Next = node.Next;
}
else
{
/* Removing head - update. */
int hashKey = GetIndexForKey(key);
_arr[hashKey] = node.Next;
}

if (node.Next != null)
{
node.Next.Prev = node.Prev;
}
return node.Value;
}

/* Get value for key. */
public V Get(K key)
{
if (key == null) return default(V);
LinkedListNode<K, V> node = GetNodeForKey(key);
return node == null ? default(V) : node.Value;
}

/* Get linked list node associated with a given key. */
private LinkedListNode<K, V> GetNodeForKey(K key)
{
int index = GetIndexForKey(key);
LinkedListNode<K, V> current = _arr[index];
while (current != null)
{
if (current.Key.Equals(key))
{
return current;
}
current = current.Next;
}
return null;
}

/* Really stupid function to map a key to an index. */
public int GetIndexForKey(K key)
{
return Math.Abs(key.GetHashCode() % _arr.Count);
}

public void PrintTable()
{
for (int i = 0; i < _arr.Count; i++)
{
string s = _arr[i] == null ? string.Empty : _arr[i].PrintForward();
Console.WriteLine($"{i}: {s}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RootNamespace>Chapter09</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ctci.Contracts\ctci.Contracts.csproj" />
<ProjectReference Include="..\ctci.Library\ctci.Library.csproj" />
</ItemGroup>
</Project>
90 changes: 90 additions & 0 deletions Ch 09. System Design and Scalability/Q9_02_Social_Network.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Linq;
using System.Collections.Generic;
using ctci.Contracts;

namespace Chapter09
{
public class Q9_02_Social_Network : Question
{
public static void PrintPeople(LinkedList<Person> path) {
if (path == null) {
Console.WriteLine("No path");
} else {
foreach (Person p in path) {
Console.WriteLine(p.Id);
}
}
}

public static bool IsEqual(LinkedList<Person> path1, LinkedList<Person> path2, bool reverse) {
if (path1 == null || path2 == null) {
return path1 == null && path2 == null;
}
if (path1.Count != path2.Count) {
return false;
}

for (int i = 0; i < path1.Count; i++) {
int other = reverse ? path2.Count - i - 1 : i;
if (path1.ElementAt(i) != path2.ElementAt(other)) {
return false;
}
}
return true;
}

public static bool IsEquivalent(LinkedList<Person> path1, LinkedList<Person> path2) {
bool f1 = IsEqual(path1, path2, false);
bool f2 = IsEqual(path1, path2, true);
return f1 || f2;
}

public override void Run()
{
int nPeople = 11;
var people = new Dictionary<int, Person>();
for (int i = 0; i < nPeople; i++) {
Person p = new Person(i);
people.Add(i, p);
}

int[][] edges = {new[]{1, 4}, new[]{1, 2}, new[]{1, 3}, new[]{3, 2}, new[]{4, 6}, new[]{3, 7}, new[]{6, 9}, new[]{9, 10}, new[]{5, 10}, new[]{2, 5}, new[]{3, 7}};
//int[][] edges = {new[]{1, 4}, new[]{1, 2}, new[]{4, 6}, new[]{6, 9}, new[]{9, 10}, new[]{5, 10}, new[]{2, 5}};
//int[][] edges = {new[]{1, 2}, new[]{1, 4}, new[]{2, 3}, new[]{3, 4}, new[]{4, 6}, new[]{5, 6}, new[]{4, 5}};
foreach (var edge in edges) {
Person source = people[edge[0]];
source.AddFriend(edge[1]);

Person destination = people[edge[1]];
destination.AddFriend(edge[0]);
}

//int j = 4;
//int z = 9;
//LinkedList<Person> path1 = QuestionA.FindPathBFS(people, j, z);
//LinkedList<Person> path2 = QuestionB.FindPathBiBFS(people, j, z);
//Console.WriteLine("Path 1");
//PrintPeople(path1);
//Console.WriteLine("Path 2");
//PrintPeople(path2);

for (int i = 0; i < nPeople; i++) {
for (int j = 0; j < nPeople; j++) {
LinkedList<Person> path1 = QuestionA.FindPathBFS(people, i, j);
LinkedList<Person> path2 = QuestionB.FindPathBiBFS(people, i, j);
if (!IsEquivalent(path1, path2)) {
Console.WriteLine($"Not equal: {i} to {j}");
Console.WriteLine("Path 1");
PrintPeople(path1);
Console.WriteLine("Path 2");
PrintPeople(path2);
break;
} else {
Console.WriteLine($"Is equal: {i} to {j}");
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;

namespace Chapter09
{
public class BFSData
{
public Queue<PathNode> ToVisit {get; set;}
public Dictionary<int, PathNode> Visited {get; set;}

public BFSData(Person root) {
ToVisit = new Queue<PathNode>();
Visited = new Dictionary<int, PathNode>();

PathNode sourcePath = new PathNode(root, null);
ToVisit.Enqueue(sourcePath);
Visited.Add(root.Id, sourcePath);
}

public bool IsFinished() {
return ToVisit.Count == 0;
}
}

}


Loading