Unit Kata; interface function IsLeap(year: integer): boolean; implementation function IsLeap(year: integer): boolean; begin result := (year mod 400 = 0) or ((year mod 4 = 0) and (year mod 100 <> 0)); end; end.
bool isLeap(int year) {return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);}- Unit Kata;
- interface
- function IsLeap(year: integer): boolean;
- implementation
- function IsLeap(year: integer): boolean;
- begin
- result := (year mod 400 = 0) or ((year mod 4 = 0) and (year mod 100 <> 0));
- end;
- end.
unit KataTests; {$mode objfpc}{$H+} interface uses TestFramework; type TKataTests = class(TTestCase) private published procedure TestIsLeap; end; procedure RegisterTests; implementation uses Kata; procedure RegisterTests; begin TestFramework.RegisterTest(TKataTests.Suite); end; procedure TKataTests.TestIsLeap; begin CheckEquals(true, IsLeap(8), 'year 8'); CheckEquals(true, IsLeap(1600), 'year 1600'); CheckEquals(false, IsLeap(1700), 'year 1700'); CheckEquals(false, IsLeap(1927), 'year 1927'); end; end.
// TODO: Replace examples and use TDD development by writing your own tests- unit KataTests;
Describe(any_group_name_you_want){It(should_do_something){Assert::That(isLeap(8), Equals(true));Assert::That(isLeap(1600), Equals(true));Assert::That(isLeap(1700), Equals(false));Assert::That(isLeap(1927), Equals(false));}};- {$mode objfpc}{$H+}
- interface
- uses
- TestFramework;
- type
- TKataTests = class(TTestCase)
- private
- published
- procedure TestIsLeap;
- end;
- procedure RegisterTests;
- implementation
- uses
- Kata;
- procedure RegisterTests;
- begin
- TestFramework.RegisterTest(TKataTests.Suite);
- end;
- procedure TKataTests.TestIsLeap;
- begin
- CheckEquals(true, IsLeap(8), 'year 8');
- CheckEquals(true, IsLeap(1600), 'year 1600');
- CheckEquals(false, IsLeap(1700), 'year 1700');
- CheckEquals(false, IsLeap(1927), 'year 1927');
- end;
- end.
Pascal translation
unit Kata; interface function div2(a: UInt64): UInt64; implementation function div2(a: UInt64): UInt64; begin // shift only works for non-negative numbers // bet you thought you had to use shr 1, seems like they added the C style >> result := a >> 1; // equivalent to a shr 1 end; end.
unsigned long long div2(unsigned long long a){//Use binary operators...I-Oreturn a >> 1;}- unit Kata;
- interface
- function div2(a: UInt64): UInt64;
- implementation
- function div2(a: UInt64): UInt64;
- begin
- // shift only works for non-negative numbers
- // bet you thought you had to use shr 1, seems like they added the C style >>
- result := a >> 1; // equivalent to a shr 1
- end;
- end.
unit KataTests; {$mode objfpc}{$H+} interface uses Kata, TestFramework; type TDiv2Tests = class(TTestCase) private procedure TestDiv2(input, expected: UInt64); published procedure Under10; procedure Huge_numbers; end; procedure RegisterTests; implementation uses sysutils; procedure RegisterTests; begin TestFramework.RegisterTest(TDiv2Tests.Suite); end; procedure TDiv2Tests.TestDiv2(input, expected: UInt64); var actual: UInt64; begin actual := div2(input); Check(actual = expected, format('Expected div2(%d)=%d, but got %d', [input, expected, actual])); end; procedure TDiv2Tests.Under10; begin TestDiv2(10, 5); TestDiv2(0, 0); TestDiv2(3, 1); end; procedure TDiv2Tests.Huge_numbers; begin TestDiv2(65536, 32768); TestDiv2(6553665535, 3276832767); TestDiv2(1234567890, 617283945); TestDiv2($FFFFFFFFFFFFFFFF, $7FFFFFFFFFFFFFFF); end; end.
// TODO: Replace examples and use TDD by writing your own tests- unit KataTests;
Describe(d2){It(under10){Assert::That(div2(10), Equals(5));Assert::That(div2(0), Equals(0));Assert::That(div2(3), Equals(1));}It(huge_numbers){Assert::That(div2(65536), Equals(32768));Assert::That(div2(6553665535), Equals(3276832767));Assert::That(div2(1234567890), Equals(617283945));}};- {$mode objfpc}{$H+}
- interface
- uses
- Kata, TestFramework;
- type
- TDiv2Tests = class(TTestCase)
- private
- procedure TestDiv2(input, expected: UInt64);
- published
- procedure Under10;
- procedure Huge_numbers;
- end;
- procedure RegisterTests;
- implementation
- uses
- sysutils;
- procedure RegisterTests;
- begin
- TestFramework.RegisterTest(TDiv2Tests.Suite);
- end;
- procedure TDiv2Tests.TestDiv2(input, expected: UInt64);
- var
- actual: UInt64;
- begin
- actual := div2(input);
- Check(actual = expected, format('Expected div2(%d)=%d, but got %d', [input, expected, actual]));
- end;
- procedure TDiv2Tests.Under10;
- begin
- TestDiv2(10, 5);
- TestDiv2(0, 0);
- TestDiv2(3, 1);
- end;
- procedure TDiv2Tests.Huge_numbers;
- begin
- TestDiv2(65536, 32768);
- TestDiv2(6553665535, 3276832767);
- TestDiv2(1234567890, 617283945);
- TestDiv2($FFFFFFFFFFFFFFFF, $7FFFFFFFFFFFFFFF);
- end;
- end.
Improvements made:
used constant iterators (is that really an improvement?)
shorter back_inserter alias.
templated the method for reuse elsewhere
#include <algorithm> #include <list> #include <execution> #include <iterator> template <typename T> auto merge_list(const std::list<T>& a, const std::list<T>& b) { std::list<T> result; std::merge( std::execution::seq, a.cbegin(),a.cend(), b.cbegin(),b.cend(), std::back_inserter<std::list<T>>(result)//, std::less<>() already default ); return result; } auto merge_list(const std::list<int>& a, const std::list<int>& b) { return merge_list<int>(a,b); }
- #include <algorithm>
- #include <list>
- #include <execution>
- #include <iterator>
auto merge_list(const std::list<int>& a, const std::list<int>& b) {std::list<int> o;std::merge(std::execution::seq,a.begin(),a.end(),b.begin(),b.end(),std::back_insert_iterator<std::list<int>>(o),[](const int a, const int b){return a < b;}- template <typename T>
- auto merge_list(const std::list<T>& a, const std::list<T>& b) {
- std::list<T> result;
- std::merge( std::execution::seq,
- a.cbegin(),a.cend(),
- b.cbegin(),b.cend(),
- std::back_inserter<std::list<T>>(result)//, std::less<>() already default
- );
return o;- return result;
- }
- auto merge_list(const std::list<int>& a, const std::list<int>& b) {
- return merge_list<int>(a,b);
- }
Use of accumulate initialized with first char, starting from the second char we add space and char, returning the accummulated result. Since the fold func return will move into the accumulator, we can use std::move to convert it to an R-value, although I am not sure what that helps much here...
I leave as a challenge the handling of Unicode for the next dev, just uncomment that test
#include <string.h> #include <numeric> std::string digest(std::string param) { if (param.empty()) return{}; //acummulate by adding ' ' and char, but the inital string is the first char return std::accumulate(std::next(param.begin()), param.end(), std::string{param[0]}, [](std::string & acc, const char & c) { return std::move(acc) + ' ' + c; }); }
- #include <string.h>
- #include <numeric>
- std::string digest(std::string param) {
std::string result;for (int i = 0; sizeof(param); i++) {result += param[i];result += ' ';}return result;} //Please fix!- if (param.empty())
- return{};
- //acummulate by adding ' ' and char, but the inital string is the first char
- return std::accumulate(std::next(param.begin()), param.end(), std::string{param[0]},
- [](std::string & acc, const char & c) {
- return std::move(acc) + ' ' + c;
- });
- }
// TODO: Replace examples and use TDD by writing your own tests Describe(any_group_name_you_want) { It(should_do_something) { Assert::That(digest("Burger"), Equals("B u r g e r")); Assert::That(digest("FoEMfIp"), Equals("F o E M f I p")); Assert::That(digest("Haggis"), Equals("H a g g i s")); Assert::That(digest("chitlins"), Equals("c h i t l i n s")); Assert::That(digest("SPAM"), Equals("S P A M")); Assert::That(digest(""), Equals("")); //pop_back erases at count-1 without throwing exception // Assert::That(digest("中文假字生成器"), Equals("中 文 假 字 生 成 器")); //fake chinese letters } };
- // TODO: Replace examples and use TDD by writing your own tests
- Describe(any_group_name_you_want)
- {
- It(should_do_something)
- {
- Assert::That(digest("Burger"), Equals("B u r g e r"));
- Assert::That(digest("FoEMfIp"), Equals("F o E M f I p"));
- Assert::That(digest("Haggis"), Equals("H a g g i s"));
- Assert::That(digest("chitlins"), Equals("c h i t l i n s"));
- Assert::That(digest("SPAM"), Equals("S P A M"));
//ssert::That(digest(1234567), Equals("1 2 3 4 5 6 7"));//Assert::That(digest({1,2,3,4,5,6,7}), Equals("{ 1 , 2 , 3 , 4 , 5 , 6 , 7 }""));//Assert::That(digest({1:'a',2:'b',3:'c'}), Equals("{ 1 : ' a ' , 2 : ' b ' , 3 : ' c ' }"));- Assert::That(digest(""), Equals("")); //pop_back erases at count-1 without throwing exception
- // Assert::That(digest("中文假字生成器"), Equals("中 文 假 字 生 成 器")); //fake chinese letters
- }
- };
Nested ternary is ugly, iterators are faster. Added test 9 and 10 one of the previous challengers simply counted.
bool isBalanced(const std::string& s) { int count = 0; //using iterators seem to produce faster results auto it = s.cbegin(); auto end = s.cend(); while (it != end) { if (*it == '(') count++; // can't exit early for ( else if ((*it== ')') && (--count < 0)) // short circuit booleans return false; //exit early for mismatched ) it++; } return count == 0; }
- bool isBalanced(const std::string& s) {
- int count = 0;
for (const auto c: s) {if (count+= c=='(' ? +1: c==')'?-1:0; count <0)return false;- //using iterators seem to produce faster results
- auto it = s.cbegin();
- auto end = s.cend();
- while (it != end) {
- if (*it == '(')
- count++; // can't exit early for (
- else if ((*it== ')') && (--count < 0)) // short circuit booleans
- return false; //exit early for mismatched )
- it++;
- }
- return count == 0;
- }
Describe(tests) { It(test1) { Assert::That(isBalanced(""), Equals(true)); } It(test2) { Assert::That(isBalanced("()"), Equals(true)); } It(test3) { Assert::That(isBalanced("()()"), Equals(true)); } It(test4) { Assert::That(isBalanced("(())"), Equals(true)); } It(test5) { Assert::That(isBalanced(")()"), Equals(false)); } It(test6) { Assert::That(isBalanced("(()()()))"), Equals(false)); } It(test7) { Assert::That(isBalanced("((12+32)*33)"), Equals(true)); } It(test8) { Assert::That(isBalanced("("), Equals(false)); } It(test9) { Assert::That(isBalanced(")("), Equals(false)); } It(test10) { Assert::That(isBalanced("())("), Equals(false)); } };
- Describe(tests)
- {
- It(test1)
- {
- Assert::That(isBalanced(""), Equals(true));
- }
- It(test2)
- {
- Assert::That(isBalanced("()"), Equals(true));
- }
- It(test3)
- {
- Assert::That(isBalanced("()()"), Equals(true));
- }
- It(test4)
- {
- Assert::That(isBalanced("(())"), Equals(true));
- }
- It(test5)
- {
- Assert::That(isBalanced(")()"), Equals(false));
- }
- It(test6)
- {
- Assert::That(isBalanced("(()()()))"), Equals(false));
- }
- It(test7)
- {
- Assert::That(isBalanced("((12+32)*33)"), Equals(true));
- }
- It(test8)
- {
- Assert::That(isBalanced("("), Equals(false));
- }
- It(test9)
- {
- Assert::That(isBalanced(")("), Equals(false));
- }
- It(test10)
- {
- Assert::That(isBalanced("())("), Equals(false));
- }
- };
Given a set of M
bits get the subset of N
bits starting at least significant 0 base index pos
.
This code is not optimized. For inspiration see
https://stackoverflow.com/questions/56517542/how-can-i-cast-an-stdbitsetn-to-an-stdbitsetm
#include <bitset>
template <std::size_t M, std::size_t N>
std::bitset<N> subset(std::bitset<M> source, std::size_t pos){
// assert M > N +pos
auto result = std::bitset<N>(0);
// surely we can get better performance than this loop
for (std::size_t i=0; i < N; i++)
if (source[i+pos])
result[i] = true;
return result;
}
// TODO: Replace examples and use TDD by writing your own tests
Describe(any_group_name_you_want)
{
It(should_do_something)
{
//std::bitset<5> b = subset<11,5>(std::bitset<11>(0), 0);
Assert::That(subset<11,5>(std::bitset<11>(0), 0), Equals(std::bitset<5>(0)));
Assert::That(subset<11,5>(std::bitset<11>("10101010101"), 0), Equals(std::bitset<5>("10101")));
Assert::That(subset<11,5>(std::bitset<11>("10101010101"), 1), Equals(std::bitset<5>("1010")));
//maybe we can do something with smaller bitsets by using integral access,
// but what about larger items
Assert::That(subset<100, 3>(std::bitset<100>("10101010101"), 5), Equals(std::bitset<3>("10")));
}
};