-
Notifications
You must be signed in to change notification settings - Fork 10
Homework;) #12
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
Draft
AleksandrsStukalovs
wants to merge
3
commits into
prog-1:main
Choose a base branch
from
34thSchool:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Homework;) #12
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,17 @@ | ||
package main | ||
|
||
// Outputs first n fibonacci numbers | ||
func fibonacci(n int) []int { | ||
s := []int{0, 1} | ||
fib(n, &s) | ||
return s | ||
} | ||
|
||
func fib(n int, s *[]int) { | ||
if len(*s) < n { | ||
(*s) = append((*s), (*s)[len(*s)-1]+(*s)[len(*s)-2]) | ||
fib(n, s) | ||
} | ||
// Time complexity O(N) -- Because fib() gets called 1 time per element | ||
// Space complexity O(N) -- Due to new slice allocation happening every time append() is called | ||
} |
This file contains hidden or 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,26 @@ | ||
package main | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
func isWhole(n float64) bool { | ||
return n == math.Round(n) | ||
} | ||
|
||
func isPowerOfTwo(n int) bool { | ||
log := math.Log2(float64(n)) | ||
return isWhole(log) | ||
// Time complexity O(1) | ||
// Space complexity O(1) | ||
} | ||
|
||
// func main() { | ||
// n := 16 | ||
|
||
// if isPowerOfTwo(n) { | ||
// fmt.Println("YEAH") | ||
// } else { | ||
// fmt.Println("NOPE") | ||
// } | ||
// } |
This file contains hidden or 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,55 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestFibonacci(t *testing.T) { | ||
t.Run("Fibonacci test", func(t *testing.T) { | ||
want := []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34} | ||
got := fibonacci(10) | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got = %v, want = %v", got, want) | ||
} | ||
}) | ||
} | ||
|
||
func TestIsPowerOfTwo(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
input int | ||
want bool | ||
}{ | ||
{"Is power of two", 8, true}, | ||
{"Not power of two", 9, false}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := isPowerOfTwo(tc.input) | ||
if got != tc.want { | ||
t.Errorf("got = %v, want = %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSumDigits(t *testing.T) { | ||
t.Run("Digit sum test", func(t *testing.T) { | ||
want := 4 | ||
got := sumDigits(121) | ||
if want != got { | ||
t.Errorf("got = %v, want = %v", got, want) | ||
} | ||
}) | ||
} | ||
|
||
func TestMaxNum(t *testing.T) { | ||
t.Run("Max num test", func(t *testing.T) { | ||
want := []int{3, 2} | ||
input := []int{1, 2, 3} | ||
got := maxNum(input) | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got = %v, want = %v", got, want) | ||
} | ||
}) | ||
} |
This file contains hidden or 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,43 @@ | ||
package main | ||
|
||
func maxNumLoop(s []int) int { | ||
max := s[0] | ||
for _, el := range s { | ||
if el > max { | ||
max = el | ||
} | ||
} | ||
|
||
return max | ||
} | ||
|
||
func maxNum(s []int) []int { | ||
// Getting max num index | ||
var maxTwo []int | ||
max := maxN(0, 1, s) | ||
maxTwo = append(maxTwo, s[max]) | ||
|
||
// Removing max num from array | ||
tmp := s[:max] | ||
s = append(tmp, s[max+1:]...) | ||
|
||
// Getting max num one more time | ||
max = maxN(0, 1, s) | ||
maxTwo = append(maxTwo, s[max]) | ||
|
||
return maxTwo | ||
|
||
// Time complexity O(N) -- ~2N times maxN will be called | ||
// Space complexity O(N) -- ~2N times we will add new return address, | ||
// and also tmp and s allocation, but it's constant | ||
} | ||
func maxN(max int, i int, s []int) int { | ||
if s[i] > s[max] { | ||
max = i | ||
} | ||
if i < len(s)-1 { | ||
return maxN(max, i+1, s) | ||
} else { | ||
return max | ||
} | ||
} |
This file contains hidden or 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,21 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func numbers1(n int) { | ||
|
||
fmt.Println(n) | ||
|
||
if n > 1 { | ||
numbers1(n - 1) | ||
} | ||
} | ||
|
||
func numbers2(n int) { | ||
|
||
if n > 1 { | ||
numbers2(n - 1) | ||
} | ||
|
||
fmt.Println(n) | ||
} |
This file contains hidden or 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,20 @@ | ||
package main | ||
|
||
// func sumDigitsLoop(n int) int { | ||
// var sum int | ||
// for ; n != 0; n /= 10 { | ||
// sum += n % 10 | ||
// } | ||
// return sum | ||
// } | ||
|
||
func sumDigits(n int) int { | ||
if n != 0 { | ||
return n%10 + sumDigits(n/10) | ||
} else { | ||
return 0 | ||
} | ||
|
||
// Time complexity O(N) -- One function call for each digit | ||
// Space complexity O(N) -- One return address for each digit | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please implement this function without using
math
package (you will need a recursion).