Ad
Algorithms
Code
Diff
  • 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.

Code
Diff
  • 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 := 8
    • endWorkHour := 18
    • currentHour := dateTime.Hour()
    • currentDay := dateTime.Weekday()
    • if currentDay > 0 && currentDay < 6 {
    • if currentHour < endWorkHour && currentHour >= beginWorkHour {
    • return true
    • }
    • }
    • return false
    • }
    • func testCheckWorkHours() {
    • // Tests the function
    • timeFormat := "2006-01-02 15:04:05 -0700 MST"
    • // True Cases
    • dateTime, _ := 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 Cases
    • dateTime, _ = 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
    • }

A little function for adding 2 values (it can be all objects that implements the __add__ methon, like numbers or strings)

Code
Diff
  • add= lambda a, b: a+b
    • def add(a, b):
    • return a+b
    • add= lambda a, b: a+b