Ad
Code
Diff
  • local solution = {}
    
    function solution.removeEveryThird(str)
            local iters = math.ceil(#str/3)
            local result = ""
            local iter = 1
            for i=1, iters do
                    result = result..str:sub(iter, iter+2)
                    iter = iter + 4
            end
      
            return result  
    end
    
    return solution
    • function removeEveryThird(str) {
    • // Remove the third character from every work in the string
    • // using a zero based index
    • return 'heloword';
    • }
    • local solution = {}
    • function solution.removeEveryThird(str)
    • local iters = math.ceil(#str/3)
    • local result = ""
    • local iter = 1
    • for i=1, iters do
    • result = result..str:sub(iter, iter+2)
    • iter = iter + 4
    • end
    • return result
    • end
    • return solution
Fundamentals
Strings
Data Types
Logic
Code
Diff
  • local solution = {}
    
    function solution.getOutput(num)
      local result=""..(num%3==0 and "Fizz" or "")..(num%5==0 and "Buzz" or "")
      return result == "" and tostring(num) or result
    end
    
    return solution
    • public class FizzBuzz
    • {
    • public string GetOutput(int number)
    • {
    • if ((number % 3 == 0) && (number % 5 == 0))
    • return "FizzBuzz";
    • local solution = {}
    • else if (number % 3 == 0)
    • return "Fizz";
    • function solution.getOutput(num)
    • local result=""..(num%3==0 and "Fizz" or "")..(num%5==0 and "Buzz" or "")
    • return result == "" and tostring(num) or result
    • end
    • else if (number % 5 == 0)
    • return "Buzz";
    • else return number.ToString();
    • }
    • }
    • return solution
Fundamentals
Games
Code
Diff
  • local solution = {}
    
    function solution.riddle(str)
      return #str-1  
    end
    
    return solution
    • def riddle(word):
    • return len(word) - 1
    • local solution = {}
    • function solution.riddle(str)
    • return #str-1
    • end
    • return solution
Code
Diff
  • #!/usr/bin/lua
    
    local solution = {}
    
    function solution.sort_values(arr)
      table.sort(arr, function(a, b) return a < b end)  
      return arr
    end
    
    return solution
    • from __future__ import annotations
    • from collections import Counter
    • #!/usr/bin/lua
    • def sort_values(vals:list[int]) -> list[int]:
    • #Given an array of size N containing only 0s, 1s, and 2s;
    • #sort the array in ascending order.
    • assert set(vals) <= {0,1,2}
    • return counting_sort(vals)
    • # O(n+k) instead of n log(n)
    • local solution = {}
    • def counting_sort(vals:list[T]) -> list[T]:
    • res = []
    • c = Counter(vals)
    • for k,amt in sorted(c.items()):
    • res += [k]*amt
    • return res
    • function solution.sort_values(arr)
    • table.sort(arr, function(a, b) return a < b end)
    • return arr
    • end
    • return solution
Code
Diff
  • float scalar_product(Vector3D a, Vector3D b)
    {
        return (a.get_x()*b.get_x() + a.get_y()*b.get_y() + a.get_z()*b.get_z());
    }
    
    bool intersec(Segment a, Segment b)
    {
        Segment seg;
        float t1 = 0, t2 = 0, t = 0;
        t1+= scalar_product(a.start-b.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
        t1-= scalar_product(a.start-b.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
        t2+= scalar_product(a.end-a.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
        t2-= scalar_product(a.end-a.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
        t = t1/t2;
    
        float u1 = 0, u2 = 0;
        u1+= (scalar_product(a.start-b.start, b.end-b.start));
        u1+= t*scalar_product(a.end-a.start, b.end-b.start);
        u2 = scalar_product(b.end-b.start, b.end-b.start);
    
        seg.end = a.start + (a.end - a.start)*t;
        seg.start = b.start + (b.end - b.start)*u1/u2;
        Vector3D tmp = seg.end - seg.start;
        Vector3D close_point = (seg.end + seg.start) / 2;
    
        try
        {
            if (t2 == 0 || u2 == 0)
                throw std::string("error");
            if (scalar_product(close_point - a.start, a.end - a.start) < 0 ||
                scalar_product(close_point - a.end, a.start - a.end) < 0)
                throw std::string("error");
            if (scalar_product(close_point - b.start, b.end - b.start) < 0 ||
                scalar_product(close_point - b.end, b.start - b.end) < 0)
                throw std::string("error");
            if ((scalar_product(tmp, tmp) - 0.001) > 0)
                throw std::string("error");
        }
        catch(const std::string& ex)
        {
            return false;
        } 
        return true;
    }
    
    • float scalar_product(Vector3D a, Vector3D b)
    • {
    • return (a.get_x()*b.get_x() + a.get_y()*b.get_y() + a.get_z()*b.get_z());
    • }
    • bool intersec(Segment a, Segment b)
    • {
    • Segment seg;
    • float t1 = 0, t2 = 0, t = 0;
    • t1+= scalar_product(a.start-b.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
    • t1-= scalar_product(a.start-b.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
    • t2+= scalar_product(a.end-a.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
    • t2-= scalar_product(a.end-a.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
    • t = t1/t2;
    • float u1 = 0, u2 = 0;
    • u1+= (scalar_product(a.start-b.start, b.end-b.start));
    • u1+= t*scalar_product(a.end-a.start, b.end-b.start);
    • u2 = scalar_product(b.end-b.start, b.end-b.start);
    • seg.end = a.start + (a.end - a.start)*t;
    • seg.start = b.start + (b.end - b.start)*u1/u2;
    • Vector3D tmp = seg.end - seg.start;
    • Vector3D close_point = (seg.end + seg.start) / 2;
    • try
    • {
    • if (t2 == 0 || u2 == 0)
    • throw std::string("input segments are the same");
    • throw std::string("error");
    • if (scalar_product(close_point - a.start, a.end - a.start) < 0 ||
    • scalar_product(close_point - a.end, a.start - a.end) < 0)
    • throw std::string("point outside first segment bound");
    • throw std::string("error");
    • if (scalar_product(close_point - b.start, b.end - b.start) < 0 ||
    • scalar_product(close_point - b.end, b.start - b.end) < 0)
    • throw std::string("point outside second segment boundaries");
    • throw std::string("error");
    • if ((scalar_product(tmp, tmp) - 0.001) > 0)
    • throw std::string("no intersection point");
    • throw std::string("error");
    • }
    • catch(const std::string& ex)
    • {
    • std::cerr << ex << '\n';
    • exit(1);
    • }
    • if ((scalar_product(tmp, tmp) - 0.001) < 0)
    • return true;
    • return false;
    • return false;
    • }
    • return true;
    • }
Code
Diff
  • float scalar_product(Vector3D a, Vector3D b)
    {
        return (a.get_x()*b.get_x() + a.get_y()*b.get_y() + a.get_z()*b.get_z());
    }
    
    bool intersec(Segment a, Segment b)
    {
        Segment seg;
        float t1 = 0, t2 = 0, t = 0;
        t1+= scalar_product(a.start-b.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
        t1-= scalar_product(a.start-b.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
        t2+= scalar_product(a.end-a.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
        t2-= scalar_product(a.end-a.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
        t = t1/t2;
    
        float u1 = 0, u2 = 0;
        u1+= (scalar_product(a.start-b.start, b.end-b.start));
        u1+= t*scalar_product(a.end-a.start, b.end-b.start);
        u2 = scalar_product(b.end-b.start, b.end-b.start);
    
        seg.end = a.start + (a.end - a.start)*t;
        seg.start = b.start + (b.end - b.start)*u1/u2;
        Vector3D tmp = seg.end - seg.start;
        Vector3D close_point = (seg.end + seg.start) / 2;
    
        try
        {
            if (t2 == 0 || u2 == 0)
                throw std::string("input segments are the same");
            if (scalar_product(close_point - a.start, a.end - a.start) < 0 ||
                scalar_product(close_point - a.end, a.start - a.end) < 0)
                throw std::string("point outside first segment bound");
            if (scalar_product(close_point - b.start, b.end - b.start) < 0 ||
                scalar_product(close_point - b.end, b.start - b.end) < 0)
                throw std::string("point outside second segment boundaries");
            if ((scalar_product(tmp, tmp) - 0.001) > 0)
                throw std::string("no intersection point");
        }
        catch(const std::string& ex)
        {
            std::cerr << ex << '\n';
            return false;
        }
        
        return true;
    }
    
    • float scalar_product(Vector3D a, Vector3D b)
    • {
    • return (a.get_x()*b.get_x() + a.get_y()*b.get_y() + a.get_z()*b.get_z());
    • }
    • bool intersec(Segment a, Segment b)
    • {
    • Segment seg;
    • float t1 = 0, t2 = 0, t = 0;
    • t1+= scalar_product(a.start-b.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
    • t1-= scalar_product(a.start-b.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
    • t2+= scalar_product(a.end-a.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
    • t2-= scalar_product(a.end-a.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
    • t = t1/t2;
    • float u1 = 0, u2 = 0;
    • u1+= (scalar_product(a.start-b.start, b.end-b.start));
    • u1+= t*scalar_product(a.end-a.start, b.end-b.start);
    • u2 = scalar_product(b.end-b.start, b.end-b.start);
    • seg.end = a.start + (a.end - a.start)*t;
    • seg.start = b.start + (b.end - b.start)*u1/u2;
    • Vector3D tmp = seg.end - seg.start;
    • Vector3D close_point = (seg.end + seg.start) / 2;
    • try
    • {
    • if (t2 == 0 || u2 == 0)
    • throw std::string("input segments are the same");
    • if (scalar_product(close_point - a.start, a.end - a.start) < 0 ||
    • scalar_product(close_point - a.end, a.start - a.end) < 0)
    • throw std::string("point outside first segment bound");
    • if (scalar_product(close_point - b.start, b.end - b.start) < 0 ||
    • scalar_product(close_point - b.end, b.start - b.end) < 0)
    • throw std::string("point outside second segment boundaries");
    • if ((scalar_product(tmp, tmp) - 0.001) > 0)
    • throw std::string("no intersection point");
    • }
    • catch(const std::string& ex)
    • {
    • std::cerr << ex << '\n';
    • exit(1);
    • return false;
    • }
    • if ((scalar_product(tmp, tmp) - 0.001) < 0)
    • return true;
    • return false;
    • return true;
    • }

There is a vector class consisting of 3 coordinates:

class Vector3D
{
    private:
        float x;  
        float y;
        float z;
    public:
        Vector3D();
        Vector3D(float x, float y, float z);
        Vector3D(const Vector3D &v);

        float get_x();
        float get_y();
        float get_z();

        void set_x(float x);
        void set_y(float y);
        void set_z(float z);
};     

And a segment class consisting of 2 vectors:

class Segment
{
    public:
        Vector3D start;
        Vector3D end;

        Segment();
        Segment(Vector3D start, Vector3D end);

};

The task is to write a function that finds the intersection of two segments and return true || false value, if it exists. And return 'error' msg in other way.

Code
Diff
  • float scalar_product(Vector3D a, Vector3D b)
    {
        return (a.get_x()*b.get_x() + a.get_y()*b.get_y() + a.get_z()*b.get_z());
    }
    
    bool intersec(Segment a, Segment b)
    {
        Segment seg;
        float t1 = 0, t2 = 0, t = 0;
        t1+= scalar_product(a.start-b.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
        t1-= scalar_product(a.start-b.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
        t2+= scalar_product(a.end-a.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
        t2-= scalar_product(a.end-a.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
        t = t1/t2;
    
        float u1 = 0, u2 = 0;
        u1+= (scalar_product(a.start-b.start, b.end-b.start));
        u1+= t*scalar_product(a.end-a.start, b.end-b.start);
        u2 = scalar_product(b.end-b.start, b.end-b.start);
    
        seg.end = a.start + (a.end - a.start)*t;
        seg.start = b.start + (b.end - b.start)*u1/u2;
        Vector3D tmp = seg.end - seg.start;
        Vector3D close_point = (seg.end + seg.start) / 2;
    
        try
        {
            if (t2 == 0 || u2 == 0)
                throw std::string("input segments are the same");
            if (scalar_product(close_point - a.start, a.end - a.start) < 0 ||
                scalar_product(close_point - a.end, a.start - a.end) < 0)
                throw std::string("point outside first segment bound");
            if (scalar_product(close_point - b.start, b.end - b.start) < 0 ||
                scalar_product(close_point - b.end, b.start - b.end) < 0)
                throw std::string("point outside second segment boundaries");
            if ((scalar_product(tmp, tmp) - 0.001) > 0)
                throw std::string("no intersection point");
        }
        catch(const std::string& ex)
        {
            std::cerr << ex << '\n';
            exit(1);
        }
        
        if ((scalar_product(tmp, tmp) - 0.001) < 0)
            return true;
        return false;
    }
    
    • float scalar_product(Vector3D a, Vector3D b)
    • {
    • return (a.get_x()*b.get_x() + a.get_y()*b.get_y() + a.get_z()*b.get_z());
    • }
    • Vector3D intersec(Segment a, Segment b)
    • bool intersec(Segment a, Segment b)
    • {
    • Segment seg;
    • float t1 = 0, t2 = 0, t = 0;
    • t1+= scalar_product(a.start-b.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
    • t1-= scalar_product(a.start-b.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
    • t2+= scalar_product(a.end-a.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
    • t2-= scalar_product(a.end-a.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
    • t = t1/t2;
    • float u1 = 0, u2 = 0;
    • u1+= (scalar_product(a.start-b.start, b.end-b.start));
    • u1+= t*scalar_product(a.end-a.start, b.end-b.start);
    • u2 = scalar_product(b.end-b.start, b.end-b.start);
    • seg.end = a.start + (a.end - a.start)*t;
    • seg.start = b.start + (b.end - b.start)*u1/u2;
    • Vector3D tmp = seg.end - seg.start;
    • Vector3D close_point = (seg.end + seg.start) / 2;
    • try
    • {
    • if (t2 == 0 || u2 == 0)
    • throw std::string("input segments are the same");
    • if (scalar_product(close_point - a.start, a.end - a.start) < 0 ||
    • scalar_product(close_point - a.end, a.start - a.end) < 0)
    • throw std::string("point outside first segment bound");
    • if (scalar_product(close_point - b.start, b.end - b.start) < 0 ||
    • scalar_product(close_point - b.end, b.start - b.end) < 0)
    • throw std::string("point outside second segment boundaries");
    • if ((scalar_product(tmp, tmp) - 0.001) > 0)
    • throw std::string("no intersection point");
    • }
    • catch(const std::string& ex)
    • {
    • std::cerr << ex << '\n';
    • exit(1);
    • }
    • return close_point;
    • if ((scalar_product(tmp, tmp) - 0.001) < 0)
    • return true;
    • return false;
    • }

There is a vector class consisting of 3 coordinates and a segment class consisting of 2 vectors. The task is to write a function that finds the intersection of two segments, if it exists.

The idea of solving the problem is to find the minimum distance between the segments, and if it is less than a certain specified value, then we can assume that the segments intersect.

float scalar_product(Vector3D a, Vector3D b)
{
    return (a.get_x()*b.get_x() + a.get_y()*b.get_y() + a.get_z()*b.get_z());
}

Vector3D intersec(Segment a, Segment b)
{
    Segment seg;
    float t1 = 0, t2 = 0, t = 0;
    t1+= scalar_product(a.start-b.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
    t1-= scalar_product(a.start-b.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
    t2+= scalar_product(a.end-a.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
    t2-= scalar_product(a.end-a.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
    t = t1/t2;

    float u1 = 0, u2 = 0;
    u1+= (scalar_product(a.start-b.start, b.end-b.start));
    u1+= t*scalar_product(a.end-a.start, b.end-b.start);
    u2 = scalar_product(b.end-b.start, b.end-b.start);

    seg.end = a.start + (a.end - a.start)*t;
    seg.start = b.start + (b.end - b.start)*u1/u2;
    Vector3D tmp = seg.end - seg.start;
    Vector3D close_point = (seg.end + seg.start) / 2;

    try
    {
        if (scalar_product(close_point - a.start, a.end - a.start) < 0 ||
            scalar_product(close_point - a.end, a.start - a.end) < 0)
            throw std::string("point outside first segment bound");
        if (scalar_product(close_point - b.start, b.end - b.start) < 0 ||
            scalar_product(close_point - b.end, b.start - b.end) < 0)
            throw std::string("point outside second segment boundaries");
        if ((scalar_product(tmp, tmp) - 0.001) > 0)
            throw std::string("no intersection point");
    }
    catch(const std::string& ex)
    {
        std::cerr << ex << '\n';
        exit(1);
    }
    
    return close_point;
}