Page 4 of 4 FirstFirst ... 234
Results 31 to 37 of 37

Thread: Beginners Programming Challenge #20

  1. #31
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Programming Challenge #20

    Huh, I should probably pick a winner. I'll wait for mo.reina to finish, though.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  2. #32

    Re: Beginners Programming Challenge #20

    all done.

  3. #33
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Programming Challenge #20

    Alright! Sorry it's taken so long to pick a winner for this, school tends to be distracting.

    Anyways, the winning entry belong to jwbrase! His Haskell entry was concise, functional (in both senses of the word), and implemented the other functions in terms of reduce. Thank you to all of the participants, and congratulations jwbrase! We'll be looking out for the next challenge.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  4. #34
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Programming Challenge #20

    jwbrase has let me know he can't run the next challenge at the moment - is there anyone else interested in hosting it?
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  5. #35
    Join Date
    Jul 2007
    Location
    Austin, TX
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Programming Challenge #20

    Quote Originally Posted by schauerlich View Post
    jwbrase has let me know he can't run the next challenge at the moment - is there anyone else interested in hosting it?
    I'd be happy to do it

  6. #36
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginners Programming Challenge #20

    Quote Originally Posted by Queue29 View Post
    I'd be happy to do it
    Alright. Unless someone from the beginner's team objects, go ahead and make the next one!
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  7. #37
    Join Date
    Nov 2009
    Location
    The Netherlands
    Beans
    239
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Beginners Programming Challenge #20

    I know the challenge is officially over, but i saw noone gave a C++ solution, so i thought i should give it a try (and refresh my template skills which i haven't used for a long time).

    Code:
    #include <iostream>
    #include <vector>
    
    template <typename Function, typename T>
    std::vector<T> map(Function f, std::vector<T> const &data)
    {
        std::vector<T> result;
        for (typename std::vector<T>::const_iterator it = data.begin();
                it != data.end();
                    ++it)
            result.push_back(f(*it));
        return result;
    }
    
    template <typename T>
    class Square
    {
        public:
            T operator()(T const &x)
            {
                return x * x;
            }
    };
    
    template <typename Function, typename T>
    std::vector<T> filter(Function f, std::vector<T> const &data)
    {
        std::vector<T> result;
        for (typename std::vector<T>::const_iterator it = data.begin();
                it != data.end();
                    ++it)
            if (f(*it))
                result.push_back(*it);
        return result;
    }
    
    class isOdd
    {
        public:
            int operator()(int x)
            {
                return x & 1;
            }
    };
    
    template <typename Function, typename T>
    T reduce(Function f, std::vector<T> const & data, T const &i)
    {
        T value = i;
        for (typename std::vector<T>::const_iterator it = data.begin();
                it != data.end();
                    ++it)
                value = f(value, *it);
        return value;
    }
    
    template <typename T>
    class add
    {
        public:
            T operator()(T const &v1, T const &v2)
            {
                return v1 + v2;
            }
    };
    
    template <typename T>
    void print(std::vector<T> const &data)
    {
        for (typename std::vector<T>::const_iterator it = data.begin();
                it != data.end();
                    ++it)
            std::cout << *it << ' ';
        std::cout << '\n';
    }
    
    int main()
    {
        int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        std::vector<int> vec(array, array + 10);
    
        std::vector<int> squares = map(Square<int>(), vec);
        print(squares);
    
        std::vector<int> odds = filter(isOdd(), vec);
        print(odds);
    
        std::vector<int> vec2(array, array + 5);
        int sum = reduce(add<int>(), vec2, 0);
        std::cout << sum << std::endl;
        return 0;
    }

Page 4 of 4 FirstFirst ... 234

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •