Download the Book in PDF Form
Total Page:16
File Type:pdf, Size:1020Kb
Learn Programming Antti Salonen Aug 11, 2018 Learn Programming, first edition Antti Salonen This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. The code within this book is licensed under the MIT license. Copyright 2018, Antti Salonen. 2 CONTENTS: 1 The beginning 7 1.1 Introduction ......................... 8 1.1.1 Why this book? .................... 8 1.1.2 What is software? .................. 15 1.1.3 How does a computer work? ............ 20 1.1.4 OK, but seriously, how does a computer work? ... 27 1.1.5 The basics of programming ............. 32 1.1.6 Setting up the C toolchain .............. 42 1.1.7 The basics of programming in C .......... 46 1.1.8 Learning to learn .................. 56 1.2 Basics of programming in Python and C .......... 59 1.2.1 Quadratic formula in C ............... 59 1.2.2 Lots of quadratic equations ............. 64 1.2.3 Quadratic formula in Python ............ 69 1.2.4 Generating input data using Python ........ 76 1.3 Unix shell ........................... 77 1.3.1 Basic Unix shell usage ................ 77 1.3.2 Unix shell scripting ................. 88 1.3.3 Regular expressions ................. 95 1.4 Using libraries in Python ................... 99 1.4.1 Creating a simple web page ............. 99 1.4.2 Making our web page work ............. 104 2 Stage 1 109 2.1 Further Unix tools ...................... 110 2.1.1 Version control ................... 110 2.1.2 Working with other git repositories ........ 116 3 Contents: 2.1.3 Some related Unix tools ............... 125 2.1.4 Vim .......................... 127 2.1.5 Programming with vim ............... 130 2.2 Background on programming languages and algorithms . 136 2.2.1 Typing ........................ 136 2.2.2 Big O notation .................... 142 2.2.3 Array, stack, queue .................. 150 2.3 JavaScript ........................... 154 2.3.1 Guessing game in JS ................. 154 2.3.2 JavaScript meets algorithms ............ 160 2.4 Intermediate C ........................ 163 2.4.1 Records ........................ 163 2.4.2 C and the stack .................... 167 2.4.3 Arrays and the stack ................. 172 2.4.4 Dynamic memory allocation ............ 177 2.4.5 C and strings ..................... 181 2.4.6 Writing a toy web server .............. 189 2.4.7 Security ....................... 198 2.5 More programming concepts using Python ......... 199 2.5.1 More data structures ................ 199 2.5.2 Object oriented programming ........... 204 2.5.3 JSON ......................... 207 2.5.4 More useful Python constructs ........... 211 2.5.5 Callbacks and anonymous functions ........ 215 2.5.6 Functional programming .............. 219 3 Stage 1.5 223 3.1 Web development with Python and JavaScript ....... 224 3.1.1 HTML tables ..................... 225 3.1.2 Redis ......................... 227 3.1.3 High level architecture ............... 233 3.1.4 AJAX ......................... 237 3.1.5 Gluing AJAX and Redis together .......... 243 3.1.6 The page for starting a new game .......... 245 3.1.7 Generating the high score table ........... 250 3.1.8 Some more tips and tricks .............. 256 3.2 Working with binary data in C ............... 260 3.2.1 PNG files ....................... 260 3.2.2 Finishing our simple PNG parser .......... 268 4 Contents: 3.3 Strongly, statically typed languages ............. 275 3.3.1 Under the hood ................... 275 3.3.2 Virtual machines ................... 279 3.3.3 The case for statically, strongly typed languages .. 281 3.3.4 Established languages ................ 288 3.3.5 Newer languages .................. 296 3.4 Learning C++ using Sudoku ................. 302 3.4.1 Introduction to Sudoku ............... 302 3.4.2 Containers for Sudoku ............... 307 3.4.3 Sudoku Puzzle class ................. 313 3.4.4 Propagation and search ............... 322 4 Stage 2 331 4.1 Larger software ........................ 332 4.1.1 Introduction to larger software ........... 332 4.1.2 Breaking software down to components ...... 340 4.1.3 Drawing to a screen using SDL2 .......... 348 4.1.4 Drawing the schedule screen using SDL2 ..... 357 4.1.5 Unix way - sched ................... 363 4.1.6 Unix way - parse_gps ................ 369 4.1.7 Unix way - merge .................. 374 4.1.8 Monolithic way - parsing .............. 380 4.1.9 Monolithic way - scheduled arrivals and GPS data . 386 4.1.10 Monolithic way - merging and putting it all together 393 4.2 A fistful of Python exercises ................. 401 4.2.1 Graphs ........................ 401 4.2.2 Parsing ........................ 410 4.3 SQL and its relationship with online shops ......... 420 4.3.1 Introduction to SQL ................. 421 4.3.2 Adding data to an SQL database .......... 433 4.3.3 Querying SQL databases .............. 438 4.3.4 Generating a return form .............. 444 4.3.5 Web UI for our return form ............. 450 4.4 Final bits ........................... 462 4.4.1 Software licenses .................. 462 4.4.2 NP-hard problems .................. 465 4.4.3 Concurrency ..................... 471 4.4.4 Tech behind this book ................ 482 4.4.5 Further reading ................... 488 4.4.6 Chapter dependencies ................ 489 5 CHAPTER ONE THE BEGINNING 7 1 The beginning 1.1 Introduction 1.1.1 Why this book? This book is aimed at readers who are interested in software development but have very little to no prior experience. The book doesn’t have any new information compared to what you can find online or in other books; it has two purposes: 1. It includes what I think is important for software development from a large variety of topics, saving the reader from the dilemma “what should I learn” 2. It collates relevant information from lots of sources in one book, sav- ing the reader from going through several separate web sites and books It aims to contain an overview of almost everything that I think is impor- tant for software developers. It doesn’t contain everything; but it should contain enough for the reader to understand software development, and to be able to read about and understand any topic in further detail as needed. The book focuses on teaching the core principles around software develop- ment. It uses several technologies to this goal (e.g. C, Python, JavaScript, HTML, etc.) but is not a book about the technologies themselves. The reader will learn the basics (or in some cases more) of various technolo- gies along the way, but the focus is on building a foundation for software development. This book does have the aim of supporting interested people in pursuing a job in software development, though I obviously can’t guarantee a job. I’ll expand on this further below. Why are you writing this book? My wife was asking if she could become a software engineer like I did. I have some years of experience in software engineering, and also am now hiring and hence interviewing software engineers. I checked the offerings around Computer Science at the local university, but it seems to me like 8 1.1 Introduction there’s a bit of a gap between what industry needs and what the schools are producing, at least in my local area. To some extent this is expected: univer- sities live in the academic world, often without an explicit goal of ensuring the graduates are employed by the industry. To mitigate, universities may offer degrees directed more towards real world, but these run into the risk of not being relevant or effective enough for the industry. So I ended up teaching my wife what I think is the right stuff. Now, since we also have a little daughter, finding time for both of us at the same time was sometimes difficult, so I’d write down some notes or exercises for her that she could dive into when she had time. I ended up writing more and more and structuring things better and all of it ended up being this book. So, I’m writing this book to teach my wife all the necessary basics around software development so that she’ll be able to land a job. However, the book could be useful to others as well. No, really, why are you writing this book? If I’m being completely selfish, it’s because I’m hiring software engineers and am finding it difficult to find any. Without going into my specific re- quirements, I want to include enough in the book that if the reader were to do all of the following: • Read and understand everything in the book • Finish all the exercises • Write at least some personal mini project in some language (shouldn’t be a problem after the above two points) • Interact with the rest of the software development community, may it be through Reddit or other social media, chat, Stack Overflow or GitHub …then I think that that person should have the technical skills to at least se- riously be considered for a software engineering position. (Apart from technical skills, you’ll also need the interpersonal skills and common sense, or analytical and critical thinking skills, among others, be- fore I’d consider you for a position. I’m expecting the reader to obtain these some other way than reading this book.) 9 1 The beginning Do you mean, if I do the points above, I too can become a profes- sional software engineer? I obviously can’t guarantee anything. Actually getting a job depends on the economy and the job market, where you live, what you’re willing to do and work for etc. But I do think that after doing all the above points, the reader should have enough or almost enough technical skills for many software en- gineering jobs.