-
Notifications
You must be signed in to change notification settings - Fork 272
Adding Elixir, src/READMEs, & updated README.md #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a335d11
e29abf9
ba92e76
1d657d1
78f0b78
13ad0c3
a7d0aee
9c70cc1
7125f14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
## C Language Basic Syntax Review | ||
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language. | ||
|
||
Key features: | ||
- Structured Programming | ||
- Popular system programming language | ||
- UNIX, MySQL and Oracle are completely written in C. | ||
- Supports variety of platforms | ||
- Efficient and also handle low-level activities. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we update this to, 'Efficient and can handle low-level activities.'? |
||
- As fast as assembly language and hence used as system development language. | ||
|
||
|
||
#### 1. Read inputs | ||
````c | ||
#include <stdio.h> | ||
int main() | ||
{ | ||
char name[50]; | ||
printf("Enter name:"); | ||
scanf("%s", &name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, here, I think we would need to update this to '%49s' to limit the buffer as only '%s' could lead to a buffer overflow. As this was an introductory example, perhaps we should change the example? |
||
printf("Hello %s" , name ); | ||
return 0; | ||
|
||
} | ||
```` | ||
#### Loops and Conditionals | ||
|
||
##### 2. If-Else: | ||
|
||
When ever you want to perform a set of operations based on a condition if-else is used. | ||
```c | ||
if(conditional-expression) { | ||
// code | ||
} else { | ||
// code | ||
} | ||
``` | ||
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable. | ||
|
||
##### 3. Switch: | ||
|
||
Switch is an alternative to if-else-if ladder. | ||
|
||
````c | ||
switch(conditional-expression) { | ||
case value1: | ||
// code | ||
break; // optional | ||
case value2: | ||
// code | ||
break; // optional | ||
... | ||
|
||
default: | ||
// code to be executed when all the above cases are not matched; | ||
} | ||
```` | ||
|
||
##### 4. For: | ||
|
||
For loop is used to iterate a set of statements based on a condition. | ||
|
||
````c | ||
for(Initialization; Condition; Increment/decrement){ | ||
// code | ||
} | ||
```` | ||
|
||
##### 5. While: | ||
|
||
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance. | ||
|
||
````c | ||
while(condition) { | ||
// code | ||
} | ||
```` | ||
|
||
##### 6. Do-While: | ||
|
||
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once. | ||
|
||
````c | ||
do { | ||
// code | ||
} while (condition); | ||
|
||
```` | ||
#### 7. Arrays | ||
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1. | ||
|
||
1. One dimentional Array: ` data-type array-name[size];` | ||
2. Two dimensional array: `data-type array-name[size][size];` | ||
|
||
#### 8. Functions | ||
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. | ||
|
||
Two types of functions are present in C | ||
|
||
1. Library Functions: Library functions are the in-built functions which are declared in header files like `printf()`, `scanf()`, `puts()`, `gets()` etc., | ||
2. User defined functions: User defined functions are the ones which are written by the programmer based on the requirement. | ||
|
||
|
||
##### 9. How to declare a Function | ||
```c | ||
return_type function_name(parameters);` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we please delete the ` at the end of this line? |
||
``` | ||
##### How to call a Function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we follow the same pattern as above? We were at number 9 in 'How to declare a function'. |
||
```c | ||
function_name (parameters) | ||
``` | ||
|
||
##### How to define a FunctionHow to define a Function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we follow the same pattern as above? We were at number 9 in 'How to declare a function'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, 'How to define a Function' is repeated here. |
||
```c | ||
return_type function_name(parameters) { | ||
//code | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
## C++ Language Basic Syntax Review | ||
C++ is one of the most popular programming languages. C++ can be found in todays operating systems, Graphical User Interfaces, and embedded systems. C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs. Note, C++ is an extension of C. | ||
|
||
Key features: | ||
|
||
- Supports different platforms like Windows, various Linux flavours, MacOS etc | ||
- C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction. | ||
- Case-sensitive | ||
- C++ is a compiler based language | ||
- C++ supports structured programming language | ||
- C++ provides alot of inbuilt functions and also supports dynamic memory allocation. | ||
- Like C, C++ also allows you to play with memory using Pointers. | ||
|
||
|
||
#### 1. Read inputs | ||
````cpp | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
string name; | ||
cout << "Enter name:"; | ||
getline (cin, name); | ||
cout << "Hello " << name; | ||
return 0; | ||
} | ||
} | ||
```` | ||
#### Loops and Conditionals | ||
|
||
##### 2. If-Else: | ||
|
||
When ever you want to perform a set of operations based on a condition if-else is used. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we please update the word 'When ever' to 'Whenever' please? |
||
```cpp | ||
if(conditional-expression) { | ||
// code | ||
} else { | ||
// code | ||
} | ||
``` | ||
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable. | ||
|
||
##### 3. Switch: | ||
|
||
Switch is an alternative to if-else-if ladder. | ||
|
||
````cpp | ||
switch(conditional-expression) { | ||
case value1: | ||
// code | ||
break; // optional | ||
case value2: | ||
// code | ||
break; // optional | ||
... | ||
|
||
default: | ||
// code to be executed when all the above cases are not matched; | ||
} | ||
```` | ||
|
||
##### 4. For: | ||
|
||
For loop is used to iterate a set of statements based on a condition. | ||
|
||
````cpp | ||
for(Initialization; Condition; Increment/decrement){ | ||
// code | ||
} | ||
```` | ||
|
||
##### 5. While: | ||
|
||
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance. | ||
|
||
````cpp | ||
while(condition) { | ||
// code | ||
} | ||
```` | ||
|
||
##### 6. Do-While: | ||
|
||
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once. | ||
|
||
````cpp | ||
do { | ||
// code | ||
} while (condition); | ||
|
||
```` | ||
#### 7. Arrays | ||
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1. | ||
|
||
1. One dimentional Array: ` data-type array-name[size];` | ||
2. Two dimensional array: `data-type array-name[size][size];` | ||
|
||
#### 8. Functions | ||
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. | ||
|
||
Two types of functions are present in C++ | ||
|
||
1. Library Functions: Library functions are the in-built functions which are declared in header files like `floor(x)`, `tolower(x)`, `toupper()` etc., | ||
2. User defined functions: User defined functions are the ones which are written by the programmer based on the requirement. | ||
|
||
|
||
##### How to declare a Function | ||
```cpp | ||
return_type function_name(parameters);` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as C page. Can we please delete the ` at the end of this line? |
||
``` | ||
##### How to call a Function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we follow the same pattern as you started in C? We stopped at number 8 in this readme 'Functions'. |
||
```cpp | ||
function_name (parameters) | ||
``` | ||
|
||
##### How to define a FunctionHow to define a Function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we follow the same pattern as you started in C? We stopped at number 8 in this readme 'Functions'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate 'How to define a Function."' |
||
```cpp | ||
return_type function_name(parameters) { | ||
//code | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Module to calculate an approximation of Pi | ||
defmodule PiApproximation do | ||
|
||
# Function to calculate pi given number of terms | ||
def calculate_pi(terms) do | ||
|
||
# Initialize pi accumulator | ||
pi = 0.0 | ||
|
||
# Reduce over 1..terms range | ||
# Accumulator is a tuple with pi, denominator, and operation | ||
Enum.reduce(1..terms, {pi, 1.0, 1.0}, fn _, {acc_pi, acc_denom, acc_op} -> | ||
|
||
# Calculate new pi value | ||
new_pi = acc_pi + acc_op * (4.0 / acc_denom) | ||
|
||
# Return tuple with updated values | ||
{new_pi, acc_denom + 2.0, -acc_op} | ||
end) |> elem(0) | ||
end | ||
|
||
end | ||
|
||
# Number of terms to approximate | ||
terms = 100_000 | ||
|
||
# Call approximation function | ||
result = PiApproximation.calculate_pi(terms) | ||
|
||
# Print result | ||
IO.puts(result) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Factorial do | ||
# Define a module called Factorial. | ||
|
||
def factorial(n) when n >= 0 do | ||
# Calculate the factorial using Enum.reduce. | ||
Enum.reduce(1..n, 1, &(&1 * &2)) | ||
end | ||
|
||
defp format_factorial(i, result) do | ||
# Format the output string. | ||
"#{i}! = #{result}" | ||
end | ||
|
||
def main do | ||
# Entry point of the program. | ||
nums = [0, 1, 2, 3, 4, 5] | ||
|
||
Enum.each(nums, fn i -> | ||
# Iterate through the list of numbers. | ||
result = factorial(i) | ||
IO.puts(format_factorial(i, result)) | ||
end) | ||
end | ||
end | ||
|
||
Factorial.main() # Execute the program. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Define Math module for math utilities | ||
defmodule Math do | ||
|
||
# Recursive factorial function | ||
def factorial(0), do: 1 # Base case | ||
|
||
def factorial(n) when n > 0 do | ||
# Recursive case | ||
n * factorial(n-1) | ||
end | ||
|
||
end | ||
|
||
# Array of numbers to get factorials for | ||
nums = [0, 1, 2, 3, 4, 5] | ||
|
||
# Loop through the nums array | ||
for i <- nums do | ||
|
||
# Calculate and print factorial | ||
IO.puts "#{i}! = #{Math.factorial(i)}" | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Define a Fibonacci module | ||
defmodule Fib do | ||
|
||
# Function to calculate nth Fibonacci number | ||
def fibonacci(n) when n >= 0 do | ||
|
||
# Initialize starting values | ||
last = 0 | ||
curr = 1 | ||
|
||
# Use Enum.reduce as a loop from 0 to n-1 | ||
Enum.reduce(0..(n-1), {last, curr}, fn _, {last, curr} -> | ||
|
||
# Update last and curr, returning as a tuple | ||
{curr, curr + last} | ||
end) | ||
|
||
# Return the last value after reducing | ||
|> elem(0) | ||
end | ||
end | ||
|
||
# Print 10th Fibonacci number | ||
IO.puts(Fib.fibonacci(10)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Define a Fibonacci module | ||
defmodule Fibonacci do | ||
# Base case: If n is 0 or 1, return n. | ||
def fibonacci(n) when n <= 1, do: n | ||
|
||
# Recursive case: Calculate the Fibonacci number by summing the results | ||
# of the previous two Fibonacci numbers. | ||
def fibonacci(n) do | ||
fibonacci(n - 1) + fibonacci(n - 2) | ||
end | ||
end | ||
|
||
# Print the Fibonacci number for n = 10. | ||
IO.puts("Fibonacci(n): #{Fibonacci.fibonacci(10)}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we update 'most popular general-purpose programming language' to 'most popular general-purpose programming languages'?
Also, can we add a 'the' before 'UNIX operating system'?