From 35f9f75fd8c4039ff30456d53700fdb7ebd50823 Mon Sep 17 00:00:00 2001 From: Divesh Uttamchandani Date: Sat, 23 Sep 2017 05:32:08 +0000 Subject: [PATCH 1/6] organized content into directories rename tools.md to tools1.md --- django.md => backend/django.md | 0 CP1.md => competitive-programming/CP1.md | 0 CPSIG.md => competitive-programming/CPSIG.md | 0 git.md => tools/git.md | 0 tools.md => tools/tools1.md | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename django.md => backend/django.md (100%) rename CP1.md => competitive-programming/CP1.md (100%) rename CPSIG.md => competitive-programming/CPSIG.md (100%) rename git.md => tools/git.md (100%) rename tools.md => tools/tools1.md (100%) 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 100% rename from CP1.md rename to competitive-programming/CP1.md 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/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 From 7366dd31858ff1fb4c0abd355c34b0e071168d44 Mon Sep 17 00:00:00 2001 From: Divesh Uttamchandani Date: Sat, 23 Sep 2017 05:43:17 +0000 Subject: [PATCH 2/6] intro.md added --- intro.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 intro.md 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) From 6879163cb10304f9c813421fbb888b873c6914ad Mon Sep 17 00:00:00 2001 From: Divesh Uttamchandani Date: Sat, 23 Sep 2017 05:46:25 +0000 Subject: [PATCH 3/6] c to cpp guide added - c2cpp.md --- competitive-programming/c2cpp.md | 165 +++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 competitive-programming/c2cpp.md diff --git a/competitive-programming/c2cpp.md b/competitive-programming/c2cpp.md new file mode 100644 index 0000000..18747b4 --- /dev/null +++ b/competitive-programming/c2cpp.md @@ -0,0 +1,165 @@ +# C to C++ for competitive + +Author: [Divesh Uttamchandani](https://github.com/diveshuttam) + +This article was written keeping competetive 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 talked 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 it already supports GCC and G++ compilers (MINGW); remember to enable C++11 or C++14 option in Code::Blocks' settings. + +Once you are familiar with C its easy to migrate to and learn C++. +First of all since C++ is superset of C so you can use most of the concepts of C in C++. +So I will first mention about porting code and then a couple of things new in C++ 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, its better to have a recap first and than 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 + +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 again its not a good developement practice to include unnecesary headers but it is pretty common in competitive, +as judges use runtime for time limit and this only increases my compile time and not my runtime. +For more details you can refer to [geekforgeeks](http://www.geeksforgeeks.org/bitsstdc-h-c/) + +Also we will add another line to our code- `using namespace std;` . +Reason here is related to namespaces but ignore this for now. +If curious refer to the links at the end. NOT a reccommended read for beginners. + +Another point to keep in mind is that in C++ the return type of main() has to be int. + +Keeping above 2 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 codeblocks use `build and run` in 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 overwiew of I/O in C++, +Just to minimize the number of jumps I have included their basic usage and some useful details for them. + +In C++ the input and output are syntactically a bit easier than in C. For example refer to the following code. + +```cpp +#include +using namespase std; + +int main() +{ + int t; + cin>>t; + cout<> and << . +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;` this is known as cascading of `>>` operator similarly we have the next line where `<<` operator is cascaded. +Now if I give the following input: + +``` +1 2 +Hello World +``` +in real senerio 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 white spaces but `cin>>something;` is not executed while you are typing the input it is only executed after you press enter. +That makes me type in `Hello world` without reaching `return 0;` . +2. Multiple whitespace 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 agan broken at spaces. +Coding a few sample programs will help in understanding the working of cin in more detail. +4. Though cin and cout are useful but as such they are slow and can lead to TLE on certain problems. +The way to resolve this is to either use `scanf` and `printf` or 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) + + +### STL +C++ has many new features including classes and templates. +Although these are great and interesting concepts but we can work without knowing many of their details for most of our part in competitive. +The main advantage of c++ for competitive is its extensive range of inbuilt "functions" to use algorithms and Data Structures through Standard Template Library (STL). +Although STL is built upon templates, but one need not be knowing 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 to read about these concepts. +They are enough for our purposes (Concepts related to oop like (inheritance, polymorpism etc) are never used). +Also I recommend going through these links about classes and templates after you have read the tutorials linked in the paragraph below as you will have a bit of context of their actual usage. + +A good resource for getting started with STL are 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. +But 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 [STL Docs](https://www.sgi.com/tech/stl/) (link to official docs of HP's implementation of STL which is amongst the most popular ones) or [cppreference](http://en.cppreference.com/w/). +Even in final rounds of prestigious contests, access to some documentation is always provided. +So after completing the two tutorials, you are pretty much dependent on documentation and Google. +Once you have some idea of STL you can go through classes and templates if you feel the need. +Though not recommended initially as the ratio of their use in competitive v/s the amount of time they can potentially consume is not that good. +Most of the features of classes and templates are not required for using STL. + +#### Few google searches which may be helpful in learning some 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/ +https://stackoverflow.com/questions/10460250/cstdio-stdio-h-namespace + +#### Classes and Templates + +http://www.geeksforgeeks.org/c-classes-and-objects/ +http://www.geeksforgeeks.org/templates-cpp/ From bac4809eace42cafb1a02fcbefe4d3348acb6949 Mon Sep 17 00:00:00 2001 From: Divesh Uttamchandani Date: Sat, 23 Sep 2017 05:49:56 +0000 Subject: [PATCH 4/6] few additions in CP1.md minor changes in CP1.md --- competitive-programming/CP1.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/competitive-programming/CP1.md b/competitive-programming/CP1.md index 3688f53..ec27cbb 100644 --- a/competitive-programming/CP1.md +++ b/competitive-programming/CP1.md @@ -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. @@ -79,6 +83,11 @@ the easiest problem on Codechef - [Life, the Universe, and Everything](https://w 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 code beginning with `FLOW` are particularly easy and helpful in getting aquainted 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,7 +144,7 @@ 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++ +* 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. @@ -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) From 1dff54f643ca205ccbf4729e328e85a11523d6d6 Mon Sep 17 00:00:00 2001 From: Divesh Uttamchandani Date: Mon, 25 Sep 2017 14:57:30 +0000 Subject: [PATCH 5/6] reorganized a few details in c2cpp.md --- competitive-programming/c2cpp.md | 103 +++++++++++++++++++------------ 1 file changed, 63 insertions(+), 40 deletions(-) diff --git a/competitive-programming/c2cpp.md b/competitive-programming/c2cpp.md index 18747b4..29e4d42 100644 --- a/competitive-programming/c2cpp.md +++ b/competitive-programming/c2cpp.md @@ -1,15 +1,18 @@ # C to C++ for competitive Author: [Divesh Uttamchandani](https://github.com/diveshuttam) +Contact: diveshuttamchandani@gmail.com -This article was written keeping competetive in mind and not for using C++ in development or any other context. +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 talked 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 it already supports GCC and G++ compilers (MINGW); remember to enable C++11 or C++14 option in Code::Blocks' settings. +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 it already supports GCC and G++ compilers (MINGW). +Remember to enable C++11 or C++14 option in Code::Blocks' settings. -Once you are familiar with C its easy to migrate to and learn C++. -First of all since C++ is superset of C so you can use most of the concepts of C in C++. -So I will first mention about porting code and then a couple of things new in C++ useful for Competitive. +In this article I have primiraly 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 @@ -30,21 +33,25 @@ As long as you are familiar with the ones above you are good to go. ## Porting C Code -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 . +Once you are familiar with C its easy to migrate to and learn C++. +First of all, since C++ is 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 again its not a good developement practice to include unnecesary headers but it is pretty common in competitive, +Though its not a good developement practice to include unnecesary headers but it is pretty common in competitive, as judges use runtime for time limit and this only increases my compile time and not my runtime. For more details you can refer to [geekforgeeks](http://www.geeksforgeeks.org/bitsstdc-h-c/) Also we will add another line to our code- `using namespace std;` . Reason here is related to namespaces but ignore this for now. -If curious refer to the links at the end. NOT a reccommended read for beginners. +However if curious, refer to the links at the [end](#Namespaces) (NOT a reccommended read for beginners). Another point to keep in mind is that in C++ the return type of main() has to be int. -Keeping above 2 points in mind here is a sample program: +Keeping above points in mind, here is a sample program: ```cpp #include //includes all header files @@ -59,16 +66,17 @@ int main() return 0; } ``` -To compile this program on linux use `gcc --std=c++11 filename.cpp -o example` to run use `./example`. For codeblocks use `build and run` in build menu. +To compile this program on linux, use `gcc --std=c++11 filename.cpp -o example` to run use `./example`. +For codeblocks use `build and run` in 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 overwiew of I/O in C++, -Just to minimize the number of jumps I have included their basic usage and some useful details for them. +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. +In C++ the input and output are syntactically a bit easier than in C ; for example refer to the following code: ```cpp #include @@ -82,8 +90,16 @@ int main() cout<<"Hello world"; } ``` -This implements the same functionality as the sample C code before this, this is easier in the sense that you don't need to specify placeholders such as "%d" and "%f" etc. how to print is judged on the basis of type of the variable to be printed. Also iostream is the header file for cin and cout. cin and cout are not functions they may be thout as pointers pointing to keyboard and screen (standard input and standard output) respectively in a vague sense ; actual work for input and output is done by **operators** >> and << . -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 +This implements the same functionality as the sample C code before this. +It is easier in the sense that you don't need to specify placeholders such as "%d" and "%f" etc. +How to print a variable is judged on the basis of its type. +`` here is the header file for `cin` and `cout`. + +`cin` and `cout` are not functions. +They may be thought as pointers to keyboard and screen (standard input and standard output respectively) in a vague sense. +Actual work for input and output is done by **operators** >> and << . +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 @@ -99,7 +115,8 @@ int main() } ``` -the line `cin>>a>>b>>c;` is es exactly same as `cin>>a;` `cin>>b;` `cin>>c;` this is known as cascading of `>>` operator similarly we have the next line where `<<` operator is cascaded. +The line `cin>>a>>b>>c;` is es exactly same as `cin>>a;` `cin>>b;` `cin>>c;`. +This is known as cascading of `>>` operator similarly we have the next line where `<<` operator is cascaded. Now if I give the following input: ``` @@ -114,40 +131,47 @@ b=2 c=Hello ``` Note: -1. Though the input is broken at white spaces but `cin>>something;` is not executed while you are typing the input it is only executed after you press enter. -That makes me type in `Hello world` without reaching `return 0;` . +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 whitespace 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 agan broken at spaces. -Coding a few sample programs will help in understanding the working of cin in more detail. -4. Though cin and cout are useful but as such they are slow and can lead to TLE on certain problems. -The way to resolve this is to either use `scanf` and `printf` or 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) +3. In case of type `char` the input is broken at each character itself. Though for type char* / char[] input is agian 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) -### STL +### Standard Template Library C++ has many new features including classes and templates. -Although these are great and interesting concepts but we can work without knowing many of their details for most of our part in competitive. -The main advantage of c++ for competitive is its extensive range of inbuilt "functions" to use algorithms and Data Structures through Standard Template Library (STL). -Although STL is built upon templates, but one need not be knowing 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 to read about these concepts. -They are enough for our purposes (Concepts related to oop like (inheritance, polymorpism etc) are never used). -Also I recommend going through these links about classes and templates after you have read the tutorials linked in the paragraph below as you will have a bit of context of their actual usage. +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, polymorpism 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 are 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. -But 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 [STL Docs](https://www.sgi.com/tech/stl/) (link to official docs of HP's implementation of STL which is amongst the most popular ones) or [cppreference](http://en.cppreference.com/w/). +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/) (link to official docs of HP's implementation of STL which is amongst the most popular ones) +* [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. -So after completing the two tutorials, you are pretty much dependent on documentation and Google. -Once you have some idea of STL you can go through classes and templates if you feel the need. -Though not recommended initially as the ratio of their use in competitive v/s the amount of time they can potentially consume is not that good. -Most of the features of classes and templates are not required for using STL. +Once you have some idea of STL, you can read the [links about classes and templates](classes-and-templates). +And have a look at the topcoder tutorials again. +After this you are pretty much dependent on documentation and google searches -#### Few google searches which may be helpful in learning some new features: +#### Few google searches which may be helpful in learning more new features: * auto specifier C++11 * range based loop C++11 @@ -157,7 +181,6 @@ Most of the features of classes and templates are not required for using STL. #### Namespaces http://www.geeksforgeeks.org/namespace-in-c/ -https://stackoverflow.com/questions/10460250/cstdio-stdio-h-namespace #### Classes and Templates From 7dbed938c0e6d636ca0b4a29543967d909d3c4d8 Mon Sep 17 00:00:00 2001 From: Divesh Uttamchandani Date: Mon, 25 Sep 2017 15:37:18 +0000 Subject: [PATCH 6/6] minor corrections undoing certain unintended changes --- competitive-programming/CP1.md | 14 +++---- competitive-programming/c2cpp.md | 67 +++++++++++++++----------------- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/competitive-programming/CP1.md b/competitive-programming/CP1.md index ec27cbb..c355fb7 100644 --- a/competitive-programming/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. @@ -78,16 +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). +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 code beginning with `FLOW` are particularly easy and helpful in getting aquainted with submissions on online judges. +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. @@ -145,13 +145,13 @@ If you have Linux or Mac, it is best to use: * gcc for C * gcc/g++ for C++ -* javac for Java (both oracle and openjdk are good) +* 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 @@ -174,6 +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 +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/competitive-programming/c2cpp.md b/competitive-programming/c2cpp.md index 29e4d42..2a37ab4 100644 --- a/competitive-programming/c2cpp.md +++ b/competitive-programming/c2cpp.md @@ -6,11 +6,11 @@ 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 it already supports GCC and G++ compilers (MINGW). +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 primiraly covered 2 things: +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. @@ -27,27 +27,27 @@ We require that you are aware of at least the following concepts in C: * Structures * Pointers -If you don't know any of the above things, its better to have a recap first and than come back to this article. +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 superset of C, you can use most of the concepts of C in 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 ``. +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 its not a good developement practice to include unnecesary headers but it is pretty common in competitive, -as judges use runtime for time limit and this only increases my compile time and not my runtime. -For more details you can refer to [geekforgeeks](http://www.geeksforgeeks.org/bitsstdc-h-c/) +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;` . -Reason here is related to namespaces but ignore this for now. -However if curious, refer to the links at the [end](#Namespaces) (NOT a reccommended read for beginners). +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. @@ -67,20 +67,20 @@ int main() } ``` To compile this program on linux, use `gcc --std=c++11 filename.cpp -o example` to run use `./example`. -For codeblocks use `build and run` in build menu. +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 overwiew of I/O in C++, +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: +In C++ the input and output are syntactically a bit easier than in C ; for example, refer to the following code: ```cpp #include -using namespase std; +using namespace std; int main() { @@ -95,11 +95,8 @@ It is easier in the sense that you don't need to specify placeholders such as "% How to print a variable is judged on the basis of its type. `` here is the header file for `cin` and `cout`. -`cin` and `cout` are not functions. -They may be thought as pointers to keyboard and screen (standard input and standard output respectively) in a vague sense. -Actual work for input and output is done by **operators** >> and << . 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 +i.e consider the following program for example: ```cpp #include @@ -116,15 +113,15 @@ int main() ``` The line `cin>>a>>b>>c;` is es exactly same as `cin>>a;` `cin>>b;` `cin>>c;`. -This is known as cascading of `>>` operator similarly we have the next line where `<<` operator is cascaded. +Similarly for `cout`. Now if I give the following input: ``` 1 2 Hello World ``` -in real senerio I will press 1 'space' 2 'enter' Hello 'space' World 'enter' -the output of the above program is +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 @@ -132,9 +129,9 @@ 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 whitespace 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 agian broken at spaces. +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 @@ -144,37 +141,37 @@ There are 2 ways to resolve this ### 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. +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.). +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, polymorpism etc. are never used. +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 are topcoder tutorials +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/) (link to official docs of HP's implementation of STL which is amongst the most popular ones) +* [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). -And have a look at the topcoder tutorials again. -After this you are pretty much dependent on documentation and google searches +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 +* range-based loop C++11 * strings in C++ * unordered map in C++ @@ -185,4 +182,4 @@ http://www.geeksforgeeks.org/namespace-in-c/ #### Classes and Templates http://www.geeksforgeeks.org/c-classes-and-objects/ -http://www.geeksforgeeks.org/templates-cpp/ +http://www.geeksforgeeks.org/templates-cpp/ \ No newline at end of file