-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from TesseractCoding/master
mpr
- Loading branch information
Showing
8 changed files
with
357 additions
and
0 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
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,107 @@ | ||
/* | ||
Problem Statement: | ||
Given a stack, sort the given stack using recursion such that the greatest element is on the top. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <algorithm> | ||
#include <stack> | ||
|
||
using namespace std; | ||
|
||
void printStack(stack<int> st) | ||
{ | ||
while (!st.empty()) | ||
{ | ||
int x = st.top(); | ||
st.pop(); | ||
|
||
cout << x << " "; | ||
} | ||
|
||
return; | ||
} | ||
|
||
void insert(stack<int> &st, int temp) | ||
{ | ||
// base condition | ||
if (st.size() == 0 || st.top() < temp) | ||
{ | ||
st.push(temp); | ||
return; | ||
} | ||
|
||
int val = st.top(); | ||
st.pop(); | ||
|
||
insert(st, temp); | ||
st.push(val); | ||
|
||
return; | ||
} | ||
|
||
void sortStack(stack<int> &st) | ||
{ | ||
|
||
// base condition | ||
if (st.size() == 0) | ||
return; | ||
|
||
int temp = st.top(); | ||
st.pop(); | ||
|
||
// recursion | ||
sortStack(st); | ||
|
||
// function to insert element in stack | ||
insert(st, temp); | ||
|
||
return; | ||
} | ||
|
||
int main() | ||
{ | ||
// input number of element in stack | ||
int n; | ||
cin >> n; | ||
|
||
stack<int> st; | ||
|
||
// input stack elements | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int x; | ||
cin >> x; | ||
|
||
st.push(x); | ||
} | ||
|
||
// function to sort stack | ||
sortStack(st); | ||
|
||
// function to print stack element | ||
printStack(st); | ||
|
||
cout << endl; | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
Time Complexity: O(N*N) | ||
Space complexity: O(N) | ||
*/ | ||
|
||
/* | ||
Test Case: | ||
Input: 10 | ||
9 2 5 6 0 1 7 3 4 8 | ||
OutPut: 9 8 7 6 5 4 3 2 1 0 | ||
Input: 5 | ||
11 2 32 3 41 | ||
Output: 41 32 11 3 2 | ||
*/ |
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
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,98 @@ | ||
/* | ||
Problem: Solving Quadratic equations in Java- | ||
A program that prints all real solutions to quadratic equation ax^2+bx+c=0, if discriminant is negative displays a message "roots are imaginary". | ||
*/ | ||
import java.util.*; | ||
import java.io.*; | ||
|
||
public class Quadratic_Equation { | ||
public static void quadraticroots() | ||
{ | ||
// value a, b, and c | ||
double a, b, c; | ||
double root1, root2; | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter coefficients of the equation:"); | ||
System.out.print("Enter a: "); | ||
a = sc.nextDouble(); | ||
System.out.println(); | ||
System.out.print("Enter b: "); | ||
b = sc.nextDouble(); | ||
System.out.println(); | ||
System.out.print("Enter c: "); | ||
c = sc.nextDouble(); | ||
System.out.println(); | ||
// If a is 0, then equation is not quadratic, but linear | ||
if (a == 0) { | ||
System.out.println("Its a linear equation,Not a quadratic equation!"); | ||
return; | ||
} | ||
// calculate the discriminant (b^2 - 4ac) | ||
double discriminant = b * b - 4 * a * c; | ||
// discriminant is greater than 0, real and distinct roots | ||
if (discriminant > 0) { | ||
root1 = (-b + Math.sqrt(discriminant)) / (2 * a); | ||
root2 = (-b - Math.sqrt(discriminant)) / (2 * a); | ||
System.out.println("Roots are real and distinct"); | ||
System.out.format("root1 = %.2f and root2 = %.2f", root1, root2); | ||
} | ||
// discriminant is equal to 0, real and equal roots | ||
else if (discriminant == 0) { | ||
root1 = root2 = -b / (2 * a); | ||
System.out.println("Equal roots"); | ||
System.out.format("root1 = root2 = %.2f;", root1); | ||
} | ||
// discriminant is less than zero, imaginary roots | ||
else { | ||
double real = -b / (2 * a); | ||
double imaginary = Math.sqrt(-discriminant) / (2 * a); | ||
System.out.println("Imaginary roots!"); | ||
System.out.format("root1 = %.2f+%.2fi", real, imaginary); | ||
System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary); | ||
} | ||
} | ||
public static void main(String[] args) { | ||
quadraticroots(); | ||
} | ||
} | ||
|
||
/* | ||
Sample Test Cases: | ||
Test case 1: | ||
Enter coefficients of the equation: | ||
Enter a: 1 | ||
Enter b: -4 | ||
Enter c: 0 | ||
Roots are real and distinct | ||
root1 = 4.00 and root2 = 0.00 | ||
Test case 2: | ||
Enter coefficients of the equation: | ||
Enter a: 7.2 | ||
Enter b: 5 | ||
Enter c: 9 | ||
Imaginary roots! | ||
root1 = -0.35+1.06i | ||
root2 = -0.35-1.06i | ||
Test case 3: | ||
Enter coefficients of the equation: | ||
Enter a: 1 | ||
Enter b: -2 | ||
Enter c: 1 | ||
Equal roots | ||
root1 = root2 = 1.00; | ||
Time Complexity: O(1) | ||
Space Complexity: O(1) | ||
*/ |
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
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,46 @@ | ||
#= Given a number n, print its prime factorization. We could do it | ||
normal way which will be having time complexity of O (sqrt(N)) | ||
but we will be using Sieve to lessen the time complexity | ||
We will make a sieve to get all the prime numbers and then get the | ||
prime factorization of the number using that.=# | ||
|
||
## Function | ||
|
||
function PrimeFactor(n) | ||
a = zeros(Int64, n) | ||
for i = 2:n | ||
a[i] = i | ||
end | ||
for i = 2:n | ||
if (a[i] == i) | ||
for j = (i*i):i:n | ||
if (a[j] == j) | ||
a[j] = i | ||
end | ||
end | ||
end | ||
end | ||
while (n != 1) | ||
println(a[n]) | ||
n = div(n, a[n]) | ||
end | ||
end | ||
|
||
## Input | ||
|
||
n = readline() | ||
n = parse(Int64, n) | ||
|
||
#Calling the function | ||
|
||
PrimeFactor(n) | ||
|
||
#= | ||
Sample Test case | ||
Input: | ||
n = 495 | ||
Output: | ||
3 3 5 11 | ||
Time Complexity: O( log N ) | ||
=# |
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
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,102 @@ | ||
''' | ||
In rail-fence cipher, we are given a plaintext message and a | ||
numeric key. It is also called a zigzag cipher and is a form | ||
of transposition cipher. We re-arrange the order of alphabets | ||
in the plaintext to obtain the ciphertext. In this algorithm, | ||
the plaintext text is written diagonally downwards and upwards | ||
alternatively. The length of the diagonal is the keyword given. | ||
After each alphabet has been written, the individual rows are | ||
combined to obtain the cipher-text. | ||
''' | ||
|
||
import sys | ||
|
||
'''This is the encryption function, which takes plaintext | ||
and key as input. It then returns the ciphertext.''' | ||
|
||
def encryption(message, keyword): | ||
|
||
# This is the matrix to store the plaintext | ||
matrix = [[] for i in range(keyword)] | ||
|
||
i = 0 | ||
count = 0 | ||
|
||
''' After going diagonally downwards keyword number of | ||
times, we need to go diangonally upwards. This rev | ||
list contains those values after which the plaintext | ||
is to be stored diagonally upwards.''' | ||
rev = [] | ||
for j in range(len(message)): | ||
temp = keyword + j * (( 2 * keyword) - 2) | ||
if temp <= len(message): | ||
rev.append(temp) | ||
|
||
value = False | ||
|
||
while (i < keyword and count < len(message)): | ||
matrix[i].append(message[count]) | ||
count = count + 1 | ||
|
||
''' As we find the reversing point, we set value to | ||
be true. So, that we can decrease the value of list | ||
index.''' | ||
if count in rev: | ||
value = True | ||
dec = keyword - 1 | ||
|
||
''' If value is true, we decrement the list index keyword | ||
number of times''' | ||
if value and dec > 0: | ||
i = i - 1 | ||
dec = dec - 1 | ||
|
||
# Else we go on storing the plaintext in downwards direction | ||
else: | ||
i = i + 1 | ||
|
||
# This string stores the final ciphertext | ||
enc = "" | ||
|
||
# We read the value of matrix row by row to form the ciphertext | ||
for j in range(len(matrix)): | ||
enc += ''.join(matrix[j]) | ||
|
||
# We return the ciphertext | ||
return enc | ||
|
||
# Calling the driver function | ||
if __name__=='__main__': | ||
|
||
# Taking plaintext and key as input from the user | ||
plaintext = input("Enter a message you want to encrypt : ") | ||
key = int(input("Enter a key to encrypt the message : ")) | ||
|
||
if key <= 0: | ||
print("Enter a valid key.") | ||
sys.exit(0) | ||
|
||
elif key == 1: | ||
encrypted_message = plaintext | ||
print("Encrypted plaintext is", " "*12, ":", encrypted_message) | ||
sys.exit(0) | ||
|
||
''' Calling the encryption function on the given paintext | ||
and key''' | ||
encrypted_message = encryption(plaintext, key) | ||
|
||
# Printing the ciphertext we got using the encryption method | ||
print("Encrypted plaintext is", " "*12, ":", encrypted_message) | ||
|
||
''' | ||
Sample I/O: | ||
a) | ||
Enter a message you want to encrypt : This is Rail Fence Cipher | ||
Enter a key to encrypt the message : 4 | ||
Encrypted plaintext is : Ts rhi lFeCei Riecihsanp | ||
b) | ||
Enter a message you want to encrypt : thisisrisky | ||
Enter a key to encrypt the message : 1 | ||
Encrypted plaintext is : thisisrisky | ||
''' | ||
|