Skip to content

Commit 63c7ea3

Browse files
committed
digit counter and ring buffer
1 parent 869bfcf commit 63c7ea3

File tree

6 files changed

+295
-0
lines changed

6 files changed

+295
-0
lines changed

Advanced.Algorithms.Tests/Advanced.Algorithms.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<Compile Include="DataStructures\Lists\ArrayList_Tests.cs" />
124124
<Compile Include="DataStructures\Lists\SkipList_Tests.cs" />
125125
<Compile Include="DataStructures\Queues\PriorityQueue\MaxPriorityQueue_Tests.cs" />
126+
<Compile Include="DataStructures\Queues\CircularQueue_Tests.cs" />
126127
<Compile Include="DataStructures\Queues\Queue_Tests.cs" />
127128
<Compile Include="DataStructures\Set\BloomFilter_Tests.cs" />
128129
<Compile Include="DataStructures\Set\DisJointSet_Tests.cs" />
@@ -149,6 +150,7 @@
149150
<Compile Include="DataStructures\Tree\TestHelpers\BinarySearchTreeTester.cs" />
150151
<Compile Include="DataStructures\Tree\Tree_Tests.cs" />
151152
<Compile Include="DataStructures\Tree\BinaryTree_Tests.cs" />
153+
<Compile Include="DynamicProgramming\Count\DigitCounter_Tests.cs" />
152154
<Compile Include="Geometry\PointRotation_Tests.cs" />
153155
<Compile Include="Miscellaneous\CountInversions_Tests.cs" />
154156
<Compile Include="Miscellaneous\MatrixMultiplication_Tests.cs" />
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Advanced.Algorithms.DataStructures;
2+
using Advanced.Algorithms.DataStructures.Queues;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace Advanced.Algorithms.Tests.DataStructures.Queues
6+
{
7+
[TestClass]
8+
public class CircularQueue_Tests
9+
{
10+
11+
[TestMethod]
12+
public void CircularQueue_Test()
13+
{
14+
var Queue = new CircularQueue<int>(7);
15+
16+
Queue.Enqueue(1);
17+
Queue.Enqueue(2);
18+
19+
Queue.Enqueue(3);
20+
Queue.Enqueue(4);
21+
Queue.Enqueue(5);
22+
Queue.Enqueue(6);
23+
Queue.Enqueue(7);
24+
Queue.Enqueue(8);
25+
Queue.Enqueue(9);
26+
27+
Assert.AreEqual(Queue.Count, 7);
28+
Assert.AreEqual(3, Queue.Dequeue());
29+
30+
Assert.AreEqual(Queue.Count, 6);
31+
Assert.AreEqual(Queue.Dequeue(), 4);
32+
33+
Assert.AreEqual(Queue.Count, 5);
34+
Assert.AreEqual(Queue.Dequeue(), 5);
35+
36+
Assert.AreEqual(Queue.Count, 4);
37+
Assert.AreEqual(Queue.Dequeue(), 6);
38+
39+
Assert.AreEqual(Queue.Count, 3);
40+
Assert.AreEqual(Queue.Dequeue(), 7);
41+
42+
Assert.AreEqual(Queue.Count, 2);
43+
Assert.AreEqual(Queue.Dequeue(), 8);
44+
45+
Assert.AreEqual(Queue.Count, 1);
46+
Assert.AreEqual(Queue.Dequeue(), 9);
47+
48+
Assert.AreEqual(Queue.Count, 0);
49+
50+
Queue.Enqueue(1);
51+
Queue.Enqueue(2);
52+
53+
Assert.AreEqual(Queue.Count, 2);
54+
Assert.AreEqual(1, Queue.Dequeue());
55+
56+
Assert.AreEqual(Queue.Count, 1);
57+
Assert.AreEqual(Queue.Dequeue(), 2);
58+
}
59+
60+
61+
}
62+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Advanced.Algorithms.DynamicProgramming.Count;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Advanced.Algorithms.Tests.DynamicProgramming.Count
10+
{
11+
/// <summary>
12+
///
13+
/// </summary>
14+
[TestClass]
15+
public class DigitCounter_Tests
16+
{
17+
[TestMethod]
18+
public void DigitCount_Smoke_Test()
19+
{
20+
Assert.AreEqual(0, DigitCounter.Count(5, 9));
21+
Assert.AreEqual(1, DigitCounter.Count(9, 9));
22+
23+
Assert.AreEqual(9, DigitCounter.Count(25, 2));
24+
Assert.AreEqual(3, DigitCounter.Count(26, 6));
25+
Assert.AreEqual(4, DigitCounter.Count(30, 3));
26+
27+
Assert.AreEqual(280, DigitCounter.Count(898, 6));
28+
29+
Assert.AreEqual(910116681, DigitCounter.Count(1015242410, 6));
30+
}
31+
}
32+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Advanced.Algorithms.DataStructures.Queues
7+
{
8+
/// <summary>
9+
/// Cicular queue aka Ring Buffer using fixed size array
10+
/// </summary>
11+
/// <typeparam name="T"></typeparam>
12+
public class CircularQueue<T>
13+
{
14+
private T[] queue;
15+
16+
//points to the index of next element to be deleted
17+
private int start = 0;
18+
19+
//points to the index new element should be inserted
20+
private int end = 0;
21+
22+
public int Count { get; private set; }
23+
24+
public CircularQueue(int size)
25+
{
26+
queue = new T[size];
27+
}
28+
29+
/// <summary>
30+
/// Note: When buffer overflows oldest data will be erased
31+
/// </summary>
32+
/// <param name="data"></param>
33+
public void Enqueue(T data)
34+
{
35+
//wrap around removing oldest element
36+
if (end > queue.Length - 1)
37+
{
38+
end = 0;
39+
40+
if(start == 0)
41+
{
42+
start++;
43+
}
44+
}
45+
46+
//when end meets start after wraping around
47+
if (end == start && Count > 1)
48+
{
49+
start++;
50+
}
51+
52+
queue[end] = data;
53+
end++;
54+
55+
if (Count < queue.Length)
56+
{
57+
Count++;
58+
}
59+
}
60+
61+
62+
public T Dequeue()
63+
{
64+
if (Count == 0)
65+
{
66+
throw new Exception("Empty queue.");
67+
}
68+
69+
var element = queue[start];
70+
start++;
71+
72+
//wrap around
73+
if (start > queue.Length - 1)
74+
{
75+
start = 0;
76+
77+
if (end == 0)
78+
{
79+
end++;
80+
}
81+
}
82+
83+
Count--;
84+
85+
if (start == end && Count > 1)
86+
{
87+
end++;
88+
}
89+
90+
//reset
91+
if (Count == 0)
92+
{
93+
start = end = 0;
94+
}
95+
96+
return element;
97+
}
98+
}
99+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Advanced.Algorithms.DynamicProgramming.Count
7+
{
8+
public class DigitCounter
9+
{
10+
/// <summary>
11+
/// Counts the appearences of given digit (0-9) in all numbers from 0 to given number
12+
/// </summary>
13+
/// <param name="number"></param>
14+
/// <param name="digit"></param>
15+
/// <returns></returns>
16+
public static int Count(int number, int digit)
17+
{
18+
if(digit < 0 || digit > 9)
19+
{
20+
throw new ArgumentException("Invalid digit.");
21+
}
22+
23+
if(number < 0)
24+
{
25+
throw new ArgumentException("Invalid number.");
26+
}
27+
28+
if (number < 10)
29+
{
30+
if(digit == number)
31+
{
32+
return 1;
33+
}
34+
35+
return 0;
36+
}
37+
38+
return Count(number, digit, new Dictionary<int, int>());
39+
}
40+
/// <summary>
41+
/// Counts the appearences of given digit (0-9) in all numbers from 0 to number
42+
/// </summary>
43+
/// <param name="number"></param>
44+
/// <param name="digit"></param>
45+
public static int Count(int number, int digit, Dictionary<int, int> cache)
46+
{
47+
//comments below explains each step with the assumption number = 898
48+
if (number < 10)
49+
{
50+
return 1;
51+
}
52+
53+
if (cache.ContainsKey(number))
54+
{
55+
return cache[number];
56+
}
57+
58+
//898 => 3
59+
var digits = (int)Math.Log10(number) + 1;
60+
61+
//most significant
62+
//898 => 8
63+
var msd = (int)(number / Math.Pow(10, digits - 1));
64+
65+
//898 => count(0-99) * 8
66+
var result = Count((int)Math.Pow(10, digits - 1) - 1, digit, cache) * msd;
67+
68+
//(for 600 - 699)
69+
//6 < 8
70+
if (digit < msd)
71+
{
72+
//+100 (for 600 - 699)
73+
result += (int)Math.Pow(10, digits - 1);
74+
}
75+
76+
//898 => 98
77+
var remaining = number - msd * (int)Math.Pow(10, digits - 1);
78+
79+
if(remaining > 0)
80+
{
81+
result += Count(remaining, digit, cache);
82+
}
83+
84+
//6 == 8?
85+
// (for 800 - 898)
86+
if (digit == msd)
87+
{
88+
//+98 (for 800 - 898)
89+
//+1 for 800
90+
result += remaining + 1;
91+
}
92+
93+
cache.Add(number, result);
94+
95+
return result;
96+
}
97+
}
98+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Supports
4141
### Queue
4242

4343
- [X] Queue (using [Dynamic Array](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/DataStructures/Queues/ArrayQueue.cs) and optionally using [Doubly Linked List](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/DataStructures/Queues/LinkedListQueue.cs)) ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/DataStructures/Queues/Queue.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/DataStructures/Queues/Queue_Tests.cs))
44+
- [X] Circular Queue (Ring Buffer) ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/DataStructures/Queues/CircularQueue.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/DataStructures/Queues/CircularQueue_Tests.cs))
4445

4546
#### Priority Queue
4647

@@ -275,6 +276,7 @@ All are top down solutions with memoization technique.
275276
- [X] Count possible binary tree from a preorder sequence ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/DynamicProgramming/Count/CountBinaryTree.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/DynamicProgramming/Count/CountBinaryTree_Tests.cs))
276277
- [X] Ways to cover a distance ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/DynamicProgramming/Count/WaysToCoverDistance.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/DynamicProgramming/Count/WaysToCover_Tests.cs))
277278
- [X] Staircase problem in Fibornacci Series ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/DynamicProgramming/Count/StairCaseProblem.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/DynamicProgramming/Count/StairCaseProblem_Tests.cs))
279+
- [X] Count digits ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/DynamicProgramming/Count/DigitCounter.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/DynamicProgramming/Count/DigitCounter_Tests.cs))
278280

279281
### Maximizing
280282

0 commit comments

Comments
 (0)