Answers for "circular printer hackerrank solution"

Go
0

circular printer hackerrank solution

package main

import (
	"fmt"
	"math"
)

func getTimes(s string) int64 {

	vowels := make(map[string]int)

	for i := 1; i <= 26; i++ {
		str := 64 + i
		vowels[string(str)] = i - 1
	}

	a := float64(vowels["A"])
	b := float64(vowels[string(s[0])])

	numb := math.Min(math.Abs(a-b), 26-math.Abs(a-b))

	for i := 1; i < len(s); i++ {
		str1 := float64(vowels[string(s[i])])
		str2 := float64(vowels[string(s[i-1])])
		result := math.Abs(str1 - str2)

		if result > 13 {
			numb += 26 - result
		} else {
			numb += result
		}
	}

	return int64(numb)
}

func main() {
	records := getTimes("AZGB")
	fmt.Println(records)
}
Posted by: Guest on October-30-2021
0

circular printer hackerrank solution

function getTimes(s) {
	let vowels = []
	const strings = Array.from(s)

	for (let i = 1; i <= 26; i++) {
		let values = i - 1
		let keys = 64 + i

		if (values <= 25) {
			vowels.push(String.fromCharCode(keys))
		}
	}

	const str1 = vowels.indexOf('A')
	const str2 = vowels.indexOf(s[0])
	let numb = Math.min(Math.abs(str1 - str2), 26 - Math.abs(str1 - str2))

	for (let i = 1; i < strings.length; i++) {
		const str1 = vowels.findIndex((val) => val == [strings[i]])
		const str2 = vowels.findIndex((val) => val == [strings[i - 1]])
		let result = Math.abs(str1 - str2)

		if (result > 13) {
			numb += 26 - result
		} else {
			numb += result
		}
	}

	return numb
}

;(function () {
	const record = getTimes('BZA')
	console.log(record)
})()
Posted by: Guest on October-30-2021

Code answers related to "circular printer hackerrank solution"

Browse Popular Code Answers by Language