Sunday, 22 February 2015

Object Oriented Software(Programming) Explained by Steve Jobs

Below is one of the simplest explaination of OOPS i have read till now. Steve jobs explained it while giving  interview to  Rolling Stone's Jeff Goodel in 1994.

Here is an excerpt.

Jeff Goodel: Would you explain, in simple terms, exactly what object-oriented software is?

Steve Jobs: Objects are like people. They're living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we're doing right here.

Here's an example: If I'm your laundry object, you can give me your dirty clothes and send me a message that says, "Can you get my clothes laundered, please." I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, "Here are your clean clothes."

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can't even hail a taxi. You can't pay for one, you don't have dollars in your pocket. Yet I knew how to do all of that. And you didn't have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That's what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

Read More: www.rollingstone.com/culture/news/steve-jobs-in-1994-the-rolling-stone-interview-20110117

Monday, 16 February 2015

Mathematical Formulas for Programmer

Below are some of the formulas that can be useful while doing competitive programming.
  • 1∗1!+2∗2!+...+x∗x! = (x+1)!−1
  • (A+B)%C = (A%C + B%C )%C. 
  • (A* B)%C = ((A%C) * (B%C))%C 
  • LCM(a, b) = (a * b) / GCD(a, b)
     
I will be adding more formulas to it.
Please comment if you find anything incorrect.

Sunday, 15 February 2015

Maximum Frequency/Repeated element in a Vector/Array

Below is one of many algorithm to find maximum frequency of element in a Vector.

Algorithm Used:
  • Take a map <int,int> where its key is vector element and value is element count.
  • Iterate over the vector thereby incrementing element count in a map (map value by default is assigned to zero)
  • Iterate over the map by checking its value with maximum frequency till now.
  • Return the maximum repeated element and also get its frequency.
Program:

#include<iostream>
#include <vector>
#include <map>
using namespace std;
int MaxFrequency(vector<int> v,int& freq)
{
    int maxElement=0;
    map<int,int> m;
    for(int i=0;i<v.size();i++)
    {
        m[v[i]]= ++m[v[i]];

    }
    for(map<int,int>::iterator i=m.begin();i!=m.end();i++)
    {
        if((*i).second>freq)
        {
            freq= (*i).second;
            maxElement = (*i).first;
        }

    }

    return maxElement;
}
int main()
{
    int x,n,max=0,f=0;
    cin>>n;
    vector<int> a;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        a.push_back(x);
    }
    cout<<"Max Repeating element is: " <<MaxFrequency(a,f)<<" with Frequency: ";
    cout<<f;
    return 0;
}

Please comment if you find anything incorrect.