diff --git a/armstrong.go b/armstrong.go new file mode 100644 index 0000000..3191714 --- /dev/null +++ b/armstrong.go @@ -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) +} diff --git a/basic.go b/basic.go new file mode 100644 index 0000000..249987a --- /dev/null +++ b/basic.go @@ -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 +} diff --git a/factorial.go b/factorial.go new file mode 100644 index 0000000..f77fbc6 --- /dev/null +++ b/factorial.go @@ -0,0 +1,9 @@ +package problems + +func Factorial(n int) int { + if n == 1 { + return 1 + } + return n * Factorial(n-1) + +} diff --git a/fibonacci.go b/fibonacci.go new file mode 100644 index 0000000..298dd2f --- /dev/null +++ b/fibonacci.go @@ -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 +} diff --git a/fileOperations.go b/fileOperations.go new file mode 100644 index 0000000..180652a --- /dev/null +++ b/fileOperations.go @@ -0,0 +1 @@ +package problems diff --git a/getRequestConcurrently.go b/getRequestConcurrently.go new file mode 100644 index 0000000..91700d4 --- /dev/null +++ b/getRequestConcurrently.go @@ -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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..69db0e8 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module practice_set + +go 1.24 diff --git a/main.go b/main.go new file mode 100644 index 0000000..57c53c1 --- /dev/null +++ b/main.go @@ -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) + } + } + +} diff --git a/swap.go b/swap.go new file mode 100644 index 0000000..470229c --- /dev/null +++ b/swap.go @@ -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 +}