Archive for the ‘I code’ Category
Chm server
It’s not so fancy but I love it.
For Mac OSX Install chmlib:
sudo ports install chmlib
Now you’ll find the chm_http available and you are ready to serve the documentation.
chm_http –port=8080 –bind=127.0.0.1 file.chm
aaand now browse http://127.0.0.1:8080 and you should find the file de-compiled and served for you
Enjoy.
Using yield while generating the sum of first 1000 prime numbers
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
biggest prime palindrome under 1000.
I had that problem in CodeEval:
Write a program to determine the biggest prime palindrome under 1000.
And my solution passed i don’t know if they have other levels than pass but it passed anyway..
Here you are:
oddNumbers = [2] + range(3, 1000, 2) primeNumbers = [] ppNumbers =[] for item in oddNumbers: isPrime = True for otherItem in oddNumbers: if item % otherItem == 0 and item != otherItem: isPrime = False if isPrime: primeNumbers.append(item) for prime in primeNumbers: if prime > 100 and str(prime)[0] == str(prime)[2]: ppNumbers.append(prime) print ppNumbers[-1]
I first created script to get prime numbers then i search to know what exactly a palindrome number is and i tweaked the script to solve it:)
The personal part of the post:
I -lately- figured out that family support is not only important .. it’s incomparable to any other support..
سلام
Singleton mySQL database connection using PHP
فيما يلي مثال على الـ 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();
في رعاية الله و أمنه
yield statement .. by example
ده تمرين بسيط علىyield statement
def simpleGenerator( anyList ): for item in anyList: yield item myList = ['Ahmed', 'AlSayed', 'AbdulHameed', 'Osama', 'Gamal'] nameGenerator = simpleGenerator( myList ) for name in myItemsList: print name print myItemsList.next() print myItemsList.next() print myItemsList.next()
Primes Problem in Python
Lately I decided to give SPOJ website a try ..
And my first problem after the TEST problem was the “PRIME1” problem..
Which is basically finding the prime numbers between 2 given numbers ..
Yesterday, regardless the fact of being truly ill and I hardly could open my eyes ..
I decided to solve the problem ..
and after I think 1 hour It started to output relatively right solution ..
here is my code in python:
times = input()
while times > 0:
times -= 1
limits = raw_input()
limits = limits.split(" ")
downLimit = int(limits[0])
upLimit = int(limits[1])
numsList = []
numsList = [i for i in range(downLimit,upLimit) if (i%2 or i==2) and i != 1 and i!=0]
primesList = list(numsList)
for oddNumber in numsList:
primeNumber = True;
for number in range( 2 , oddNumber / 2):
if oddNumber % number == 0:
primeNumber = False;
break;
if not primeNumber:
primesList.remove( oddNumber )
for primeNumber in primesList:
print primeNumber
Notes:
1-in line 9, I used list() to assign the numsList value to the primesList list .. not the numsList as python-strangely- assigns the old list reference to the new list and that caused +1 hour of debug .. :@
2-As shown in page title after submitting the problem they replied that solution exceed the time limit .. so any help will be appreciated ..
3-Python version 2.6.2 on Mac