Skip to content

Commit 41ac136

Browse files
authored
Solve 'Create Phone Number' kata (#6)
1 parent 8cfe67c commit 41ac136

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef CPP_KATAS_CREATE_PHONE_NUMBER_H
2+
#define CPP_KATAS_CREATE_PHONE_NUMBER_H
3+
4+
#include <string>
5+
6+
std::string createPhoneNumber(const int arr[10]);
7+
8+
#endif //CPP_KATAS_CREATE_PHONE_NUMBER_H
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <string>
2+
#include <sstream>
3+
4+
/*
5+
* https://www.codewars.com/kata/525f50e3b73515a6db000b83/train/cpp
6+
*/
7+
8+
static char int_to_digit(int i);
9+
10+
std::string createPhoneNumber(const int arr[10]) {
11+
std::stringstream stream;
12+
13+
stream.put('(');
14+
stream.put(int_to_digit(arr[0]));
15+
stream.put(int_to_digit(arr[1]));
16+
stream.put(int_to_digit(arr[2]));
17+
stream.put(')');
18+
19+
stream.put(' ');
20+
21+
stream.put(int_to_digit(arr[3]));
22+
stream.put(int_to_digit(arr[4]));
23+
stream.put(int_to_digit(arr[5]));
24+
25+
stream.put('-');
26+
27+
stream.put(int_to_digit(arr[6]));
28+
stream.put(int_to_digit(arr[7]));
29+
stream.put(int_to_digit(arr[8]));
30+
stream.put(int_to_digit(arr[9]));
31+
32+
return stream.str();
33+
}
34+
35+
static char int_to_digit(int i) {
36+
return (char) (i + '0');
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <igloo/igloo_alt.h>
2+
#include "create_phone_number/create_phone_number.h"
3+
4+
using namespace igloo;
5+
6+
Describe(CreatePhoneNumber) {
7+
It(BasicTests1) {
8+
static const int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
9+
Assert::That(createPhoneNumber(arr), Equals("(123) 456-7890"));
10+
}
11+
12+
It(BasicTests2) {
13+
static const int arr[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
14+
Assert::That(createPhoneNumber(arr), Equals("(111) 111-1111"));
15+
}
16+
17+
It(BasicTests3) {
18+
static const int arr[10] = {1, 2, 3, 4, 5, 6, 8, 8, 0, 0};
19+
Assert::That(createPhoneNumber(arr), Equals("(123) 456-8800"));
20+
}
21+
22+
It(BasicTests4) {
23+
static const int arr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
24+
Assert::That(createPhoneNumber(arr), Equals("(000) 000-0000"));
25+
}
26+
};

0 commit comments

Comments
 (0)