int main;
#include<criterion/criterion.h>
#include <stdbool.h> bool odd_even(int n) { goto start; uwu: goto end; start: goto uwu; end: return !(n&1); goto start; }
def odd_even(n):def invert_sign(s):if s == "odd":return "e" + "v" + "e" + "n"else:return "o" + "d" + "d"if n < 0:return odd_even(n - 2 * (-abs(-n)))n = int(n ** (81 ** .25))sign = "even"x = 0.while x < n:sign = invert_sign(sign)x += 3.14159265358 // 3n = pow(n, 1)return sign == "odd"- #include <stdbool.h>
- bool odd_even(int n)
- {
- goto start;
- uwu:
- goto end;
- start:
- goto uwu;
- end:
- return !(n&1);
- goto start;
- }
#include<criterion/criterion.h> #include<stdbool.h> bool odd_even(int); Test(odd_even, all) { cr_assert(odd_even(0)); cr_assert(odd_even(4)); cr_assert(odd_even(2)); cr_assert(odd_even(6)); cr_assert(odd_even(8)); cr_assert(!odd_even(1)); cr_assert(!odd_even(3)); cr_assert(!odd_even(5)); cr_assert(!odd_even(7)); cr_assert(!odd_even(9)); cr_assert(odd_even(-4)); cr_assert(odd_even(-2)); cr_assert(odd_even(-6)); cr_assert(odd_even(-8)); cr_assert(!odd_even(-1)); cr_assert(!odd_even(-3)); cr_assert(!odd_even(-5)); cr_assert(!odd_even(-7)); cr_assert(!odd_even(-9)); }
import codewars_test as testfrom solution import odd_even- #include<criterion/criterion.h>
- #include<stdbool.h>
@test.describe("Example")def test_group():@test.it("test case 1: Testing Even Numbers")def test_case():for n in range(-100, 101, 2):test.assert_equals(odd_even(n), 0)@test.it("test case 2: Testing Odd Numbers")def test_case():for n in range(-101, 101, 2):test.assert_equals(odd_even(n), 1)- bool odd_even(int);
- Test(odd_even, all) {
- cr_assert(odd_even(0));
- cr_assert(odd_even(4));
- cr_assert(odd_even(2));
- cr_assert(odd_even(6));
- cr_assert(odd_even(8));
- cr_assert(!odd_even(1));
- cr_assert(!odd_even(3));
- cr_assert(!odd_even(5));
- cr_assert(!odd_even(7));
- cr_assert(!odd_even(9));
- cr_assert(odd_even(-4));
- cr_assert(odd_even(-2));
- cr_assert(odd_even(-6));
- cr_assert(odd_even(-8));
- cr_assert(!odd_even(-1));
- cr_assert(!odd_even(-3));
- cr_assert(!odd_even(-5));
- cr_assert(!odd_even(-7));
- cr_assert(!odd_even(-9));
- }
#include <vector> long add_arr(std::vector<int> arr) { long result = 0; while(arr.size()) { result += arr[0]; arr.erase(arr.begin()); } return result; }
#include <array>- #include <vector>
template <std::size_t L>- long
add_arr(std::array<int, L> arr)- add_arr(std::vector<int> arr)
- {
- long result = 0;
for (int i : arr) result += i;- while(arr.size()) {
- result += arr[0];
- arr.erase(arr.begin());
- }
- return result;
}template long add_arr(std::array<int, 9> arr);- }
#include <vector> #include <criterion/criterion.h> long add_arr(std::vector<int> arr); Test(add_arr, all) { std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; cr_assert_eq(add_arr(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); std::vector<int> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9}; cr_assert_eq(add_arr(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9); }
#include <array>- #include <vector>
- #include <criterion/criterion.h>
template <std::size_t L>long add_arr(std::array<int, L> arr);- long add_arr(std::vector<int> arr);
- Test(add_arr, all)
- {
std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};cr_assert_eq(add_arr<9>(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);- std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
- cr_assert_eq(add_arr(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);
std::array<int, 9> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9};cr_assert_eq(add_arr<9>(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9);- std::vector<int> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9};
- cr_assert_eq(add_arr(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9);
- }
Algorithms
Arrays
create a pop_first and pop_last functions which will delete first/last element from vector and then return it
#include<vector>
template <typename T>
T
pop_last(std::vector<T>* v) {
T result = (*v)[v->size() - 1];
v->pop_back();
return result;
}
template <typename T>
T
pop_first(std::vector<T>* v) {
T result = (*v)[0];
v->erase(v->begin());
return result;
}
template int pop_last(std::vector<int>*);
template int pop_first(std::vector<int>*);
#include<vector>
template <typename T> T pop_last(std::vector<T>* v);
template <typename T> T pop_first(std::vector<T>* v);
Describe(pop)
{
It(_pop_last_)
{
std::vector<int> vec = {1,2,3,4};
Assert::That(pop_last(&vec), Equals(4));
Assert::That(pop_last(&vec), Equals(3));
Assert::That(pop_last(&vec), Equals(2));
Assert::That(pop_last(&vec), Equals(1));
}
It(_pop_first_)
{
std::vector<int> vec2 = {1,2,3,4};
Assert::That(pop_first(&vec2), Equals(1));
Assert::That(pop_first(&vec2), Equals(2));
Assert::That(pop_first(&vec2), Equals(3));
Assert::That(pop_first(&vec2), Equals(4));
}
};
module AddNumbers where add_arr x = sum x
pub fn add_arr(arr: &[i16]) -> i16{arr.iter().sum()}- module AddNumbers where
- add_arr x = sum x
module AddNumbersSpec where import Test.Hspec import AddNumbers spec :: Spec spec = do describe "add_arr" $ do it "1..9" $ do (add_arr [1..9]) `shouldBe` (45) it "1..3,-4,5..9" $ do (add_arr [1,2,3,-4,5,6,7,8,9]) `shouldBe` (37)
#[cfg(test)]mod tests {use super::*;- module AddNumbersSpec where
#[test]fn test_add_arr() {assert_eq!(add_arr(&[1,2,3,4,5,6,7,8,9]), 45);assert_eq!(add_arr(&[1,2,3,-4,5,6,7,8,9]), 37);}}- import Test.Hspec
- import AddNumbers
- spec :: Spec
- spec = do
- describe "add_arr" $ do
- it "1..9" $ do
- (add_arr [1..9]) `shouldBe` (45)
- it "1..3,-4,5..9" $ do
- (add_arr [1,2,3,-4,5,6,7,8,9]) `shouldBe` (37)
pub fn add_arr(arr: &[i16]) -> i16{ arr.iter().sum() }
#include <array>template <std::size_t L>longadd_arr(std::array<int, L> arr){long result = 0;for (int i : arr) result += i;return result;}template long add_arr(std::array<int, 9> arr);- pub fn add_arr(arr: &[i16]) -> i16{
- arr.iter().sum()
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test_add_arr() { assert_eq!(add_arr(&[1,2,3,4,5,6,7,8,9]), 45); assert_eq!(add_arr(&[1,2,3,-4,5,6,7,8,9]), 37); } }
#include <array>#include <criterion/criterion.h>- #[cfg(test)]
- mod tests {
- use super::*;
template <std::size_t L>long add_arr(std::array<int, L> arr);Test(add_arr, all){std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};cr_assert_eq(add_arr<9>(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);std::array<int, 9> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9};cr_assert_eq(add_arr<9>(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9);- #[test]
- fn test_add_arr() {
- assert_eq!(add_arr(&[1,2,3,4,5,6,7,8,9]), 45);
- assert_eq!(add_arr(&[1,2,3,-4,5,6,7,8,9]), 37);
- }
- }
add-sum(){ expr \ $( echo -n 0 for i in "$@"; do { echo -n " + ${i}" }; done ) } add-sum "$@"
function addArr(arr){if(arr.length === 0) return nulllet final = 0arr.forEach(num => {final += num})return final}- add-sum(){
- expr \
- $(
- echo -n 0
- for i in "$@"; do {
- echo -n " + ${i}"
- }; done
- )
- }
- add-sum "$@"
output = run_shell args: [1,2,3,4,5,6,7,8,9] describe "Solution" do it "sum of 1..9" do expect(output).to include('45') end end output2 = run_shell args: [1,2,3,4,5,6,7,8] describe "Solution" do it "sum of 1..8" do expect(output2).to include('36') end end output3 = run_shell args: [1,2,3,4,5,6,7] describe "Solution" do it "sum of 1..7" do expect(output3).to include('28') end end
- output = run_shell args: [1,2,3,4,5,6,7,8,9]
const chai = require("chai");const assert = chai.assert;- describe "Solution" do
- it "sum of 1..9" do
- expect(output).to include('45')
- end
- end
describe("Solution", function() {it("should test for something", function() {assert.strictEqual(addArr([1, 2, 3, 4, 5]), 15);assert.strictEqual(addArr([1, 100]), 101)assert.strictEqual(addArr([]), null)});});- output2 = run_shell args: [1,2,3,4,5,6,7,8]
- describe "Solution" do
- it "sum of 1..8" do
- expect(output2).to include('36')
- end
- end
- output3 = run_shell args: [1,2,3,4,5,6,7]
- describe "Solution" do
- it "sum of 1..7" do
- expect(output3).to include('28')
- end
- end
(ns arr-sum) (def arr-add #(reduce + %))
function addArr(arr){if(arr.length === 0) return nulllet final = 0arr.forEach(num => {final += num})return final}- (ns arr-sum)
- (def arr-add #(reduce + %))
(ns arr-sum-test (:require [clojure.test :refer :all] [arr-sum :refer [arr-add]])) (deftest arr-add-test (is (arr-add (list 1 2 3 4 5 6 7 8 9)) (+ 1 2 3 4 5 6 7 8 9)) (is (arr-add (list 1 2 3 -4 5 6 7 8 9)) (+ 1 2 3 -4 5 6 7 8 9)))
const chai = require("chai");const assert = chai.assert;describe("Solution", function() {it("should test for something", function() {assert.strictEqual(addArr([1, 2, 3, 4, 5]), 15);assert.strictEqual(addArr([1, 100]), 101)assert.strictEqual(addArr([]), null)});});- (ns arr-sum-test
- (:require [clojure.test :refer :all]
- [arr-sum :refer [arr-add]]))
- (deftest arr-add-test
- (is
- (arr-add (list 1 2 3 4 5 6 7 8 9))
- (+ 1 2 3 4 5 6 7 8 9))
- (is
- (arr-add (list 1 2 3 -4 5 6 7 8 9))
- (+ 1 2 3 -4 5 6 7 8 9)))
#include <array> template <std::size_t L> long add_arr(std::array<int, L> arr) { long result = 0; for (int i : arr) result += i; return result; } template long add_arr(std::array<int, 9> arr);
- #include <array>
- template <std::size_t L>
- long
add_arr(const unsigned long len, const int arr[len])- add_arr(std::array<int, L> arr)
- {
- long result = 0;
for(unsigned long i = 0;i < len;result += arr[i++]);- for (int i : arr) result += i;
- return result;
}- }
- template long add_arr(std::array<int, 9> arr);
#include <array> #include <criterion/criterion.h> template <std::size_t L> long add_arr(std::array<int, L> arr); Test(add_arr, all) { std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; cr_assert_eq(add_arr<9>(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); std::array<int, 9> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9}; cr_assert_eq(add_arr<9>(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9); }
#include<criterion/criterion.h>- #include <array>
- #include <criterion/criterion.h>
long add_arr(const unsigned long len, const int[len]);- template <std::size_t L>
- long add_arr(std::array<int, L> arr);
Test (add_arr, all) {int arr[] = {1,2,3,4,5,6,7,8,9};cr_assert_eq(add_arr(9, arr), 1+2+3+4+5+6+7+8+9);int arr2[] = {1,-2,3,4,5,6,7,8,9};cr_assert_eq(add_arr(9, arr2), 1+(-2)+3+4+5+6+7+8+9);- Test(add_arr, all)
- {
- std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
- cr_assert_eq(add_arr<9>(arr), 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);
- std::array<int, 9> arr2 = {1, -2, 3, 4, 5, 6, 7, 8, 9};
- cr_assert_eq(add_arr<9>(arr2), 1 + (-2) + 3 + 4 + 5 + 6 + 7 + 8 + 9);
- }
long add_arr(const unsigned long len, const int arr[len]) { long result = 0; for ( unsigned long i = 0; i < len; result += arr[i++] ); return result; }
function addArr(arr){- long
- add_arr(const unsigned long len, const int arr[len])
- {
- long result = 0;
if(arr.length === 0) return null- for
- (
- unsigned long i = 0;
- i < len;
- result += arr[i++]
- );
let final = 0arr.forEach(num => {final += num})return final- return result;
- }
#include<criterion/criterion.h> long add_arr(const unsigned long len, const int[len]); Test (add_arr, all) { int arr[] = {1,2,3,4,5,6,7,8,9}; cr_assert_eq(add_arr(9, arr), 1+2+3+4+5+6+7+8+9); int arr2[] = {1,-2,3,4,5,6,7,8,9}; cr_assert_eq(add_arr(9, arr2), 1+(-2)+3+4+5+6+7+8+9); }
- #include<criterion/criterion.h>
const chai = require("chai");const assert = chai.assert;- long add_arr(const unsigned long len, const int[len]);
describe("Solution", function() {it("should test for something", function() {assert.strictEqual(addArr([1, 2, 3, 4, 5]), 15);assert.strictEqual(addArr([1, 100]), 101)assert.strictEqual(addArr([]), null)});});- Test (add_arr, all) {
- int arr[] = {1,2,3,4,5,6,7,8,9};
- cr_assert_eq(add_arr(9, arr), 1+2+3+4+5+6+7+8+9);
- int arr2[] = {1,-2,3,4,5,6,7,8,9};
- cr_assert_eq(add_arr(9, arr2), 1+(-2)+3+4+5+6+7+8+9);
- }
Strings
Big Integers
more test cases
added logs
__uint128_t solution(char* str1, char* str2) { __uint128_t result = 0; for (unsigned long i = 0; i < 7; ++i) { result += (((__uint128_t) str1[i]) << (8*(i + 0))) + (((__uint128_t) str2[i]) << (8*(i + 8))); } return result; }
- __uint128_t solution(char* str1, char* str2) {
- __uint128_t result = 0;
- for (unsigned long i = 0; i < 7; ++i) {
- result +=
(((__uint128_t) str1[i]) << (8*i)) +- (((__uint128_t) str1[i]) << (8*(i + 0))) +
- (((__uint128_t) str2[i]) << (8*(i + 8)));
- }
- return result;
- }
#include <stdio.h> #include <string.h> #include <criterion/criterion.h> __uint128_t solution(char* str1, char* str2); #define test_case(name,str1,str2)\ Test(solution, name) {\ __uint128_t name##_r = solution(str1,str2);\ cr_assert_eq(strcmp(((char*) &name##_r)+0,str1),0);\ cr_assert_eq(strcmp(((char*) &name##_r)+8,str2),0);\ printf("solution(\"" str1 "\", \"" str2 "\"); -> {\"%s\\0\", \"%s\\0\"}\n", ((char*) &name##_r)+0, ((char*) &name##_r)+8);\ } test_case(amogus , "amogus ", "so sus!"); test_case(hello , "Hello, ", "World!" ); test_case(ok_guys, "ok " , "guys" );
- #include <stdio.h>
- #include <string.h>
- #include <criterion/criterion.h>
- __uint128_t solution(char* str1, char* str2);
Test(solution, hello_world) {__uint128_t hello_r = solution("hello, ","world!\n");cr_assert_eq(strcmp(((char*) &hello_r)+0,"hello, "),0);cr_assert_eq(strcmp(((char*) &hello_r)+8,"world!\n"),0);printf("%s", ((char*) &hello_r)+0);printf("%s", ((char*) &hello_r)+8);}- #define test_case(name,str1,str2)\
- Test(solution, name) {\
- __uint128_t name##_r = solution(str1,str2);\
- cr_assert_eq(strcmp(((char*) &name##_r)+0,str1),0);\
- cr_assert_eq(strcmp(((char*) &name##_r)+8,str2),0);\
- printf("solution(\"" str1 "\", \"" str2 "\"); -> {\"%s\\0\", \"%s\\0\"}\n", ((char*) &name##_r)+0, ((char*) &name##_r)+8);\
- }
- test_case(amogus , "amogus ", "so sus!");
- test_case(hello , "Hello, ", "World!" );
- test_case(ok_guys, "ok " , "guys" );
Strings
all described in title lol
__uint128_t solution(char* str1, char* str2) {
__uint128_t result = 0;
for (unsigned long i = 0; i < 7; ++i) {
result +=
(((__uint128_t) str1[i]) << (8*i)) +
(((__uint128_t) str2[i]) << (8*(i + 8)));
}
return result;
}
#include <stdio.h>
#include <string.h>
#include <criterion/criterion.h>
__uint128_t solution(char* str1, char* str2);
Test(solution, hello_world) {
__uint128_t hello_r = solution("hello, ","world!\n");
cr_assert_eq(strcmp(((char*) &hello_r)+0,"hello, "),0);
cr_assert_eq(strcmp(((char*) &hello_r)+8,"world!\n"),0);
printf("%s", ((char*) &hello_r)+0);
printf("%s", ((char*) &hello_r)+8);
}