tpphu / golang-training

Golang for Backend Developer with Nordic Coder
https://nordiccoder.com/khoa-hoc/golang-for-backend-dev/
131 stars 50 forks source link

Challenge: Pentagonal Number #12

Closed tpphu closed 5 years ago

tpphu commented 5 years ago

Challenge

Have the function PentagonalNumber(num) read num which will be a positive integer and determine how many dots exist in a pentagonal shape around a center dot on the Nth iteration. For example, in the image below you can see that on the first iteration there is only a single dot, on the second iteration there are 6 dots, on the third there are 16 dots, and on the fourth there are 31 dots.

Image

Your program should return the number of dots that exist in the whole pentagon on the Nth iteration.

Sample Test Cases

Input:2 Output:6

Input:5 Output:51

Credits: https://www.coderbyte.com/editor/guest:Pentagonal%20Number:Go

hiepndd commented 5 years ago

Hi @tpphu , em submit bai a

func Solution(num int) int {
    result := 1
    for i := 1; i < num; i++ {
        result += 5 * i
    }
    return result

}
func TestSolution(t *testing.T) {
    cases := []struct {
        name   string
        input  int
        expect int
    }{
        {"Test case 1", 2, 6},
        {"Test case 2", 5, 51},
    }

    for _, testcase := range cases {
        t.Run(testcase.name, func(t *testing.T) {
            got := Solution(testcase.input)
            if !reflect.DeepEqual(got, testcase.expect) {
                t.Fatalf("expected: %v, but got: %v, with inputs: %v",
                    testcase.expect, got, testcase.input)
            }
        })
    }
}
tpphu commented 5 years ago

Có chứng minh gì ở chương trình đó không, sao nhìn đơn giản vậy :)

hiepndd commented 5 years ago

Da em de y thay la tong so cham xanh trong cai hinh pentagonal thi minh dua vao cai iteration * 5 roi cong cho tong cai so cham xanh trong lan iteration truoc. Iteration la cai input do anh @tpphu