Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions fibonacci.go
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
21 changes: 21 additions & 0 deletions isPowerOf2.go
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)
Comment on lines +15 to +16
Copy link

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).


}

//Time complexity: O(1) - no loops, no recursion, one calculation
//Space complexity: O(1) - only input variable and logN variable
91 changes: 91 additions & 0 deletions main_test.go
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
}
49 changes: 49 additions & 0 deletions maxNumber.go
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 ;)
Copy link

Choose a reason for hiding this comment

The 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--
}
}
21 changes: 21 additions & 0 deletions numbers.go
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)
// }
// }
33 changes: 33 additions & 0 deletions partition.go
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]

}
}
44 changes: 44 additions & 0 deletions partition_test.go
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
Copy link

Choose a reason for hiding this comment

The 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
}
26 changes: 26 additions & 0 deletions sumDigits.go
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)