Saturday, January 30, 2016

Go Code to Find GCD / HCF of Two or More Integers


In mathematics, the greatest common divisor (GCD) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder.
                                                                                                                                  --- src: Wikipedia

For example, the GCD of 20 and 25 is 5.

The GCD is also known as the: 
      • Greatest common factor (GCF), 
      • Highest common factor (HCF), 
      • Greatest common measure (GCM), 
      • Highest common divisor (HCD)

Prerequisites to understand the following code:
Code follows:

package main

import "fmt"

func main() {

 var n int
 var num int
 var result int

 fmt.Println("How many integers GCD do you want to calculate?")
 fmt.Scanf("%d\n", &n)
 fmt.Println("Enter space separated integers:")
 numList := map[int]int{}

 for i := 1; i <= n; i++ {
  fmt.Scanf("%d", &num)
  numList[i] = num
 }
 //This is the result for only 2 integers
 result = gcd(numList[1], numList[2])

 //for loop in case there're more than 2 ints
 for j := 3; j <= n; j++ {
  result = gcd(result, numList[j])
 }

 fmt.Println("The GCD/HCF of given integers is: ", result)
}

//Func to implement Euclid Algo
func gcd(x, y int) int {
 for y != 0 {
  x, y = y, x%y
 }
 return x
}

Output



Play with the above code

Please share your feedback for improvement of this code.

Monday, January 25, 2016

Check if a String is Palindrome

The best way to learn a new computer language is by solving puzzles, games & code challenges. It's so much fun apart from the satisfaction you get once you crack them! Solving puzzles can push your language skills to the next level apart from improving your problem solving skills. From my experience I can tell you that in the beginning they're in fact a tough-nut-to-crack but once you persist enough to solve a few of them... it's kinda addictive! Start with the simple ones... and don't quit! 

For impatient learners who don't like preaching:

Play with the code and see the results in your browser - Once this page is open, click run and follow the instructions.

Another post that might interest you:


Tip - Take loads of break while solving puzzles. Sometimes you might need to take a break for a whole day so that you can reset your thoughts into a different direction. Also, if you've solved a puzzle in a single sitting, it's not a puzzle in the first place :) 

In this post we'll see how to find if an input string is a palindrome or not using Golang.

What is a Palindrome?

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.

Examples of Palindrome
      • Madam
      • Civic
      • Noon
      • Refer
      • Rotor
      • Level
      • Malayalam
      • 1230321
Problem# 1

Enter a string and find out if the string is a Palindrome or not.

Solution# 1


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
 "fmt"
 "strings"
)

func main() {

 var ip string
 fmt.Println("Enter string:")
 fmt.Scanf("%s\n", &ip)
 ip = strings.ToLower(ip)
 fmt.Println(isP(ip))
}
//Function to test if the string entered is a Palindrome
func isP(s string) string { mid := len(s) / 2 last := len(s) - 1 for i := 0; i < mid; i++ { if s[i] != s[last-i] { return "NO. It's not a Palimdrome." } } return "YES! You've entered a Palindrome" }


Now let us build an advanced version of the problem where we can see a practical usage of Slices.

Problem# 2

Enter fixed number of strings, each in a new line, and find out if they are palindrome or not.


Solution# 2

To understand this solution you must have working knowledge of Slices in Go Programming.


package main

import (
 "fmt"
 "strings"
)

func main() {
 var n int
 var ip string
 fmt.Println("How many strings you want to test?")
 fmt.Scanf("%d\n", &n)
 strSlice := make([]string, 0)
 for i := 0; i < n; i++ {

  fmt.Printf("Enter %d string\n", i+1)
  fmt.Scanf("%s\n", &ip)
  ip := strings.ToLower(ip)
  strSlice = append(strSlice, ip)
 }
 for _, v := range strSlice {
  fmt.Println(isP(v))
 }
}

//Function to test if the string entered is a Palindrome
func isP(s string) string {

 mid := len(s) / 2
 last := len(s) - 1
 for i := 0; i < mid; i++ {
  if s[i] != s[last-i] {
   return "NO. " + s + " is Not a Palindrome"
  }
 }
 return "YES. " + s + " is a Palindrome"
}

Output# 2




Play with the above code

Exercise

If you find this interesting you can improve this code:

  1. To take care of the null/empty input strings.
  2. To return appropriate message if number of strings to test is entered zero.