Algorithms
fn digits(mut n: u64) -> usize { std::iter::from_fn(|| { n /= 10; Some(n) }) .take_while(|n| n > &0) .count() + 1 }
- fn digits(mut n: u64) -> usize {
std::iter::from_fn(|| { n = n / 10; Some(n) }).take_while(|n| *n > 0)- std::iter::from_fn(|| { n /= 10; Some(n) })
- .take_while(|n| n > &0)
- .count() + 1
- }
Use constants instead of local variable.
Plus, complete tests.
package kumite import "time" func CheckWorkHours(dateTime time.Time) bool { // It's better to have constants for work interval const ( WK_HOUR_BEGIN = 8 WK_HOUR_END = 18 WK_DAY_BEGIN = 1 WK_DAY_END = 6 ) hour := dateTime.Hour() day := dateTime.Weekday() return WK_DAY_BEGIN <= day && day < WK_DAY_END && WK_HOUR_BEGIN <= hour && hour < WK_HOUR_END }
package main- package kumite
import ("fmt""time")- import "time"
func main() {testCheckWorkHours()}func checkWorkHours(dateTime time.Time) bool {beginWorkHour := 8endWorkHour := 18currentHour := dateTime.Hour()currentDay := dateTime.Weekday()if currentDay > 0 && currentDay < 6 {if currentHour < endWorkHour && currentHour >= beginWorkHour {return true}}return false}func testCheckWorkHours() {// Tests the functiontimeFormat := "2006-01-02 15:04:05 -0700 MST"// True CasesdateTime, _ := time.Parse(timeFormat, "2017-01-16 15:00:00 -0500 EST")fmt.Println(fmt.Sprintf("Monday @ 3pm should be True: %t", checkWorkHours(dateTime)))dateTime, _ = time.Parse(timeFormat, "2017-01-16 17:59:59 -0500 EST")fmt.Println(fmt.Sprintf("Friday @ 5:59pm should be True: %t", checkWorkHours(dateTime)))// False CasesdateTime, _ = time.Parse(timeFormat, "2017-01-15 15:00:00 -0500 EST")fmt.Println(fmt.Sprintf("Sunday @ 3pm should be False: %t", checkWorkHours(dateTime)))dateTime, _ = time.Parse(timeFormat, "2017-01-13 18:00:00 -0500 EST")fmt.Println(fmt.Sprintf("Friday @ 6pm should be False: %t", checkWorkHours(dateTime)))dateTime, _ = time.Parse(timeFormat, "2017-01-17 07:00:00 -0500 EST")fmt.Println(fmt.Sprintf("Tuesday @ 7am should be False: %t", checkWorkHours(dateTime)))- func CheckWorkHours(dateTime time.Time) bool {
- // It's better to have constants for work interval
- const (
- WK_HOUR_BEGIN = 8
- WK_HOUR_END = 18
- WK_DAY_BEGIN = 1
- WK_DAY_END = 6
- )
- hour := dateTime.Hour()
- day := dateTime.Weekday()
- return WK_DAY_BEGIN <= day && day < WK_DAY_END && WK_HOUR_BEGIN <= hour && hour < WK_HOUR_END
- }
// TODO: replace with your own tests (TDD). An example to get you started is included below. // Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/> // Gomega Matcher Library <http://onsi.github.io/gomega/> package kumite_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "codewarrior/kumite" "time" ) var _ = Describe("Test Example", func() { // Tests the function timeFormat := "2006-01-02 15:04:05 -0700 MST" It("Monday @ 3pm should be True", func() { dateTime, err := time.Parse(timeFormat, "2017-01-16 15:00:00 -0500 EST") if err != nil { panic(err) } Expect(CheckWorkHours(dateTime)).To(Equal(true)) }) It("Friday @ 5:59pm should be True", func() { dateTime, err := time.Parse(timeFormat, "2017-01-16 17:59:59 -0500 EST") if err != nil { panic(err) } Expect(CheckWorkHours(dateTime)).To(Equal(true)) }) // False Cases It("Sunday @ 3pm should be False", func() { dateTime, err := time.Parse(timeFormat, "2017-01-15 15:00:00 -0500 EST") if err != nil { panic(err) } Expect(CheckWorkHours(dateTime)).To(Equal(false)) }) It("Friday @ 6pm should be False", func() { dateTime, err := time.Parse(timeFormat, "2017-01-13 18:00:00 -0500 EST") if err != nil { panic(err) } Expect(CheckWorkHours(dateTime)).To(Equal(false)) }) It("Tuesday @ 7am should be False", func() { dateTime, err := time.Parse(timeFormat, "2017-01-17 07:00:00 -0500 EST") if err != nil { panic(err) } Expect(CheckWorkHours(dateTime)).To(Equal(false)) }) })
- // TODO: replace with your own tests (TDD). An example to get you started is included below.
- // Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/>
- // Gomega Matcher Library <http://onsi.github.io/gomega/>
- package kumite_test
- import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- . "codewarrior/kumite"
- "time"
- )
- var _ = Describe("Test Example", func() {
- // Tests the function
- timeFormat := "2006-01-02 15:04:05 -0700 MST"
- It("Monday @ 3pm should be True", func() {
- dateTime, err := time.Parse(timeFormat, "2017-01-16 15:00:00 -0500 EST")
- if err != nil {
- panic(err)
- }
- Expect(CheckWorkHours(dateTime)).To(Equal(true))
- })
- It("Friday @ 5:59pm should be True", func() {
- dateTime, err := time.Parse(timeFormat, "2017-01-16 17:59:59 -0500 EST")
- if err != nil {
- panic(err)
- }
- Expect(CheckWorkHours(dateTime)).To(Equal(true))
- })
- // False Cases
- It("Sunday @ 3pm should be False", func() {
- dateTime, err := time.Parse(timeFormat, "2017-01-15 15:00:00 -0500 EST")
- if err != nil {
- panic(err)
- }
- Expect(CheckWorkHours(dateTime)).To(Equal(false))
- })
- It("Friday @ 6pm should be False", func() {
- dateTime, err := time.Parse(timeFormat, "2017-01-13 18:00:00 -0500 EST")
- if err != nil {
- panic(err)
- }
- Expect(CheckWorkHours(dateTime)).To(Equal(false))
- })
- It("Tuesday @ 7am should be False", func() {
- dateTime, err := time.Parse(timeFormat, "2017-01-17 07:00:00 -0500 EST")
- if err != nil {
- panic(err)
- }
- Expect(CheckWorkHours(dateTime)).To(Equal(false))
- })
- })
A little function for adding 2 values (it can be all objects that implements the __add__
methon, like numbers or strings)