-
Notifications
You must be signed in to change notification settings - Fork 35
/
TheTimeInWords.cs
91 lines (76 loc) · 2.27 KB
/
TheTimeInWords.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// https://www.hackerrank.com/challenges/the-time-in-words/problem
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution
{
// Complete the timeInWords function below.
static string timeInWords(int h, int m)
{
var numbers = new[] {
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
"twenty",
"twenty one",
"twenty two",
"twenty three",
"twenty four",
"twenty five",
"twenty six",
"twenty seven",
"twenty eight",
"twenty nine"
};
if (m == 0) return $"{numbers[h]} o' clock";
if (m == 30) return $"half past {numbers[h]}";
var effectiveMinutes = m > 30 ? 60 - m : m;
var minutes = numbers[effectiveMinutes];
if (effectiveMinutes == 15)
minutes = "quarter";
else if (effectiveMinutes > 1)
minutes += " minutes";
else
minutes += " minute";
var linker = m > 30 ? "to" : "past";
if (m > 30) h = (h + 1) % 24;
return $"{minutes} {linker} {numbers[h]}";
}
static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int h = Convert.ToInt32(Console.ReadLine());
int m = Convert.ToInt32(Console.ReadLine());
string result = timeInWords(h, m);
textWriter.WriteLine(result);
textWriter.Flush();
textWriter.Close();
}
}