Created
September 5, 2016 07:44
-
-
Save genuinelucifer/7c8a321144fa411765649f71c1177ec7 to your computer and use it in GitHub Desktop.
My solution to the problem https://www.hackerrank.com/challenges/stockprediction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <map> | |
#include <set> | |
#include <list> | |
#include <cmath> | |
#include <ctime> | |
#include <cstdio> | |
#include <vector> | |
#include <cstdlib> | |
#include <numeric> | |
#include <sstream> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
bool printYO = false; | |
#define NUM_ITR 4000 | |
#define NORMALISATION 10000.0f | |
double nextPred(vector<double> curval) | |
{ | |
double alpha = 0.001d; | |
vector< vector<double> > X; | |
vector<double> Y; | |
for(int i=0;i<2;++i) | |
{ | |
vector<double> tv; | |
tv.push_back(1.0); | |
for(int j=0;j<3;++j) | |
{ | |
tv.push_back(curval[i+j]/NORMALISATION); | |
tv.push_back(pow(curval[i+j], 0.05)/NORMALISATION); | |
} | |
Y.push_back(curval[i+3]); | |
X.push_back(tv); | |
} | |
//system("PAUSE"); | |
vector<double> theta; | |
for(int i=0; i<7; ++i) | |
{ | |
theta.push_back(((double)(rand()%100))/100.0d); | |
} | |
vector<double> delta(7), tdelta(2); | |
double s; | |
for(int i=0; i<NUM_ITR; ++i) | |
{ | |
/* | |
Temp = X*Theta-Y; | |
Temp2 = X'*Temp; | |
Temp = Theta - alpha*Temp2; | |
Theta = Temp; | |
*/ | |
for(int k=0;k <2;++k) | |
{ | |
s = 0; | |
for(int j=0;j<7;++j) | |
{ | |
s += X[k][j]*theta[j]; | |
} | |
tdelta[k] = s - Y[k]; | |
} | |
if(!(i%500) && printYO) | |
{ | |
cout<<"tdelta is : "<<tdelta[0]<<" "<<tdelta[1]<<endl; | |
} | |
for(int j=0; j<7;++j) | |
{ | |
delta[j] = X[0][j] * tdelta[0] + X[1][j] * tdelta[1]; | |
theta[j] = theta[j] - alpha*delta[j]; | |
}/* | |
if(!(i%500)) | |
{ | |
cout<<"Theta after :"<<i<<"Iterations :\n"; | |
for(int i=0;i<7;++i) | |
cout<<theta[i]<<" "; | |
cout<<endl; | |
}*/ | |
} | |
vector<double> FS; | |
FS.push_back(1); | |
for(int j=0;j<3;++j) | |
{ | |
FS.push_back(curval[2+j]/NORMALISATION); | |
FS.push_back(pow(curval[2+j], 0.05)/NORMALISATION); | |
} | |
s = 0; | |
for(int i=0;i<7;++i) | |
{ | |
s += FS[i] * theta[i]; | |
} | |
return s; | |
} | |
struct profVal | |
{ | |
double prof; | |
int index; | |
profVal(double p, double i) : prof(p), index(i) {} | |
bool operator < (const profVal& oth) const | |
{ | |
return (prof < oth.prof); | |
} | |
}; | |
void printTransactions(double m, int k, int d, vector <string> name, vector <int> owned, vector < vector <double> > prices) { | |
//Enter your code here | |
vector<profVal> profVals; | |
double pricenext, prof, curmaxprof = 0; | |
int profi = 0; | |
vector<string> alltransactions; | |
for(int i=0;i<k;++i) | |
{/* | |
if(i==1) | |
printYO = true;*/ | |
pricenext = nextPred(prices[i]); | |
if(pricenext<prices[i][4] && owned[i]) | |
{ | |
stringstream temp; | |
temp << name[i]; | |
temp << " SELL "; | |
temp << owned[i]; | |
alltransactions.push_back(temp.str()); | |
} | |
//cout<<"next price "<<i<<" : "<<pricenext<<endl; | |
prof = (pricenext - prices[i][4])/prices[i][4]; | |
profVals.push_back(profVal(prof, i)); | |
if(prof>curmaxprof) | |
{ | |
curmaxprof = prof; | |
profi = i; | |
} | |
} | |
if(curmaxprof>0) | |
{ | |
sort(profVals.begin(), profVals.end()); | |
int j = k-1, n, t; | |
while(profVals[j].prof > 0 && j>=0) | |
{ | |
t = profVals[j].index; | |
n = m / prices[t][4]; | |
if(n>0) | |
{ | |
m -= n*prices[t][4]; | |
stringstream temp; | |
temp << name[t]; | |
temp << " BUY "; | |
temp << n; | |
alltransactions.push_back(temp.str()); | |
} | |
--j; | |
} | |
} | |
int sz = alltransactions.size(); | |
cout<<sz<<endl; | |
for(int i=0; i<sz; ++i) | |
cout<<alltransactions[i]<<endl; | |
} | |
int main(void) { | |
//ios_base::sync_with_stdio(false); | |
double _m; | |
cin >> _m; | |
int _k; | |
cin >> _k; | |
int _d; | |
cin >> _d; | |
vector<string> _name; | |
vector<int> _owned; | |
vector < vector <double> > _prices; | |
string _name_item; | |
char name[100]; | |
int _owned_item; | |
double _prices_item_item; | |
for(int _i=0; _i<_k; _i++) { | |
cin >> name; | |
_name_item = name; | |
_name.push_back(_name_item); | |
cin >> _owned_item; | |
_owned.push_back(_owned_item); | |
vector <double> _prices_item; | |
for(int _j = 0; _j<5; _j++) { | |
cin >> _prices_item_item; | |
_prices_item.push_back(_prices_item_item); | |
} | |
_prices.push_back(_prices_item); | |
} | |
printTransactions(_m, _k, _d, _name, _owned, _prices); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment