Saturday, September 30, 2023
HomeSoftware EngineeringAlternate Capitalization in Golang

Alternate Capitalization in Golang


The problem

Given a string, capitalize the letters that occupy even indexes and odd indexes individually, and return as proven beneath. Index “ will likely be thought-about even.

For instance, capitalize("abcdef") = ['AbCdEf', 'aBcDeF']. See check circumstances for extra examples.

The enter will likely be a lowercase string with no areas.

The answer in Golang

Possibility 1:

package deal resolution

import "unicode"

func Capitalize(s string) []string {
  a, b := []rune(s),[]rune(s)
  for i := vary a {
    if ipercent2 == 0 {
      a[i] = unicode.ToUpper(a[i])
    } else {
      b[i] = unicode.ToUpper(b[i])
    }
  }
  return []string{string(a), string(b)}
}

Possibility 2:

package deal resolution

import "strings"

func Capitalize(st string) []string {
  s1 := ""
  s2 := ""
  for i, c :=vary st {
    if ipercent2==0 {
      s1 += strings.ToUpper(string(c))
      s2 += strings.ToLower(string(c))
    } else {
      s1 += strings.ToLower(string(c))
      s2 += strings.ToUpper(string(c))
    }
  }
  
  return []string{s1,s2}
}

Possibility 3:

package deal resolution

import "unicode"

func Capitalize(st string) []string {
  outcome := make([]string, 2)
  for p, s := vary st {
    outcome[p % 2 & 1] += string(unicode.ToUpper(s))
    outcome[p % 2 & 1 ^ 1] += string(unicode.ToLower(s))
  }
  return outcome
}

Check circumstances to validate our resolution

package deal our_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)

func dotest(st string, exp []string) {
    var ans = Capitalize(st)
    Anticipate(ans).To(Equal(exp))
}

var _ = Describe("Instance exams", func() {
  It("It ought to work for primary exams", func() {         
    dotest("abcdef", []string{"AbCdEf", "aBcDeF"})   
    dotest("abracadabra", []string{"AbRaCaDaBrA", "aBrAcAdAbRa"})
    dotest("indexinglessons", []string{"TeStInG", "tEsTiNg"})
    dotest("codingisafunactivity", []string{"ThIsIsCoOl", "tHiSiScoOl"})
  })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments