-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package day214 | ||
|
||
import "strconv" | ||
|
||
// LengthLongestConsecutiveRunOnesBitShift calculates the longest | ||
// run of 1's using bit shifting. | ||
func LengthLongestConsecutiveRunOnesBitShift(n int) int { | ||
var max, count int | ||
for n != 0 { | ||
if n&1 == 1 { | ||
count++ | ||
} else { | ||
if count > max { | ||
max = count | ||
} | ||
count = 0 | ||
} | ||
n >>= 1 | ||
} | ||
if count > max { | ||
return count | ||
} | ||
return max | ||
} | ||
|
||
// LengthLongestConsecutiveRunOnesString converts the number | ||
// into a binary representation string. | ||
func LengthLongestConsecutiveRunOnesString(n int) int { | ||
var max, count int | ||
for _, digit := range strconv.FormatInt(int64(n), 2) { | ||
if digit == '1' { | ||
count++ | ||
} else { | ||
if count > max { | ||
max = count | ||
} | ||
count = 0 | ||
} | ||
} | ||
if count > max { | ||
return count | ||
} | ||
return max | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package day214 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
n int | ||
longestConsecutiveOnes int | ||
}{ | ||
{156, 3}, | ||
{20432348234, 3}, | ||
{65535, 16}, | ||
} | ||
|
||
func TestLengthLongestConsecutiveRunOnesBitShift(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := LengthLongestConsecutiveRunOnesBitShift(tc.n); result != tc.longestConsecutiveOnes { | ||
t.Errorf("Given n=%d, expected %d, got %d", tc.n, tc.longestConsecutiveOnes, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLengthLongestConsecutiveRunOnesBitShift(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
LengthLongestConsecutiveRunOnesBitShift(tc.n) | ||
} | ||
} | ||
} | ||
|
||
func TestLengthLongestConsecutiveRunOnesString(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := LengthLongestConsecutiveRunOnesString(tc.n); result != tc.longestConsecutiveOnes { | ||
t.Errorf("Given n=%d, expected %d, got %d", tc.n, tc.longestConsecutiveOnes, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLengthLongestConsecutiveRunOnesString(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
LengthLongestConsecutiveRunOnesString(tc.n) | ||
} | ||
} | ||
} |