-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from WGrape/testing
feat: add new packages
- Loading branch information
Showing
9 changed files
with
147 additions
and
27 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
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
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,30 @@ | ||
// Copyright 2022 The golib Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style(https://en.wikipedia.org/wiki/BSD_licenses) | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package binary provides some operations of binary number and decimal number. | ||
package binary | ||
|
||
import "fmt" | ||
|
||
// DecimalNumberToBinary Return a binary array. | ||
func DecimalNumberToBinary(num int) ([]int, []string, string) { | ||
var ( | ||
binary []int | ||
binaryString []string | ||
str string | ||
) | ||
|
||
for num != 0 { | ||
var ( | ||
bit = num % 2 | ||
bitStr = fmt.Sprintf("%d", bit) | ||
) | ||
binary = append([]int{bit}, binary...) | ||
binaryString = append([]string{bitStr}, binaryString...) | ||
str = bitStr + str | ||
num = num / 2 | ||
} | ||
|
||
return binary, binaryString, str | ||
} |
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,15 @@ | ||
package binary | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// TestDecimalNumberToBinary Test the function DecimalNumberToBinary | ||
func TestDecimalNumberToBinary(t *testing.T) { | ||
var intArr, strArr, str = DecimalNumberToBinary(4) | ||
if !reflect.DeepEqual(intArr, []int{1, 0, 0}) || !reflect.DeepEqual(strArr, []string{"1", "0", "0"}) || str != "100" { | ||
t.Fail() | ||
return | ||
} | ||
} |
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,18 @@ | ||
package permutation | ||
|
||
// Subsets Given an integer array nums of unique elements, return all possible subsets. | ||
func Subsets(nums []int) [][]int { | ||
var result [][]int | ||
for _, num := range nums { | ||
var newSubsets [][]int | ||
for _, item := range result { | ||
tempItem := make([]int, len(item)) | ||
copy(tempItem, item) | ||
newSubsets = append(newSubsets, append(tempItem, num)) | ||
} | ||
result = append(result, newSubsets...) | ||
result = append(result, []int{num}) | ||
} | ||
result = append(result, []int{}) | ||
return result | ||
} |
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,12 @@ | ||
package permutation | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSubsets(t *testing.T) { | ||
if len(Subsets([]int{0, 1, 3, 5, 9})) != 32 { | ||
t.Fail() | ||
return | ||
} | ||
} |