diff --git a/django.md b/backend/django.md similarity index 100% rename from django.md rename to backend/django.md diff --git a/CP1.md b/competitive-programming/CP1.md similarity index 86% rename from CP1.md rename to competitive-programming/CP1.md index 3688f53..c355fb7 100644 --- a/CP1.md +++ b/competitive-programming/CP1.md @@ -5,7 +5,7 @@ If you have solved 100+ problems and are looking for guidance on how to solve problems involving algorithms and data structures, this document is not for you.) -Competitive Programming is an interesting activity which mixes problem solving with programming. +Competitive Programming is an interesting activity which mixes problem-solving with programming. It is not only enjoyable but also very demanded in placements. Competitive programming will make you very good at writing efficient programs quickly. If you get really serious with competitive programming, it will make you an expert in data structures and algorithms. @@ -31,10 +31,14 @@ But at some point of time (especially when you reach advanced stages), you'll need features which most languages have but C does not. Learning C++ is very easy if you know C. If you already know C, you should start competitive programming in C and learn C++ in parallel. +> You may refer to [C to C++ for Competitive Programming](c2cpp.md) for a brief intro and resources after you solve [a few basic problems](#making-the-first-step) using C. Even if you are not confident of your skills in a programming language, you can (and should) still start. Competitive programming is also a good way to practice a new language you have learned. +If you have no prior knowledge you can begin with [CS50 2016](http://cs50.tv/2016/fall/) (week 0 to week 5) +The course teaches C which anyways is useful as it's a part of BITS curriculum. + ## Making the first step You'll find problems on many websites. @@ -74,11 +78,16 @@ For each difficulty level, easier problems generally have more submissions. So you can sort problems based on number of submissions to find the easiest ones. For beginners, Codechef is a good site. -If you have never before solved a problems on an online judge, you can begin by solving +If you have never before solved problems on an online judge, you can begin by solving the easiest problem on Codechef - [Life, the Universe, and Everything](https://www.codechef.com/problems/TEST). You will have to read the [Input/Output tutorial](http://blog.codechef.com/2009/02/24/54/) to solve the problem. If you face problems, you can refer to [Eklavya's solutions](https://www.codechef.com/status/TEST,sharmaeklavya2). He has submitted code in many languages, so you'll most likely find a solution in the language of your choice. +Also, Codechef has [screencasts](https://www.youtube.com/playlist?list=PLi0ZM-RCX5ntfqwXRirwA_pcufHinjllG) explaining this problem in C, C++ and Java. + +After this, you can move on to more problems from [Codechef's beginner section](https://www.codechef.com/problems/school?sort_by=SuccessfulSubmission&sorting_order=desc). +These problems require only basic knowledge of programming (arrays, strings, loops) and math/logic. +The problems with the problem-code beginning with `FLOW` are particularly easy and helpful in getting acquainted with submissions on online judges. SPOJ is also a good place to start. There are problems there even for people who are new to programming. @@ -135,14 +144,14 @@ One frequently asked question is what compiler or IDE to use. If you have Linux or Mac, it is best to use: * gcc for C -* g++ for C++ -* javac for Java (both oracle and openjdk are good) +* gcc/g++ for C++ +* javac for Java (both Oracle and OpenJDK are good) If you are on windows, you might want to use an IDE. Code::Blocks is good for C and C++. There are also some online compilers available. -The most well known is [ideone](https://ideone.com/). +The most well-known is [ideone](https://ideone.com/). Codechef has an online compiler called [code-compile-run](https://www.codechef.com/ide). ## Standard library @@ -165,5 +174,6 @@ cplusplus.com has better explanations for these topics. Some people realize very late that C++ offers a [sort function](http://www.cplusplus.com/reference/algorithm/sort/). This is one of the most used functions in competitive programming. +Links to documentation of other languages: * [Python library reference](https://docs.python.org/3/library/index.html) * [Java library reference](https://docs.oracle.com/javase/8/docs/api/overview-summary.html) diff --git a/CPSIG.md b/competitive-programming/CPSIG.md similarity index 100% rename from CPSIG.md rename to competitive-programming/CPSIG.md diff --git a/competitive-programming/c2cpp.md b/competitive-programming/c2cpp.md new file mode 100644 index 0000000..2a37ab4 --- /dev/null +++ b/competitive-programming/c2cpp.md @@ -0,0 +1,185 @@ +# C to C++ for competitive + +Author: [Divesh Uttamchandani](https://github.com/diveshuttam) +Contact: diveshuttamchandani@gmail.com + +This article was written keeping competitive in mind and not for using C++ in development or any other context. +The aim here is to get started with important features of C++ as quickly as possible, so skips many details. +Most of the C++ features mentioned here work on C++98 and after but some work only after C++11 so for simplicity we will assume to be using C++11. +Also, we will be using GCC/G++ compiler. +You can also use an IDE like [Code::Blocks](http://www.codeblocks.org/downloads/26) it supports GCC and G++ compiler (Windows users need to download the binary with MinGW). +Remember to enable C++11 or C++14 option in Code::Blocks' settings. + +In this article I have primarily covered 2 things: +1) Porting C's code to C++ +2) A couple of things new/different in C++ w.r.t C and are useful for competitive. + +## Prerequisite + +We require that you are aware of at least the following concepts in C: + +* Basic Syntax of C + * Basic Input and Output + * Operators +* Loops +* Arrays/Strings +* Functions +* Structures +* Pointers + +If you don't know any of the above things, it's better to have a recap first and then come back to this article. +We WON'T be requiring concepts like `enum`, `union`, `static`, file I/O etc. +As long as you are familiar with the ones above you are good to go. + +## Porting C Code + +Once you are familiar with C its easy to migrate to and learn C++. +First of all, since C++ is the superset of C, you can use most of the concepts of C in C++. +All of the things listed in prerequisites remain same in C++. + +You can use C's `scanf` and `printf` in C++ also; they are defined in header file ``. +Similarly, string functions are defined in header file ``, math functions in ``. +We will be using the line `#include` in our code to resolve all issues at once. +`` is basically a header file containing all the standard C++ header files (header files for C/C++ , most STL files etc.). +Though it's not a good development practice to include unnecessary headers. It is pretty common in competitive, +as judges use runtime for the time limit and this only increases my compile time and not my runtime. +For more details, you can refer to [GeeksforGeeks](http://www.geeksforgeeks.org/bitsstdc-h-c/) + +Also, we will add another line to our code- `using namespace std;`. +The reason here is related to namespaces but ignore this for now. +However if curious, refer to the links at the [end](#Namespaces) (NOT a recommended read for beginners). + +Another point to keep in mind is that in C++ the return type of main() has to be int. + +Keeping above points in mind, here is a sample program: + +```cpp +#include //includes all header files +using namespace std; + +int main() +{ + int t; + scanf("%d",&t); + printf("%d",t); + printf("Hello World") + return 0; +} +``` +To compile this program on linux, use `gcc --std=c++11 filename.cpp -o example` to run use `./example`. +For Code::Blocks use `build and run` in the build menu. + +## New in C++ (useful for competitive) + +### Easier I/O (cin and cout) + +Note: You can refer to [this tutorial](http://www.cplusplus.com/doc/tutorial/basic_io/) for a better overview of I/O in C++, +Just to minimize the number of jumps I have included their basic usage and some useful details here. + +In C++ the input and output are syntactically a bit easier than in C ; for example, refer to the following code: + +```cpp +#include +using namespace std; + +int main() +{ + int t; + cin>>t; + cout<` here is the header file for `cin` and `cout`. + +If I write `cin>>a;` remember that the input is generally broken at whitespaces(including tabs, newline, space) +i.e consider the following program for example: + +```cpp +#include +using namespace std; + +int main() +{ + int a,b; + char Str[10]; + + cin>>a>>b>>Str; + cout<<"a="<>a>>b>>c;` is es exactly same as `cin>>a;` `cin>>b;` `cin>>c;`. +Similarly for `cout`. +Now if I give the following input: + +``` +1 2 +Hello World +``` +In the real scenario, I will press 1 'space' 2 'enter' Hello 'space' World 'enter' +The output of the above program is +``` +a=1 +b=2 +c=Hello +``` +Note: +1. Though the input is broken at whitespaces, `cin>>something;` is not executed while you are typing the input. It is only executed after pressing enter. +That makes us type in `Hello world` without reaching `return 0;`. +2. Multiple whitespaces between the input are ignored while using cin. +3. In case of type `char`, the input is broken at each character itself. Though for type char* / char[] input is again broken at spaces. +Coding a few sample programs will help in understanding the working of `cin` in more detail. +4. Although `cin` and `cout` are easier than `scanf` and `printf`, as such they are relatively slow. This may lead to TLE on certain problems. +There are 2 ways to resolve this + 1. Use `scanf` and `printf` instead. + 2. Add the line `ios::sync_with_stdio(false)` in begining of `main()` and then use `cin` and `cout` ONLY. For more details refer to [this quora question](https://www.quora.com/Is-cin-cout-slower-than-scanf-printf) + + +### Standard Template Library +C++ has many new features including classes and templates. +Although these are great and interesting concepts, for most of our part we can work without knowing their details. +The main advantage of C++ for competitive is its extensive range of inbuilt "functions" offered through Standard Template Library (STL). +These include implementations of various Algorithms (sorting, binary search etc.) and Data Structures(stacks, queues, hash map etc.). + +Although STL is built upon templates, one doesn't need to know much of templates/classes to use it. +Just a basic overview of __What is a class__ and __What is a template__ will suffice. +You may refer to the links given at the end for an introduction to these concepts. They are enough for our purposes. +Concepts related to OOP like inheritance, polymorphism etc. are never used. +I recommend going through these links about classes and templates after you have read the tutorials linked in the paragraph below. +This is to ensure that you have a bit of a context of their actual usage. + +A good resource for getting started with STL is Topcoder tutorials +([PART I](https://www.topcoder.com/community/data-science/data-science-tutorials/power-up-c-with-the-standard-template-library-part-1/) | +[PART II](https://www.topcoder.com/community/data-science/data-science-tutorials/power-up-c-with-the-standard-template-library-part-2/) ). +They teach how to use STL in context of competitive. +Since STL was added later to C++, Its syntax may seem a bit unusual at first. +It gets easier after you use it in a few programs. +There is no need to remember the exact prototype of functions in STL as you can always refer to the following links: +* [STL Docs](https://www.sgi.com/tech/stl/) +* [cppreference](http://en.cppreference.com/w/) +* [cplusplus.com](http://www.cplusplus.com/reference/stl/) + +Even in final rounds of prestigious contests, access to some documentation is always provided. +Once you have some idea of STL, you can read the [links about classes and templates](classes-and-templates). +Then have a look at the Topcoder tutorials again. This generally helps in getting started easily. +After this, you are pretty much dependent on documentation and google searches + +#### Few google searches which may be helpful in learning more new features: + +* auto specifier C++11 +* range-based loop C++11 +* strings in C++ +* unordered map in C++ + +#### Namespaces + +http://www.geeksforgeeks.org/namespace-in-c/ + +#### Classes and Templates + +http://www.geeksforgeeks.org/c-classes-and-objects/ +http://www.geeksforgeeks.org/templates-cpp/ \ No newline at end of file diff --git a/intro.md b/intro.md new file mode 100644 index 0000000..0ee8c85 --- /dev/null +++ b/intro.md @@ -0,0 +1,20 @@ +This file has links for various articles in this repository + +## Backend +* [Learning Django](./backend/django.md) + +## Competitive +* [Starting out with Competitive Programming](./competitive-programming/CP1.md) +* [ACM-CPSIG session 2015](./competitive-programming/CPSIG.md) +* [C to C++ for Competitive](./competitive-programming/c2cpp.md) + +## Linux +* [Summary of the introductory session on Linux 2015](./linux/session-summary.md) +* Using Apt + * [Part 1](./linux/package-management/p1.md) + * [Part 2](./linux/package-management/p2.md) + * [Part 3](./linux/package-management/p3.md) + +## Tools +* [Tools for developers](./tools/tools1.md) +* [Git](./tools/git.md) diff --git a/git.md b/tools/git.md similarity index 100% rename from git.md rename to tools/git.md diff --git a/tools.md b/tools/tools1.md similarity index 100% rename from tools.md rename to tools/tools1.md