-
Notifications
You must be signed in to change notification settings - Fork 10
Homework #2
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
Open
vay4ie
wants to merge
2
commits into
prog-1:main
Choose a base branch
from
34th-School: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.
Open
Homework #2
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,23 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main23() { | ||
fibonacci(0) | ||
} | ||
|
||
//my interpritation of fibonacci using recursion | ||
func fibonacci(n int) { | ||
var n1, n2 = 1, 0 | ||
fibo(n1, n2, n) | ||
} | ||
|
||
func fibo(n1, n2, n int) { | ||
if n2 <= n { | ||
fmt.Println(n2) | ||
fibo(n1+n2, n1, n) | ||
} | ||
} | ||
|
||
//Time complexity: O(n) - calling recursion till we will not reach value of N | ||
//Space complexity: O(1) - creating 2 variables at the start and then creating 3 input variables for each recursive function |
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" | ||
"math" | ||
) | ||
|
||
func main() { | ||
fmt.Println(isPowerOf2(1)) | ||
} | ||
func isPowerOf2(n int) bool { | ||
if n < 2 { | ||
return false | ||
} | ||
logN := math.Log2(float64(n)) //taking logarithm with base of 2 from N | ||
return logN == math.Round(logN) //if number is integer (has no leftover) | ||
|
||
} | ||
|
||
//Time complexity: O(1) - no loops, no recursion, one calculation | ||
//Space complexity: O(1) - only input variable and logN variable |
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,91 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func ExampleAllNum() { | ||
allNum(1, 10) | ||
|
||
//Output: | ||
//1 | ||
//2 | ||
//3 | ||
//4 | ||
//5 | ||
//6 | ||
//7 | ||
//8 | ||
//9 | ||
//10 | ||
//10 | ||
//9 | ||
//8 | ||
//7 | ||
//6 | ||
//5 | ||
//4 | ||
//3 | ||
//2 | ||
//1 | ||
} | ||
|
||
func ExampleFibonacci() { | ||
fibonacci(10) | ||
fibonacci(1) | ||
fibonacci(0) | ||
|
||
//Output: | ||
//0 | ||
//1 | ||
//1 | ||
//2 | ||
//3 | ||
//5 | ||
//8 | ||
//0 | ||
//1 | ||
//1 | ||
//0 | ||
} | ||
|
||
func ExamplePowerOf2() { | ||
fmt.Println(isPowerOf2(256)) | ||
fmt.Println(isPowerOf2(5)) | ||
fmt.Println(isPowerOf2(65536)) | ||
fmt.Println(isPowerOf2(0)) | ||
fmt.Println(isPowerOf2(1)) | ||
|
||
//Output: | ||
//true | ||
//false | ||
//true | ||
//false | ||
//false | ||
} | ||
|
||
func ExampleSumDigits() { | ||
fmt.Println(sumDigits(343)) | ||
fmt.Println(sumDigits(11)) | ||
fmt.Println(sumDigits(1)) | ||
fmt.Println(sumDigits(0)) | ||
fmt.Println(sumDigits(1000)) | ||
|
||
//Output: | ||
//10 | ||
//2 | ||
//1 | ||
//0 | ||
//1 | ||
} | ||
|
||
func ExampleMaxNumber() { | ||
maxNumber([]int{7, 1, 2, 3, 5, 4}) | ||
maxNumber([]int{0}) | ||
maxNumber([]int{0, 1}) | ||
maxNumber([]int{-100, -50, -200}) | ||
|
||
//Output: | ||
//First max number: 7 Second max number: 5 | ||
//First max number: 0 | ||
//First max number: 1 Second max number: 0 | ||
//First max number: -50 Second max number: -100 | ||
} |
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,49 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main2() { | ||
s := []int{ /*7, 1, 2, 3, 5, 4*/ 0, 1} | ||
maxNumber(s) | ||
} | ||
|
||
// Time Complexity: O(N^2) - Worst case | ||
// Space Complexity: O(logN) | ||
|
||
|
||
func maxNumber(s []int) { | ||
if len(s)-1 > 0 { | ||
quickSort(s) // programmers must be lazy ;) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the most implementation. Can you improve it? |
||
fmt.Println("First max number:", s[len(s)-1], "Second max number:", s[len(s)-2]) | ||
} else { | ||
fmt.Println("First max number:", s[len(s)-1]) | ||
} | ||
} | ||
|
||
func quickSort(s []int) { | ||
if len(s) < 2 { | ||
return | ||
} | ||
p := partition(s) + 1 | ||
quickSort(s[:p]) | ||
quickSort(s[p:]) | ||
} | ||
|
||
func partition(s []int) (i int) { | ||
l, r := 0, len(s)-1 | ||
p := s[r/2] | ||
for { | ||
for s[l] < p { | ||
l++ | ||
} | ||
for s[r] > p { | ||
r-- | ||
} | ||
if l >= r { | ||
return l | ||
} | ||
s[l], s[r] = s[r], s[l] | ||
//l++ | ||
r-- | ||
} | ||
} |
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 allNum(i, n int) { | ||
fmt.Println(i) | ||
if i < n { | ||
allNum(i+1, n) | ||
} | ||
if i > 0 { | ||
fmt.Println(i) | ||
i-- | ||
} | ||
} | ||
|
||
// func allNumOld(n int) { | ||
// if n > 0 { | ||
// fmt.Println(n) | ||
// allNumOld(n - 1) | ||
// } | ||
// } |
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,33 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
s := []int{9, 6, 1, 7, 5, 9, 6, 5, 2, 0} | ||
fmt.Println("Initial s: ", s) | ||
i := partition(s, 9) | ||
fmt.Println("I: ", i) | ||
fmt.Println("Recieved s: ", s) | ||
fmt.Println("Left side: ", s[:i]) // < i | ||
fmt.Println("Right side: ", s[i:]) // other | ||
} | ||
|
||
func partition(s []int, pivot int) (i int) { | ||
l, r := 0, len(s)-1 | ||
p := s[pivot] | ||
for { | ||
for s[l] < p { | ||
l++ | ||
} | ||
for s[r] >= p && r > 0 { // 3rd works with validation of r on 0 | ||
r-- | ||
} | ||
if l >= r && r > 0 { | ||
return r + 1 | ||
} else if l >= r && r <= 0 { | ||
return r | ||
} | ||
s[l], s[r] = s[r], s[l] | ||
|
||
} | ||
} |
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,44 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestPartition(t *testing.T) { | ||
for _, tc := range []tests{ | ||
{"#1: no repeated elements", Input{[]int{8, 4, 1, 0, 5, 9, 3, 7, 2, 6}, 4}, 5}, | ||
{"#2: repeated elements", Input{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 6}, 9}, 4}, | ||
{"#3: empty left side", Input{[]int{9, 6, 1, 7, 5, 9, 6, 5, 2, 0}, 9}, 0}, | ||
} { | ||
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add more tests (not only given examples). |
||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
input := tc.input | ||
|
||
got := partition(input.s, input.p) | ||
|
||
check(input.s[:got], input.s[got], t) | ||
|
||
if got != tc.want { | ||
t.Errorf("got = %v, want = %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func check(s []int, p int, t *testing.T) { | ||
//checking if each el in left side is smaller then pivot | ||
for _, elem := range s { | ||
if elem > p { | ||
t.Errorf("%v > %v", elem, p) | ||
} | ||
} | ||
} | ||
|
||
type tests struct { | ||
name string | ||
input Input | ||
want int | ||
} | ||
|
||
type Input struct { | ||
s []int | ||
p int | ||
} |
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 "fmt" | ||
|
||
func main44() { | ||
fmt.Println(sumDigits(1)) | ||
} | ||
|
||
func sumDigits(n int) int { | ||
var sum, remainder int | ||
return sDigits(n, sum, remainder) | ||
} | ||
|
||
func sDigits(n, sum, remainder int) int { | ||
if n > 0 { | ||
remainder = n % 10 | ||
n /= 10 | ||
sum += remainder | ||
return sDigits(n, sum, remainder) | ||
} else { | ||
return sum | ||
} | ||
} | ||
|
||
//Time complexity: O(n) | ||
//Space complexity: O(n) |
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).