MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / timeInWords

Function timeInWords

Hackerrank_problems/Time_In_Words/solution.cpp:18–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16}
17
18string timeInWords(int h, int m) {
19 // initialize string for collecting timeInWords answer
20 string result = "";
21
22 // load number vocab for reading purpose hour and minute
23 vector<string> vocab = numberVocab();
24
25 // just concate "o' clock" usual if minute = 0
26 if (m == 0) {
27 result = vocab[h] + " o' clock";
28 }
29 else if (1 <= m && m <= 30) {
30 if (m == 15 || m == 30) {
31 // select between 15 or 30
32 result += m == 15 ? "quarter" : "half";
33 } else {
34 // convert minute into vocab and check minute unit vocab
35 result += vocab[m];
36 result += m == 1 ? " minute" : " minutes";
37 }
38
39 result += " past " + vocab[h];
40 } else if (m > 30) {
41 // display quarter if minute == 45
42 if (m == 45) {
43 result += "quarter";
44 } else {
45 // find how many minute left before going into next hour
46 result += vocab[abs(m-60)];
47 result += abs(m-60) == 1 ? " minute" : " minutes";
48 }
49
50 result += " to " + vocab[h+1];
51 }
52
53 return result;
54}
55
56int main()
57{

Callers 1

mainFunction · 0.85

Calls 2

numberVocabFunction · 0.85
absFunction · 0.85

Tested by

no test coverage detected