Add horse-racing-duals

[?]
Dec 3, 2020, 6:41 PM
PPAU3LBGISEACFGJHQDYQXZUFFLMMQSPYWBG6MTR6WPUKS4L6SWQC

Dependencies

  • [2] 44BSZW7B codingame.com: solve bank-robbers

Change contents

  • file addition: horse-racing-duals (dxwrxwrx-r)
    [2.16]
  • file addition: main_test.go (-xw-x--x--)
    [0.21]
    package main
    import (
    "testing"
    "github.com/stretchr/testify/assert"
    )
    func TestHorseDual(t *testing.T) {
    cases := []struct {
    horses int
    power []int
    expected int
    }{
    {3, []int{5, 8, 9}, 1},
    {10, []int{5, 15, 17, 3, 8, 11, 28, 6, 55, 7}, 1},
    }
    for _, tc := range cases {
    t.Run("", func(t *testing.T) {
    assert.Equal(t, tc.expected, HorseDuals(tc.horses, tc.power))
    })
    }
    }
  • file addition: main.go (-xw-x--x--)
    [0.21]
    package main
    import (
    "fmt"
    )
    func main() {
    var horses int
    fmt.Println("How many horse are in the race?")
    fmt.Scan(&horses)
    power := make([]int, horses)
    for i := 0; i < horses; i++ {
    fmt.Printf("Horse[%d] power: ", i)
    fmt.Scan(&power[i])
    }
    fmt.Println(HorseDuals(horses, power))
    }
    func HorseDuals(lovak int, loero []int) (szoros int) {
    szoros = Abs(loero[0] - loero[1])
    for key, value := range loero {
    for i := key + 1; i < lovak; i++ {
    if Abs(value-loero[i]) < szoros {
    szoros = Abs(value - loero[i])
    }
    }
    }
    return
    }
    func Abs(x int) int {
    if x < 0 {
    return -x
    }
    return x
    }
  • file addition: game.md (-xw-x--x--)
    [0.21]
    # Horse-racing Duals
    Casablanca’s hippodrome is organizing a new type of horse racing:
    duals. During a dual, only two horses will participate in the race. In
    order for the race to be interesting, it is necessary to try to select
    two horses with similar strength.
    Write a program which, using a given number of strengths, identifies the
    two closest strengths and shows their difference with an integer (≥ 0).
    ## Input
    Line 1: Number N of horses
    The N following lines: the strength P[i] of each horse. P[i] is an integer.
    ##Output
    The difference D between the two closest strengths. D is an integer greater than or equal to 0.
    ##Constraints
    1 < N < 100000
    0 < P[i] ≤ 10000000