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
21 changes: 21 additions & 0 deletions armstrong.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
//
// For example, 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153).
package problems

import (
"fmt"
"math"
"strconv"
)

func CheckArmstrong() {
num := 153
n := len(strconv.Itoa(num))
sum := 0
for num > 0 {
sum += int(math.Pow(float64(num%10), float64(n)))
num = num / 10
}
fmt.Println(sum)
}
54 changes: 54 additions & 0 deletions basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package concurrency

import (
"fmt"
"reflect"
"sync"
)

func BasicChanOp(nums []int) {
// fmt.Println("basic op", nums)
inputChan := make(chan int)

wc := 2
i := 0
var wg sync.WaitGroup

opList := make([]<-chan int, wc)

for i < wc {
wg.Add(1)
opList[i] = readNprint(inputChan, &wg)
i += 1
}

go func() {
defer close(inputChan)
for _, num := range nums {
inputChan <- num
}
}()

for _, opchn := range opList {
for op := range opchn {
fmt.Println(op)
}

}
wg.Wait() //
//why wait is here bcz we have added wait.Done() once we returns the output, so go routine should wait to consume the outputalso
// else it will be called prematurly closing of op
}

func readNprint(inputChan <-chan int, wg *sync.WaitGroup) <-chan int {
opChan := make(chan int)
go func() {
defer close(opChan)
defer wg.Done()
for input := range inputChan {
fmt.Println(input, reflect.TypeOf(input))
opChan <- input * input
}
}()
return opChan
}
9 changes: 9 additions & 0 deletions factorial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package problems

func Factorial(n int) int {
if n == 1 {
return 1
}
return n * Factorial(n-1)

}
11 changes: 11 additions & 0 deletions fibonacci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package problems

func Fibonacci(n int) int {

if n <= 1 {
return n
}
res := Fibonacci((n - 1)) + Fibonacci(n-2)
// fmt.Println("fib num of n: ", res)
return res
}
1 change: 1 addition & 0 deletions fileOperations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package problems
70 changes: 70 additions & 0 deletions getRequestConcurrently.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package httpresp

import (
"fmt"
"io"
"net/http"
"sync"
"time"
)

type Response struct {
Body string
StatusCode int
}

func BasicOP(urls []string) []Response {
var responseList []Response
wc := 2
var wg sync.WaitGroup
inputChan := make(chan string)
var collective_resp []<-chan Response
for i := range wc {
fmt.Println(i)
wg.Add(1)
collective_resp = append(collective_resp, hitURl(inputChan, &wg))
}

go func() {
defer close(inputChan)
for _, url := range urls {
inputChan <- url
}
}()

for _, resp := range collective_resp {
go func(resp <-chan Response) {
for response := range resp {
responseList = append(responseList, response)
}
}(resp)

}

wg.Wait()
return responseList
}

func hitURl(inputChan chan string, wg *sync.WaitGroup) <-chan Response {
response := make(chan Response)
go func() {
defer close(response)
defer wg.Done()
for url := range inputChan {
client := http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(url)
if err != nil {
fmt.Println("eeror received")
fmt.Println(err.Error())
response <- Response{Body: err.Error(), StatusCode: resp.StatusCode}
}
result, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
}
response <- Response{Body: string(result), StatusCode: resp.StatusCode}
}

}()
return response
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module practice_set

go 1.24
48 changes: 48 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"log"
"net/http"
httpresp "practice_set/httpReq"
)

//"practice_set/concurrency"

// Matches the module name defined in your go.mod

// func checkPlndrome(number int) bool {
// // Convert the integer to a string

// num := strconv.Itoa(number)

// i, j := 0, len(num)-1
// for i < j {
// if num[i] != num[j] {
// return false
// }
// i += 1
// j -= 1
// }
// return true
// }

func main() {
// num,i := 12364321, 0
// fmt.Println(checkPlndrome(num))a
// armstrong.CheckArmstrong()
// num = 3
// for i := 1; i < 10; i++ {
// fmt.Println(problems.Factorial(i))
// }
// fmt.Println(problems.Swap(2, 3))
// concurrency.Fibnacci_num([]int{1, 2, 3, 45})
responseList := httpresp.BasicOP([]string{"https://www.example.com",
"https://www.google.com",
"https://www.github.com"})
for _, resp := range responseList {
if resp.StatusCode != http.StatusOK {
log.Fatalf("%v", resp.Body)
}
}

}
9 changes: 9 additions & 0 deletions swap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package problems

func Swap(num1 int, num2 int) (int, int) {
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2

return num1, num2
}