نفس الكود، ولكن 7 مستويات! و7 طرق للتفكير🧠

أسرار التفوق للمبرمجين
24 Apr 202409:40

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

00:00

😀 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.

05:02

🚀 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

The binary system is a method of representing numeric values using only two symbols, typically 0 and 1. In the context of the video, it is used to represent numbers in a way that makes it easier to determine if they are even or odd, as even numbers end with a zero in binary representation. This is a key concept in the second level of thinking demonstrated in the video.

💡List Comprehension

List comprehension is a feature in Python that allows for the creation of lists from existing lists or iterables using a single line of code. The video mentions replacing a loop with a list comprehension to streamline the code, which is a technique used in the third level of thinking to improve performance.

💡Filter Function

The filter function in Python is used to filter out elements from an iterable based on a specified condition. In the video, it is used to create a more efficient function that returns a generator object, which is faster and more memory-efficient than a list. This is a significant improvement shown in the fourth level of thinking.

💡Recursion

Recursion is a programming technique where a function calls itself to solve a problem. In the video, it is used to check if a number is even by repeatedly subtracting two from the number until it reaches zero. However, the video points out that while recursion is a powerful concept, it is not always the most efficient solution for this specific problem, as demonstrated in the sixth level of thinking.

💡NumPy

NumPy is a Python library that provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. The video uses NumPy's array and filter functionalities to demonstrate a highly efficient way to find even numbers, showcasing the power of using libraries in the fifth level of thinking.

💡Range Function

The range function in Python is used to generate a sequence of numbers within a specified range. In the video, it is used in conjunction with a step parameter to create a sequence of even numbers, which is a key part of the final, most efficient solution presented in the seventh level of thinking.

💡Performance

Performance refers to how quickly and efficiently a piece of code runs. The video script focuses heavily on improving the performance of a function that finds even numbers within a range. It does this by comparing the execution times of different methods, emphasizing the importance of optimizing code for speed and efficiency.

💡Even Numbers

Even numbers are integers that are exactly divisible by two, meaning they have no remainder when divided by two. The video's main theme revolves around identifying and generating even numbers within a given range, which is a mathematical concept central to all the levels of thinking presented.

💡Odd Numbers

Odd numbers are integers that are not divisible by two, resulting in a remainder of one when divided by two. The concept of odd numbers is used in the video to contrast with even numbers and to illustrate the differences in how they are handled in binary representation and arithmetic operations.

💡Modulus Operator

The modulus operator in programming is used to find the remainder of a division operation. In the video, it is initially used to determine if a number is even by checking if the remainder of the division by two is zero. However, the script later suggests alternative methods that avoid the use of the modulus operator for improved efficiency.

💡Thinking Levels

The concept of 'thinking levels' in the video refers to the different approaches or strategies used to solve the same problem, with each level representing a different method or level of complexity. The video progresses through seven levels, each offering a different way to find even numbers, culminating in the most efficient solution.

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

play00:00

نفس الكود ولكن سبع مستويات وسبع طرق

play00:02

للتفكير المستوى الاول حسنا سنعمل مع كود

play00:06

بسيط جدا حتى يفهم الجميع المساله هي

play00:09

كالتالي اكتب كودا يقوم بالبحث عن الاعداد

play00:12

الزوجيه ضمن نطاق او رنج محدد النتيجه يجب

play00:15

ان تكون قائمه ليست او مصفوفه منطقيا

play00:18

لمعرفه عدد ما اذا كان زوجيا ام لا فكل ما

play00:21

عليك القيام به هو قسمته على اثنان واذا

play00:24

كان باقي القسمه صفر فهو عدد زوجي هذه

play00:27

العمليه يطلق عليها الموديل نعود الى

play00:30

المساله الخاصه بنا سنقوم بانشاء داله

play00:33

تاخذ متغيرين ستارت

play00:37

واند ننشئ قائمه

play00:40

فارغه ثم ننشئ هذه الحلقه التي لا هدف لها

play00:44

في الحياه سوى الدوران على الاعداد

play00:45

الموجوده في النطاق بين اند وستارت تحاول

play00:48

قسمه كل عدد على اثنين اذا كان باقي

play00:50

القسمه صفر تقوم باضافته الى القائمه ايفن

play00:57

نمبرز اخيرا الداله تعيد تلك القائمه هذا

play01:01

كان كل شيء دعنا نجرب النطاق من 99 الى 10

play01:19

ملايين يؤدي الغرض المطلوب لكن دعنا نقيس

play01:24

اداء هذا الكود عبر اضافه تايمت اعلاه

play01:27

وتنفيذه ننتظر قليلا

play01:31

النتيجه هي ان هذا الكود يستغرق

play01:33

3.22 ثانيه للتنفيذ في المستويات القادمه

play01:36

لن اتعمق كثيرا في الشرح لكن سنرى طرقا

play01:39

مختلفه للتفكير المستوى الثاني دعنا

play01:42

نستخدم مقاربه مختلفه في النظام الثنائي

play01:45

باينري سيستم تبدو الارقام بالشكل التالي

play01:48

كما تلاحظ فان كل الارقام الزوجيه تنتهي

play01:51

بصفر على اليمين اذا بدل استخدام باقي

play01:53

القسمه سنستخدم معامل المقارنه الثنائيه

play01:56

اند مع واحد وهكذا اذا كان البيت الايمن

play01:59

من العدد المدخل في صفر فان نتيجه العمليه

play02:01

اند ستكون صفر ايضا بالتالي فالعدد الزوجي

play02:05

دعنا نقوم بقياس اداء

play02:18

الكود استغرق

play02:20

2.82 ثانيه وهو اسرع بقليل من كود المستوى

play02:23

الاول هذه المقاربه افضل ولكنها لم تقدم

play02:27

اي تحسن كبير دعنا نجرب جعل الكود

play02:30

المستوى الثالث اذا كان هناك شيء يجب ان

play02:33

تعرفه عن مبرمج بايثون فهو انهم يحبون

play02:36

اشتمال القوائم ليس كومبنشن وما سنفعله

play02:39

الان هو استبدال الحلقه الموجوده في

play02:41

الداله باجمال

play02:46

القائمه وهكذا اختصرنا خمس اسطر في سطر

play02:49

واحد ولم يتبقى لنا الا تجربته ام النتيجه

play02:54

هي

play02:55

2.48 ثانيه مره اخرى تحسن طفيف ومتواضع

play02:59

جدا العبره في هذا المستوى هو الطرق

play03:02

الانيقه والاكواد القصيره لا تقدم اداء

play03:04

قويا بالضروره اذا لقد حان الوقت لتغيير

play03:07

طريقه تفكيرنا من جديد المستوى الرابع

play03:10

لماذا لا نستخدم فلتر فهي ميزه موجوده في

play03:13

كل لغات البرمجه وقد سبق و شرحناها هي

play03:15

وماب فيلتر واضحه من اسمها انها تقوم

play03:18

بفلتر القوائم والسلاسل اولا يجب ان نكتب

play03:21

داله تتعامل مع عنصر واحد وترجع ترو اذا

play03:24

كان زوجيا والا ففول واضح الان سنكتب داله

play03:28

تطبق فلتر وت رع لنا نتيجتها حان وقت

play03:31

التجربه الان دعنا نقيس اداء هذا

play03:36

الكود رائع تنفيذ هذا الكود استغرق

play03:40

1.59 ميكرو ثانيه انتبهوا الميكرو ثانيه

play03:43

وهي جزء من المليون من الثانيه اذا فهذا

play03:46

الكود اسرع من الكود السابق باكثر من

play03:48

مليون ونصف مره واو في نظركم ما سبب سرعه

play03:51

هذا الكود السبب هو ان فيلتر ترجع كائن

play03:54

جنريتور وليس كائن الليست المولدات

play03:57

جنريتورز هي نوع من الكائنات الاداء سريعه

play04:00

وكسوله ولا تستهلك الكثير من الذاكره لكن

play04:03

كان المطلوب منا في التمرين ارجاع قائمه

play04:05

او مصفوفه وبالتالي سنضطر لاضافه محول

play04:08

ليست لنختبر اداء الكود

play04:13

الان اربع ثواني ونصف سحقا اصبح الاداء

play04:17

اسوا حتى من المستويات السابقه وهذا يثبت

play04:20

لنا ان سر قوه فلتر هي المولدات يجب ان

play04:24

نفكر في طريقه اخرى اكثر ابداعا المستوى

play04:27

الخامس في المستويات السابقه استخ

play04:29

استخدمنا باقي القسمه واستخدمنا المقارنه

play04:31

الثنائيه الان سنستخدم طريقه مختلفه وهي

play04:35

مقاربه يتبجح بها المحترفون عاده مقاربه

play04:38

قويه تستخدم لحل الكثير من المشاكل الا

play04:41

وهي الاجترار ريكور جن حسنا فكره الاجترار

play04:45

لمعرفه اذا كان العدد زوجيا تعمل بالطريقه

play04:48

التاليه ناخذ اي عدد ونستمر بطرح اثنان

play04:51

منه اذا وصل الى صفر نتوقف فهو زوجي

play04:54

الاعداد الفرديه بهذه الطريقه لا تصل

play04:56

الصفر ابدا واخر عدد موجب تصل اليه هو

play04:59

واحد نعدل الداله از ابن ونجعلها ترجع ترو

play05:02

اذا كان المتغير نمبر صفر وفولس اذا كان

play05:05

المتغير واحد والا فانه مطلوب منها ان

play05:07

تنادي نفسها مع نمبر ناقص اين وهكذا

play05:10

ستستمر بازرار نفسها حتى تصل الى ترو او

play05:13

فولس لست متفائلا بهذا الكود لكن دعنا

play05:21

نجرب الكود استغرق اكثر من ساعتين ولم

play05:25

ينتهي بعد قطعا هذه المقارنه غير صالحه هي

play05:28

مقاربه قويه في حل الكثير من المشاكل ولكن

play05:31

ليس هنا لن اكذب كنت اعرف النتيجه مسبقا

play05:34

لان التعقيد الزمني لهذا الكود هو او ان

play05:37

كي ولكن اردت فقط ان اثبت لكم ان الاساليب

play05:40

المتقدمه ليست فعاله دائما في حل المشاكل

play05:43

المستوى السادس في هذا المستوى سنبدا

play05:46

بتجربه الطرق القويه التي نستخدمها في

play05:48

العالم الحقيقي سنستخدم المصفوفات مع نام

play05:51

باي في الداله سنستخدم نم باي ارينج لانها

play05:54

اسرع من

play05:56

رينج وبعدها سنستخدم الفلتر الخاص

play05:59

بالمصفوفات نام باي وكما ترى هو انيق

play06:02

وبسيط كل ما عليك فعله هو فتح القوسين

play06:05

المربعين بعد اسم المصفوفه وتحديد الشرط

play06:08

الذي تريد ان يتوفر في القيم التي تريد

play06:10

ابقائها طبعا هذه المقارنه الثنائيه سبق

play06:13

وشرحنا لنقوم الان بقياس اداء

play06:18

الكود انه سريع جدا استغرق 366 ميلي ثانيه

play06:23

وهو اسرع من كود المستوى الاول بتسع مرات

play06:26

تقريبا هذا فرق جيد فعلا الان هل عرفت

play06:29

لماذا الجميع يحبون نام باي ولماذا تجدها

play06:32

في الكثير من المشاريع الضخمه المكتبات

play06:35

تقدم حلولا انيقه وقويه استخدم المكتبات

play06:38

المستوى السابع وصلنا الى المستوى الاخير

play06:41

في هذا المستوى ستعرف ان البرمجه ليست

play06:44

مجرد اكواد بل طريقه تفكير اكثر مهاره

play06:47

ستحتاجها هي القدره على تبسيط العمليات في

play06:50

هذا المستوى سنعتمد على مقاربه في غايه

play06:53

البساطه وهي كالتالي اذا بدانا من عدد

play06:55

زوجي واضفنا اليه اثنين كل مره سنحصل على

play06:58

سلسله لانهائيه من الاعداد الزوجيه اذا

play07:01

يمكننا فقط استخدام داله رانج وجعلها تقفز

play07:04

بخطوتين كل مره لماذا لم نفكر في هذا من

play07:06

الاول ببساطه لانه لدينا مشكله واحده وهي

play07:10

ان المستخدم قد يحدد رقم بدايه فردي في

play07:13

ستارت وستنتج الرانج اعدادا فرديه وهي

play07:15

نتيجه لا نريدها اطلاقا هناك حل بسيط هو

play07:18

ان نتحقق اذا كان المستخدم ادخل عدد بدايه

play07:21

فردي اذا كان كذلك سنقوم باضافه واحد

play07:24

للعدد المدخل وهكذا سيصبح عددا زوجيا ثم

play07:28

نبدا الرنج منه اذا كان العدد المدخل

play07:30

زوجيا

play07:52

فسنبقى الاعداد من ستارت حتى اند مع القفز

play07:55

خطوتين كل مره والا فسنعيد نطاق الاعداد

play07:59

من ارت زائد واحد حتى ان طبعا مع القفز

play08:02

خطوتين كل مره دعنا نجرب

play08:18

الكود الان دع نقيس

play08:27

اداءه رائع جدا هذ هذه المقاربه استغرقت

play08:30

38 ملي ثانيه وهذا اسرع من الكود في

play08:34

المستوى الاول ب 84 مره فرق كبير وملحوظ

play08:37

كان هناك اكواد معقده وصعبه واكواد يمكن

play08:40

استخدام تقنيات الحوسبه المتوازيه ودعم

play08:43

الجي بي يو فيها ولكن ادائها لم يكن رائعا

play08:46

هذا الكود البسيط الذي يمكن ان يكتبه اي

play08:48

شخص مبتدئ في البرمجه هو الكود الاكثر

play08:51

فعاليه تذكر جيدا ان اقوى مهارات البرمجه

play08:54

تتلخص في التفكير الموجه بالحلول لا في

play08:57

قدرتك على كتابه الاكواد او حفظها كانت

play08:59

هذه سبع مستويات من الكود ادناها المقاربه

play09:02

السادش واعلاها المقاربه الابسط ويظهر على

play09:06

الشاشه الان مقارنه لاداء الاكواد يمكننا

play09:08

عمل حلقات مشابهه ولكن مع مسائل اكثر

play09:11

صعوبه او مشاكل رياضيه في العالم الحقيقي

play09:14

اخبروني في التعليقات اذا احببتم هذا

play09:16

النوع من الفيديوهات

play09:20

وسنكرس والاشتراك لعل هذه المواد تصل الى

play09:23

المزيد من الاشخاص تعليقاتكم واعجابات

play09:25

تشجعنا على الاستمرار وتقديم الافضل شكرا

play09:28

جزيلا على المتاب وجزيل الشكر موصول لرع

play09:31

تنا على بيتريون المجال مفتوح لاي شخص

play09:33

يريد التطوع معنا لاثراء المحتوى العربي

play09:36

الى اللقاء في الحلقه القادمه والسلام

play09:38

عليكم ورحمه الله وبركاته

Rate This

5.0 / 5 (0 votes)

Related Tags
Coding LevelsBinary SystemPython EfficiencyLoop OptimizationList ComprehensionFilter FunctionRecursion MethodArray UtilizationSimplification ApproachPerformance ComparisonAlgorithmic ThinkingCode PerformanceTechnical TutorialArabic ProgrammingProblem SolvingOptimization Techniques