Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 380
Good morning! Here's your coding interview problem for today.

This problem was asked by Nextdoor.

Implement integer division without using the division operator. Your function should return a tuple of (dividend, remainder) and it should take two numbers, the product and divisor.

For example, calling `divide(10, 3)` should return `(3, 1)` since the divisor is 3 and the remainder is 1.

Bonus: Can you do it in O(log n) time?

Pull request description:
Day380
Resolves #766.

Patch:
diff --git a/README.md b/README.md
index e3d0159..aa76e21 100644
--- a/README.md
+++ b/README.md
@@ -375,4 +375,5 @@ problems from
 * [Day 377](https://github.com/vaskoz/dailycodingproblem-go/issues/760)
 * [Day 378](https://github.com/vaskoz/dailycodingproblem-go/issues/762)
 * [Day 379](https://github.com/vaskoz/dailycodingproblem-go/issues/764)
+* [Day 380](https://github.com/vaskoz/dailycodingproblem-go/issues/766)
 
diff --git a/day380/problem.go b/day380/problem.go
new file mode 100644
index 0000000..8f3f499
--- /dev/null
+++ b/day380/problem.go
@@ -0,0 +1,43 @@
+package day380
+
+// DivisionBrute returns the quotient and remainder of division
+// without using the division operator using brute-force.
+func DivisionBrute(dividend, divisor int) (int, int) {
+	if divisor == 0 {
+		panic("divide by zero")
+	}
+
+	var dividendNeg, divisorNeg bool
+
+	if dividend < 0 {
+		dividendNeg = true
+		dividend = -dividend
+	}
+
+	if divisor < 0 {
+		divisorNeg = true
+		divisor = -divisor
+	}
+
+	var quotient int
+
+	for dividend >= 0 {
+		dividend -= divisor
+		quotient++
+	}
+
+	remainder := dividend + divisor
+	quotient--
+
+	if dividendNeg {
+		quotient = -quotient
+		remainder = -remainder
+	}
+
+	if divisorNeg {
+		quotient = -quotient
+		remainder = -remainder
+	}
+
+	return quotient, remainder
+}


Test patch:
diff --git a/day380/problem_test.go b/day380/problem_test.go
new file mode 100644
index 0000000..f5dfaf5
--- /dev/null
+++ b/day380/problem_test.go
@@ -0,0 +1,44 @@
+package day380
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	dividend, divisor   int
+	quotient, remainder int
+}{
+	{10, 3, 3, 1},
+	{9, 3, 3, 0},
+	{-9, 3, -3, 0},
+	{9, -3, -3, 0},
+	{-9, -3, 3, 0},
+}
+
+func TestDivisionBrute(t *testing.T) {
+	t.Parallel()
+
+	for _, tc := range testcases {
+		if q, r := DivisionBrute(tc.dividend, tc.divisor); q != tc.quotient || r != tc.remainder {
+			t.Errorf("Expected (%v,%v), got (%v,%v)", tc.quotient, tc.remainder, q, r)
+		}
+	}
+}
+
+func TestDivisionBruteDivideByZero(t *testing.T) {
+	t.Parallel()
+
+	defer func() {
+		if err := recover(); err == nil {
+			t.Errorf("expected a panic when divisor is zero")
+		}
+	}()
+	DivisionBrute(1, 0)
+}
+
+func BenchmarkDivisionBrute(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			DivisionBrute(tc.dividend, tc.divisor)
+		}
+	}
+}