- عنوان کتاب: Computer Science From Scratch -Building Interpreters, Art, Emulators and ML in Python
- نویسنده: David Kopec
- حوزه: علوم کامپیوتر
- سال انتشار: 2025
- تعداد صفحه: 398
- زبان اصلی: انگلیسی
- نوع فایل: pdf
- حجم فایل: 5.87 مگابایت
این کتاب برای برنامهنویسان پایتون در سطح متوسط و پیشرفته است. اگر شما یک برنامهنویس مبتدی هستید، احتمالاً باید در مراحل بعدی به این کتاب برگردید. در طول متن، فرض میکنم خواننده نحو و معناشناسی پایتون را میداند، در نوشتن برنامههایی با پیچیدگی متوسط راحت است، میداند چگونه کتابخانههای پایتون را نصب کند و ساختارهای داده اولیه مانند لیستها، مجموعهها و دیکشنریها را درک میکند. در حالی که فرض میکنم خوانندگان تجربه برنامهنویسی دارند، فرض نمیکنم که خوانندگان دانش علوم کامپیوتر یا ریاضیات پیشرفته داشته باشند. این کتاب برای کسانی طراحی شده است که یا فاقد آموزش رسمی علوم کامپیوتر هستند یا میخواهند برخی از شکافهای دانش خود را پر کنند. به عنوان مثال، اگر به نوشتن زبان برنامهنویسی خود علاقه دارید اما هرگز دورهای در مورد کامپایلرها نگذراندهاید، این کتاب نقطه شروع بسیار خوبی است. اگر میخواهید یک شبیهساز کنسول بازی ویدیویی بنویسید، این کتاب نحوه انجام آن را به شما نشان میدهد. حتی مقدمهای بسیار قابل فهم در مورد یادگیری ماشین دارد. پروژههای دقیق موجود در کتاب ممکن است خودشان هدف نهایی شما نباشند، اما نکته اصلی این نیست. آنها را به عنوان وسیلهای برای گشودن دانش عمیقتر در مورد تفکر الگوریتمی و نحوه عملکرد نرمافزار و به عنوان نقطه شروعی برای کاوشهای خود در نظر بگیرید. هر فصل، به جز فصلهای ۷ و ۸ که روی هم رفته یک پروژه را تشکیل میدهند، یک پروژه کامل را تشکیل میدهد. هفت پروژه موجود در کتاب از آسان (مفسر Brainfuck در فصل ۱) تا دشوار (شبیهساز NES در فصل ۶) متغیر هستند، اما از آنجایی که تمام کد منبع ارائه شده است، هرگز گیر نمیکنید و قادر به ادامه کار نخواهید بود. هر پروژه با مقداری نظریه آغاز میشود – فقط به اندازهای که بفهمیم چه چیزی را پیادهسازی خواهیم کرد، بدون اینکه درگیر جزئیات شوید – و سپس به بررسی کد میپردازد. فصلها همچنین شامل داستانهایی در مورد چگونگی علاقهمند شدن من به موضوع، بحثی در مورد نحوه استفاده از الگوریتمها یا تکنیکهای محاسباتی پیادهسازی شده در دنیای واقعی و چالشهایی برای خواننده برای گسترش کد ارائه شده است. این کتاب به چهار بخش تقسیم شده است. در بخش اول، با ایجاد پیادهسازیهایی از دو زبان برنامهنویسی ساده، دنیای مفسرها را بررسی خواهیم کرد. فصل 1: کوچکترین زبان برنامهنویسی ممکن Brainfuck یک زبان برنامهنویسی مینیمال است که اغلب به دلیل سادگیاش برای اهداف آموزشی استفاده میشود – کل زبان فقط از هشت کاراکتر تشکیل شده است. با پیادهسازی یک مفسر که میتواند هر برنامه Brainfuck را اجرا کند، یاد خواهیم گرفت که یک مفسر بسیار ساده چگونه کار میکند. همچنین یاد خواهیم گرفت که منظور از Turing-complete بودن یک زبان چیست. فصل 2: نوشتن یک مفسر BASIC زبان برنامهنویسی BASIC و گویش خلاصه شده آن، Tiny BASIC، در طول انقلاب کامپیوتر شخصی در اواخر دهه 1970 محبوب بودند. ما یک مفسر برای یک نوع کمی ساده شده از Tiny BASIC به نام NanoBASIC پیادهسازی خواهیم کرد. انجام این کار، اجزای تشکیل دهنده مفسرهای پیچیدهتر، از جمله یک توکنایزر، تجزیه کننده و محیط زمان اجرا را نشان میدهد. در بخش دوم، به دنیای پر جنب و جوش هنر محاسباتی خواهیم پرداخت. فصل 3: پردازش تصویر قدیمی هنگامی که فناوری نمایشگر سادهتر بود، الگوریتمهای dithering برای تطبیق تصاویر برای دستگاههایی که از پالت رنگی محدودی استفاده میکردند، ضروری بودند. ما یک الگوریتم دیترینگ پیادهسازی خواهیم کرد که قادر به نمایش عکسهای رنگی مدرن روی صفحه سیاه و سفید یک مکینتاش اصلی است. سپس، تصاویر دیترینگ شده را با استفاده از الگوریتم فشردهسازی کدگذاری طول اجرا در این فرآیند، به فرمتی سازگار با برنامه کلاسیک MacPaint تبدیل خواهیم کرد. تصاویری که ما خروجی میدهیم میتوانند روی سختافزار واقعی مکینتاش دهه 1980 نمایش داده شوند. فصل 4: یک الگوریتم نقاشی تصادفی آیا یک الگوریتم نسبتاً ساده میتواند هنر انتزاعی پیچیدهای ایجاد کند؟ ما از یک تکنیک تصادفی برای تولید “برداشتها” از تصاویر موجود با تطبیق اشکال تصادفی با تصویر زیرین استفاده خواهیم کرد و خواهیم دید که چگونه یک الگوریتم تپه نوردی میتواند به بهینهسازی نتایج کمک کند. بخش سوم در مورد شبیهسازها است – برنامههایی که به یک نوع کامپیوتر اجازه میدهند وانمود کند که نوع دیگری از کامپیوتر است. فصل 5: ساخت یک ماشین مجازی CHIP-8 CHIP-8 یک مشخصات ماشین مجازی (VM) است که در ابتدا برای توسعه بازیهای ویدیویی در دهه 1970 استفاده میشد. ساخت یک ماشین مجازی CHIP-8 اغلب به عنوان بهترین قدم اول در دنیای شبیهسازی در نظر گرفته میشود: نسبتاً ساده است اما همچنان شامل تمام مراحل لازم برای ایجاد یک شبیهساز است. ماشین مجازی CHIP-8 ما قادر به اجرای تمام بازیهای CHIP-8 خواهد بود که در دهه 1970 روی دستگاهها اجرا میشدند. فصل 6: شبیهسازی کنسول بازی NES NES یکی از پرفروشترین کنسولهای بازی ویدیویی در تمام دوران بود. ما یک شبیهساز خواهیم ساخت که بتواند بازیهای واقعی NES را اجرا کند. این شبیهساز صدایی نخواهد داشت، نسبتاً کند خواهد بود و کاملاً دقیق یا سازگار با همه نخواهد بود، اما همچنان راهی عالی برای یادگیری نه تنها در مورد شبیهسازها، بلکه در مورد نحوه کار کامپیوترها در سطح پایین خواهد بود. در نهایت، بخش چهارم مقدمهای بسیار ملایم بر دنیای یادگیری ماشین با استفاده از الگوریتم k-نزدیکترین همسایه (KNN) است. فصل 7: طبقهبندی با K-نزدیکترین همسایهها ما KNN را یاد خواهیم گرفت…
This book is for intermediate and advanced Python programmers. If you’re a beginning programmer, you should probably come back to this book at a later point. Throughout the text, I assume the reader knows the syntax and semantics of Python, is comfortable writing programs of moderate complexity, knows how to install Python libraries, and understands basic data structures like lists, sets, and dictionaries. While I do assume readers will have some programming experience, I don’t assume readers’ knowledge of computer science or advanced mathematics. This book is designed for those who either lack a formal computer science education or want to fill in some gaps in their knowledge. For example, if you have an interest in writing your own programming language but you never took a course on compilers, this book is a great starting point. If you want to write a video game console emulator, this book will show you how. It even has a very digestible introduction to machine learning. The exact projects in the book may not themselves be your end goal, but that’s not the point. Think of them as a means to unlock deeper knowledge about algorithmic thinking and how software works, and as a jumping-off point for your own explorations. Each chapter constitutes one complete project, except for Chapters 7 and 8, which together make up one project. The seven projects in the book range from easy (the Brainfuck interpreter in Chapter 1) to difficult (the NES emulator in Chapter 6), but since all the source code is provided, you’ll never get stuck and be unable to proceed. Each project begins with some theory—just enough to understand what we’ll be implementing, without getting bogged down in the details—and then walks through the code. The chapters also include stories about how I personally got interested in the subject, a discussion of how the implemented algorithms or computational techniques are used in the real world, and challenges for the reader to extend the provided code. The book is divided into four parts. In Part I, we’ll explore the world of interpreters by creating implementations of two simple programming languages. Chapter 1: The Smallest Possible Programming Language Brainfuck is a minimal programming language often used for educational purposes because of its simplicity—the whole language consists of just eight characters. We’ll learn how a very simple interpreter works by implementing one that can run any Brainfuck program. We’ll also learn what it means for a language to be Turing-complete. Chapter 2: Writing a BASIC Interpreter The BASIC programming language and its pared-down dialect, Tiny BASIC, were popular during the PC revolution of the late 1970s. We’ll implement an interpreter for a slightly simplified variant of Tiny BASIC called NanoBASIC. Doing so will demonstrate the constituent parts of more sophisticated interpreters, including a tokenizer, parser, and runtime environment. In Part II, we’ll get into the vibrant world of computational art. Chapter 3: Retro Image Processing When display technology was simpler, dithering algorithms were necessary to adapt images for devices that used a limited color palette. We’ll implement a dithering algorithm capable of displaying modern color photos on the black-and-white screen of an original Macintosh. Then, we’ll convert the dithered images to a format compatible with the classic MacPaint application, using the run-length encoding compression algorithm in the process. The images we output can be displayed on actual 1980s Macintosh hardware. Chapter 4: A Stochastic Painting Algorithm Can a relatively simple algorithm create sophisticated abstract art? We’ll use a stochastic technique to generate “impressions” of existing images by matching random shapes to the underlying image, and we’ll see how a hill-climbing algorithm can help optimize the results. Part III is all about emulators—programs that allow one type of computer to pretend to be another type of computer. Chapter 5: Building a CHIP-8 Virtual Machine CHIP-8 is a virtual machine (VM) specification that was originally used for developing video games in the 1970s. Building a CHIP-8 VM is often considered the best first step into the world of emulation: it’s relatively simple but still involves all the steps necessary to create an emulator. Our CHIP-8 VM will be capable of playing all the CHIP-8 games that ran on machines in the 1970s. Chapter 6: Emulating the NES Game Console The NES was one of the best-selling video game consoles of all time. We’ll create an emulator that can play real NES games. It will have no sound, be rather slow, and not be completely accurate or universally compatible, but it will still be a great way to learn not just about emulators but also about how computers work at a low level. Finally, Part IV is a very gentle introduction to the world of machine learning using the k-nearest neighbors (KNN) algorithm. Chapter 7: Classification with K-Nearest Neighbors We’ll learn KNN, perhaps the simplest algorithm in machine learning (ML), and use it as a gateway to understand some introductory ML topics. We’ll use KNN to classify fish as well as images of handwritten digits. Amazingly, it will complete the latter task with 98 percent accuracy. Chapter 8: Regression with K-Nearest Neighbors We’ll take KNN to the next level by using it not just to classify items into categories but also to predict unknown attributes of data points. In the chapter finale, we’ll use it to predict the missing pixels from an image of a digit that the user draws. Beyond the main chapters, the afterword features some suggested resources for learning more about the topics in this book, and the appendix covers the basics of low-level bit manipulation in Python, an essential component of several projects. This Book’s Approach I try to keep my books as succinct as possible. I value your time. I use a tutorial-like, code-centric format to teach, and where possible, I let the code speak for itself. This is not a textbook. You’ll find some theory, especially at the beginning of each chapter, but it will never be too long before we get to some code. There’s just enough information to help you understand how each of the projects works, and enough pointers so that you know where to look next if you want to dive deeper into any of the covered topics. I’m not claiming to be an expert on interpreters, computational art, emulators, or machine learning. That may sound weird coming from the author of a book on those topics, but it’s true. I’m not an expert; I’m a teacher. I’ve worked as a software developer, and I’ve worked as computer science faculty at a teaching college. My claim is that I’m able to write clean code and explain that code to you in an exceptionally comprehensible manner. And since I’m not an expert, I won’t be talking down to you. I’ll be treating you like my peer as we go on this journey together. This is the guide I wish I had as I tried doing projects in these areas on my own. About the Code All the source code in this book is available on the companion GitHub repository at https://github.com /davecom/ComputerScienceFromScratch. The code was created and tested against Python versions 3.12 and 3.13. Because some type hint–related features of Python 3.12 are utilized, some of the code won’t work with earlier versions of Python (but will likely work with any new version of Python in the foreseeable future). However, if you remove the type hints, the vast majority of the code will work with Python version 3.10 and later. I’ve used Python type hints (or “type annotations”) throughout the source code because I believe they increase readability by telling you a function’s parameter and return types without you needing to scrutinize the code or the comments. If you don’t like them, you can ignore them; they don’t change anything about how the code works. I’ve tried not to overuse type hints, as some find them to be too verbose. For example, I rarely use them within function bodies, but I do use them in every function signature. I type-checked all the source code against the contemporary version of Pyright at the time of the book’s writing. Several of the projects in this book use external libraries. You should have Pygame, NumPy, and Pillow installed in the virtual environment you create for the book’s source code or in your system Python interpreter. For most readers, installing them should be as simple as running pip install pygame, numpy, pillow. A requirements.txt file that pip can use is included in the book’s source code repository.
این کتاب را میتوانید از لینک زیر بصورت رایگان دانلود کنید:
Download: Computer Science From Scratch
نظرات کاربران