/log

life.log()

Archive for the ‘Uncategorized’ Category

Using yield while generating the sum of first 1000 prime numbers

with 2 comments

I like using yield here and I was happy when I knew that scala has yield

def oddNumbers(xlist):
	for item in xlist:
		item += 2
		xlist.append(item)
		yield item

oddNumbers 		= oddNumbers([7])
primeNumbers	= [2, 3, 5, 7]
count			= 3
summation		= 17

for item in oddNumbers:
	isPrime = True
	for otherItem in primeNumbers:
		if item % otherItem == 0 and item != otherItem:
			isPrime = False
	if isPrime:
		primeNumbers.append(item)
		count += 1
		summation += item
	if count == 999:
		break

print summation

Written by me

September 10th, 2011 at 11:20 pm

شهر رمضان..

with 2 comments

Written by me

August 13th, 2011 at 10:43 am

Posted in Uncategorized

٣سنين ..

with 7 comments

٣ سنين عدت .. يا ترى إتنسيت ..!!
طيب لو إتنسيت .. هوا ممكن أنا أنسى .. ؟؟

Written by me

August 22nd, 2010 at 2:33 pm

Posted in Uncategorized

Tagged with ,

Singleton mySQL database connection using PHP

with 9 comments

السلام عليكم و رحمة الله و بركاته
فيما يلي مثال على الـ Singleton Design Pattern
و هو ما يعطيك الإمكانية في الحصول علي فقط نسخة واحدة من أي مورد تريد إستخدامه .. في هذة الحالة مثلا الإتصال بقاعدة البيانات و ذلك بأن تستخدم الدالة getConnection بدلا من عمل نسخة جديدة من connection كل مرة هتحاول فيها الإتصال بقاعدة البيانات..

Following is a brief example for the singleton design patterns which will enable you from having only one instance of the resource you are controlling .. in this example it’s the mySQL database connection.
Else you will have multiple instances of the same class “connection” each time you want to connect.

define('DB_NAME', "test");
define('DB_USER', "root");
define('DB_PASS', "rootpass");
define('DB_HOST', "localhost");

class Connection{
	private static $connection_;

	private function __construct(){
		$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);
		mysql_select_db(DB_NAME, $con);
		Connection::$connection_ =  $con;
	}

	public static function getConnection(){
		if( !Connection::$connection_ )
			new Connection;
		return Connection::$connection_;
	}

}

$con = Connection::getConnection();
و نسألكم الدعاء بظهر الغيب بالله عليكم :)
في رعاية الله و أمنه

Written by me

June 29th, 2010 at 11:57 am

testing syntax highlighting

without comments

testing ..

echo "hello, highlighting .. ";

Written by me

February 2nd, 2010 at 2:31 pm

Posted in Uncategorized

Shutting my babies down ..

with 2 comments

Shutting my babies down .. and now ..
They started ..

Written by me

January 13th, 2010 at 1:50 am

Posted in Uncategorized

Tagged with , , ,

Get Adobe Flash playerPlugin by wpburn.com wordpress themes