Tuesday, October 15, 2013

Building a data store API with PHP (2)

Next plan,

1. To reduce the speed of the query, we will add Family concept.

Each query just works on all objects of same Family.


2. We will add the User, Session, Role and Security concept.



Wednesday, October 9, 2013

Building a data store API with PHP (1)

What I want to do is try to use PHP to build a web application so that you can create JSON objects and stored in a PHP server.

The design is try to make it simple and generic.

These are some of the API functions.

1. New(json) --> You can create a completely new Object and store it into the server. A new Id supposed to be returned.

2. Wash(id, json) --> Empty fields, attributes and relations in an object.

3. Add(id, json) --> Just add new attributes and relations without delete the existing old data.

4. Mine(id, json) --> Just replace all attributes and relations with my data.

5. Your(id, json) --> Just add new attributes and relations if it does not exists.

6. Kill(id) --> Completed remove an object.

7. Query(json) --> Get a list of ids with given condition. 

8. Obtain(id[]) --> get a array of records based on the list of ids.


Thursday, May 17, 2012

SRM 156 DIV 2 250


import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class DiskSpace
{
public int minDrives(int[] used, int[] total)
{
int n= used.length;
int t=0;

ArrayList<Integer> l=new ArrayList();
for(int i=0;i<n;i++)
l.add(total[i]);
Collections.sort(l);

for(int i=0;i<n;i++)
{
t+=used[i];
}
int c=0;

 System.out.println(t);

for(int i=0;i<n;i++)
 System.out.println("-"+l.get(n-i-1));
for(int i=n-1;i>=0;i--,c++)
{
t-=l.get(i);
if(t<=0)break;
}
return c+1;
}

<%:testing-code%>
}
//Powered by [KawigiEdit] 2.0!

SRM 154 DIV-2 450


import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class SuperRot
{
    public String cd="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
    public String dc="NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm5678901234 ";
public String decoder(String message)
{
String r="";
for(int i=0;i<message.length();i++)
{
r+=dc.charAt(cd.indexOf(message.charAt(i)));
}
return r;

}

<%:testing-code%>
}
//Powered by [KawigiEdit] 2.0!

SRM 153 DIV1 - 250


import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class Inventory
{
public int monthlyOrder(int[] sales, int[] daysAvailable)
{
double p=0;
double d=0;
double s=0;
double c=0;
for(int i=0;i<sales.length;i++)
{
if(sales[i]>0)
{
p=sales[i];
d=daysAvailable[i];
s+=(p*30/d);
c+=1.0f;
System.out.println((p*30/d));
System.out.println(s);
}
}
System.out.println(s/c);
System.out.println(Math.floor((s/c)*10e9)/10e9);
return (int)Math.ceil((Math.floor((s/c)*10e9))/10e9);
}


}
//Powered by [KawigiEdit] 2.0!

SRM 152 DIV1 - 500


import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;


public class QuiningTopCoder
{
public class Stk
{
public long st[]=null;
public int si=-1;
public boolean overflow=false;

public Stk(int lm)
{
st=new long[lm];
}
public void push(long s)
{
st[++si]=s;
if(s<-1000000000l||s>1000000000l)overflow=true;
}
public long pop()
{
long s=0;
if(si>=0)s=st[si];
si--;if(si<0)si=-1;
return s;
}
public void prt()
{
 if(si<0)System.out.println("Empty");
 for(int i=0;i<=si;i++)
 {
if(i>0)System.out.print(",");
System.out.print(st[i]);
 }
 System.out.println("");
}
}

public String testCode(String source)
{
int ip=0;
long d=1;
long dd=1;
int n=source.length();
int c=0;
int lm=80000;
Stk st=new Stk(lm*2+1);
String pp=new String();


while(c<lm)
{
char a=source.charAt(ip);
dd=1;
if(a>='0'&&a<='9')
{
st.push((long)(a-'0'));
}
else if(a=='$')
{
st.pop();
}
else if(a==':')
{
long s=st.pop();
st.push(s);
st.push(s);
}
else if(a=='W')
{
long s1=st.pop();
long s2=st.pop();
st.push(s1);
st.push(s2);
}
else if(a==',')
{
long s=st.pop();
pp+=source.charAt((int)(Math.abs(s)%n));
}
else if(a=='+')
{
long s1=st.pop();
long s2=st.pop();
st.push(s1+s2);
}
else if(a=='-')
{
long s1=st.pop();
long s2=st.pop();
st.push(s1-s2);
}
else if(a=='#')
{
dd=2;
}
else if(a=='R')
{
d*=-1;
}
else if(a=='S')
{
long s1=st.pop();
st.push(s1>0?1:-1);
}
else if(a=='_')
{
long s1=st.pop();
d=s1%n;
}
else if(a=='J')
{
long s1=st.pop();
ip=(int)(Math.abs(s1)%n);
}
else if(a=='@')
break;
//if(c>0)
//{
//  System.out.println("c="+c+",a="+a+",pp="+pp);
//  st.prt();
//}
if(st.overflow)return "OVERFLOW "+c;
if(pp.length()==n)if(pp.equals(source))return "QUINES "+c;
if(source.indexOf(pp)!=0)return "MISMATCH "+c;


c++;
if(a!='J')
ip=(int)((ip+d*dd+3*n)%n);
}
if(c>=lm)return "TIMEOUT";
System.out.println("pp:"+pp);
return "BADEND "+c;
}


}
//Powered by [KawigiEdit] 2.0!

Tuesday, May 15, 2012

SRM 150 DIV1 - 250


import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class InterestingDigits
{
    private int newsum(int n,int b)
    {
    int s=0;
    while(n!=0)
    {
    s+=n%b;
    n=n/b;
    }
    return s;
    }
 
public int[] digits(int base)
{
int m=1;
int r[]=new int[base];
int ri=0;
for(int i=0;i<4;i++)
m*=base;

for(int p=2;p<base;p++)
{
boolean pass=true;
for(int i=0;i<m;i+=p)
{
if((newsum(i,base)%p)!=0)pass=false;
}
if(pass)r[ri++]=p;
}

int r2[]=new int[ri];
for(int i=0;i<ri;i++)
r2[i]=r[i];
return r2;
}

}