Public Service Announcement

Originally posted on Programming Praxis:

Long-time readers of this blog will remember that five years ago I suffered a bi-lateral pulmonary embolism that nearly killed me; my right lung was 100% blocked, my left lung 60%. This past Tuesday evening I suffered a second pulmonary embolism. It was not nearly as serious as the first, I even went to work as normal on Wednesday, but with growing pain during the day I went to the hospital on Wednesday evening, was diagnosed, received medication to break up the clots ? two shots in the belly, twelve hours apart, no fun I assure you ? and came home Thursday afternoon.

Broadly speaking, there are two contributing factors to pulmonary embolism. The primary factor is blood chemistry, and that’s genetic; there’s nothing you can do about it, though if you know you are predisposed to blood clots, as I am, there is medication that can attenuate the risk…

View original 141 more words

Find The Missing Number

Originally posted on Programming Praxis:

Today’s exercise is a tricky little homework problem:

Given a string consisting only of digits, find the missing number. For instance, given the string 596597598600601602 the missing number is 599. You may assume all the numbers are positive integers and the sequence increases by one at each number except the missing number. The numbers will have no more than five digits and the string will have no more than two hundred characters.

Your task is to write a program to find the missing number. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

This is harder than it looks. One problem is that the length of the numbers may increase in the middle of the string; for instance, the string 99989999… begins with four-digit numbers but the next number has five…

View original 278 more words

الزخارف في بايثون

طبعاً الترجمة الحرفية هي زخرفة لكن المقصود هنا هي إضافة تسبق الدالة function أو الصنف class لكي تضيف إلى عمله أثناء التشغيل إضافة جديدة تتكامل مع العمل الأصلي للدالة أو الصنف و هي في ذلك تشبه الماكرو في بعض اللغات الأخرى و إلى حد ما تشبه annotations في جافا و سي شارب.

كمثال كيف يمكننا عمل الآتي, لاحظ أن الدالة الأصلية تعيد كلمة “Hello” بدون وسوم, و بإضافة الزخارف أعلى الدالة أضيفت الوسوم للكلمة المرتجعة من الدالة.

 
@makebold
@makeitalic
def say():
   return "Hello"

ناتج الدالة: هو طباعة <b><i>Hello</i></b>

الزخارف @makebold و @makeitalic ماهي إلا دوال معرفة مسبقا:

def makebold(fn):
    def wrapped():
        return "&lt;b&gt;" + fn() + "&lt;/b&gt;"
    return wrapped

def makeitalic(fn):
    def wrapped():
        return "&lt;i&gt;" + fn() + "&lt;/i&gt;"
    return wrapped

@makebold
@makeitalic
def hello():
    return "hello world"

print hello() ## returns &lt;b&gt;&lt;i&gt;hello world&lt;/i&gt;&lt;/b&gt;</pre>
للإستزادة يمكنك قراْة هذه المقالة: