WeixiangYan/CodeTransOcean huggingface dataset:
Task: Solve a Numbrix puzzle


C++: #include <vector>
#include <sstream>
#include <iostream>
#include <iterator>
#include <cstdlib>
#include <string>
#include <bitset>

using namespace std;
typedef bitset<4> hood_t;

struct node
{
	int val;
	hood_t neighbors;
};

class nSolver
{
public:

	void solve(vector<string>& puzz, int max_wid)
	{
		if (puzz.size() < 1) return;
		wid = max_wid; 
		hei = static_cast<int>(puzz.size()) / wid;
		max = wid * hei;
		int len = max, c = 0;
		arr = vector<node>(len, node({ 0, 0 }));
		weHave = vector<bool>(len + 1, false);

		for (const auto& s : puzz)
		{
			if (s == "*") { max--; arr[c++].val = -1; continue; }
			arr[c].val = atoi(s.c_str());
			if (arr[c].val > 0) weHave[arr[c].val] = true;
			c++;
		}

		solveIt(); c = 0;
		for (auto&& s : puzz)
		{
			if (s == ".")
				s = std::to_string(arr[c].val);
			c++;
		}
	}

private:
	bool search(int x, int y, int w, int dr)
	{
		if ((w > max && dr > 0) || (w < 1 && dr < 0) || (w == max && weHave[w])) return true;

		node& n = arr[x + y * wid];
		n.neighbors = getNeighbors(x, y);
		if (weHave[w])
		{
			for (int d = 0; d < 4; d++)
			{
				if (n.neighbors[d])
				{
					int a = x + dx[d], b = y + dy[d];
					if (arr[a + b * wid].val == w)
						if (search(a, b, w + dr, dr)) 
							return true;
				}
			}
			return false;
		}

		for (int d = 0; d < 4; d++)
		{
			if (n.neighbors[d])
			{
				int a = x + dx[d], b = y + dy[d];
				if (arr[a + b * wid].val == 0)
				{
					arr[a + b * wid].val = w;
					if (search(a, b, w + dr, dr)) 
						return true;
					arr[a + b * wid].val = 0;
				}
			}
		}
		return false;
	}

	hood_t getNeighbors(int x, int y)
	{
		hood_t retval;
		for (int xx = 0; xx < 4; xx++)
		{
			int a = x + dx[xx], b = y + dy[xx];
			if (a < 0 || b < 0 || a >= wid || b >= hei) 
				continue;
			if (arr[a + b * wid].val > -1)
				retval.set(xx);
		}
		return retval;
	}

	void solveIt()
	{
		int x, y, z; findStart(x, y, z);
		if (z == 99999) { cout << "\nCan't find start point!\n"; return; }
		search(x, y, z + 1, 1);
		if (z > 1) search(x, y, z - 1, -1);
	}

	void findStart(int& x, int& y, int& z)
	{
		z = 99999;
		for (int b = 0; b < hei; b++)
		for (int a = 0; a < wid; a++)
		if (arr[a + wid * b].val > 0 && arr[a + wid * b].val < z)
		{
			x = a; y = b;
			z = arr[a + wid * b].val;
		}

	}

	vector<int> dx = vector<int>({ -1, 1, 0, 0 });
	vector<int> dy = vector<int>({ 0, 0, -1, 1 });
	int wid, hei, max;
	vector<node> arr;
	vector<bool> weHave;
};


int main(int argc, char* argv[])
{
	int wid; string p;
	
	
	p = "17 . . . 11 . . . 59 . 15 . . 6 . . 61 . . . 3 . . .  63 . . . . . . 66 . . . . 23 24 . 68 67 78 . 54 55 . . . . 72 . . . . . . 35 . . . 49 . . . 29 . . 40 . . 47 . 31 . . . 39 . . . 45"; wid = 9;

	istringstream iss(p); vector<string> puzz;
	copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(puzz));
	nSolver s; s.solve(puzz, wid);

	int c = 0;
	for (const auto& s : puzz)
	{
		if (s != "*" && s != ".")
		{
			if (atoi(s.c_str()) < 10) cout << "0";
			cout << s << " ";
		}
		else cout << "   ";
		if (++c >= wid) { cout << endl; c = 0; }
	}
	cout << endl << endl;
	return system("pause");
}
Task: Nested templated data


C++: #include <iostream>
#include <set>
#include <tuple>
#include <vector>

using namespace std;


template<typename P>
void PrintPayloads(const P &payloads, int index, bool isLast)
{
    if(index < 0 || index >= (int)size(payloads)) cout << "null";        
    else cout << "'" << payloads[index] << "'";
    if (!isLast) cout << ", ";  
}


template<typename P, typename... Ts>
void PrintPayloads(const P &payloads, tuple<Ts...> const& nestedTuple, bool isLast = true)
{
    std::apply  
    (
        [&payloads, isLast](Ts const&... tupleArgs)
        {
            size_t n{0};
            cout << "[";
            (PrintPayloads(payloads, tupleArgs, (++n == sizeof...(Ts)) ), ...);
            cout << "]";
            cout << (isLast ? "\n" : ",\n");
        }, nestedTuple
    );
}


void FindUniqueIndexes(set<int> &indexes, int index)
{
    indexes.insert(index);
}


template<typename... Ts>
void FindUniqueIndexes(set<int> &indexes, std::tuple<Ts...> const& nestedTuple)
{
    std::apply
    (
        [&indexes](Ts const&... tupleArgs)
        {
            (FindUniqueIndexes(indexes, tupleArgs),...);
        }, nestedTuple
    );
}


template<typename P>
void PrintUnusedPayloads(const set<int> &usedIndexes, const P &payloads)
{
    for(size_t i = 0; i < size(payloads); i++)
    {
        if(usedIndexes.find(i) == usedIndexes.end() ) cout << payloads[i] << "\n";
    }
}

int main()
{
    
    vector payloads {"Payload#0", "Payload#1", "Payload#2", "Payload#3", "Payload#4", "Payload#5", "Payload#6"};
    const char *shortPayloads[] {"Payload#0", "Payload#1", "Payload#2", "Payload#3"}; 

    
    auto tpl = make_tuple(make_tuple(
        make_tuple(1, 2),
        make_tuple(3, 4, 1),
        5));

    cout << "Mapping indexes to payloads:\n";
    PrintPayloads(payloads, tpl);      

    cout << "\nFinding unused payloads:\n";
    set<int> usedIndexes;
    FindUniqueIndexes(usedIndexes, tpl);
    PrintUnusedPayloads(usedIndexes, payloads);

    cout << "\nMapping to some out of range payloads:\n";
    PrintPayloads(shortPayloads, tpl);      
    
    return 0;
}
Task: Main step of GOST 28147-89


C++: UINT_64 TGost::SWAP32(UINT_32 N1, UINT_32 N2)
{
    UINT_64 N;
	N = N1;
	N = (N<<32)|N2;
	return UINT_64(N);
}

UINT_32 TGost::ReplaceBlock(UINT_32 x)
{   
    register i;
    UINT_32 res = 0UL;
    for(i=7;i>=0;i--)
    {
       ui4_0 = x>>(i*4);
       ui4_0 = BS[ui4_0][i];
       res = (res<<4)|ui4_0;
    }
    return res;
}

UINT_64 TGost::MainStep(UINT_64 N,UINT_32 X)
{
   UINT_32 N1,N2,S=0UL;
   N1=UINT_32(N);
   N2=N>>32;
   S = N1 + X % 0x4000000000000;
   S = ReplaceBlock(S);
   S = (S<<11)|(S>>21);
   S ^= N2;
   N2 = N1;
   N1 = S;
   return SWAP32(N2,N1);
}
Task: Weird numbers


C++: #include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

std::vector<int> divisors(int n) {
    std::vector<int> divs = { 1 };
    std::vector<int> divs2;

    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            int j = n / i;
            divs.push_back(i);
            if (i != j) {
                divs2.push_back(j);
            }
        }
    }

    std::copy(divs.cbegin(), divs.cend(), std::back_inserter(divs2));
    return divs2;
}

bool abundant(int n, const std::vector<int> &divs) {
    return std::accumulate(divs.cbegin(), divs.cend(), 0) > n;
}

template<typename IT>
bool semiperfect(int n, const IT &it, const IT &end) {
    if (it != end) {
        auto h = *it;
        auto t = std::next(it);
        if (n < h) {
            return semiperfect(n, t, end);
        } else {
            return n == h
                || semiperfect(n - h, t, end)
                || semiperfect(n, t, end);
        }
    } else {
        return false;
    }
}

template<typename C>
bool semiperfect(int n, const C &c) {
    return semiperfect(n, std::cbegin(c), std::cend(c));
}

std::vector<bool> sieve(int limit) {
    
    
    std::vector<bool> w(limit);
    for (int i = 2; i < limit; i += 2) {
        if (w[i]) continue;
        auto divs = divisors(i);
        if (!abundant(i, divs)) {
            w[i] = true;
        } else if (semiperfect(i, divs)) {
            for (int j = i; j < limit; j += i) {
                w[j] = true;
            }
        }
    }
    return w;
}

int main() {
    auto w = sieve(17000);
    int count = 0;
    int max = 25;
    std::cout << "The first 25 weird numbers:";
    for (int n = 2; count < max; n += 2) {
        if (!w[n]) {
            std::cout << n << ' ';
            count++;
        }
    }
    std::cout << '\n';
    return 0;
}
Task: Addition chains


C++: #include <iostream>
#include <tuple>
#include <vector>

std::pair<int, int> tryPerm(int, int, const std::vector<int>&, int, int);

std::pair<int, int> checkSeq(int pos, const std::vector<int>& seq, int n, int minLen) {
    if (pos > minLen || seq[0] > n) return { minLen, 0 };
    else if (seq[0] == n)           return { pos, 1 };
    else if (pos < minLen)          return tryPerm(0, pos, seq, n, minLen);
    else                            return { minLen, 0 };
}

std::pair<int, int> tryPerm(int i, int pos, const std::vector<int>& seq, int n, int minLen) {
    if (i > pos) return { minLen, 0 };

    std::vector<int> seq2{ seq[0] + seq[i] };
    seq2.insert(seq2.end(), seq.cbegin(), seq.cend());
    auto res1 = checkSeq(pos + 1, seq2, n, minLen);
    auto res2 = tryPerm(i + 1, pos, seq, n, res1.first);

    if (res2.first < res1.first)       return res2;
    else if (res2.first == res1.first) return { res2.first, res1.second + res2.second };
    else                               throw std::runtime_error("tryPerm exception");
}

std::pair<int, int> initTryPerm(int x) {
    return tryPerm(0, 0, { 1 }, x, 12);
}

void findBrauer(int num) {
    auto res = initTryPerm(num);
    std::cout << '\n';
    std::cout << "N = " << num << '\n';
    std::cout << "Minimum length of chains: L(n)= " << res.first << '\n';
    std::cout << "Number of minimum length Brauer chains: " << res.second << '\n';
}

int main() {
    std::vector<int> nums{ 7, 14, 21, 29, 32, 42, 64, 47, 79, 191, 382, 379 };
    for (int i : nums) {
        findBrauer(i);
    }

    return 0;
}
Task: Just in time processing on a character stream


C++: #include <vector>
#include <iostream>
#include <fstream>
#include <sstream>

typedef struct {
    int s[4];
}userI;

class jit{
public:
    void decode( std::string& file, std::vector<userI>& ui ) {
        std::ifstream f( file.c_str(), std::ios_base::in );
        fileBuffer = std::string( ( std::istreambuf_iterator<char>( f ) ), std::istreambuf_iterator<char>() );
        f.close();
        for( std::vector<userI>::iterator t = ui.begin(); t != ui.end(); t++ ) {
            if( !decode( ( *t ).s ) ) break;
        }
        std::cout << "\n\n";
    }
private:
    bool decode( int* ui ) {
        int l = 0, t = 0, p = 0, c = 0, a = 0;
        for( std::string::iterator i = fileBuffer.begin(); i != fileBuffer.end(); i++ ) {
            if( p == ui[0] && l == ui[1] && t == ui[2] && c == ui[3] ) {
                if( *i == '!' )  return false;
                std::cout << *i; return true;
            }
            if( *i == '\n' )      { l++; t = c = 0; }  
            else if( *i == '\t' ) { t++; c = 0; }
            else if( *i == '\f' ) { p++; l = t = c = 0; }
            else                  { c++;}
        }
        return false;
    }
    std::string fileBuffer;
};
void getUserInput( std::vector<userI>& u ) {
    std::string h = "0 18 0 0 0 68 0 1 0 100 0 32 0 114 0 45 0 38 0 26 0 16 0 21 0 17 0 59 0 11 "
                    "0 29 0 102 0 0 0 10 0 50 0 39 0 42 0 33 0 50 0 46 0 54 0 76 0 47 0 84 2 28";
    
    std::stringstream ss( h );
    userI a;
    int x = 0;
    while( std::getline( ss, h, ' ' ) ) {
        a.s[x] = atoi( h.c_str() );
        if( ++x == 4 ) {
            u.push_back( a );
            x = 0;
        }
    }
}
int main( int argc, char* argv[] ) {
    std::vector<userI> ui;
    getUserInput( ui );

    jit j;
    j.decode( std::string( "theRaven.txt" ), ui );
    return 0;
}
Task: Montgomery reduction


C++: #include<iostream>
#include<conio.h>
using namespace std;
typedef unsigned long ulong;

int ith_digit_finder(long long n, long b, long i){
 
    while(i>0){
        n/=b;
        i--;
    }
    return (n%b);
}

long eeuclid(long m, long b, long *inverse){        
    
    long A1 = 1, A2 = 0, A3 = m,
         B1 = 0, B2 = 1, B3 = b,
         T1, T2, T3, Q;

         cout<<endl<<"eeuclid() started"<<endl;

        while(1){
            if(B3 == 0){
                *inverse = 0;
                return A3;      
            }

            if(B3 == 1){
                *inverse = B2; 
                return B3;      
            }

            Q = A3/B3;

            T1 = A1 - Q*B1;
            T2 = A2 - Q*B2;
            T3 = A3 - Q*B3;

            A1 = B1; A2 = B2; A3 = B3;
            B1 = T1; B2 = T2; B3 = T3;

       }
    cout<<endl<<"ending eeuclid() "<<endl;
}

long long mon_red(long m, long m_dash, long T, int n, long b = 2){

    long long A,ui, temp, Ai;       
    if( m_dash < 0 ) m_dash = m_dash + b;
    A = T;
    for(int i = 0; i<n; i++){
    
        Ai = ith_digit_finder(A, b, i);
        ui = ( ( Ai % b) * m_dash ) % b;
        temp  = ui*m*power(b, i);
        A = A + temp;
    }
    A = A/power(b, n);
    if(A >= m) A = A - m;
    return A;
}

int main(){
    long a, b, c, d=0, e, inverse = 0;
    cout<<"m >> ";
    cin >> a;
    cout<<"T >> ";
    cin>>b;
    cout<<"Radix b >> ";
    cin>>c;
    eeuclid(c, a, &d);      
    e = mon_red(a, -d, b, length_finder(a, c), c);
    cout<<"Montgomery domain representation = "<<e;
    return 0;
}
Task: Eertree


C++: #include <iostream>
#include <functional>
#include <map>
#include <vector>

struct Node {
    int length;
    std::map<char, int> edges;
    int suffix;

    Node(int l) : length(l), suffix(0) {
        
    }

    Node(int l, const std::map<char, int>& m, int s) : length(l), edges(m), suffix(s) {
        
    }
};

constexpr int evenRoot = 0;
constexpr int oddRoot = 1;

std::vector<Node> eertree(const std::string& s) {
    std::vector<Node> tree = {
        Node(0, {}, oddRoot),
        Node(-1, {}, oddRoot)
    };
    int suffix = oddRoot;
    int n, k;

    for (size_t i = 0; i < s.length(); ++i) {
        char c = s[i];
        for (n = suffix; ; n = tree[n].suffix) {
            k = tree[n].length;
            int b = i - k - 1;
            if (b >= 0 && s[b] == c) {
                break;
            }
        }

        auto it = tree[n].edges.find(c);
        auto end = tree[n].edges.end();
        if (it != end) {
            suffix = it->second;
            continue;
        }
        suffix = tree.size();
        tree.push_back(Node(k + 2));
        tree[n].edges[c] = suffix;
        if (tree[suffix].length == 1) {
            tree[suffix].suffix = 0;
            continue;
        }
        while (true) {
            n = tree[n].suffix;
            int b = i - tree[n].length - 1;
            if (b >= 0 && s[b] == c) {
                break;
            }
        }
        tree[suffix].suffix = tree[n].edges[c];
    }

    return tree;
}

std::vector<std::string> subPalindromes(const std::vector<Node>& tree) {
    std::vector<std::string> s;

    std::function<void(int, std::string)> children;
    children = [&children, &tree, &s](int n, std::string p) {
        auto it = tree[n].edges.cbegin();
        auto end = tree[n].edges.cend();
        for (; it != end; it = std::next(it)) {
            auto c = it->first;
            auto m = it->second;

            std::string pl = c + p + c;
            s.push_back(pl);
            children(m, pl);
        }
    };
    children(0, "");

    auto it = tree[1].edges.cbegin();
    auto end = tree[1].edges.cend();
    for (; it != end; it = std::next(it)) {
        auto c = it->first;
        auto n = it->second;

        std::string ct(1, c);
        s.push_back(ct);

        children(n, ct);
    }

    return s;
}

int main() {
    using namespace std;

    auto tree = eertree("eertree");
    auto pal = subPalindromes(tree);

    auto it = pal.cbegin();
    auto end = pal.cend();

    cout << "[";
    if (it != end) {
        cout << it->c_str();
        it++;
    }
    while (it != end) {
        cout << ", " << it->c_str();
        it++;
    }
    cout << "]" << endl;

    return 0;
}
Task: Wilson primes of order n


C++: #include <iomanip>
#include <iostream>
#include <vector>
#include <gmpxx.h>

std::vector<int> generate_primes(int limit) {
    std::vector<bool> sieve(limit >> 1, true);
    for (int p = 3, s = 9; s < limit; p += 2) {
        if (sieve[p >> 1]) {
            for (int q = s; q < limit; q += p << 1)
                sieve[q >> 1] = false;
        }
        s += (p + 1) << 2;
    }
    std::vector<int> primes;
    if (limit > 2)
        primes.push_back(2);
    for (int i = 1; i < sieve.size(); ++i) {
        if (sieve[i])
            primes.push_back((i << 1) + 1);
    }
    return primes;
}

int main() {
    using big_int = mpz_class;
    const int limit = 11000;
    std::vector<big_int> f{1};
    f.reserve(limit);
    big_int factorial = 1;
    for (int i = 1; i < limit; ++i) {
        factorial *= i;
        f.push_back(factorial);
    }
    std::vector<int> primes = generate_primes(limit);
    std::cout << " n | Wilson primes\n--------------------\n";
    for (int n = 1, s = -1; n <= 11; ++n, s = -s) {
        std::cout << std::setw(2) << n << " |";
        for (int p : primes) {
            if (p >= n && (f[n - 1] * f[p - n] - s) % (p * p) == 0)
                std::cout << ' ' << p;
        }
        std::cout << '\n';
    }
}
Task: Singly-linked list_Element removal


C++: #include <iostream>


struct link
{
  link* next;
  int data;

  link(int newItem, link* head)
  : next{head}, data{newItem}{}
};

void PrintList(link* head)
{
    if(!head) return;
    std::cout << head->data << " ";
    PrintList(head->next);
}

link* RemoveItem(int valueToRemove, link*&head)
{
    
    
    for(link** node = &head; *node; node = &((*node)->next))
    {
        if((*node)->data == valueToRemove)
        {
            
            link* removedNode = *node;
            *node = removedNode->next;
            removedNode->next = nullptr;
            return removedNode;
        }
    }

    
    return nullptr;
}

int main()
{
    
    link link33{33, nullptr};
    link link42{42, &link33};
    link link99{99, &link42};
    link link55{55, &link99};
    link* head = &link55;

    std::cout << "Full list: ";
    PrintList(head);

    std::cout << "\nRemove 55: ";
    auto removed = RemoveItem(55, head);
    PrintList(head);
    std::cout << "\nThe removed item: ";
    PrintList(removed);

    std::cout << "\nTry to remove -3: ";
    auto removed2 = RemoveItem(-3, head);
    PrintList(head);
    if (!removed2) std::cout << "\nItem not found\n";
}
Task: Function definition






PHP: function multiply( $a, $b )
{
    return $a * $b;
}
Task: Sum of elements below main diagonal of matrix


C++: #include <iostream>
#include <vector>

template<typename T>
T sum_below_diagonal(const std::vector<std::vector<T>>& matrix) {
    T sum = 0;
    for (std::size_t y = 0; y < matrix.size(); y++)
        for (std::size_t x = 0; x < matrix[y].size() && x < y; x++)
            sum += matrix[y][x];
    return sum;
}

int main() {
    std::vector<std::vector<int>> matrix = {
        {1,3,7,8,10},
        {2,4,16,14,4},
        {3,1,9,18,11},
        {12,14,17,18,20},
        {7,1,3,9,5}
    };
    
    std::cout << sum_below_diagonal(matrix) << std::endl;
    return 0;
}
Task: Sequence_ smallest number greater than previous term with exactly n divisors


C++: #include <iostream>

#define MAX 15

using namespace std;

int count_divisors(int n) {
    int count = 0;
    for (int i = 1; i * i <= n; ++i) {
        if (!(n % i)) {
            if (i == n / i)
                count++;
            else
                count += 2;
        }
    }
    return count;
}

int main() {
    cout << "The first " << MAX << " terms of the sequence are:" << endl;
    for (int i = 1, next = 1; next <= MAX; ++i) {
        if (next == count_divisors(i)) {           
            cout << i << " ";
            next++;
        }
    }
    cout << endl;
    return 0;
}
Task: Changeable words


C++: #include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

int hamming_distance(const std::string& str1, const std::string& str2) {
    size_t len1 = str1.size();
    size_t len2 = str2.size();
    if (len1 != len2)
        return 0;
    int count = 0;
    for (size_t i = 0; i < len1; ++i) {
        if (str1[i] != str2[i])
            ++count;
        
        if (count == 2)
            break;
    }
    return count;
}

int main(int argc, char** argv) {
    const char* filename(argc < 2 ? "unixdict.txt" : argv[1]);
    std::ifstream in(filename);
    if (!in) {
        std::cerr << "Cannot open file '" << filename << "'.\n";
        return EXIT_FAILURE;
    }
    std::string line;
    std::vector<std::string> dictionary;
    while (getline(in, line)) {
        if (line.size() > 11)
            dictionary.push_back(line);
    }
    std::cout << "Changeable words in " << filename << ":\n";
    int n = 1;
    for (const std::string& word1 : dictionary) {
        for (const std::string& word2 : dictionary) {
            if (hamming_distance(word1, word2) == 1)
                std::cout << std::setw(2) << std::right << n++
                    << ": " << std::setw(14) << std::left << word1
                    << " -> " << word2 << '\n';
        }
    }
    return EXIT_SUCCESS;
}
Task: Decision tables


C++: #include <iostream>
#include <string>
#include <vector>

const std::vector<std::pair<std::string, std::string>> conditions = {
    {"Printer prints", "NNNNYYYY"},
    {"A red light is flashing", "YYNNYYNN"},
    {"Printer is recognized by computer", "NYNYNYNY"}
};

const std::vector<std::pair<std::string, std::string>> actions = {
    {"Check the power cable", "NNYNNNNN"},
    {"Check the printer-computer cable", "YNYNNNNN"},
    {"Ensure printer software is installed", "YNYNYNYN"},
    {"Check/replace ink", "YYNNNYNN"},
    {"Check for paper jam", "NYNYNNNN"},
};

int main() {
    const size_t nc = conditions.size();
    const size_t na = actions.size();
    const size_t nr = conditions[0].second.size();
    const int np = 7;

    auto answers = new bool[nc];
    std::string input;

    std::cout << "Please answer the following questions with a y or n:\n";
    for (size_t c = 0; c < nc; c++) {
        char ans;
        do {
            std::cout << conditions[c].first << "? ";

            std::getline(std::cin, input);
            ans = std::toupper(input[0]);
        } while (ans != 'Y' && ans != 'N');
        answers[c] = ans == 'Y';
    }

    std::cout << "Recommended action(s)\n";
    for (size_t r = 0; r < nr; r++) {
        for (size_t c = 0; c < nc; c++) {
            char yn = answers[c] ? 'Y' : 'N';
            if (conditions[c].second[r] != yn) {
                goto outer;
            }
        }
        if (r == np) {
            std::cout << "  None (no problem detected)\n";
        } else {
            for (auto action : actions) {
                if (action.second[r] == 'Y') {
                    std::cout << "  " << action.first << '\n';
                }
            }
        }
        break;

    outer: {}
    }

    delete[] answers;
    return 0;
}
Task: Mosaic matrix


C++: #include <concepts>
#include <iostream>



void PrintMatrix(std::predicate<int, int, int> auto f, int size)
{
  for(int y = 0; y < size; y++)
  {
    for(int x = 0; x < size; x++)
    {
      std::cout << " " << f(x, y, size);
    }
    std::cout << "\n";
  }
  std::cout << "\n";
}

int main()
{
  
  auto mosaic = [](int x, int y, [[maybe_unused]]int size)
  {
    return (x + y) % 2 == 0;
  };

  PrintMatrix(mosaic, 8);
  PrintMatrix(mosaic, 9);
}
Task: Length of an arc between two angles


C++: #include <iostream>

#define _USE_MATH_DEFINES
#include <math.h>

double arcLength(double radius, double angle1, double angle2) {
    return (360.0 - abs(angle2 - angle1)) * M_PI * radius / 180.0;
}

int main() {
    auto al = arcLength(10.0, 10.0, 120.0);
    std::cout << "arc length: " << al << '\n';
    return 0;
}
Task: Shift list elements to left by 3


C++: #include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

template <typename T>
void print(const std::vector<T>& v) {
    std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
    std::cout << '\n';
}

int main() {
    std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::cout << "Before: ";
    print(vec);
    std::rotate(vec.begin(), vec.begin() + 3, vec.end());
    std::cout << " After: ";
    print(vec);
}