You will be given a number of minutes. Your task is to return a string on the heap that formats the number into 'hours:minutes".
Make sure you always return two integers for the minutes section of the conversion.
i.e. ('0:01' instead of '0:1')
Example:
minutes(90) => '1:30'
char *minutes(int num){ char *ret = calloc(64, sizeof(char)); sprintf(ret, "%d:%02d", (num / 60), (num % 60)); return ret; }
function minutes(num){return num % 60 < 10 ? Math.floor(num/60) + ':' + '0' + (num % 60) : Math.floor(num/60) + ':' + (num % 60)- char *minutes(int num){
- char *ret = calloc(64, sizeof(char));
- sprintf(ret, "%d:%02d", (num / 60), (num % 60));
- return ret;
- }
#include <criterion/criterion.h> #include <stdio.h> char *minutes(int); Test(minutes, sample_tests) { char *ptr; ptr = minutes(0); cr_assert_eq(strcmp(ptr, "0:00"), 0); free(ptr); ptr = minutes(1); cr_assert_eq(strcmp(ptr, "0:01"), 0); free(ptr); ptr = minutes(18); cr_assert_eq(strcmp(ptr, "0:18"), 0); free(ptr); ptr = minutes(13267); cr_assert_eq(strcmp(ptr, "221:07"), 0); free(ptr); ptr = minutes(351); cr_assert_eq(strcmp(ptr, "5:51"), 0); free(ptr); ptr = minutes(985); cr_assert_eq(strcmp(ptr, "16:25"), 0); free(ptr); ptr = minutes(156113); cr_assert_eq(strcmp(ptr, "2601:53"), 0); free(ptr); }
describe("Solution", function(){it("Did you make the correct conversion?", function(){Test.assertEquals(minutes(0), "0:00", "better try again");Test.assertEquals(minutes(1), "0:01", "better try again");Test.assertEquals(minutes(18), "0:18", "better try again");Test.assertEquals(minutes(13267), "221:07", "better try again");Test.assertEquals(minutes(985), "16:25", "better try again");Test.assertEquals(minutes(351), "5:51", "better try again");Test.assertEquals(minutes(156113), "2601:53", "better try again");});});- #include <criterion/criterion.h>
- #include <stdio.h>
- char *minutes(int);
- Test(minutes, sample_tests) {
- char *ptr;
- ptr = minutes(0);
- cr_assert_eq(strcmp(ptr, "0:00"), 0);
- free(ptr);
- ptr = minutes(1);
- cr_assert_eq(strcmp(ptr, "0:01"), 0);
- free(ptr);
- ptr = minutes(18);
- cr_assert_eq(strcmp(ptr, "0:18"), 0);
- free(ptr);
- ptr = minutes(13267);
- cr_assert_eq(strcmp(ptr, "221:07"), 0);
- free(ptr);
- ptr = minutes(351);
- cr_assert_eq(strcmp(ptr, "5:51"), 0);
- free(ptr);
- ptr = minutes(985);
- cr_assert_eq(strcmp(ptr, "16:25"), 0);
- free(ptr);
- ptr = minutes(156113);
- cr_assert_eq(strcmp(ptr, "2601:53"), 0);
- free(ptr);
- }