Skip to content

[Hacker Rank] Warmup: Time Conversion solved ✅ #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/hackerrank/warmup/time_conversion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# [Time Conversion](https://www.hackerrank.com/challenges/time-conversion)

Difficulty: #easy
Category: #warmup

Given a time in
12-[hour AM/PM format](https://en.wikipedia.org/wiki/12-hour_clock),
convert it to military (24-hour) time.

Note:

- 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

## Example

- s = '12:01:00PM' \
Return '12:01:00'
- s = '12:01:00AM' \
Return '00:01:00'

## Function Description

Complete the timeConversion function in the editor below.
It should return a new string representing the input time in 24 hour format
timeConversion has the following parameter(s):

- string s: a time in 12 hour format

## Returns

- string: the time in 24 hour format

## Input Format

A single string s that represents a time in 12-hour clock format
(i.e.: hh_mm_ssAM or hh:mm:ssPM).

## Constraints

- All input times are valid

## Sample Input 0

```text
07:05:45PM
```

## Sample Output 0

```text
19:05:45
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

char *HACKERRANK_WARMUP_getStringFragment(const char *s, unsigned long len,
unsigned long from, unsigned long to);

char *HACKERRANK_WARMUP_timeConversion(const char *s);

#ifdef __cplusplus
} // extern "C"
#endif
68 changes: 68 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/time_conversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <exercises/hackerrank/warmup/time_conversion.h>

/**
* @link Problem definition [[docs/hackerrank/warmup/time_conversion.md]]
*/

#include <stdio.h> // snprintf
#include <stdlib.h> // malloc, free, strtol
#include <string.h> // strcmp, strnlen

#define HACKERRANK_WARMUP_LONG_TIME_FORMAT_SIZE 10
#define HACKERRANK_WARMUP_SHORT_TIME_FORMAT_SIZE 8
#define HACKERRANK_WARMUP_HOUR_FORMAT_SIZE 2

char *HACKERRANK_WARMUP_timeConversion(const char *s) {
if (s == NULL) {
return NULL;
}

size_t s_len = strnlen(s, HACKERRANK_WARMUP_LONG_TIME_FORMAT_SIZE);

if (s_len != HACKERRANK_WARMUP_LONG_TIME_FORMAT_SIZE) {
return NULL;
}

char hour_str[HACKERRANK_WARMUP_HOUR_FORMAT_SIZE + 1];

hour_str[0] = s[0];
hour_str[1] = s[1];
hour_str[2] = '\0';

char meridian[3];
meridian[0] = s[s_len - 2];
meridian[1] = s[s_len - 1];
meridian[2] = '\0';

char *endptr;
long hour = strtol(hour_str, &endptr, 10);

if (*endptr != '\0') {
printf("Conversion error, non-convertible part: %s\n", endptr);

return NULL;
} else {
printf("The integer value is: %ld\n", hour);
}

hour = hour % 12;

if (strcmp(meridian, "PM") == 0) {
hour += 12;
}

char *conversion =
malloc((HACKERRANK_WARMUP_SHORT_TIME_FORMAT_SIZE + 1) * sizeof(char));

for (int i = 0; i < HACKERRANK_WARMUP_SHORT_TIME_FORMAT_SIZE; i++) {
conversion[i] = s[i];
}
conversion[HACKERRANK_WARMUP_SHORT_TIME_FORMAT_SIZE] = '\0';

snprintf(hour_str, HACKERRANK_WARMUP_HOUR_FORMAT_SIZE + 1, "%02ld", hour);

conversion[0] = hour_str[0];
conversion[1] = hour_str[1];

return conversion;
}
41 changes: 41 additions & 0 deletions src/tests/unit/lib/hackerrank/warmup/time_conversion.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/warmup/time_conversion.h>
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include <vector>

using json = nlohmann::json;

TEST_CASE("time_conversion JSON Test Cases",
"[hackerrank] [jsontestcase] [warmup]") {
std::filesystem::path cwd = std::filesystem::current_path();
std::string path =
cwd.string() +
"/unit/lib/hackerrank/warmup/time_conversion.testcases.json";

INFO("time_conversion JSON test cases FILE: " << path);

std::ifstream f(path);
json data = json::parse(f);

for (auto testcase : data) {
char *result = HACKERRANK_WARMUP_timeConversion(
testcase["input"].get<std::string>().c_str());

std::string result_as_string(result);

free(result);

CHECK(result_as_string == testcase["expected"]);
}
}

TEST_CASE("time_conversion edge cases", "[hackerrank] [helper] [warmup]") {
CHECK(HACKERRANK_WARMUP_timeConversion(nullptr) == nullptr);

CHECK(HACKERRANK_WARMUP_timeConversion("") == nullptr);

CHECK(HACKERRANK_WARMUP_timeConversion("aa:bb:ccXM") == nullptr);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "input": "12:01:00PM", "expected": "12:01:00" },
{ "input": "12:01:00AM", "expected": "00:01:00" }
]