Giter VIP home page Giter VIP logo

codility-solutions's Introduction

Lesson 01 Binary Gap (calculate max gap of '0' in binary numbers)

JavaScript

function solution(N) {
    let binaryNumbers = N.toString(2);
    let maxGap = 0;
    let currentGap = 0;

    for (let digit of binaryNumbers) {
        // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
        if (digit === '0') {
            currentGap++;
        } else {
            maxGap = Math.max(maxGap, currentGap) //function returns the largest of the zero or more numbers
            // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max)
            currentGap = 0;
        }
    }
    return maxGap;
}

console.log(solution(1041)) 
// binary = 10000010001; maxGap = 5;
console.log(solution(32)) 
// binary = 100000; maxGap = 0;
console.log(solution(529)) 
// binary = 1000010001; maxGap = 4;

GoLang ๐Ÿ‘ˆ

package solution

import (
	"fmt"
	"strconv"
)

var binaryNumbers = 0
var maxGap = 0
var currentGap int = 0

func Max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func Solution(N int) int {
	var binaryNumbers = strconv.FormatInt(int64(N), 2)
	// https://pkg.go.dev/strconv#FormatInt
	for _, digit := range binaryNumbers {
		// https://gobyexample.com/range
		if digit == '0' {
			currentGap++
		} else {
			maxGap = Max(maxGap, currentGap)
			currentGap = 0
		}
	}
	return maxGap
}

func main() {
	fmt.Println(Solution(1041))
	// binary = 10000010001; maxGap = 5;
}

Lesson 02 Cyclic Rotation (Rotate an array to the right by a given number of steps)

function solution(A, K) {
    for (i = 0; i < K; i++) {
        A.unshift(A.pop());
    }
    return A;
}

// The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
// The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop

A = [3, 8, 9, 7, 6]
K = 3
console.log(solution(A, K)) 
// [9, 7, 6, 3, 8]

GoLang ๐Ÿ‘ˆ

package solution

import "fmt"

func Solution(A []int, K int) []int {
	if len(A) <= 1 || K == 0 {
		return A
	}
	// a is the last element followed by the n elements
	// previously before the last element
	for i := 0; i < K; i++ {
		A = append(A[len(A)-1:], A[:len(A)-1]...)
	}
	return A
}
func main() {
	fmt.Println(Solution([]int{2,4,5,7}, 2))
	//[5 7 2 4]
}


Explanation:
package main

import (
	"fmt"
)

func main() {

	/*
	   Basically, we're getting the last value of the slice and assigning it as the first element.
	   After that, we get all the other values that originally came before it and put them in front of the last element (which is now the first one).

	   On how `[:]` works: https://tour.golang.org/moretypes/10
	*/

	// Given the original slice
	s := []int{2, 3, 4, 7}
	fmt.Println("s:", s)

	// We get the last value in s
	lastVal := s[len(s)-1:]
	fmt.Println(lastVal)

	// Then we get all the other values in s, except the last one
	valuesBeforeLastVal := s[:len(s)-1]

	// Finally, we assign s to receive the lastVal together with all the values that came before lastVal (valuesBeforeLastVal)
	s = append(lastVal, valuesBeforeLastVal...)

	fmt.Println("result:", s)
}

codility-solutions's People

Contributors

ebazhanov avatar monkrus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.