Ad
Code
Diff
  • 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.

Pascal translation

Code
Diff
  • 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-O
    • return 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.

Improvements made:
used constant iterators (is that really an improvement?)
shorter back_inserter alias.
templated the method for reuse elsewhere

Code
Diff
  • #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);
    • }
Fundamentals
Strings

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

Code
Diff
  • #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;
    • });
    • }

Nested ternary is ugly, iterators are faster. Added test 9 and 10 one of the previous challengers simply counted.

Code
Diff
  • 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;
    • }

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;
}