forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into nehaNIT_hello_world_in_java
- Loading branch information
Showing
19 changed files
with
506 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <bits/stdc++.h> | ||
#define ll long long | ||
|
||
using namespace std; | ||
vector<ll> gr[1010]; | ||
|
||
void bfs(ll u,ll V,bool vis[]) | ||
{ | ||
|
||
|
||
queue<ll> q; | ||
vis[u]=true; | ||
q.push(u); | ||
while(!q.empty()) | ||
{ | ||
ll ver=q.front(); | ||
q.pop(); | ||
cout<<ver<<" "; | ||
for(ll i=0;i<gr[ver].size();i++) | ||
if(!vis[gr[ver][i]]){ | ||
vis[gr[ver][i]]=true; | ||
q.push(gr[ver][i]); | ||
} | ||
} | ||
|
||
|
||
} | ||
void bfsMain(ll src,ll V) | ||
{ | ||
bool vis[V]; | ||
fill(vis,vis+V,false); | ||
bfs(src,V,vis); | ||
for(ll i=0;i<V;i++) | ||
if(!vis[i]) | ||
bfs(i,V,vis); | ||
} | ||
int main() | ||
{ | ||
ll v,e; | ||
cin>>v>>e; | ||
while(e--) | ||
{ | ||
ll u,v; | ||
cin>>u>>v; | ||
gr[u].push_back(v); | ||
gr[v].push_back(u); | ||
|
||
} | ||
ll src; | ||
cin>>src; | ||
bfsMain(src,v); | ||
return 0; | ||
} | ||
/* | ||
8 9 | ||
0 1 | ||
0 2 | ||
1 3 | ||
2 4 | ||
1 4 | ||
3 4 | ||
3 5 | ||
4 5 | ||
6 7 | ||
0 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Bubble Sort in C++ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() { | ||
|
||
int array[] = {10, 6, 56, 45, 37, 7, 23}; // Array to sort | ||
int size = sizeof(array) / sizeof(array[0]); | ||
|
||
while (true) { | ||
bool sorted = true; | ||
for (int i = 0; i < size; i++) { | ||
if (array[i] > array[i + 1]) { | ||
int temp = array[i]; | ||
array[i] = array[i + 1]; | ||
array[i + 1] = temp; | ||
sorted = false; | ||
} | ||
} | ||
if (sorted) break; | ||
} | ||
|
||
// Printing sorted array | ||
for (int i = 0; i < size; i++) | ||
cout << array[i] << " "; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'''Bubble Sort in Python''' | ||
|
||
def bubble_sort(alist): | ||
for i in range(len(alist)-1,0,-1): | ||
for j in range(i): | ||
if alist[j]>alist[j+1]: | ||
temp = alist[j] | ||
alist[j] = alist[j+1] | ||
alist[j+1] = temp | ||
|
||
if __name__ == '__main__': | ||
#Sample data for sorting. | ||
ALIST = [54, 26, 93, 17, 77, 31, 44, 55, 20] | ||
bubble_sort(ALIST) | ||
print(ALIST) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using namespace std; | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
int BirthMonth, BirthYear; | ||
int CurrentMonth, CurrentYear; | ||
int AgeYear, AgeMonth; | ||
|
||
cout<<"\n\n\t\t\tCount the age person\n\n"; | ||
cout<<"Enter Your Birth Year(Eg:1989):"; | ||
cin>>BirthYear; | ||
|
||
cout<<"\n\nEnter Your Birth Month(Eg:7):"; | ||
cin>>BirthMonth; | ||
|
||
cout<<"\nEnter The Current Month(Eg:7):"; | ||
cin>>CurrentMonth; | ||
|
||
cout<<"\nEnter The Current Year(Eg:2010):"; | ||
cin>>CurrentYear; | ||
|
||
AgeYear=CurrentYear-BirthYear; | ||
AgeMonth=12-BirthMonth; | ||
|
||
cout<<"\n\n\t\tYour Age is "<<AgeYear<<" Years And "<<AgeMonth<<" Months "; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <bits/stdc++.h> | ||
#define ll long long | ||
|
||
using namespace std; | ||
vector<ll> gr[1010]; | ||
|
||
void bfs(ll u,ll V,bool vis[]) | ||
{ | ||
|
||
|
||
queue<ll> q; | ||
vis[u]=true; | ||
q.push(u); | ||
while(!q.empty()) | ||
{ | ||
ll ver=q.front(); | ||
q.pop(); | ||
cout<<ver<<" "; | ||
for(ll i=0;i<gr[ver].size();i++) | ||
if(!vis[gr[ver][i]]){ | ||
vis[gr[ver][i]]=true; | ||
q.push(gr[ver][i]); | ||
} | ||
} | ||
|
||
|
||
} | ||
void bfsMain(ll src,ll V) | ||
{ | ||
bool vis[V]; | ||
fill(vis,vis+V,false); | ||
bfs(src,V,vis); | ||
for(ll i=0;i<V;i++) | ||
if(!vis[i]) | ||
bfs(i,V,vis); | ||
} | ||
int main() | ||
{ | ||
ll v,e; | ||
cin>>v>>e; | ||
while(e--) | ||
{ | ||
ll u,v; | ||
cin>>u>>v; | ||
gr[u].push_back(v); | ||
gr[v].push_back(u); | ||
|
||
} | ||
ll src; | ||
cin>>src; | ||
bfsMain(src,v); | ||
return 0; | ||
} | ||
/* | ||
8 9 | ||
0 1 | ||
0 2 | ||
1 3 | ||
2 4 | ||
1 4 | ||
3 4 | ||
3 5 | ||
4 5 | ||
6 7 | ||
0 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Depth First Traversal | ||
|
||
Depth First Traversal for a graph is similar to Depth First Traversal of a tree. | ||
The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. | ||
To avoid processing a node more than once, we use a boolean visited array. | ||
|
||
The function dfsMain is used for cases when all vertices are not reachable from the starting vertex. | ||
|
||
Time Complexity: O(V+E) where V is number of vertices in the graph and E is number of edges in the graph. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#RSA encryption algorithm | ||
|
||
def main(): | ||
n = e = d = 0 | ||
while 1: | ||
print(""" | ||
1. Set Public Key | ||
2. Encode | ||
3. Decode | ||
0. Quit | ||
Your choice? """, end = "") | ||
choice = int(input()) | ||
if not choice: | ||
return | ||
if choice == 1: | ||
n, e, d = set_keys() | ||
if choice == 2: | ||
if not n: | ||
n = int(input("Public Key: ")) | ||
e = int(input("e: ")) | ||
encode(n, e) | ||
if choice == 3: | ||
if not d: | ||
n, e, d = set_keys() | ||
decode(d, n) | ||
|
||
def set_keys(): | ||
"""This fuction asks for 2 primes. | ||
It sets a public key and an encoding number, 'e'.""" | ||
p = int(input("prime(p): ")) | ||
q = int(input("prime(q): ")) | ||
n = p * q | ||
m = (p - 1) * (q - 1) | ||
e = get_e(m) | ||
print("N = ", n, "\ne = ", e) | ||
d = get_d(e, m) | ||
while d < 0: | ||
d += m | ||
return [n, e, d] | ||
|
||
def encode(n, e): | ||
"""This function asks for a number and encodes it using 'n' and 'e'.""" | ||
while 1: | ||
c = int(input("Number to encode: ")) | ||
if not c: | ||
return | ||
print(pow(c, e, n)) | ||
|
||
def decode(d, n): | ||
"""This function asks for a number and decodes it using 'd' and 'n'.""" | ||
while 1: | ||
c = int(input("Number to decode: ")) | ||
if not c: | ||
return | ||
else: | ||
print(pow(c, d, n)) | ||
|
||
def even(x): | ||
"""True if x is even.""" | ||
return x % 2 == 0 | ||
|
||
def get_e(m): | ||
"""Finds an e coprime with m.""" | ||
e = 2 | ||
while gcd(e, m) != 1: | ||
e += 1 | ||
return e | ||
|
||
def gcd(a,b): | ||
"""Euclid's Algorithm: Takes two integers and returns gcd.""" | ||
while b > 0: | ||
a, b = b, a % b | ||
return a | ||
|
||
def get_d(e, m): | ||
"""Takes encoding number, 'e' and the value for 'm' (p-1) * (q-1). | ||
Returns a decoding number.""" | ||
x = lasty = 0 | ||
lastx = y = 1 | ||
while m != 0: | ||
q = e // m | ||
e, m = m, e % m | ||
x, lastx = lastx - q*x, x | ||
y, lasty = lasty - q*y, y | ||
return lastx | ||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## The Fibonacci Sequence | ||
### Algorithm | ||
|
||
The Fibonacci Sequence is the series of numbers: | ||
|
||
`0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...` | ||
|
||
The next number is found by adding up the two numbers before it. | ||
|
||
* The 2 is found by adding the two numbers before it (1+1) | ||
* The 3 is found by adding the two numbers before it (1+2), | ||
* And the 5 is (2+3), | ||
* and so on! | ||
|
||
### Output of the program | ||
* `Enter the number of terms: 10` | ||
* `Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stdio.h> | ||
int main() | ||
{ | ||
int i, n, t1 = 0, t2 = 1, nextTerm; | ||
|
||
|
||
printf("Enter the number of terms: "); | ||
scanf("%d", &n); | ||
|
||
|
||
printf("Fibonacci Series: "); | ||
|
||
|
||
for (i = 1; i <= n; ++i) | ||
{ | ||
printf("%d, ", t1); | ||
nextTerm = t1 + t2; | ||
t1 = t2; | ||
t2 = nextTerm; | ||
} | ||
return 0; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func fibonacci() func() int { | ||
a, b := 0, 1 | ||
return func() int { | ||
a = a + b | ||
a, b = b, a | ||
return a | ||
} | ||
} | ||
|
||
func main() { | ||
var n int | ||
fmt.Print("Enter value: ") | ||
fmt.Scanf("%d", &n) | ||
|
||
f := fibonacci() | ||
var fib int | ||
for i := 0; i < n; i++ { | ||
fib = f() | ||
fmt.Printf("%d, ", fib) | ||
} | ||
fmt.Printf("\nfib(%d) = %d\n", n, fib) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.