لم يصل الحل :D , يبدوا أن لدي مشكله في صندوق واردي..
لقد قمت بإرسال لك رساله, قم بالرد عليها و أرسل فيها الحل....... :D
لم يصل الحل :D , يبدوا أن لدي مشكله في صندوق واردي..
لقد قمت بإرسال لك رساله, قم بالرد عليها و أرسل فيها الحل....... :D
هويدي كتب:يبدوا أن لدي مشكله في صندوق واردي..
عدنا إلى نفس المشكلة - ربما المشكلة عمل تخريبي :lol:
=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=
كوني صرت خارج البلد لم أجد الفرصة لحل المسألة
سأبدأ بالبرمجة الآن وبإذن الله سأنهي قبل نهاية المهلة
الهدف طبعاً هو الإفادة لا أكثر
سلامي ..
البرمجة = الرياضيات
@حمزه (فضاء الأسماء)
خذ وقتك يا أخي...... سأنتظر حتى صباح باكر إن شاء الله..
@محمد.. تم الإستلام
جاري التصحيح و مقارنة النتائج....
يبدوا أن الحلان متقاربان جدا ....
بسم الله الرحمن الرحيم...
يسعدني و يشرفني أن أعرض نتائج المسابقه......
و نتائج المسابقة هي:
Start Hamza: OK OK OK OK OK OK OK OK OK OK OK OK OK ERROR: at line 73 function returned: '0' instread of '-1' OK OK OK OK OK OK OK OK OK OK OK WARNNING: at line 113 function returned: 'AB ' instread of 'AB D' OK OK OK Start Mohammed Alla: OK OK OK OK OK OK OK WARNNING: at line 140 function returned: 'NULL' instread of 'rld' OK ERROR: at line 142 function returned: 'NULL' instread of '' OK OK OK OK OK OK OK OK OK OK OK OK OK ERROR: at line 177 function returned: '' instread of 'AB D' OK WARNNING: at line 186 function returned: 'AB DFG' instread of 'AB D' OK OK OK
و يبدوا من النتائج أن الفائز هو ....... حمزه
حمزه: عنده 1 خطأ, و 1 تحذيز
محمد علاء: عنده 2 خطأ و 2 تحذير
و هذا لا يعني أبدأ أن إجابة إحداهما أفضل من إجابة الأخر... بل يعني هذه نتائج ال unit testing الذي قمت بها و التي أرتضيت بها أن تكون هي الحكم بينهما.....
و إن كان محمد علاء أكثر إلتزاما بتوقيعات الدوال من حمزه.....
كود الأخي محمد علاء:
//mohammed_string2.h
#ifndef STRING2_H
#define STRING2_H
/*************************/
/* FOR INTERNAL USE ONLY */
/*************************/
int find_string_ex(const char* str, const char* find, int startIndex, int count);
int remove_string2(char* src, const char* remStr);
int* string_indexes(const char* str, const char* find, int& count);
/************************/
/* FOR PUBLIC USE */
/************************/
void* memcpy2(void* dest,const void* src, int count);
int str_len(const char* str);
char* substring(const char* source, int index, int count);
inline int find_string(const char* str, const char* find)
{ return find_string_ex(str, find, 0, str_len(str)); }
void remove_string(char* src, int index, int count);
void insert_string(char* dest, const char* str, int index);
int replace_string(char* src, const char* s1, const char* s2);
#endif // STRING2_H//mohammed_string2.cc
#include <malloc.h>
#include "mohammed_string2.h"
void* memcpy2(void* dest,const void* src, int count)
{
if (dest == 0 || src == 0 || count == 0)
return 0;
__asm
{
mov edi, dest
mov esi, src
mov ecx, count
rep movsb
}
return dest;
}
int str_len(const char* str)
{
// if null pointer
if (str == 0) return -1;
// get non constant string pointer
char* tmp = const_cast<char*>(str);
// loop to null character
while(*tmp++);
// return number of characters
return tmp - str - 1;
}
char* substring(const char* source, int index, int count)
{
// if null pointer
if (source == 0) return 0;
// get string length
int len = str_len(source);
// if invalid parameters passed
if (index < 0 || index >= len || count <= 0 || index + count > len)
// return null pointer
return 0;
// space for substring
char* tmp = (char*)malloc(count+1); // +1 for null terminator
// set null terminator
tmp[count] = 0;
// return tmp after copy
return (char*)memcpy2(tmp, source + index, count);
}
void remove_string(char* src, int index, int count)
{
// if invalid input, return
if (src == 0 || index < 0 || count <= 0) return;
// number of characters in src
int len = str_len(src);
// if invalid input, return
if (index >= len || count > len || index + count > len)
return;
// if user want to delete all the string
if (index + count == len)
{
// free input string and return
free(src);
return;
}
// do the remove
memcpy2(src + index, src + index + count, len - index - count);
// set null terminator
src[len - count] = 0;
// resize memory to fit
realloc(src, len - count + 1);
}
void insert_string(char* dest, const char* str, int index)
{
// if invalid input, return
if (dest == 0 || str == 0) return;
// dest string size
int dest_size = str_len(dest);
// string to be inserted size
int str_size = str_len(str);
// if invalid input, return
if (index > dest_size) return;
// dest string new size
int new_size = dest_size + str_size;
// resize dest string to fit itself and str
realloc(dest, new_size + 1); // +1 for null terminator
// set new null terminator
dest[new_size] = 0;
// if str will inserted at dest end
if (index == dest_size)
{
// copy str all at once
memcpy2(dest + dest_size, str, str_size);
// we're done
return;
}
// copy tail string to its new location
// from right to left to avoid overlap
for (int idx_old = dest_size - 1, idx_new = new_size - 1;
idx_old >= index; idx_old--, idx_new--)
dest[idx_new] = dest[idx_old];
// insert new string at requested index
memcpy2(dest + index, str, str_size);
}
int find_string_ex(const char* str, const char* find, int startIndex, int count)
{
// if null pointer
if (str == 0 || find == 0 || count == 0) return -1;
// string length
int str_size = str_len(str);
int find_size = str_len(find);
// if invalid input
if (startIndex >= str_size || startIndex + count > str_size)
return -1;
// if find string contains character
// more than requested range
if (find_size > count)
// nothing to search in
return -1;
// string to search from
char* str_front = const_cast<char*>(str) + startIndex;
char* find_front = const_cast<char*>(find);
// temporary store character for search
char tmp1 = 0, tmp2 = 0;
// result index
int result_idx = -1;
// if single character search
if (find_size == 1)
{
// set character case
tmp1 = *find_front;
// loop as long as there is a place to search in
while(count)
{
tmp2 = *str_front++;
// if there is a match
if (tmp1 == tmp2)
{
// set the index
result_idx = str_front - str - 1;
// get out
break;
}
// decrement count
--count;
}
// return the index
return result_idx;
}
// multi-character search
// source string last possible index
int str_last_index = startIndex + count - find_size;
// find string last character index
int find_last_index = find_size - 1;
// last character in source string and find string
char* str_end = str_front + find_last_index;
char* find_end = find_front + find_last_index;
// temporary store character for search
char tmp3 = 0, tmp4 = 0;
// this constant represents number of characters to match
// in find string
const int LOOP_SIZE = find_size - 2;
// loop as long as there is a place to search in
while(startIndex <= str_last_index)
{
// set current character to compare
tmp1 = *str_front;
tmp2 = *find_front;
// get last character from both strings
tmp3 = *str_end;
tmp4 = *find_end;
// if no match
if (tmp1 != tmp2 || tmp3 != tmp4)
{
// update base string
str_front++;
str_end++;
// and the start index
startIndex++;
// jump to next loop
continue;
}
// skip first and last character because
// we know they are match
str_front++;
str_end--;
find_front++;
find_end--;
// set counter to equal to LOOP_SIZE /2
// LOOP_SIZE: is the number of character to search in
// decremented by 2 and they are the first
// and last characters we already match
// /2 : this division used because we'll match 4 characters
// at a time.
// get loop size to save calculation time
int counter = LOOP_SIZE / 2;
// if number of characters is not even number
if (!(find_size & 0))
// then add one to counter to check the remain characters
counter += 1;
// loop as long as the counter is not zero
while (counter)
{
tmp1 = *str_front++;
tmp2 = *find_front++;
tmp3 = *str_end--;
tmp4 = *find_end--;
// if no match
if (tmp1 != tmp2 || tmp3 != tmp4)
break;
else // otherwise
counter--; // decrement the counter
}
// if the counter is 0
if (counter == 0)
{
// we got a match
// store current index in result_idx
result_idx = startIndex;
// and break out the main loop
break;
}
else // otherwise
{
// set the start_index beyond the string that searched
startIndex++;
// reset the pointers
str_front = const_cast<char*>(str) + startIndex;;
find_front = const_cast<char*>(find);
str_end = str_front + find_last_index,
find_end = find_front + find_last_index;
}
}
// return the result
return result_idx;
}
int* string_indexes(const char* str, const char* find, int& resultLen)
{
// reset result count
resultLen = 0;
// if null pointer
if (str == 0 || find == 0) return 0;
// string length
int count = str_len(str);
int find_size = str_len(find);
// if find string contains character
// more than requested range
if (find_size > count)
// nothing to search in
return 0;
// index to stop at
int last_idx = count - 1;
// will hold indexes
int* resIndxs = 0;
// will hold first actual index
int idx = find_string_ex(str, find, 0, count);
// if nothing found
if (idx == -1)
{
// nothing to return
resultLen = 0;
return 0;
}
else
{
// allocate space for max possible indexes
// because we know first possible index and last index to
// stop at, we'll allocate space to minimize memory use and also
// to minimize allocation time
// in next equation
// (1) [last_idx - idx] : will get the number of characters
// will be searched.
// (2) [- find_len] : then we subtract find_len from (1)
// because if you stepped one more
// character the rest number of character
// logically will not equal find_len.
// (3) (+1) : [+1] is added to equation to balance it with
// next code (founded after [Increase:]).
int max_array_size = last_idx - idx - find_size + 2;
// do the allocation
resIndxs = (int*)malloc(max_array_size * sizeof(int));
// if allocation failed
if (resIndxs == 0)
return 0;
// use unconditional jump
goto Increase; // bad programming style, so what??
do
{
// get index of value
idx = find_string_ex(str, find, idx, count);
// if we found a match
if(idx != -1)
{
Increase:
// set index and increase the index counter
resIndxs[resultLen++] = idx;
// increase the index
idx++;
// recalculate the character count
count = last_idx - idx + 1;
// if the count less than zero or
// the index exceed last_index
if (count <= 0 || idx > last_idx)
break;
}
} while (idx != -1); // loop until idx is negative
// resize the indexes array to fit contents
int* tmpArr = (int*)realloc(resIndxs, resultLen * sizeof(int));
// if resizing fail
if (tmpArr == 0)
{
free(resIndxs);
return 0;
}
// copy array actual pointer
resIndxs = tmpArr;
}
// return array
return resIndxs;
}
int remove_string2(char* src, const char* remStr)
{
// if invalid input
if (src == 0 || remStr == 0)
// operation failed
return 0;
// string size
int src_size = str_len(src),
del_size = str_len(remStr);
// get indexes of remStr in src string
int count = 0,
*idxs = string_indexes(src, remStr, count);
// if string_indexes has failed
if (idxs == 0)
// operation failed
return 0;
// if about to remove the entire string
if (src_size == count * del_size)
{
// free local allocated space
free(idxs);
// remove entire string
remove_string(src, 0, src_size);
}
// position to copy at
int internal_pos = idxs[0];
// loop over all the indexes but least
for(int i = 0; i < count - 1; i++)
{
// if next index is directly after current index
if (idxs[i + 1] == idxs + del_size)
continue; // continue loop
// copy string between next index and current index to current position
memcpy2(src + internal_pos, src + idxs + del_size, idxs[i + 1] - idxs - del_size);
// update current position
internal_pos += idxs[i + 1] - idxs - del_size;
}
// copy last string
memcpy2(src + internal_pos, src + idxs[count - 1] + del_size, src_size - idxs[count - 1] - del_size);
// update position
internal_pos += src_size - idxs[count - 1] - del_size;
// set null terminator
src[internal_pos] = 0;
// clean up first to avoid memory leak
free(idxs);
// resize the string to fit
realloc(src, internal_pos + 1);
// well done
return 1;
}
int replace_string(char* src, const char* s1, const char* s2)
{
// if invalid input
if (src == 0 || s1 == 0 || s2 == 0)
// operation failed
return 0;
// input string length
int src_size = str_len(src),
s1_size = str_len(s1),
s2_size = str_len(s2);
// if logically nothing to replace
if (src_size == 0 || s1_size > src_size)
// operation failed
return 0;
// if user want to remove s1 from src
if (s2_size == 0)
// remove all instance of s1 in src
return remove_string2(src, s1);
// get indexes and count of s1 string
int idxs_cnt = 0,
*idxs = string_indexes(src, s1, idxs_cnt);
// if nothing found
if (idxs == 0)
// we're done
return 1;
// there is 3 algorithms i'll use here, one for each condition
//
// i'll refer to s1 as find, s2 as replace
//
// [1] find_len == replace_len : direct copy and replace nothing more.
//
// [2] find_len > replace_len : src size will be shrieked.
//
// [3] find_len < replace_len : will allocate new buffer for new string
// calculate the new size
int new_size = src_size + s2_size * idxs_cnt - s1_size * idxs_cnt;
// case 3
if (s1_size < s2_size)
{
// allocate new buffer
char* tmp = (char*)malloc(new_size + 1); // +1 for null terminate
// if allocation failed
if (tmp == 0)
{
// clear indexes buffer
free(idxs);
// operation failed
return 0;
}
// put the null terminator
tmp[new_size] = 0;
// index in tmp where we'll copy mStr to
int curIdx = 0;
// size of substring to copy
int tmpSize = 0;
// if first index is not at string beginning
if (idxs[0] != 0)
{
// get the size from beginning to first index
tmpSize = idxs[0];
// start copy from beginning until reach first index
memcpy2(tmp, src, tmpSize);
// increment curIdx with copied size
curIdx += tmpSize;
}
// copy the replace string to its new place
memcpy2(tmp + curIdx, s2, s2_size);
// reposition the index
curIdx += s2_size;
// loop over remain indexes and copy to new buffer
for (int i = 1; i < idxs_cnt; i++)
{
// set size of string between previous founded find string and current find string
tmpSize = idxs - idxs[i - 1] - s1_size;
// copy the substring
memcpy2(tmp + curIdx, src + idxs[i - 1] + s1_size, tmpSize);
// update the index
curIdx += tmpSize;
// copy the replace string to its new place
memcpy2(tmp + curIdx, s2, s2_size);
// reposition the index
curIdx += s2_size;
}
// if last index is not the last character in string
if(idxs[idxs_cnt - 1] != src_size - s1_size)
{
// set size of substring between string end and last index
tmpSize = src_size - idxs[idxs_cnt - 1] - s1_size;
// copy string after last founded index
memcpy2(tmp + curIdx, src + idxs[idxs_cnt - 1] + s1_size, tmpSize);
}
// free old string
realloc(src, new_size + 1);
memcpy2(src, tmp, new_size + 1); // copy new string with its null terminator
}
// other cases
else
{
// case 1
if (s1_size == s2_size)
{
// copy replace string to override find string in mStr
for (int i = 0; i < idxs_cnt; i++)
memcpy2(src + idxs, s2, s2_size);
}
// case 2
else
{
// replace mechanism will start from start_index to
// start_index + length, in other word from left to right
// variables will used many times
int Index_1 = 0, Index_2 = 0, Internal_Index = 0, Internal_Size = 0;
// Index_1 : this represent the index of string to replace
// Index_2 : this represent the next index of string to replace
// Internal_Index : is an index inside src to put shifted strings after replace
// Internal_Size : size of string between 2 indexes
Index_1 = idxs[0];
Internal_Index = Index_1;
// we'll loop over all exist indexes but the last one
for (int i = 0; i < idxs_cnt - 1; i++)
{
// 1 - overwrite find string with replace string
memcpy2(src + Internal_Index, s2, s2_size);
// 2 - update internal position
Internal_Index += s2_size;
// 3 - get next index of find string
Index_2 = idxs[i + 1];
// 4 - get size of string between the Index_1 and Index_2
Internal_Size = Index_2 - (Index_1 + s1_size);
// 5 - copy the string between index_1 and index_2
memcpy2(src + Internal_Index, src + Index_1 + s1_size, Internal_Size);
// 6 - update internal position
Internal_Index += Internal_Size;
// 7 - update indexes
Index_1 = Index_2;
}
// 1 - overwrite find string with replace string
memcpy2(src + Internal_Index, s2, s2_size);
// 2 - update internal position
Internal_Index += s2_size;
// 3 - get size of string between the Index_1 and src_size
Internal_Size = src_size - (Index_1 + s1_size);
// 4 - copy the string at end of dest
memcpy2(src + Internal_Index, src + Index_1 + s1_size, Internal_Size);
// put the null terminator
src[new_size] = 0;
// shrink dest pointer size
realloc(src, new_size + 1); // +1 for null terminator
}
}
// free indexes array
free(idxs);
// mission accomplished
return 1;
}كود الأخ حمزه:
#include<iostream>
using namespace std;
// hamza_string.h
//MHEWEDY: I wanted a static fuctions, not instance
class mystring
{
public:
int size;
char* str;
mystring() { }
mystring(char s[])
{
size = str_len(s);
str = new char[size + 1];
for(int i=0; i<=size; i++)
str = s;
}
mystring(mystring& m)
{
size = m.size;
str = new char[m.size + 1];
for(int i=0; i<=size; i++)
str = m.str;
}
void set(char s[])
{
delete str;
size = str_len(s);
str = new char[size + 1];
for(int i = 0; i<=size; i++)
str = s;
}
// calculates the frequency of s in the text
int count(mystring s) // mhewedy: why not private static, I don't mentioned as part of the interface?
{
int c = 0;
int start = - s.size;
while((start = find_string(s, start+s.size)) != size)
c++;
return c;
}
int str_len(char s[]) // mhewedy: why not static?
{
int sz = 0;
while(s[sz] != '\0')
sz++;
return sz;
}
void substring(int index, int length, mystring& dest)
{
dest.size = length;
dest.str = new char[length + 1];
int i;
for(i=index; i<index+length; i++)
dest.str[i-index] = str;
dest.str[i-index] = '\0';
}
// search for s starting from n
int find_string(mystring s, int n=0)
{
int i, k; //mhewedy
for(i=n; i<=size-s.size+n; i++)
{
for(k=i; k<i+s.size; k++) //mhewedy, I removed the declaration of k to be at start of this func
if(s.str[k-i] != str[k])
break;
if(k == i+s.size)
return i;
}
return size;
}
void remove_string(int index, int length)
{
char* cstr = new char[size - length + 1];
for(int i=0; i<index; i++)
cstr = str;
for(int k=index+length; k<size; k++)
cstr[k-length] = str[k];
cstr[size-length] = '\0';
set(cstr);
}
void insert_string(mystring txt, int index)
{
char* cstr = new char[size + txt.size + 1];
for(int i=0; i<index; i++)
cstr = str;
for(int k=index+txt.size; k<=size+txt.size; k++)
cstr[k] = str[k-txt.size];
for(int g=0; g<txt.size; g++)
cstr[g+index] = txt.str[g];;
set(cstr);
}
void replace_string(mystring s1, mystring s2) //mhewedy, should return int, not void
{
int c;
while(find_string(s1) != size)
{
c = find_string(s1);
(*this).remove_string(c, s1.size);
(*this).insert_string(s2, c);
}
}
friend ostream& operator<<(ostream& out, mystring m)
{
out << m.str;
return out;
}
~mystring()
{
delete str;
}
};و هذا كود الإختبار:
//unit_asserts.h #ifndef _UNIT_ASSERTS_H #define _UNIT_ASSERTS_H #define ASSERT_INT_EQUALS(x,y, l) if (x != y)\ cout << ((l==0)?"ERROR:": "WARNNING:")<<" at line "<< __LINE__ << " function returned: '" <<x<< \ "' instread of '"<< y << "'"<<endl; else cout <<"OK"<<endl #define ASSERT_STR_EQUALS(x,y, l) if (compare(x, y) != 0) \ cout << ((l==0)?"ERROR:": "WARNNING:")<<" at line "<< __LINE__ << " function returned: '" << x\ << "' instread of '"<< y << "'"<<endl; else cout <<"OK"<<endl #define ASSERT_STR_EQUALS_2(x,y, l) if (x == NULL || strcmp(x, y) != 0) \ cout << ((l==0)?"ERROR:": "WARNNING:")<<" at line "<< __LINE__ << " function returned: '" <<((x==NULL) ? "NULL" : x)\ << "' instread of '"<< y << "'"<<endl; else cout <<"OK"<<endl #endif
// test.cc
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "namespace/hamza_string.h"
#include "mallaa/mohammed_string2.h"
#include "unit_asserts.h"
using namespace std;
static int compare(mystring s1, mystring s2);
void test_hamza();
void test_mohammed();
int main(void)
{
cout <<"Start Hamza:"<<endl;
test_hamza();
cout <<"\nStart Mohammed Alla:"<<endl;
test_mohammed();
return 0;
}
void test_hamza(void)
{
mystring s = "ABC";
// int str_len(const char* str);
ASSERT_INT_EQUALS (s.str_len("ABC") , 3 , 0);
ASSERT_INT_EQUALS (s.str_len("") , 0 , 0);
ASSERT_INT_EQUALS (s.str_len("1") , 1 , 0);
ASSERT_INT_EQUALS (s.str_len("XXXXXXXXXXXXXXXXXXXX") , 20, 0);
//char* substring(const char* source, int index, int count);
mystring s1 = "hello World";
mystring s1r;
s1.substring(5, 1, s1r);
ASSERT_STR_EQUALS(s1r , " ", 0);
s1.substring(0, 5, s1r);
ASSERT_STR_EQUALS(s1r , "hello", 0);
s1.substring(8, 3, s1r);
ASSERT_STR_EQUALS(s1r , "rld", 0);
s1.substring(8, 4, s1r);
ASSERT_STR_EQUALS(s1r , "rld", 1);
s1.substring(10, 1, s1r);
ASSERT_STR_EQUALS(s1r , "d", 0);
s1.substring(8, 0, s1r);
ASSERT_STR_EQUALS(s1r , "", 0);
////int find_string(const char* str, const char* find);
mystring fs1("HELLO"), fs2 ("HE");
ASSERT_INT_EQUALS(fs1.find_string(fs2) , 0, 0);
mystring fs3("HELL hel World"), fs4("hel");
ASSERT_INT_EQUALS(fs3.find_string(fs4) , 5, 0);
mystring fs5 ("hellhello World"), fs6("hello");
ASSERT_INT_EQUALS(fs5.find_string(fs6) , 4, 0);
mystring fs7("hellhello World"), fs8("");
ASSERT_INT_EQUALS(fs7.find_string(fs8) , -1, 0);
mystring fs9(" hellhello World"), fs10(" ");
ASSERT_INT_EQUALS(fs9.find_string(fs10) , 0, 0);
mystring fs11("hellhello World"), fs12(" ");
ASSERT_INT_EQUALS(fs11.find_string(fs12) , 9, 0);
mystring fs13(" hellhello World"), fs14(" ");
ASSERT_INT_EQUALS(fs13.find_string(fs14) , 0, 0);
//void remove_string(char* src, int index, int count);
//void insert_string(char* dest, const char* str, int index);
mystring rs1 ("ABXXDFG");
rs1.remove_string(2,2);
ASSERT_STR_EQUALS(rs1 , "ABDFG", 0);
rs1.insert_string("XX", 2);
ASSERT_STR_EQUALS(rs1 , "ABXXDFG", 0);
rs1.remove_string(0,1);
ASSERT_STR_EQUALS(rs1 , "BXXDFG", 0);
rs1.insert_string("A", 0);
ASSERT_STR_EQUALS(rs1 , "ABXXDFG", 0);
rs1.remove_string(0,0);
ASSERT_STR_EQUALS(rs1 , "ABXXDFG", 0);
rs1.insert_string("", 0);
ASSERT_STR_EQUALS(rs1 , "ABXXDFG", 0);
mystring rs2("AB DFG");
rs2.remove_string(5,2);
ASSERT_STR_EQUALS(rs2 , "AB D", 0);
rs2.insert_string("FG", 5);
ASSERT_STR_EQUALS(rs2 , "AB DFG", 0);
rs2.remove_string(5,3);
ASSERT_STR_EQUALS(rs2 , "AB D", 1);
mystring rs3("Hello Arabina World");
rs3.remove_string(5,8);
ASSERT_STR_EQUALS(rs3, "Hello World", 0);
rs3.insert_string(" Arabina", 5);
ASSERT_STR_EQUALS(rs3 , "Hello Arabina World", 0);
//void replace_string(mystring s1, mystring s2)
mystring rps1 ("get a free lancer");
rps1.replace_string(" ", "*");
ASSERT_STR_EQUALS(rps1, "get*a*free*lancer", 0);
}
void test_mohammed()
{
// int str_len(const char* str);
ASSERT_INT_EQUALS (str_len("ABC") , 3 , 0);
ASSERT_INT_EQUALS (str_len("") , 0 , 0);
ASSERT_INT_EQUALS (str_len("1") , 1 , 0);
ASSERT_INT_EQUALS (str_len("XXXXXXXXXXXXXXXXXXXX") , 20, 0);
//char* substring(const char* source, int index, int count);
ASSERT_STR_EQUALS_2(substring("hello World", 5, 1) , " ", 0);
ASSERT_STR_EQUALS_2(substring("hello World", 0, 5) , "hello", 0);
ASSERT_STR_EQUALS_2(substring("hello World", 8, 3) , "rld", 0);
ASSERT_STR_EQUALS_2(substring("hello World", 8, 4) , "rld", 1);
ASSERT_STR_EQUALS_2(substring("hello World", 10, 1) , "d", 0);
ASSERT_STR_EQUALS_2(substring("hello World", 8, 0) , "", 0);
//int find_string(const char* str, const char* find);
ASSERT_INT_EQUALS(find_string("HELLO", "HE") , 0, 0);
ASSERT_INT_EQUALS(find_string("HELL hel World", "hel") , 5, 0);
ASSERT_INT_EQUALS(find_string("hellhello World", "hello") , 4, 0);
ASSERT_INT_EQUALS(find_string("hellhello World", "") , -1, 0);
ASSERT_INT_EQUALS(find_string(" hellhello World", " ") , 0, 0);
ASSERT_INT_EQUALS(find_string("hellhello World", " ") , 9, 0);
ASSERT_INT_EQUALS(find_string(" hellhello World", " ") , 0, 0);
//void remove_string(char* src, int index, int count);
//void insert_string(char* dest, const char* str, int index);
char* str = (char*) malloc( 8 );
memcpy2(str, "ABXXDFG", 8);
remove_string(str,2,2);
ASSERT_STR_EQUALS_2(str , "ABDFG", 0);
insert_string(str, "XX", 2);
ASSERT_STR_EQUALS_2(str , "ABXXDFG", 0);
memcpy2(str, "ABXXDFG", 8);
remove_string(str,0,1);
ASSERT_STR_EQUALS_2(str , "BXXDFG", 0);
insert_string(str, "A", 0);
ASSERT_STR_EQUALS_2(str , "ABXXDFG", 0);
memcpy2(str, "ABXXDFG", 8);
remove_string(str,0,0);
ASSERT_STR_EQUALS_2(str , "ABXXDFG", 0);
insert_string(str, "", 0);
ASSERT_STR_EQUALS_2(str , "ABXXDFG", 0);
memcpy2(str, "AB DFG", 8);
remove_string(str,5,2);
ASSERT_STR_EQUALS_2(str , "AB D", 0);
char *sttr = (char*)malloc(8);
memcpy2(sttr, "ABXXD", 8);
insert_string(sttr, "XX", 5);
ASSERT_STR_EQUALS_2(sttr , "ABXXDXX", 0);
memcpy2(str, "AB DFG", 8);
remove_string(str,5,3);
ASSERT_STR_EQUALS_2(str , "AB D", 1);
free(str);
char *str2 = (char*) malloc(20);
memcpy2(str2, "Hello Arabina World", 20);
remove_string(str2,5,8);
ASSERT_STR_EQUALS_2(str2 , "Hello World", 0);
insert_string(str2, " Arabina", 5);
ASSERT_STR_EQUALS_2(str2 , "Hello Arabina World", 0);
free(str2);
//int replace_string(char* src, const char* s1, const char* s2);
char* rps1 = (char*) malloc( 20 );
memcpy2(rps1, "get a free lancer", 20);
replace_string(rps1, " ", "*");
ASSERT_STR_EQUALS(rps1, "get*a*free*lancer", 0);
free(rps1);
}
static int compare(mystring s1, mystring s2)
{
return strcmp(s1.str, s2.str);
}هنيئا لنا جميعا...... الأن يعود الميكروفون (أقصد التحكم :D ) لحمزه ....
إيه رأيكوا يا جماعه في شوية ال macros دي.. ينفع أعمل بيهم cUnit :D
مبروك علينا جميعا التشارك والتنافس في مثل هذه المسابقات.... و بجد جزاه الله خيرا الأخ العزيز "أحمد المتألق" صاحب هذه الفكره الجميله..
السلام عليكم
في البداية كل الشكر لك أخي هويدي
على جهودك معنا في تصحيح اللغز
وأعتذر من الأخ محمد علاء الدين
والله ما كان قصدي أن أفوز باللغز :lol:
يبدو أنك تعبت كثيراً في حله - الله يعطيك العافية
اللغز القادم - قادم إليكم من فلسطين
ستجدون فيه لون جبال نابلس ونسيمها
لأنني ببساطة سأكتبه من هناك
كل الأمنيات لكم بالتوفيق
سلام
البرمجة = الرياضيات
انتباه انتباه
الليلة بإذن الله لغز جديد
اللغز يتكون من مسألتين
الأسئلة سهلة - لكن الإجابات صعبة :P
نظراً لسهولة الأسئلة ، فالوضع الطبيعي أن يشارك الجميع
فكونوا على استعداد
البرمجة = الرياضيات
خطك وحش أوي :P ...
أنا لم أستخدم قلمين للتلوين أبدا في حياتي , و لم أستخدم قلم أبدا بعد 2 ثانوي إلا فيما ندر (ايام الإمتحانات وماشابه ):D
جاري النظر في المسائل ....
تم تعديل هذه المشاركة بواسطة هويدي في 26 أغسطس 2010 في 17:52
تم إرسال حل اللغز الأول
حمزه... لم افهم الجزء الثاني... ممكن توضح أكثر...
المهلة المحددة لحل اللغز تنتهي بنهاية يوم الأربعاء
بالنسبة للفرع الثاني فهو يكاد يكون أسهل من الفرع الأول
قم بتمثيل النقطة بـ struct كالآتي :
struct point
{
float x;
float y;
void set(float X, float Y)
{
x = X;
y = Y;
}
};لا يلزمك إيجاد المسافات بين النقاط إلا عند التأكد من حلك إذا رغبت ، والصيغة كالآتي :

ومنه فاقتران المسافة هو :
float distance(point p1, point p2)
{
return (sqrt((p1.y - p2.y)*(p1.y - p2.y) + (p1.x - p2.x)*(p1.x - p2.x)));
}ستحتاج لتضمين cmath في البداية .
نحن الآن في الاقتران الرئيسي :
const pts = 5; // total number of points float nums[pts*2]; // the numbers you are to give to the program point p[pts]; // the array to be initialized such that high density of points is required
ستكون مهمتك هي تفريغ المصفوفة nums في المصفوفة p بحيث لو جمعنا المسافات بينها سنحصل على أقل نتيجة ممكنة ، نجمع المسافات كما يلي :
float sum = 0;
for(f=0; f<pts; f++)
{
for(g=0; g<pts; g++)
{
sum += distance(p[f], p[g]);
}
}
cout << sum/2 << endl;دون القسمة على 2 سنكون أضفنا المسافة بين النقطتين a و b مرتين ، مرة من a إلى b ، ومرة من b إلى a . على أية حال الرقم المطبوع هو إشارة إلى دقة حلك ليس أكثر ، حاول أن يكون أقل ما يمكن .
مثال :
دعنا نفترض أن محتويات nums كانت ما يلي :
4 ; 6 ; 16 ; 3 ; 12 ; 2 ; 0 ; 10 ; 6 ; 7
إحدى الطرق تعطي الناتج : 57
ربما هناك طريقة أفضل
بالتوفيق للجميع ...
تم تعديل هذه المشاركة بواسطة namespace في 30 أغسطس 2010 في 23:50
البرمجة = الرياضيات
تم إرسال الجزء الأول..
جاري النظر في الجزء الثاني...
dz-php كتب:صعب
المسألة الثانية أسهل من الأولى
فكر كالتالي : حتى يكون مجموع المسافات بينها أقل ما يمكن يجب أن يكون شكلها أقرب ما يمكن إلى ......
الحل لا يتجاوز أسطراً معدودة
تنتهي المهلة بنهاية هذا اليوم إن وصلني حل صحيح أو مع أول حل صحيح يصلني إن لم يصلني اليوم
" والله ولي التوفيق "
البرمجة = الرياضيات
اتمنى ان اشارك معكم
قريب بأذن الله عندما اقدر على فهم اللغز من الاساس
Just a beginner
.Everyone has a dream
.I never thought that I would be the one I am on now
...No Pain No Gain
مشرف قسم السى/سى++ و الاسيمبلى سنة 2015 بأذن الله ...
تعديل: مشرف قسم السى/سى++ من 2012.
Khaled.Alshaya كتب:السلام عليكم و رحمة الله و بركاته,
أخ وجدي, من أين أتيت بهذا الحل العجيب :cool:
أود أن أفهم جوابك أكثر بعد إذنك,
بداية, وضعت حلي بناءً على المعلومة التالية: أي باب, سيمر عليه أشخاص بعدد قواسمه الصحيحة. فمثلاً, العدد 4 سيمر عليه 1, 2, 4 و كما تلاحظ, فإن الباب في الأساس يكون مغلقاً, و فتح بالواحد و أغلق بـ 2 و فتح مرة أخرى بـ 4. إذاً, إذا كان عدد الأشخاص الذي يمرون على باب ما فردياً, فإنه سيكون مفتوحاً, و إذا عددهم زوجياً, سيكون مغلقاً. الأبواب ذات الأعداد الأولية كلها ستكون مغلقة, لأن عدد القواسم زوجي, العدد 1 و العدد الأولي نفسه.
تمام؟
كل ما علينا هو الحصول على قواسم العدد, و بناء على كونها زوجية أو فردية, يمكننا أن نحدد كون الباب مفتوحاً أم لا.الذي أعرفه, أن عدد القواسم لاتوجد له دالة تعوض فيها مباشرة Explicit. و حتى الدوال التي تقبل التعويض المباشر, تشترط حصولك على الأعداد الأولية المكونة و تكراراها المكون للعدد.
كل ما يقوم به حلي, هو أن يمر على قواسم أي عدد بطريقة الـ Sieving. السؤال هو كيف يمكنك القول بأن الـ perfect squares هي الوحيدة فقط التي لديها عدد قواسم فردي؟ ربما هناك شيء لم أفهمه في حلك, و أعتقد أني أغفلت أشياء بسيطة أخرى.
تحياتي أخ وجدي,,,
لكن تم استعمال دوال مسبقة و هذا لا يعني حل
مشكل......فكرة.......حل........وسيلة.......سكريبت
مشاريعي و طموحاتي تبقيني كلي املا
لا اخاف ان أسئل لكي لا احرج عندما اوسئل

مادام اللغز إنتهى .. :lol:
اقتباسفكر كالتالي : حتى يكون مجموع المسافات بينها أقل ما يمكن يجب أن يكون شكلها أقرب ما يمكن إلى ......
الخط المستقيم ؟ أو النقطة ؟
لأن شروط السؤال ليست واضحة جدا ... فيمكن أن أختار 10 نقاط كالتالي (1,1) , (1,1),(1,1) , (1,1),(1,1) , (1,1),(1,1) , (1,1),(1,1) , (1,1)
حيث قلت أن التكرار مسموح ! يمكن أن تكون العشرة نقاط في نفس النقطة !
لو قلت أن التكرار غير مسموح ستكون على شكل خط مستقيم والفرق بين كل نقطة والأخرى 1 في محور x أو محور y , بالتالي المسافة القصوى ستكون 9 ..
يعني إما 9 في حالة خط مستقيم ..
أو صفر في حالة كل النقاط نفس النقطة .. ؟
أم أن هناك شيء غير مفهوم في السؤال ؟
ممكن أي شخص يحط مسألة نتسلى بها قبل أن يتم تصحيح اللغز السابق :)
هذا الموضوع مغلق.
المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية
جارٍ التحقق من المتواجدين…