We know that Finn
is friends with Jake
, Flame Princess
is friends with Finn
as is Princess Bubblegum
, Jake
is friends with Lady Unicorn
and Princess Bubblegum
.
Write a function friends(X) -> [Y]
to find out who are the friends of a person X
.
friend('Finn', 'Jake').
friend('Flame Princess', 'Finn').
friend('Princess Bubblegum', 'Finn').
friend('Jake', 'Lady Unicorn').
friend('Jake', 'Princess Bubblegum').
friends(X, R) :- findall(Y, friend(X, Y), R1), findall(Y, friend(Y, X), R2), append(R1, R2, R).
% plunit can be used to test solution
:- begin_tests(friends).
:- include(friends).
test(friends_of_finn) :-
friends('Finn', X),
assertion(X == ['Jake', 'Flame Princess', 'Princess Bubblegum']).
test(friends_of_bubblegum) :-
friends('Princess Bubblegum', X),
assertion(X == ['Finn', 'Jake']).
:- end_tests(friends).
hello :- write("Hello, Prolog!").
System.Console.WriteLine("Hello, C#!");- hello :- write("Hello, Prolog!").
% plunit can be used to test solution :- begin_tests(example). :- include(example). test(example_test) :- hello, assertion(true == true). :- end_tests(example).
- % plunit can be used to test solution
- :- begin_tests(example).
- :- include(example).
- test(example_test) :-
- hello,
- assertion(true == true).
- :- end_tests(example).
add(X, 0, X) :- !. add(0, Y, Y) :- !. add(X, 1, R) :- succ(X, R), !. add(1, Y, R) :- succ(Y, R), !. add(X, X, R) :- plus(X, X, R), !. add(X, Y, R) :- plus(X, Y, R).
def add(a, b):return a + b- add(X, 0, X) :- !.
- add(0, Y, Y) :- !.
- add(X, 1, R) :- succ(X, R), !.
- add(1, Y, R) :- succ(Y, R), !.
- add(X, X, R) :- plus(X, X, R), !.
- add(X, Y, R) :- plus(X, Y, R).
% plunit can be used to test solution :- begin_tests(example). :- include(example). test(test_1) :- add(1, 2, R), assertion(R == 3). test(test_2) :- add(2, 1, R), assertion(R == 3). test(test_3) :- add(2, 2, R), assertion(R == 4). test(test_4) :- add(2, 0, R), assertion(R == 2). test(test_5) :- add(2, 3, R), assertion(R == 5). :- end_tests(example).
import codewars_test as test# TODO Write testsimport solution # or from solution import example- % plunit can be used to test solution
- :- begin_tests(example).
- :- include(example).
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(1 + 1, 2)- test(test_1) :-
- add(1, 2, R),
- assertion(R == 3).
- test(test_2) :-
- add(2, 1, R),
- assertion(R == 3).
- test(test_3) :-
- add(2, 2, R),
- assertion(R == 4).
- test(test_4) :-
- add(2, 0, R),
- assertion(R == 2).
- test(test_5) :-
- add(2, 3, R),
- assertion(R == 5).
- :- end_tests(example).
is_odd(X) :- 1 is X mod 2. odd_count(N, R) :- is_odd(N) -> odd_count_recur(N, R); odd_count_recur(N + 1, R). odd_count_recur(0, 0) :- !. odd_count_recur(1, 0) :- !. odd_count_recur(N, R) :- N1 is N - 2, odd_count_recur(N1, R1), R is 1 + R1.
#import <Foundation/Foundation.h>@interface SampleClass:NSObject/* method declaration */- (int)odd_count:(int)n;@end@implementation SampleClass- (int) odd_count:(int) n{//your code hereint odd_counter = 0;for (int i = 1; i < n; i=i+2) {// Numbers that are divisible by 2if (i % 2 != 0)odd_counter++;}return odd_counter;}@end- is_odd(X) :- 1 is X mod 2.
- odd_count(N, R) :- is_odd(N) -> odd_count_recur(N, R); odd_count_recur(N + 1, R).
- odd_count_recur(0, 0) :- !.
- odd_count_recur(1, 0) :- !.
- odd_count_recur(N, R) :- N1 is N - 2, odd_count_recur(N1, R1), R is 1 + R1.
% plunit can be used to test solution :- begin_tests(problem). :- include(problem). test(test_odd_1) :- is_odd(3), assertion(true = true). test(test_odd_2) :- \+ is_odd(4), assertion(true = true). test(test_1) :- odd_count(5, R), assertion(R = 2). test(test_2) :- odd_count(6, R), assertion(R = 3). test(test_3) :- odd_count(15023, R), assertion(R = 7511). test(test_4) :- odd_count(15024, R), assertion(R = 7512). :- end_tests(problem).
// TODO: replace with your own tests, these are just how-to examples.// Codewars uses UnitKit unit testing framework.// See https://github.com/codewars/codewars.com/wiki/UnitKit@implementation TestSuite- (void)testCanBeSolvedWithBasicComputations{int ret;SampleClass *sampleClass = [[SampleClass alloc]init];ret = [sampleClass odd_count:5];UKIntsEqual(2, ret);ret = [sampleClass odd_count:15023];UKIntsEqual(7511, ret);}@end- % plunit can be used to test solution
- :- begin_tests(problem).
- :- include(problem).
- test(test_odd_1) :- is_odd(3), assertion(true = true).
- test(test_odd_2) :- \+ is_odd(4), assertion(true = true).
- test(test_1) :- odd_count(5, R), assertion(R = 2).
- test(test_2) :- odd_count(6, R), assertion(R = 3).
- test(test_3) :- odd_count(15023, R), assertion(R = 7511).
- test(test_4) :- odd_count(15024, R), assertion(R = 7512).
- :- end_tests(problem).
Fundamentals
Numbers
Data Types
Integers
is_odd(X, true) :- 1 is X /\ 1, !. is_odd(_X, false).
public static class Kata{public static bool IsOdd(int input){return ((input % 10 == 1) || (input % 10 == 3) || (input % 10 == 5) || (input % 10 == 7) || (input % 10 == 9));}}- is_odd(X, true) :- 1 is X /\ 1, !.
- is_odd(_X, false).
% plunit can be used to test solution :- begin_tests(challenge). :- include(challenge). test(test_1) :- is_odd(1, Z), assertion(Z == true). test(test_2) :- is_odd(2, Z), assertion(Z == false). test(test_3) :- is_odd(13, Z), assertion(Z == true). test(test_4) :- is_odd(18284, Z), assertion(Z == false). :- end_tests(challenge).
namespace Solution {using NUnit.Framework;using System;- % plunit can be used to test solution
- :- begin_tests(challenge).
- :- include(challenge).
// TODO: Replace examples and use TDD by writing your own tests- test(test_1) :- is_odd(1, Z), assertion(Z == true).
- test(test_2) :- is_odd(2, Z), assertion(Z == false).
- test(test_3) :- is_odd(13, Z), assertion(Z == true).
- test(test_4) :- is_odd(18284, Z), assertion(Z == false).
[TestFixture]public class SolutionTest{[Test]public void MyTest(){Assert.AreEqual(true, Kata.IsOdd(1));Assert.AreEqual(false, Kata.IsOdd(2));Assert.AreEqual(true, Kata.IsOdd(13));Assert.AreEqual(false, Kata.IsOdd(18284));}}}- :- end_tests(challenge).
div2(X, Y) :- Y is X >> 1.
unsigned long long div2(unsigned long long a){//Use binary operators...I-Oreturn a >> 1;}- div2(X, Y) :- Y is X >> 1.
% plunit can be used to test solution :- begin_tests(challenge). :- include(challenge). test(test_1) :- div2(10, X), assertion(X == 5). test(test_2) :- div2(0, X), assertion(X == 0). test(test_3) :- div2(3, X), assertion(X == 1). test(test_4) :- div2(1234567890, X), assertion(X == 617283945). :- end_tests(challenge).
// TODO: Replace examples and use TDD by writing your own tests- % plunit can be used to test solution
- :- begin_tests(challenge).
- :- include(challenge).
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));}};- test(test_1) :- div2(10, X), assertion(X == 5).
- test(test_2) :- div2(0, X), assertion(X == 0).
- test(test_3) :- div2(3, X), assertion(X == 1).
- test(test_4) :- div2(1234567890, X), assertion(X == 617283945).
- :- end_tests(challenge).
digit_sum = x => String(Math.abs(x)).split('').reduce((a, x) => a + +x, 0);
def digit_sum(number: int) -> int:return(sum([int(num) for num in str(abs(number))]))- digit_sum = x => String(Math.abs(x)).split('').reduce((a, x) => a + +x, 0);
const chai = require("chai"); const assert = chai.assert; chai.config.truncateThreshold=0; describe("Tests", () => { it("Test Cases", () => { assert.strictEqual(1 + 1, 2); assert.strictEqual(digit_sum(55001),11); assert.strictEqual(digit_sum(104),5); assert.strictEqual(digit_sum(0),0); assert.strictEqual(digit_sum(-1204),7); assert.strictEqual(digit_sum(Number.MAX_SAFE_INTEGER),76); }); });
from unittest import TestCasefrom solution import digit_sum- const chai = require("chai");
- const assert = chai.assert;
- chai.config.truncateThreshold=0;
import codewars_test as testfrom solution import digit_sum@test.describe('Tests')def tests():@test.it('Test Cases')def test_sums():test.assert_equals(digit_sum(10000000000000000000000000000),1)test.assert_equals(digit_sum(55001),11)test.assert_equals(digit_sum(104),5)test.assert_equals(digit_sum(0),0)test.assert_equals(digit_sum(-1204),7)- describe("Tests", () => {
- it("Test Cases", () => {
- assert.strictEqual(1 + 1, 2);
- assert.strictEqual(digit_sum(55001),11);
- assert.strictEqual(digit_sum(104),5);
- assert.strictEqual(digit_sum(0),0);
- assert.strictEqual(digit_sum(-1204),7);
- assert.strictEqual(digit_sum(Number.MAX_SAFE_INTEGER),76);
- });
- });