Learn Programming
Total Page:16
File Type:pdf, Size:1020Kb
Learn Programming Antti Salonen Aug 04, 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? ............ 19 1.1.4 OK, but seriously, how does a computer work? ... 26 1.1.5 The basics of programming ............. 30 1.1.6 Setting up the C toolchain .............. 39 1.1.7 The basics of programming in C .......... 43 1.1.8 Learning to learn .................. 53 1.2 Basics of programming in Python and C .......... 56 1.2.1 Quadratic formula in C ............... 56 1.2.2 Lots of quadratic equations ............. 60 1.2.3 Quadratic formula in Python ............ 65 1.2.4 Generating input data using Python ........ 71 1.3 Unix shell ........................... 72 1.3.1 Basic Unix shell usage ................ 72 1.3.2 Unix shell scripting ................. 82 1.3.3 Regular expressions ................. 89 1.4 Using libraries in Python ................... 93 1.4.1 Creating a simple web page ............. 93 1.4.2 Making our web page work ............. 97 2 Stage 1 103 2.1 Further Unix tools ...................... 104 2.1.1 Version control ................... 104 2.1.2 Working with other git repositories ........ 109 3 Contents: 2.1.3 Some related Unix tools ............... 117 2.1.4 Vim .......................... 119 2.1.5 Programming with vim ............... 121 2.2 Background on programming languages and algorithms . 128 2.2.1 Typing ........................ 128 2.2.2 Big O notation .................... 134 2.2.3 Array, stack, queue .................. 141 2.3 JavaScript ........................... 145 2.3.1 Guessing game in JS ................. 145 2.3.2 JavaScript meets algorithms ............ 149 2.4 Intermediate C ........................ 153 2.4.1 Records ........................ 153 2.4.2 C and the stack .................... 156 2.4.3 Arrays and the stack ................. 161 2.4.4 Dynamic memory allocation ............ 165 2.4.5 C and strings ..................... 169 2.4.6 Writing a toy web server .............. 176 2.4.7 Security ....................... 185 2.5 More programming concepts using Python ......... 186 2.5.1 More data structures ................ 186 2.5.2 Object oriented programming ........... 190 2.5.3 JSON ......................... 193 2.5.4 More useful Python constructs ........... 197 2.5.5 Callbacks and anonymous functions ........ 201 2.5.6 Functional programming .............. 204 3 Stage 1.5 209 3.1 Web development with Python and JavaScript ....... 210 3.1.1 HTML tables ..................... 211 3.1.2 Redis ......................... 212 3.1.3 High level architecture ............... 217 3.1.4 AJAX ......................... 220 3.1.5 Gluing AJAX and Redis together .......... 226 3.1.6 The page for starting a new game .......... 228 3.1.7 Generating the high score table ........... 234 3.1.8 Some more tips and tricks .............. 239 3.2 Working with binary data in C ............... 243 3.2.1 PNG files ....................... 243 3.2.2 Finishing our simple PNG parser .......... 251 4 Contents: 3.3 Strongly, statically typed languages ............. 259 3.3.1 Under the hood ................... 259 3.3.2 Virtual machines ................... 262 3.3.3 The case for statically, strongly typed languages .. 264 3.3.4 Established languages ................ 271 3.3.5 Newer languages .................. 278 3.4 Learning C++ using Sudoku ................. 285 3.4.1 Introduction to Sudoku ............... 285 3.4.2 Containers for Sudoku ............... 290 3.4.3 Sudoku Puzzle class ................. 295 3.4.4 Propagation and search ............... 304 4 Stage 2 315 4.1 Larger software ........................ 316 4.1.1 Introduction to larger software ........... 316 4.1.2 Breaking software down to components ...... 322 4.1.3 Drawing to a screen using SDL2 .......... 330 4.1.4 Drawing the schedule screen using SDL2 ..... 338 4.1.5 Unix way - sched ................... 344 4.1.6 Unix way - parse_gps ................ 349 4.1.7 Unix way - merge .................. 353 4.1.8 Monolithic way - parsing .............. 358 4.1.9 Monolithic way - scheduled arrivals and GPS data . 364 4.1.10 Monolithic way - merging and putting it all together 370 4.2 A fistful of Python exercises ................. 379 4.2.1 Graphs ........................ 379 4.2.2 Parsing ........................ 389 4.3 SQL and its relationship with online shops ......... 400 4.3.1 Introduction to SQL ................. 401 4.3.2 Adding data to an SQL database .......... 412 4.3.3 Querying SQL databases .............. 416 4.3.4 Generating a return form .............. 421 4.3.5 Web UI for our return form ............. 427 4.4 Final bits ........................... 439 4.4.1 Software licenses .................. 439 4.4.2 NP-hard problems .................. 441 4.4.3 Concurrency ..................... 446 4.4.4 Tech behind this book ................ 457 4.4.5 Further reading ................... 462 5 Contents: 5 Chapter dependencies 465 6 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.