int main(void) { char* a = "abcijfghij klmn"; char* b = "ghij kl"; int an = 15; int bn = 7; findCommon(a, an, b, bn); return 0; }
string longestCommonPrefix(vector<string> &strs) { if(strs.size() == 0) return ""; if(strs.size() == 1) return strs[0]; string res = strs[0]; for(int i = 1; i < strs.size(); ++i) { res = findCommon(res, strs[i]); } return res; }
int main(int argc,char *argv[]){ // Usage(); int num_seq=2; double r_d; unsigned int seed=0; time_t cur_t, cur_lat_t; clock_t start; struct tm *loc_t; int num_divisions=1000; int maxitems_in_seq=10; if (argc > 1){ maxitems_in_seq = atoi(argv[1]); } double r_unit ; RandomGen rng(seed); double *rnd_seq1 = (double *)malloc(maxitems_in_seq*sizeof(double));; double *rnd_seq2 = (double *)malloc(maxitems_in_seq*sizeof(double));; // double rnd_seq1[maxitems_in_seq]; // double rnd_seq2[maxitems_in_seq]; //TODO can be convered to a 2D array instead of two 1D arrays ? for (int j=0; j < num_seq; j++ ) { printf("\n Generationg the Sequence %d\n",j+1); // Seed based on current time on every run cur_t = time(NULL); loc_t = localtime(&cur_t); printf ("\n#########################################\n"); printf("Current Time hh %d MM %d SS %d gmtoff = %ld \n",loc_t->tm_hour,loc_t->tm_min,loc_t->tm_sec,loc_t->tm_gmtoff); seed = (loc_t->tm_hour+1)*(loc_t->tm_min+1)*(loc_t->tm_sec+1)*(loc_t->tm_year); printf("Seed for Sequence %d = %d \n",j+1,seed); printf ("#########################################\n"); rng.randomize_seed(seed); r_d = rng.rand_d(); printf("Sample Rand Number = %lf \n",r_d); // Generate and store random numbers for(int i=0;i<maxitems_in_seq;i++) { // UGLY FIX ME if (j==0) { rnd_seq1[i] = rng.rand_d(); } else { rnd_seq2[i] = rng.rand_d(); } } cur_lat_t = time(NULL); while (cur_lat_t == cur_t){ cur_lat_t = time(NULL); } } /* Commenting out print statement printf("\nSequence 1 \n"); for(int i=0;i<maxitems_in_seq;i++){ printf("%f \n",rnd_seq1[i]); //printf("%1.3f \n",rnd_seq1[i]); } printf("Sequence 2 \n"); for(int i=0;i<maxitems_in_seq;i++){ printf("%f \n",rnd_seq2[i]); } */ printf("\n Sorting the Sequence 1 \n"); qsort (rnd_seq1, maxitems_in_seq, sizeof(double), compare4qsort); printf("\n Sorting the Sequence 2 \n"); qsort (rnd_seq2, maxitems_in_seq, sizeof(double), compare4qsort); /* Commenting out print statement printf("\nSequence 1 (Sorted)\n"); for(int i=0;i<maxitems_in_seq;i++){ printf("%f \n",rnd_seq1[i]); //printf("%1.3f \n",rnd_seq1[i]); } printf("Sequence 2 (Sorted)\n"); for(int i=0;i<maxitems_in_seq;i++){ printf("%f \n",rnd_seq2[i]); } */ //http://www.leetcode.com/2010/03/here-is-phone-screening-question-from.html printf("\nComparing the two sequences....\n"); findCommon(rnd_seq1,rnd_seq2,maxitems_in_seq); return 0; }
int main(int argc, char *argv[]) { #define stepSize 10000 #define extraBases 1000 static struct noiseTrack noiseTrack[stepSize]; int chromIx; int chromSize; int baseOff; char *chromName; int dnaStart, dnaEnd; char *outName; FILE *out; struct hash *dupeHash; if (argc != 2) { errAbort("editbase - lists bases for which there is evidence of RNA editing\n" "usage:\n" " editbase outfile.txt"); } dnaUtilOpen(); initVlookup(); outName = argv[1]; out = mustOpen(outName, "w"); printf("Scanning for cDNAs that align more than once.\n"); dupeHash = buildMultiAlignHash(); printf("Loading worm genome\n"); wormLoadNt4Genome(&chrom, &chromCount); wormChromNames(&chromNames, &chromCount); for (chromIx = 0; chromIx < chromCount; ++chromIx) { chromName = chromNames[chromIx]; printf("Processing chromosome %s\n", chromName); chromSize = wormChromSize(chromName); for (baseOff = 0; baseOff < chromSize; baseOff += stepSize) { struct wormFeature *cdnaNamesList, *name; struct cdnaAli *caList = NULL, *ca; int dnaSize; DNA *dna; int chunkSize; DNA *chunk; int i; /* Figure out how much DNA to get and get it. Include some * extra around chunk so can align better. */ chunkSize = chromSize - baseOff; if (chunkSize > stepSize) chunkSize = stepSize; dnaStart = baseOff - extraBases; dnaEnd = baseOff + stepSize + extraBases; wormClipRangeToChrom(chromName, &dnaStart, &dnaEnd); dnaSize = dnaEnd - dnaStart; dna = wormChromPart(chromName, dnaStart, dnaSize); /* Get the cDNAs */ cdnaNamesList = wormCdnasInRange(chromName, baseOff, baseOff + chunkSize); for (name = cdnaNamesList; name != NULL; name = name->next) { if (!hashLookup(dupeHash, name->name) ) { ca = makeCdnaAli(name->name, dna, dnaSize); slAddHead(&caList, ca); } } slReverse(&caList); /* Add cdnas to noise track. */ chunk = dna + baseOff - dnaStart; for (ca = caList; ca != NULL; ca = ca->next) { addNoiseTrack(noiseTrack, chunk, chunkSize, ca); } /* Step through base by base evaluating noise and reporting it if * it's interesting. */ for (i=0; i<chunkSize; ++i) { struct noiseTrack *nt = &noiseTrack[i]; struct noise *noise = nt->noise; int noiseCount = slCount(noise); if (noiseCount > 1) { char commonVal; int commonCount; findCommon(noise, &commonVal, &commonCount); if (commonCount*2 > noiseCount && commonVal != 'n') { double ratio = (double)commonCount/noiseCount; double score; ratio = ratio * ratio * ratio; score = ratio * commonCount; if (score >= 4.0) { fprintf(stdout, "%f %s:%d %c->%c in %d out of %d out of %d %s\n", ratio*commonCount, chromName, i+baseOff+1, chunk[i], commonVal, commonCount, noiseCount, nt->cdnaCount, nt->noise->ca->cdna->srn->name); fprintf(out, "%f %s:%d %c->%c in %d out of %d out of %d %s\n", ratio*ratio*commonCount, chromName, i+baseOff+1, chunk[i], commonVal, commonCount, noiseCount, nt->cdnaCount, nt->noise->ca->cdna->srn->name); } } } } freeCdnaAliList(&caList); slFreeList(&cdnaNamesList); freez(&dna); recycleNoiseTrack(noiseTrack, chunkSize); printf("%s %d maxNoise %d\n", chromName, baseOff, slCount(freeNoiseList)); } } return 0; }