نفس الكود، ولكن 7 مستويات! و7 طرق للتفكير🧠
Summary
TLDRThe video script details a journey through seven levels of coding to solve a specific problem: identifying even numbers within a given range and returning them as a list or array. The first level uses a simple loop to iterate through numbers, checking for evenness by the modulus operator. Subsequent levels introduce various optimizations, such as binary system approximation, list comprehension, and filtering with the 'filter' function. The fifth level explores recursion as a method to determine even numbers. The sixth level leverages numpy arrays and filtering for efficiency. The seventh and final level simplifies the process by using a range function with a step of two, adjusting for odd starting numbers. Each level is tested for performance, with the seventh level demonstrating the most significant efficiency with a time complexity vastly improved over the initial approach. The script emphasizes the importance of thinking critically about solutions rather than relying solely on complex code or advanced techniques.
Takeaways
- 📈 The video demonstrates seven levels of coding to solve the problem of finding even numbers within a specified range.
- 🔍 The first level uses a simple loop to iterate through numbers, checking for evenness by dividing by two and using the modulus operation.
- ⏱️ Performance measurements show that the initial code takes 3.22 seconds to execute, which is considered slow for the task.
- 🧮 In the second level, binary system concepts are applied to check for even numbers by looking at the rightmost bit, which is faster than the first method.
- 🚀 The third level optimizes the code by replacing the loop with list comprehension, reducing the execution time to 2.48 seconds.
- 🔬 The fourth level introduces the use of Python's built-in `filter` function, which significantly improves performance, taking only 1.59 microseconds.
- 🛠 The fifth level explores recursion as a method to determine even numbers, although it is not efficient for this specific problem, taking over two hours to run.
- 📚 The sixth level uses numpy arrays and the `numpy.arange` function with a filter to find even numbers, achieving a performance of 366 milliseconds.
- 💡 The seventh and final level simplifies the process by using a range function with a step of two, bypassing the need for checking each number individually, and achieves an execution time of 38 milliseconds.
- 🤔 The video emphasizes the importance of thinking through solutions rather than just writing code, as more advanced methods are not always the most effective.
- 🌟 Using built-in functions and libraries, such as numpy, can lead to more efficient and cleaner code, as demonstrated by the significant performance improvements in the higher levels.
Q & A
What is the primary goal of the code discussed in the script?
-The primary goal of the code is to search for even numbers within a specified range and return a list or array of these numbers.
How does the first level of thinking approach the problem?
-The first level of thinking approaches the problem by iterating through numbers in a given range, checking if each number is even by dividing it by two and checking the remainder. If the remainder is zero, the number is added to a list.
What is the significance of using binary system approximation in the second level of thinking?
-The significance of using binary system approximation is that it allows for a more efficient check of even numbers by observing the rightmost bit of a binary number. Even numbers always end with a zero bit in binary representation.
How does the third level of thinking improve the performance of the code?
-The third level of thinking improves the performance by replacing the loop in the function with a list comprehension, which is a more efficient way to create lists in Python.
What is the main advantage of using the filter function in the fourth level of thinking?
-The main advantage of using the filter function is that it allows for a concise and efficient way to filter out even numbers from a range without the need for an explicit loop or conditional statements.
Why is the recursive approach in the fifth level of thinking not suitable for this problem?
-The recursive approach is not suitable for this problem because it involves repeatedly subtracting two from a number until it reaches zero, which does not scale well for large ranges and can lead to a high time complexity.
What is the key concept used in the sixth level of thinking to optimize the code?
-The key concept used in the sixth level of thinking is the use of NumPy arrays and the NumPy library's built-in functions, which are highly optimized for performance and can handle large datasets efficiently.
How does the seventh level of thinking simplify the problem and improve the efficiency of the code?
-The seventh level of thinking simplifies the problem by recognizing that even numbers can be generated in a sequence by adding two to the previous even number. This allows the use of a simple range function with a step of two, avoiding the need for complex checks or loops.
What is the importance of considering the starting number's parity when implementing the range in the seventh level of thinking?
-Considering the starting number's parity is important because if the starting number is odd, the range would initially generate odd numbers. By adding one to an odd starting number, it becomes even, and the range can then correctly generate even numbers in steps of two.
Why is the performance of the code in the seventh level of thinking significantly better than the previous levels?
-The performance is significantly better because the approach is highly optimized and takes advantage of Python's built-in range function with a step of two, which is a very efficient way to generate a sequence of even numbers without the need for additional checks or operations.
What is the main takeaway from the script regarding the relationship between code complexity and problem-solving efficiency?
-The main takeaway is that the most efficient solutions are not always the most complex ones. Simplifying the problem and using the right approach or tools can lead to more effective and faster solutions, emphasizing the importance of thinking and problem-solving skills in programming.
How does the script demonstrate the importance of understanding different levels of thinking in programming?
-The script demonstrates this by progressively showing how different approaches, from simple loops to advanced techniques like list comprehensions, filter functions, and NumPy arrays, can be used to solve the same problem with varying levels of efficiency, highlighting the need for a deep understanding of programming concepts.
Outlines
😀 Introduction to Binary Search and List Creation
The first paragraph introduces a simple coding task to search for even numbers within a specified range. The task involves writing code that returns a list or logical array to identify whether numbers are even or odd by dividing them by two and checking the remainder. The process is demonstrated through the creation of a function that takes two variables, 'start' and 'end', initializes an empty list, and iterates through numbers within the range to add even numbers to the list. The performance of the code is tested with a range from 99 to 10, and the execution time is noted.
🚀 Optimizing Code with Binary System and List Comprehensions
The second paragraph delves into optimizing the initial code by using different approaches. It starts with a binary system approach to represent numbers and utilizes a binary comparison operator to determine even numbers. The paragraph then explores the use of list comprehensions, which are favored by Python programmers for their efficiency. By replacing the loop in the function with a list comprehension, the code is significantly shortened and the performance is improved. The paragraph also discusses the importance of thinking differently and creatively in programming, and presents a recursive approach as a powerful method used by professionals to solve various problems.
Mindmap
Keywords
💡Binary System
💡List Comprehension
💡Filter Function
💡Recursion
💡NumPy
💡Range Function
💡Performance
💡Even Numbers
💡Odd Numbers
💡Modulus Operator
💡Thinking Levels
Highlights
The code aims to find even numbers within a specified range and return a list or array of the results.
The code is divided into seven levels, each using a different approach to solve the problem.
Level 1 uses a simple loop to iterate through numbers, checking if they are even by taking the modulus.
Adding a timeit decorator above the code execution allows measuring its performance.
Level 2 uses a binary system approach to check if numbers end with a zero, indicating they are even.
Level 3 optimizes the code by replacing the loop with list comprehension, reducing lines of code.
Level 4 introduces the use of the filter function to streamline the process of finding even numbers.
Using generators instead of lists in Level 4 improves performance by reducing memory usage.
Level 5 explores the use of recursion to determine if a number is even by repeatedly subtracting two.
However, the recursive approach in Level 5 is found to be inefficient, taking over two hours to execute.
Level 6 utilizes numpy arrays and the numpy.arange function for faster performance.
The numpy approach in Level 6 is over nine times faster than the initial code in Level 1.
Level 7 simplifies the process by using a range function with a step of two, generating an infinite sequence of even numbers.
A simple check is added to ensure the starting number of the range is even.
The final code in Level 7 is the most efficient, taking only 38 milliseconds to execute, 84 times faster than Level 1.
The video demonstrates the power of numpy and the importance of thinking through the most efficient approach.
Advanced methods are not always the most effective in solving problems, as shown by the comparison of different levels.
The video concludes by emphasizing that the strongest programming skills lie in directed thinking towards solutions, not just in writing or memorizing code.
Transcripts
نفس الكود ولكن سبع مستويات وسبع طرق
للتفكير المستوى الاول حسنا سنعمل مع كود
بسيط جدا حتى يفهم الجميع المساله هي
كالتالي اكتب كودا يقوم بالبحث عن الاعداد
الزوجيه ضمن نطاق او رنج محدد النتيجه يجب
ان تكون قائمه ليست او مصفوفه منطقيا
لمعرفه عدد ما اذا كان زوجيا ام لا فكل ما
عليك القيام به هو قسمته على اثنان واذا
كان باقي القسمه صفر فهو عدد زوجي هذه
العمليه يطلق عليها الموديل نعود الى
المساله الخاصه بنا سنقوم بانشاء داله
تاخذ متغيرين ستارت
واند ننشئ قائمه
فارغه ثم ننشئ هذه الحلقه التي لا هدف لها
في الحياه سوى الدوران على الاعداد
الموجوده في النطاق بين اند وستارت تحاول
قسمه كل عدد على اثنين اذا كان باقي
القسمه صفر تقوم باضافته الى القائمه ايفن
نمبرز اخيرا الداله تعيد تلك القائمه هذا
كان كل شيء دعنا نجرب النطاق من 99 الى 10
ملايين يؤدي الغرض المطلوب لكن دعنا نقيس
اداء هذا الكود عبر اضافه تايمت اعلاه
وتنفيذه ننتظر قليلا
النتيجه هي ان هذا الكود يستغرق
3.22 ثانيه للتنفيذ في المستويات القادمه
لن اتعمق كثيرا في الشرح لكن سنرى طرقا
مختلفه للتفكير المستوى الثاني دعنا
نستخدم مقاربه مختلفه في النظام الثنائي
باينري سيستم تبدو الارقام بالشكل التالي
كما تلاحظ فان كل الارقام الزوجيه تنتهي
بصفر على اليمين اذا بدل استخدام باقي
القسمه سنستخدم معامل المقارنه الثنائيه
اند مع واحد وهكذا اذا كان البيت الايمن
من العدد المدخل في صفر فان نتيجه العمليه
اند ستكون صفر ايضا بالتالي فالعدد الزوجي
دعنا نقوم بقياس اداء
الكود استغرق
2.82 ثانيه وهو اسرع بقليل من كود المستوى
الاول هذه المقاربه افضل ولكنها لم تقدم
اي تحسن كبير دعنا نجرب جعل الكود
المستوى الثالث اذا كان هناك شيء يجب ان
تعرفه عن مبرمج بايثون فهو انهم يحبون
اشتمال القوائم ليس كومبنشن وما سنفعله
الان هو استبدال الحلقه الموجوده في
الداله باجمال
القائمه وهكذا اختصرنا خمس اسطر في سطر
واحد ولم يتبقى لنا الا تجربته ام النتيجه
هي
2.48 ثانيه مره اخرى تحسن طفيف ومتواضع
جدا العبره في هذا المستوى هو الطرق
الانيقه والاكواد القصيره لا تقدم اداء
قويا بالضروره اذا لقد حان الوقت لتغيير
طريقه تفكيرنا من جديد المستوى الرابع
لماذا لا نستخدم فلتر فهي ميزه موجوده في
كل لغات البرمجه وقد سبق و شرحناها هي
وماب فيلتر واضحه من اسمها انها تقوم
بفلتر القوائم والسلاسل اولا يجب ان نكتب
داله تتعامل مع عنصر واحد وترجع ترو اذا
كان زوجيا والا ففول واضح الان سنكتب داله
تطبق فلتر وت رع لنا نتيجتها حان وقت
التجربه الان دعنا نقيس اداء هذا
الكود رائع تنفيذ هذا الكود استغرق
1.59 ميكرو ثانيه انتبهوا الميكرو ثانيه
وهي جزء من المليون من الثانيه اذا فهذا
الكود اسرع من الكود السابق باكثر من
مليون ونصف مره واو في نظركم ما سبب سرعه
هذا الكود السبب هو ان فيلتر ترجع كائن
جنريتور وليس كائن الليست المولدات
جنريتورز هي نوع من الكائنات الاداء سريعه
وكسوله ولا تستهلك الكثير من الذاكره لكن
كان المطلوب منا في التمرين ارجاع قائمه
او مصفوفه وبالتالي سنضطر لاضافه محول
ليست لنختبر اداء الكود
الان اربع ثواني ونصف سحقا اصبح الاداء
اسوا حتى من المستويات السابقه وهذا يثبت
لنا ان سر قوه فلتر هي المولدات يجب ان
نفكر في طريقه اخرى اكثر ابداعا المستوى
الخامس في المستويات السابقه استخ
استخدمنا باقي القسمه واستخدمنا المقارنه
الثنائيه الان سنستخدم طريقه مختلفه وهي
مقاربه يتبجح بها المحترفون عاده مقاربه
قويه تستخدم لحل الكثير من المشاكل الا
وهي الاجترار ريكور جن حسنا فكره الاجترار
لمعرفه اذا كان العدد زوجيا تعمل بالطريقه
التاليه ناخذ اي عدد ونستمر بطرح اثنان
منه اذا وصل الى صفر نتوقف فهو زوجي
الاعداد الفرديه بهذه الطريقه لا تصل
الصفر ابدا واخر عدد موجب تصل اليه هو
واحد نعدل الداله از ابن ونجعلها ترجع ترو
اذا كان المتغير نمبر صفر وفولس اذا كان
المتغير واحد والا فانه مطلوب منها ان
تنادي نفسها مع نمبر ناقص اين وهكذا
ستستمر بازرار نفسها حتى تصل الى ترو او
فولس لست متفائلا بهذا الكود لكن دعنا
نجرب الكود استغرق اكثر من ساعتين ولم
ينتهي بعد قطعا هذه المقارنه غير صالحه هي
مقاربه قويه في حل الكثير من المشاكل ولكن
ليس هنا لن اكذب كنت اعرف النتيجه مسبقا
لان التعقيد الزمني لهذا الكود هو او ان
كي ولكن اردت فقط ان اثبت لكم ان الاساليب
المتقدمه ليست فعاله دائما في حل المشاكل
المستوى السادس في هذا المستوى سنبدا
بتجربه الطرق القويه التي نستخدمها في
العالم الحقيقي سنستخدم المصفوفات مع نام
باي في الداله سنستخدم نم باي ارينج لانها
اسرع من
رينج وبعدها سنستخدم الفلتر الخاص
بالمصفوفات نام باي وكما ترى هو انيق
وبسيط كل ما عليك فعله هو فتح القوسين
المربعين بعد اسم المصفوفه وتحديد الشرط
الذي تريد ان يتوفر في القيم التي تريد
ابقائها طبعا هذه المقارنه الثنائيه سبق
وشرحنا لنقوم الان بقياس اداء
الكود انه سريع جدا استغرق 366 ميلي ثانيه
وهو اسرع من كود المستوى الاول بتسع مرات
تقريبا هذا فرق جيد فعلا الان هل عرفت
لماذا الجميع يحبون نام باي ولماذا تجدها
في الكثير من المشاريع الضخمه المكتبات
تقدم حلولا انيقه وقويه استخدم المكتبات
المستوى السابع وصلنا الى المستوى الاخير
في هذا المستوى ستعرف ان البرمجه ليست
مجرد اكواد بل طريقه تفكير اكثر مهاره
ستحتاجها هي القدره على تبسيط العمليات في
هذا المستوى سنعتمد على مقاربه في غايه
البساطه وهي كالتالي اذا بدانا من عدد
زوجي واضفنا اليه اثنين كل مره سنحصل على
سلسله لانهائيه من الاعداد الزوجيه اذا
يمكننا فقط استخدام داله رانج وجعلها تقفز
بخطوتين كل مره لماذا لم نفكر في هذا من
الاول ببساطه لانه لدينا مشكله واحده وهي
ان المستخدم قد يحدد رقم بدايه فردي في
ستارت وستنتج الرانج اعدادا فرديه وهي
نتيجه لا نريدها اطلاقا هناك حل بسيط هو
ان نتحقق اذا كان المستخدم ادخل عدد بدايه
فردي اذا كان كذلك سنقوم باضافه واحد
للعدد المدخل وهكذا سيصبح عددا زوجيا ثم
نبدا الرنج منه اذا كان العدد المدخل
زوجيا
فسنبقى الاعداد من ستارت حتى اند مع القفز
خطوتين كل مره والا فسنعيد نطاق الاعداد
من ارت زائد واحد حتى ان طبعا مع القفز
خطوتين كل مره دعنا نجرب
الكود الان دع نقيس
اداءه رائع جدا هذ هذه المقاربه استغرقت
38 ملي ثانيه وهذا اسرع من الكود في
المستوى الاول ب 84 مره فرق كبير وملحوظ
كان هناك اكواد معقده وصعبه واكواد يمكن
استخدام تقنيات الحوسبه المتوازيه ودعم
الجي بي يو فيها ولكن ادائها لم يكن رائعا
هذا الكود البسيط الذي يمكن ان يكتبه اي
شخص مبتدئ في البرمجه هو الكود الاكثر
فعاليه تذكر جيدا ان اقوى مهارات البرمجه
تتلخص في التفكير الموجه بالحلول لا في
قدرتك على كتابه الاكواد او حفظها كانت
هذه سبع مستويات من الكود ادناها المقاربه
السادش واعلاها المقاربه الابسط ويظهر على
الشاشه الان مقارنه لاداء الاكواد يمكننا
عمل حلقات مشابهه ولكن مع مسائل اكثر
صعوبه او مشاكل رياضيه في العالم الحقيقي
اخبروني في التعليقات اذا احببتم هذا
النوع من الفيديوهات
وسنكرس والاشتراك لعل هذه المواد تصل الى
المزيد من الاشخاص تعليقاتكم واعجابات
تشجعنا على الاستمرار وتقديم الافضل شكرا
جزيلا على المتاب وجزيل الشكر موصول لرع
تنا على بيتريون المجال مفتوح لاي شخص
يريد التطوع معنا لاثراء المحتوى العربي
الى اللقاء في الحلقه القادمه والسلام
عليكم ورحمه الله وبركاته
Ver Más Videos Relacionados
Numbered Headings and Subheadings | Microsoft Word Tutorial
AS & A Level Computer Science (9618) - Chapter 16: ADVANCED Data Representation
List Comprehension in Python 🐍 with Execution | Best Python Tutorials for Beginners
What is Pseudocode Explained | How to Write Pseudocode Algorithm | Examples, Benefits & Steps
Numpy Operations - Data Analysis with Python Course
#21 Python Tutorial for Beginners | For Loop in Python
5.0 / 5 (0 votes)