int main() { int N,M; scanf("%d %d",&N,&M); memset(island,0,sizeof(int)*(N+1)); memset(unionset,0,sizeof(int)*(N+1)); int i=0,b1,b2; for(;i<M;i++){ scanf("%d %d",&b1,&b2); island[b1]++; island[b2]++; if(unionset[b1]==0&&unionset[b2]==0){ unionset[b1] = b1; unionset[b2] = b1; }else if(unionset[b1]!=0&&unionset[b2]!=0){ int end1 = findEnd(b1); int end2 = findEnd(b2); unionset[end2] = end1; }else{ if(unionset[b1]==0) unionset[b1] = b2; else unionset[b2] = b1; } } int odd_count = (island[1]%2 != 0); int ROOT = findEnd(1); for(i=2;i<N+1;i++){ /* exam odd and even */ if(island[i]%2){ odd_count++; if(odd_count > 2) break; } /* exam connected */ int thisRoot = findEnd(i); if(thisRoot != ROOT){ odd_count = -1; break; } } if(odd_count==2 || odd_count==0) printf("Full\n"); else printf("Part\n"); return 0; }
TEST_F( OpenDDLExportTest, writeValueArrayTest ) { OpenDDLExportMock myExporter; bool ok( true ); std::string statement; ok = myExporter.writeValueArrayTester( ddl_nullptr, statement ); EXPECT_FALSE( ok ); EXPECT_TRUE( statement.empty() ); char token [] = "float[ 3 ]\n" "{\n" " {1, 2, 3}\n" "}\n"; size_t len( 0 ); char *end = findEnd( token, len ); DataArrayList *dataArrayList( ddl_nullptr ); Value::ValueType type; char *in = OpenDDLParser::parsePrimitiveDataType( token, end, type, len ); ASSERT_EQ( Value::ddl_float, type ); ASSERT_EQ( 3U, len ); in = OpenDDLParser::parseDataArrayList( in, end, type, &dataArrayList ); ASSERT_FALSE( ddl_nullptr == dataArrayList ); ok = myExporter.writeValueArrayTester( dataArrayList, statement ); EXPECT_TRUE( ok ); EXPECT_EQ( "{ 1, 2, 3 }", statement ); delete dataArrayList; }
bool findEnd(int r, int c) // Recursion State: r, c { if( !valid(r, c) || maze[r][c] == 'X') return false; // invalid position or block position if( maze[r][c] =='E') return true; // we found End // Try the 4 neighbor cells if(findEnd(r, c-1)) return true; // search up if(findEnd(r, c+1)) return true; // search down if(findEnd(r-1, c)) return true; // search left if(findEnd(r+1, c)) return true; // search right // Can't find a way for it! return false; }
int sortText(Text_t *text, int key, int *StringCount, size_t Textlen) { assert(text); if (!text) Nerror = MEMERR; ErrPrint(Nerror); if (key == 1) { int jump = *StringCount; bool swapped = true; while (jump > 1 || swapped) { if (jump > 1) jump /= 1.24733; swapped = false; for (int i = 0; i + jump < *StringCount; ++i) { assert(&text[i] && &text[i + jump]); if (!(&text[i]) && !(&text[i + jump])) Nerror = STRERR; if (mlStrcmp((&text[i + jump])->StringPos, (&text[i])->StringPos) == -1) { if (!(&text[i]) && !(&text[i + jump])) Nerror = STRERR; assert(&text[i] && &text[i + jump]); swapPtr(&text[i], &text[i + jump]); swapped = true; } ErrPrint(Nerror); } } } else if (key == 2) { int jump = *StringCount; bool swapped = true; while (jump > 1 || swapped) { if (jump > 1) jump /= 1.24733; swapped = false; for (int i = 0; i + jump < *StringCount; ++i) { if (!(&text[i]) && !(&text[i + jump])) Nerror = STRERR; if (mlStrcmpEnd(text[i + jump], text[i]) == -1) { assert(&text[i] && &text[i + jump]); if (!(&text[i]) && !(&text[i + jump])) Nerror = STRERR; swapPtr(&text[i], &text[i + jump]); swapped = true; } ErrPrint(Nerror); } } } findEnd(text, StringCount, Textlen, key); return 1; }
vector<int> searchRange(int A[], int n, int target) { vector <int> ans; int begin = findBegin(A, n, target); int end = findEnd(A, n, target); if (begin <= end) { ans.push_back(begin); ans.push_back(end); } else { ans.push_back(-1); ans.push_back(-1); } return ans; }
int readDataFromClient(int pSock, struct shairbuffer *pClientBuffer) { char tReadBuf[MAX_SIZE]; strcpy(tReadBuf, ""); int tRetval = 1; int tEnd = -1; while(tRetval > 0 && tEnd < 0) { // Read from socket until \n\n, \r\n\r\n, or \r\r is found slog(LOG_DEBUG_V, "Waiting To Read...\n"); fflush(stdout); tRetval = read(pSock, tReadBuf, MAX_SIZE); // if new buffer contains the end of request string, only copy partial buffer? tEnd = findEnd(tReadBuf); if(tEnd >= 0) { if(pClientBuffer->marker == 0) { pClientBuffer->marker = tEnd+1; // Marks start of content } slog(SOCKET_LOG_LEVEL, "Found end of http request at: %d\n", tEnd); fflush(stdout); } else { tEnd = MAX_SIZE; slog(SOCKET_LOG_LEVEL, "Read %d of data so far\n%s\n", tRetval, tReadBuf); fflush(stdout); } if(tRetval > 0) { // Copy read data into tReceive; slog(SOCKET_LOG_LEVEL, "Read %d data, using %d of it\n", tRetval, tEnd); addNToShairBuffer(pClientBuffer, tReadBuf, tRetval); slog(LOG_DEBUG_VV, "Finished copying data\n"); } else { slog(LOG_DEBUG, "Error reading data from socket, got: %d bytes", tRetval); return tRetval; } } if(tEnd + 1 != tRetval) { slog(SOCKET_LOG_LEVEL, "Read more data after end of http request. %d instead of %d\n", tRetval, tEnd+1); } slog(SOCKET_LOG_LEVEL, "Finished Reading Data:\n%s\nEndOfData\n", pClientBuffer->data); fflush(stdout); return 0; }
void InputStmt::syntaxErrorOnMissingQuote(char * str) { if (!str) str = getPackedString(); SqlciError (SQLCI_INPUT_MISSING_QUOTE, (ErrorParam *) 0); size_t quote_pos = 0; findEnd(str, quote_pos); // subtract one from quote_pos to convert 1-based offset to 0-based #pragma nowarn(1506) // warning elimination StoreSyntaxError(str, --quote_pos, sqlci_DA); #pragma warn(1506) // warning elimination sqlci_parser_syntax_error_cleanup(NULL,sqlci_env); } // syntaxErrorOnMissingQuote
TEST_F( OpenDDLDefectsTest, Issue20_WrongColorNodeParsing ) { char token[] = "{0.588235, 0.588235, 0.588235}\n"; size_t len( 0 ); char *end = findEnd( token, len ); Value *data( ddl_nullptr ); Reference *refs( ddl_nullptr ); size_t numRefs( 0 ), numValues( 0 ); Value::ValueType type( Value::ddl_none ); char *in = OpenDDLParser::parseDataList( token, end, type, &data, numValues, &refs, numRefs ); ASSERT_FALSE( ddl_nullptr == in ); ASSERT_FALSE( ddl_nullptr == data ); ASSERT_EQ( 3U, numValues ); delete data; delete refs; }
int main(void) { initIRSensor(); initMotor(); initGPIOLineSensor(); initServo(); SetServo(servo,0); stage = 0; //while(1){followLine();} //while(1){followWall();} //SetMotor(leftMotor, 1); SetMotor(rightMotor, 1); while(true){ LineSensorReadArray(gls, line); if (stage==0){ //start state if(line[0]<0.5&&line[1]<0.5&&line[2]<0.5&&line[3]<0.5&&line[4]<0.5&&line[5]<0.5&&line[6]<0.5&&line[7]<0.5) { followWall(); }else{ followLine(); } }else if(stage==1){ //once 90degree turn passed // SetMotor(leftMotor, 1); // SetMotor(rightMotor, -1); SetPin(PIN_F2, 1); followWall(); if (wallPresent()) {followWall();} else { findLine(); } SetPin(PIN_F2, 0); }else if (stage==2){ //once line found again after walled section SetPin(PIN_F1, 1); if((line[0]<0.5&&line[1]<0.5&&line[2]<0.5&&line[3]<0.5&&line[4]<0.5&&line[5]<0.5&&line[6]<0.5&&line[7]<0.5)|| (mostDark())) { //line[0]>0.5&&line[1]>0.5&&line[2]>0.5&&line[3]>0.5&&line[4]>0.5&&line[5]>0.5&&line[6]>0.5&&line[7]>0.5) findEnd(); }else{ followLine(); }; SetPin(PIN_F1, 0); }else{//end of course look for flag findObject(); break; } } }
int sortText(char** text, int key, int *StringCount, size_t Textlen) { assert(text); if (key == 1) { int jump = *StringCount; bool swapped = true; while (jump > 1 || swapped) { if (jump > 1) jump /= 1.24733; swapped = false; for (int i = 0; i + jump < *StringCount; ++i) { assert(&text[i] && &text[i + jump]); if (mlStrcmp(text[i + jump], text[i]) == -1) { swapPtr(&text[i], &text[i + jump]); swapped = true; } } } } else if (key == 2) { int jump = *StringCount; bool swapped = true; while (jump > 1 || swapped) { if (jump > 1) jump /= 1.24733; swapped = false; for (int i = 0; i + jump < *StringCount; ++i) if (mlStrcmpEnd(text[i + jump], text[i]) == -1) { assert(&text[i] && &text[i + jump]); swapPtr(&text[i], &text[i + jump]); swapped = true; } } } findEnd(text, StringCount, Textlen, key); return Ok; }
void splitlist(node* orig, int firstB, node** listA, node** listB) { node* tmp = orig; while(tmp->next && tmp->next->data != firstB) { tmp = tmp->next; } if(!tmp->next) { return; } *listB = tmp->next; findEnd(orig)->next = *listB; *listA = orig; tmp->next = *listA; }
bool JsonEngine::storeMessage(const HistoryItem &item) { if(!m_save_history) return false; QFile file(getAccountDir(item.m_user).filePath(getFileName(item))); bool new_file = !file.exists(); if(!file.open(QIODevice::ReadWrite | QIODevice::Text)) return false; if(new_file) { file.write("[\n"); } else { uint end = findEnd(file); file.resize(end); file.seek(end); file.write(",\n"); } file.write(" {\n \"datetime\": \""); file.write(item.m_time.toString(Qt::ISODate).toLatin1()); file.write("\",\n \"type\": "); file.write(QString::number(item.m_type).toLatin1()); file.write(",\n \"in\": "); file.write(item.m_in ? "true" : "false"); file.write(",\n \"text\": "); file.write(K8JSON::quote(item.m_message).toUtf8()); file.write("\n }\n]"); file.close(); // It will produce something like this: // { // "datetime": "2009-06-20T01:42:22", // "type": 1, // "in": true, // "text": "some cool text" // } return true; }
bool ArmContourFinder::findHand(int n) { unsigned int l = getLabel(n); //First, find ends ends[l] = findEnd(n); if( ends[l].x == -1 and ends[l].y == -1) return false; //Now, get the tip tips[l] = findTip(n); //See if it's far enough away float d1 = ofDistSquared(tips[l].x, tips[l].y, ends[l].x, ends[l].y); float d2 = ofDistSquared(tips[l].x, tips[l].y, ends[l].x, ends[l].y); if( d1 < MIN_HAND_SIZE * MIN_HAND_SIZE and d2 < MIN_HAND_SIZE * MIN_HAND_SIZE ) return false; // Too small! // if( d1 > MAX_HAND_SIZE * MAX_HAND_SIZE and d2 > MAX_HAND_SIZE * MAX_HAND_SIZE ) // return false; // Too big! //Now find the wrists wrists[l] = findWrists(n); if( wrists[l].size() != 2 ) return false; tips[l] = refitTip(n); d1 = ofDistSquared(wrists[l][0].x, wrists[l][0].y, tips[l].x, tips[l].y); d2 = ofDistSquared(wrists[l][1].x, wrists[l][1].y, tips[l].x, tips[l].y); if(d1 <= MIN_HAND_SIZE * MIN_HAND_SIZE or d1 >= MAX_HAND_SIZE * MAX_HAND_SIZE or d2 <= MIN_HAND_SIZE * MIN_HAND_SIZE or d2 >= MAX_HAND_SIZE * MAX_HAND_SIZE) { wrists[l] = findWrists(n); if(wrists[l].size() != 2) return false; } return true; }
int main(){ node h; h.next=NULL; int choice,c,val,i; do{ printf("\n--------MENU-----------\n" "1. Insert\n2. Delete\n3. Exit\nSelect your choice : "); scanf("%d",&choice); switch(choice){ case 1: printf("\nWhere to insert?\n1. Front\n2. End\n3. At an Index\n4. After Key\n5. Before Key\nChoice : "); scanf("%d",&c); switch(c){ case 1: printf("\nEnter a value : "); scanf("%d",&val); insert(&h,val); break; case 2: printf("\nEnter a value : "); scanf("%d",&val); if(h.next!=NULL) insert(findEnd(&h)->next,val); else insert(&h,val); break; case 3: printf("\nEnter a value : "); scanf("%d",&val); printf("\nEnter Index : "); scanf("%d",&i); insert(findIndex(&h,i),val); break; case 4: printf("\nEnter a value : "); scanf("%d",&val); printf("\nEnter key : "); scanf("%d",&i); insert(findKey(&h,i)->next,val); break; case 5: printf("\nEnter a value : "); scanf("%d",&val); printf("\nEnter key : "); scanf("%d",&i); insert(findKey(&h,i),val); break; } display(&h); break; case 2: printf("\nWhere to Delete?\n1. Front\n2. End\n3. A Key\n4. At an Index\nChoice : "); scanf("%d",&c); switch(c){ case 1: del(&h); break; case 2: del(findEnd(&h)); break; case 3: printf("\nEnter key : "); scanf("%d",&i); del(findKey(&h,i)); break; case 4: printf("\nEnter Index : "); scanf("%d",&i); del(findIndex(&h,i)); break; } display(&h); break; } }while(choice!=3); return 0; }
/* ** Run a script. */ static void runScript( int iClient, /* The client number, or 0 for the master */ int taskId, /* The task ID for clients. 0 for master */ char *zScript, /* Text of the script */ char *zFilename /* File from which script was read. */ ){ int lineno = 1; int prevLine = 1; int ii = 0; int iBegin = 0; int n, c, j; int len; int nArg; String sResult; char zCmd[30]; char zError[1000]; char azArg[MX_ARG][100]; memset(&sResult, 0, sizeof(sResult)); stringReset(&sResult); while( (c = zScript[ii])!=0 ){ prevLine = lineno; len = tokenLength(zScript+ii, &lineno); if( isspace(c) || (c=='/' && zScript[ii+1]=='*') ){ ii += len; continue; } if( c!='-' || zScript[ii+1]!='-' || !isalpha(zScript[ii+2]) ){ ii += len; continue; } /* Run any prior SQL before processing the new --command */ if( ii>iBegin ){ char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin); evalSql(&sResult, zSql); sqlite3_free(zSql); iBegin = ii + len; } /* Parse the --command */ if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii); n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd)); for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){ while( n<len-2 && isspace(zScript[ii+2+n]) ){ n++; } if( n>=len-2 ) break; n += extractToken(zScript+ii+2+n, len-2-n, azArg[nArg], sizeof(azArg[nArg])); } for(j=nArg; j<MX_ARG; j++) azArg[j++][0] = 0; /* ** --sleep N ** ** Pause for N milliseconds */ if( strcmp(zCmd, "sleep")==0 ){ sqlite3_sleep(atoi(azArg[0])); }else /* ** --exit N ** ** Exit this process. If N>0 then exit without shutting down ** SQLite. (In other words, simulate a crash.) */ if( strcmp(zCmd, "exit")==0 ){ int rc = atoi(azArg[0]); finishScript(iClient, taskId, 1); if( rc==0 ) sqlite3_close(g.db); exit(rc); }else /* ** --testcase NAME ** ** Begin a new test case. Announce in the log that the test case ** has begun. */ if( strcmp(zCmd, "testcase")==0 ){ if( g.iTrace==1 ) logMessage("%.*s", len - 1, zScript+ii); stringReset(&sResult); }else /* ** --finish ** ** Mark the current task as having finished, even if it is not. ** This can be used in conjunction with --exit to simulate a crash. */ if( strcmp(zCmd, "finish")==0 && iClient>0 ){ finishScript(iClient, taskId, 1); }else /* ** --reset ** ** Reset accumulated results back to an empty string */ if( strcmp(zCmd, "reset")==0 ){ stringReset(&sResult); }else /* ** --match ANSWER... ** ** Check to see if output matches ANSWER. Report an error if not. */ if( strcmp(zCmd, "match")==0 ){ int jj; char *zAns = zScript+ii; for(jj=7; jj<len-1 && isspace(zAns[jj]); jj++){} zAns += jj; if( len-jj-1!=sResult.n || strncmp(sResult.z, zAns, len-jj-1) ){ errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]", prevLine, zFilename, len-jj-1, zAns, sResult.z); } g.nTest++; stringReset(&sResult); }else /* ** --glob ANSWER... ** --notglob ANSWER.... ** ** Check to see if output does or does not match the glob pattern ** ANSWER. */ if( strcmp(zCmd, "glob")==0 || strcmp(zCmd, "notglob")==0 ){ int jj; char *zAns = zScript+ii; char *zCopy; int isGlob = (zCmd[0]=='g'); for(jj=9-3*isGlob; jj<len-1 && isspace(zAns[jj]); jj++){} zAns += jj; zCopy = sqlite3_mprintf("%.*s", len-jj-1, zAns); if( (sqlite3_strglob(zCopy, sResult.z)==0)^isGlob ){ errorMessage("line %d of %s:\nExpected [%s]\n Got [%s]", prevLine, zFilename, zCopy, sResult.z); } sqlite3_free(zCopy); g.nTest++; stringReset(&sResult); }else /* ** --output ** ** Output the result of the previous SQL. */ if( strcmp(zCmd, "output")==0 ){ logMessage("%s", sResult.z); }else /* ** --source FILENAME ** ** Run a subscript from a separate file. */ if( strcmp(zCmd, "source")==0 ){ char *zNewFile, *zNewScript; char *zToDel = 0; zNewFile = azArg[0]; if( zNewFile[0]!='/' ){ int k; for(k=(int)strlen(zFilename)-1; k>=0 && zFilename[k]!='/'; k--){} if( k>0 ){ zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile); } } zNewScript = readFile(zNewFile); if( g.iTrace ) logMessage("begin script [%s]\n", zNewFile); runScript(0, 0, zNewScript, zNewFile); sqlite3_free(zNewScript); if( g.iTrace ) logMessage("end script [%s]\n", zNewFile); sqlite3_free(zToDel); }else /* ** --print MESSAGE.... ** ** Output the remainder of the line to the log file */ if( strcmp(zCmd, "print")==0 ){ int jj; for(jj=7; jj<len && isspace(zScript[ii+jj]); jj++){} logMessage("%.*s", len-jj, zScript+ii+jj); }else /* ** --if EXPR ** ** Skip forward to the next matching --endif or --else if EXPR is false. */ if( strcmp(zCmd, "if")==0 ){ int jj, rc; sqlite3_stmt *pStmt; for(jj=4; jj<len && isspace(zScript[ii+jj]); jj++){} pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj); rc = sqlite3_step(pStmt); if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){ ii += findEndif(zScript+ii+len, 1, &lineno); } sqlite3_finalize(pStmt); }else /* ** --else ** ** This command can only be encountered if currently inside an --if that ** is true. Skip forward to the next matching --endif. */ if( strcmp(zCmd, "else")==0 ){ ii += findEndif(zScript+ii+len, 0, &lineno); }else /* ** --endif ** ** This command can only be encountered if currently inside an --if that ** is true or an --else of a false if. This is a no-op. */ if( strcmp(zCmd, "endif")==0 ){ /* no-op */ }else /* ** --start CLIENT ** ** Start up the given client. */ if( strcmp(zCmd, "start")==0 && iClient==0 ){ int iNewClient = atoi(azArg[0]); if( iNewClient>0 ){ startClient(iNewClient); } }else /* ** --wait CLIENT TIMEOUT ** ** Wait until all tasks complete for the given client. If CLIENT is ** "all" then wait for all clients to complete. Wait no longer than ** TIMEOUT milliseconds (default 10,000) */ if( strcmp(zCmd, "wait")==0 && iClient==0 ){ int iTimeout = nArg>=2 ? atoi(azArg[1]) : 10000; sqlite3_snprintf(sizeof(zError),zError,"line %d of %s\n", prevLine, zFilename); waitForClient(atoi(azArg[0]), iTimeout, zError); }else /* ** --task CLIENT ** <task-content-here> ** --end ** ** Assign work to a client. Start the client if it is not running ** already. */ if( strcmp(zCmd, "task")==0 && iClient==0 ){ int iTarget = atoi(azArg[0]); int iEnd; char *zTask; char *zTName; iEnd = findEnd(zScript+ii+len, &lineno); if( iTarget<0 ){ errorMessage("line %d of %s: bad client number: %d", prevLine, zFilename, iTarget); }else{ zTask = sqlite3_mprintf("%.*s", iEnd, zScript+ii+len); if( nArg>1 ){ zTName = sqlite3_mprintf("%s", azArg[1]); }else{ zTName = sqlite3_mprintf("%s:%d", filenameTail(zFilename), prevLine); } startClient(iTarget); runSql("INSERT INTO task(client,script,name)" " VALUES(%d,'%q',%Q)", iTarget, zTask, zTName); sqlite3_free(zTask); sqlite3_free(zTName); } iEnd += tokenLength(zScript+ii+len+iEnd, &lineno); len += iEnd; iBegin = ii+len; }else /* ** --breakpoint ** ** This command calls "test_breakpoint()" which is a routine provided ** as a convenient place to set a debugger breakpoint. */ if( strcmp(zCmd, "breakpoint")==0 ){ test_breakpoint(); }else /* ** --show-sql-errors BOOLEAN ** ** Turn display of SQL errors on and off. */ if( strcmp(zCmd, "show-sql-errors")==0 ){ g.bIgnoreSqlErrors = nArg>=1 ? !booleanValue(azArg[0]) : 1; }else /* error */{ errorMessage("line %d of %s: unknown command --%s", prevLine, zFilename, zCmd); } ii += len; } if( iBegin<ii ){ char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin); runSql(zSql); sqlite3_free(zSql); } stringFree(&sResult); }