Last active
December 19, 2023 17:23
-
-
Save thiagozs/e4b429e5fe55101f33f8d6d0ea17fe0c to your computer and use it in GitHub Desktop.
BairesDev Transform duration to Text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func solution (seconds int) string { | |
if seconds == 0 { | |
return "now" | |
} | |
year := 365 * 24 * 60 * 60 | |
day := 24 * 60 * 60 | |
hour := 60 * 60 | |
minute := 60 | |
years := seconds / year | |
seconds %= year | |
days := seconds / day | |
seconds %= day | |
hours := seconds / hour | |
seconds %= hour | |
minutes := seconds / minute | |
seconds %= minute | |
result := "" | |
// Append years | |
if years > 0 { | |
result += fmt.Sprintf("%d year", years) | |
if years > 1 { | |
result += "s" | |
} | |
} | |
// Append days | |
if days > 0 { | |
if result != "" { | |
result += ", " | |
} | |
result += fmt.Sprintf("%d day", days) | |
if days > 1 { | |
result += "s" | |
} | |
} | |
// Append hours | |
if hours > 0 { | |
if result != "" { | |
result += ", " | |
} | |
result += fmt.Sprintf("%d hour", hours) | |
if hours > 1 { | |
result += "s" | |
} | |
} | |
// Append minutes | |
if minutes > 0 { | |
if result != "" { | |
result += ", " | |
} | |
result += fmt.Sprintf("%d minute", minutes) | |
if minutes > 1 { | |
result += "s" | |
} | |
} | |
// Append seconds | |
if seconds > 0 { | |
if result != "" { | |
result += " and " | |
} | |
result += fmt.Sprintf("%d second", seconds) | |
if seconds > 1 { | |
result += "s" | |
} | |
} | |
return result | |
} | |
func main() { | |
var seconds int | |
fmt.Scanln(&seconds) | |
var out_ string = solution(seconds) | |
fmt.Println(out_) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment