From 48abee1db7b9c5b7428afe7049676dfc618494e5 Mon Sep 17 00:00:00 2001 From: Roma Vardiyani <59890472+bohemian98@users.noreply.github.com> Date: Wed, 20 May 2020 21:12:25 +0530 Subject: [PATCH 01/19] Ford Fulkerson in JS(changes made) (#2865) --- .../Ford_Fulkerson_Method.js | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 Ford_Fulkerson_Method/Ford_Fulkerson_Method.js diff --git a/Ford_Fulkerson_Method/Ford_Fulkerson_Method.js b/Ford_Fulkerson_Method/Ford_Fulkerson_Method.js new file mode 100644 index 0000000000..fc177a7258 --- /dev/null +++ b/Ford_Fulkerson_Method/Ford_Fulkerson_Method.js @@ -0,0 +1,142 @@ +//+++++++++++++++Utility+++++++++++++++++ +//Queue Data Structure implemented +function Queue() { + this.elements = []; + } + Queue.prototype.enqueue = function (e) { + this.elements.push(e); + }; + // remove an element from the front of the queue +Queue.prototype.dequeue = function () { + return this.elements.shift(); +}; +// check if the queue is empty +Queue.prototype.isEmpty = function () { + return this.elements.length == 0; +}; +// get the element at the front of the queue +Queue.prototype.peek = function () { + return !this.isEmpty() ? this.elements[0] : undefined; +}; +Queue.prototype.length = function() { + return this.elements.length; +} + +//++++++++++++++++++++++++++++++++++++++++++ +//Function to do breadth first search in graph +function bfs(reducedGraph, source, sink, path, V) +{ + //array to store if the node is visited or not + var visited = [V]; + //intially no node is visited + for (var i = 0; i < V; i++) + visited[i] = false; + let q = new Queue(); + //Starting from source + q.enqueue(source); + visited[source] = true; + //parent of starting node will be nothing hence -1 is stored in it + path[source] = -1; + + while (!q.isEmpty()) + { + var u = q.peek(); + q.dequeue(); + for(var v = 0; v < V; v++) + { + if(visited[v] === false && reducedGraph[u][v] > 0) + { + q.enqueue(v); + path[v] = u; + visited[v] = true; + } + } + } + //If there exist path from source to sink then it will return true else false + return (visited[sink] === true); +} + +function fordFulkerson(graph,source,sink,V) +{ + //reducedGraph is used to store the remaining capacity. reducedGraph[i][j] + //indicates remaining capacity of edge from i to jif edge exists. + var reducedGraph = []; + for( var i = 0; i < V; i++) + { + reducedGraph[i] = []; + for(var j = 0; j < V; j++) + { + reducedGraph[i][j] = graph[i][j]; + } + } + //Path is used to store parent + var path = [V]; + //variable to store maximum flow in the path chosen + var max_flow = 0; + while (bfs(reducedGraph, source, sink, path, V)) + { + var path_flow = Number.MAX_VALUE; + //Finding maximum flow in the path selected + for(var v = sink; v != source; v = path[v]) + { + var u = path[v]; + path_flow = Math.min( path_flow, reducedGraph[u][v]); + } + //Updating remaining capacity odf the edges + for( var v = sink ; v != source; v = path[v]) + { + var u = path[v]; + reducedGraph[u][v] -= path_flow; + reducedGraph[v][u] += path_flow; + } + max_flow += path_flow; + } + return max_flow; +} +//Used synchronous readline package to take console input +var readlineSync = require('readline-sync'); +//Input number of nodes in a graph +var V = readlineSync.question('Enter number of nodes in a graph: '); +//Input the weight matrix for the directed graph + +var graph = []; +for(var i = 0; i < V; i++) +{ + graph[i] = []; + for(var j = 0; j < V; j++) + { + console.log(`Enter the weight form ` , i , `to `,j, `: `); + var a = readlineSync.question(); + graph[i][j] = a; + } +} +var source = readlineSync.question("Enter source: "); +var sink = readlineSync.question("Enter Sink: "); +var ans = fordFulkerson(graph,source,sink,V); +console.log('Maximum flow from source to sink is: ', ans); +/*Input1: +V = 6 +graph = [ [0, 16, 13, 0, 0, 0], + [0, 0, 10, 12, 0, 0], + [0, 4, 0, 0, 14, 0], + [0, 0, 9, 0, 0, 20], + [0, 0, 0, 7, 0, 4], + [0, 0, 0, 0, 0, 0] + ]; +source = 0 sink = 5 +Output1: 23 + +Input2: +V = 6 +graph = [ [0, 10, 0, 3, 0, 0], + [0, 0, 8, 2, 0, 0], + [0, 0, 0, 0, , 10], + [0, 0, 6, 0, 7, 0], + [0, 0, 0, 0, 10, 0], + [0, 0, 0, 0, 0, 0] + ]; +source = 0 +sink = 5 +Output2: 10 +*/ + From 1fcf5733d7f09005ff8db011febabc55aa5804e0 Mon Sep 17 00:00:00 2001 From: Raksha <57195964+raksha009@users.noreply.github.com> Date: Thu, 21 May 2020 23:20:38 +0530 Subject: [PATCH 02/19] linear search in rust (#2950) linear search in rust Linear search in rust --- Linear_Search/Linear_Search.rs | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Linear_Search/Linear_Search.rs diff --git a/Linear_Search/Linear_Search.rs b/Linear_Search/Linear_Search.rs new file mode 100644 index 0000000000..d6c5d2aef3 --- /dev/null +++ b/Linear_Search/Linear_Search.rs @@ -0,0 +1,68 @@ +// Rust program for Linear Search +use std::io; + +fn main() { + println!(" Enter number of elements to be entered in the array "); + let mut size = String::new(); + + io::stdin().read_line(&mut size).expect("failed to read input."); + let size: usize = size.trim_end().parse().expect("invalid input"); + let mut vector: Vec = Vec::with_capacity(size as usize); + + println!("Enter element to be searched " ); + let mut element = usize::new(); + + println!("Enter elements of array "); + let mut index = 0; + + // Enter values into vector + while index < size { + index += 1; + // Note: Rust takes spaces as non-intergral value + let mut temp_arr = String::new(); + io::stdin().read_line(&mut temp_arr).expect("failed to read input."); + let temp_arr: usize = temp_arr.trim_end().parse().expect("invalid input"); + vector.push(temp_arr); + } + + // Linear Search process + let mut checkpoint = 0; + for index in 0..size - 1 { + if element == vector[index]{ + checkpoint = 1; + println!("Element is found at index "); + println!("{:?}" , index + 1); + } + } + + if checkpoint == 0 { + println!("Element is not found in the array "); + } +} +/* +TEST CASE +INPUT + Enter number of elements to be entered in the array +3 +Enter element to be searched +2 +Enter elements of array +1 +2 +3 +OUTPUT +Element is found at index +2 +INPUT + Enter number of elements to be entered in the array +3 +Enter element to be searched +5 +Enter elements of array +1 +2 +3 +OUTPUT +Element is not found in the array +*/ + From f4b3b3f73fa895376ab362906e382c3e8b09799a Mon Sep 17 00:00:00 2001 From: Abhinav Anand Date: Fri, 22 May 2020 21:45:25 +0530 Subject: [PATCH 03/19] Create Extended_Euclidean_Algorithm.rb (#2949) --- .../Extended_Euclidean_Algorithm.rb | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.rb diff --git a/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.rb b/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.rb new file mode 100644 index 0000000000..e4076814a5 --- /dev/null +++ b/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.rb @@ -0,0 +1,36 @@ +=begin +Extended Euclidean Algorithm +============================== +GCD of two numbers is the largest number that divides both of them. +A simple way to find GCD is to factorize both numbers and multiply common factors. + +GCD(a,b) = ax + by +If we can find the value of x and y then we can easily find the +value of GCD(a,b) by replacing (x,y) with their respective values. +=end + +def extended_gcd(a, b, x, y) #Function for extended Euclidean Algorithm + + #Base Case + return b if a == 0 + + x1, y1 = 1, 1 #To store results of recursive call + gcd = extended_gcd(b % a, a, x1, y1) + #Update x and y using results of recursive call + x = y1 - (b / a) * x1 + y = x1 + return gcd +end + +x , y = 1, 1 +a = gets.to_i +b = gets.to_i +puts "GCD of numbers #{a} and #{b} is #{extended_gcd(a, b, x, y)}" + +=begin +INPUT: +27 +81 +OUTPUT: +GCD of numbers 27 and 81 is 27 +=end From dc117b69a6f8f461d5f6e131e495a9ee42fb125d Mon Sep 17 00:00:00 2001 From: Lakshyajit Laxmikant <30868587+lakshyajit165@users.noreply.github.com> Date: Fri, 22 May 2020 13:27:44 -0400 Subject: [PATCH 04/19] Modified spacing extended euclidean algorithm in java (#2953) --- .../Extended_Euclidean_Algorithm.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.java diff --git a/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.java b/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.java new file mode 100644 index 0000000000..eaa13be6b0 --- /dev/null +++ b/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.java @@ -0,0 +1,46 @@ +import java.util.*; + +public class Extended_Euclidean_Algorithm +{ + // extended Euclidean Algorithm + public static int getGCD(int a, int b, int x, int y) + { + // Base Case + if (a == 0) + { + x = 0; + y = 1; + return b; + } + + int x1 = 1, y1 = 1; // To store results of recursive call + int gcd = getGCD(b % a, a, x1, y1); + + // Update x and y after recursive call + x = y1 - (b / a) * x1; + y = x1; + + return gcd; + } + +// Main function + public static void main(String[] args) + { + int x = 1, y = 1; + Scanner s = new Scanner(System.in); + int a = s.nextInt(); + int b = s.nextInt(); + int g = getGCD(a, b, x, y); + System.out.print("GCD of " + a + " , " + b + " = " + g); + } +} + + + +/* + INPUT + a = 35, b = 15; + + Output: + GCD of 35 , 15 = 5 +*/ From 4f2b73b0b6f1ceb4b50f551c8ef9754cbaecf7a6 Mon Sep 17 00:00:00 2001 From: thakran14 <47153756+thakran14@users.noreply.github.com> Date: Sat, 23 May 2020 00:06:25 +0530 Subject: [PATCH 05/19] Added Exponential Search in PHP (#2957) Update Exponential_Search.php --- Exponential_Search/Exponential_Search.php | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Exponential_Search/Exponential_Search.php diff --git a/Exponential_Search/Exponential_Search.php b/Exponential_Search/Exponential_Search.php new file mode 100644 index 0000000000..11a24146cd --- /dev/null +++ b/Exponential_Search/Exponential_Search.php @@ -0,0 +1,50 @@ + $left) + { + $mid = $left + ($right - $left) / 2; + if ($input_arr[$mid] == $x) + return $mid; + if ($x < $input_arr[$mid] ) + return Binary_Search($input_arr, $left, $mid - 1, $x); + + return Binary_Search($input_arr, $mid + 1, $right, $x); + } + return -1; +} + +function Exponential_Search($input_arr,$size,$x) +{ + if ($input_arr[0] == $x) + return 0; + $i = 1; + while ($i < $size and $input_arr[$i] <= $x) + { + $i = $i * 2; + } + + return Binary_Search($input_arr, $i / 2, min($i, $size), $x); + +} + +//Driver Code + +$input_arr = array(1, 2, 3, 4, 5, 6); +$size = count($input_arr); +$x = 4; +$result = exponential_search($input_arr, $size, $x); +if ($result != -1) + echo "The index at which the element " , $x, " is present is " , $result; + +else + echo "The element is not present in array"; + +/* +OUTPUT: +The index at which the element 4 is present is 3 +*/ + +?> From 2e71476b4193ee8831c1a8335d81d334224eb1bd Mon Sep 17 00:00:00 2001 From: shivanidalmia1203 <47073500+shivanidalmia1203@users.noreply.github.com> Date: Sat, 23 May 2020 18:41:14 +0530 Subject: [PATCH 06/19] Add Aho-Corasick in Python (#2962) --- Aho-Corasick/Aho-Corasick.py | 89 ++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Aho-Corasick/Aho-Corasick.py diff --git a/Aho-Corasick/Aho-Corasick.py b/Aho-Corasick/Aho-Corasick.py new file mode 100644 index 0000000000..91f96f1699 --- /dev/null +++ b/Aho-Corasick/Aho-Corasick.py @@ -0,0 +1,89 @@ +# AhoNode class +class AhoNode: + def __init__(self): + self.goto = {} + self.out = [] + self.fail = None + + +# creating aho forest +def aho_create_forest(patterns): + root = AhoNode() + + for path in patterns: + node = root + for symbol in path: + node = node.goto.setdefault(symbol, AhoNode()) + node.out.append(path) + return root + + +# Creating aho automata +def aho_create_statemachine(patterns): + root = aho_create_forest(patterns) + queue = [] + for node in root.goto.values(): + queue.append(node) + node.fail = root + + while len(queue) > 0: + rnode = queue.pop(0) + + for key, unode in rnode.goto.items(): + queue.append(unode) + fnode = rnode.fail + while fnode is not None and key not in fnode.goto: + fnode = fnode.fail + unode.fail = fnode.goto[key] if fnode else root + unode.out += unode.fail.out + + return root + + +def aho_find_all(s, root, callback): + node = root + + for i in range(len(s)): + while node is not None and s[i] not in node.goto: + node = node.fail + if node is None: + node = root + continue + node = node.goto[s[i]] + for pattern in node.out: + callback(i - len(pattern) + 1, pattern) + + +# Printing the position of pattern found +def print_pattern(pos, patterns): + print("At pos %s found pattern: %s" % (pos, patterns)) + + +if __name__ == "__main__": + patterns = [] + + # taking inputs + n = int(input("Enter no. of elements :- ")) + for i in range(0, n): + ele = input("Enter element :- ") + patterns.append(ele) + s = input("Enter text :- ") + + root = aho_create_statemachine(patterns) + aho_find_all(s, root, print_pattern) + + +""" +INPUT: +n=6 +patterns = ['a', 'ab', 'abc', 'bc', 'c', 'cba'] +s = "abcba" + +OUTPUT: +At pos 0 found pattern: a +At pos 0 found pattern: ab +At pos 0 found pattern: abc +At pos 1 found pattern: bc +At pos 2 found pattern: c +At pos 2 found pattern: cba +""" \ No newline at end of file From 25e6d6128a8d3c9041aad42a8b797e8dee0bb7f8 Mon Sep 17 00:00:00 2001 From: Raksha <57195964+raksha009@users.noreply.github.com> Date: Mon, 25 May 2020 00:43:18 +0530 Subject: [PATCH 07/19] Binary search in rust (#2927) Binary search in rust --- Binary_Search/Binary_Search.rs | 88 ++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Binary_Search/Binary_Search.rs diff --git a/Binary_Search/Binary_Search.rs b/Binary_Search/Binary_Search.rs new file mode 100644 index 0000000000..08d24b231d --- /dev/null +++ b/Binary_Search/Binary_Search.rs @@ -0,0 +1,88 @@ +// Rust program for Binary Search +use std::io; + +fn main() { + println!(" Enter number of elements to be entered in the array "); + let mut size = String::new(); + + io::stdin().read_line(&mut size).expect("failed to read input."); + let size: usize = size.trim_end().parse().expect("invalid input"); + let mut vector: Vec = Vec::with_capacity(size as usize); + + println!("Enter element to be searched " ); + let mut element = usize::new(); + + println!("Enter elements of array in a sorted array "); + let mut index = 0; + + // Enter values into vector + while index < size { + index += 1; + // Note: Rust takes spaces as non-intergral value + let mut temp_arr = String::new(); + io::stdin().read_line(&mut temp_arr).expect("failed to read input."); + let temp_arr: usize = temp_arr.trim_end().parse().expect("invalid input"); + vector.push(temp_arr); + } + + // Binary Search process + let mut leftpart = 0; + let mut rightpart = size - 1; + let mut midindex = 0; + let mut checkpoint = 0; + while leftpart <= rightpart { + midindex = leftpart + (rightpart - leftpart) / 2; + + // Element found + if vector[midindex] == element{ + checkpoint = 1; + println!("Element is found at index "); + println!("{:?}" , midindex + 1); + break; + } + + else if vector[midindex] < element{ + leftpart = midindex + 1; + } + + else { + rightpart = midindex - 1; + } + + } + + if checkpoint == 0 { + println!("Element is not found in the array "); + } +} +/* +TEST CASE + +INPUT + Enter number of elements to be entered in the array +3 +Enter element to be searched +2 +Enter elements of array in a sorted array +1 +2 +3 + +OUTPUT +Element is found at index +2 + +INPUT + Enter number of elements to be entered in the array +3 +Enter element to be searched +5 +Enter elements of array in a sorted array +1 +2 +3 + +OUTPUT +Element is not found in the array +*/ + From d8e3a027e632c1310522218a16bf85a331dd7d51 Mon Sep 17 00:00:00 2001 From: Hardev Khandhar <54733460+HardevKhandhar@users.noreply.github.com> Date: Mon, 25 May 2020 00:54:13 +0530 Subject: [PATCH 08/19] Shell Sort In Ruby (#2969) Updated Minor Changes --- Shell_Sort/Shell_Sort.rb | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Shell_Sort/Shell_Sort.rb diff --git a/Shell_Sort/Shell_Sort.rb b/Shell_Sort/Shell_Sort.rb new file mode 100644 index 0000000000..30b17ada86 --- /dev/null +++ b/Shell_Sort/Shell_Sort.rb @@ -0,0 +1,76 @@ +# Shell Sort In Ruby + +# Function Definition +def shell_sort(values) + inct = values.size / 2 + + while inct > 0 + inct.upto(values.size - 1){ |i| + j = i + temp = values[i] + + while j >= inct and values[j - inct] > temp + values[j] = values[j - inct] + j -= inct + end + + values[j] = temp + } + inct = (inct == 2 ? 1 : inct * 10 / 22) + end + values +end + +# Taking User Input +puts "Enter the size of array: " +size = gets.to_i +puts +input_array = Array.new(size) + +counter = 0 + +# Input Array Values Using While Loop +while(counter != size) + puts "Enter the value at index #{counter}: " + input_array[counter] = gets.to_i + counter += 1 +end + +puts + +puts "Input Array: #{input_array}" +puts "Shell Sort: " + +# Function Call +shell_sort(input_array) +puts "#{input_array}" + += begin + +Enter the size of array: 5 + +Enter the value at index 0: 121 +Enter the value at index 1: 71 +Enter the value at index 2: 1 +Enter the value at index 3: 88 +Enter the value at index 4: 9 + +Input Array: [121, 71, 1, 88, 9] +Shell Sort: [1, 9, 71, 88, 121] + +----------------------------------------------- + +Enter the size of array: 7 + +Enter the value at index 0: 68 +Enter the value at index 1: 1 +Enter the value at index 2: 11 +Enter the value at index 3: -8 +Enter the value at index 4: 0 +Enter the value at index 5: 3 +Enter the value at index 6: 35 + +Input Array: [68, 1, 11, -8, 0, 3, 35] +Shell Sort: [-8, 0, 1, 3, 11, 35, 68] + += end From 27e85302f140dc4169b5335bc791a5b37d96abe8 Mon Sep 17 00:00:00 2001 From: Vyom Chandra Gallani <36289357+vyom153069@users.noreply.github.com> Date: Mon, 25 May 2020 14:22:15 +0530 Subject: [PATCH 09/19] Tower_Of_Honoi.ts (#2964) --- Tower_Of_Hanoi/Tower_Of_Hanoi.ts | 118 +++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Tower_Of_Hanoi/Tower_Of_Hanoi.ts diff --git a/Tower_Of_Hanoi/Tower_Of_Hanoi.ts b/Tower_Of_Hanoi/Tower_Of_Hanoi.ts new file mode 100644 index 0000000000..8aabec37d5 --- /dev/null +++ b/Tower_Of_Hanoi/Tower_Of_Hanoi.ts @@ -0,0 +1,118 @@ +function solveHanoi( + count: number, + from: string, + to: string, + other: string, + move: (from: string, to: string) => void +) +{ + if (count > 0) { + //shift plat "from" to "to" using "other" plate + solveHanoi(count - 1, from, other, to, move) + move(from, to) + //shift plat "other" to "from" using "to" plate + solveHanoi(count - 1, other, to, from, move) + } +} +/**/ +// Example: six discs +var moveCount = 0 +solveHanoi(6, "Left", "Right", "Middle", (from , to) => { + ++moveCount + console.log(moveCount + ": Move Disk from " + from + " to " + to + ".") +}) + +//output for 3 +/* +1: Move Disk from Left to Right. +2: Move Disk from Left to Middle. +3: Move Disk from Right to Middle. +4: Move Disk from Left to Right. +5: Move Disk from Middle to Left. +6: Move Disk from Middle to Right. +7: Move Disk from Left to Right. +*/ + +//output for 4 +/* +1: Move Disk from Left to Middle. +2: Move Disk from Left to Right. +3: Move Disk from Middle to Right. +4: Move Disk from Left to Middle. +5: Move Disk from Right to Left. +6: Move Disk from Right to Middle. +7: Move Disk from Left to Middle. +8: Move Disk from Left to Right. +9: Move Disk from Middle to Right. +10: Move Disk from Middle to Left. +11: Move Disk from Right to Left. +12: Move Disk from Middle to Right. +13: Move Disk from Left to Middle. +14: Move Disk from Left to Right. +15: Move Disk from Middle to Right. +*/ + +//output for 6 +/* +1: Move Disk from Left to Middle. +2: Move Disk from Left to Right. +3: Move Disk from Middle to Right. +4: Move Disk from Left to Middle. +5: Move Disk from Right to Left. +6: Move Disk from Right to Middle. +7: Move Disk from Left to Middle. +8: Move Disk from Left to Right. +9: Move Disk from Middle to Right. +10: Move Disk from Middle to Left. +11: Move Disk from Right to Left. +12: Move Disk from Middle to Right. +13: Move Disk from Left to Middle. +14: Move Disk from Left to Right. +15: Move Disk from Middle to Right. +16: Move Disk from Left to Middle. +17: Move Disk from Right to Left. +18: Move Disk from Right to Middle. +19: Move Disk from Left to Middle. +20: Move Disk from Right to Left. +21: Move Disk from Middle to Right. +22: Move Disk from Middle to Left. +23: Move Disk from Right to Left. +24: Move Disk from Right to Middle. +25: Move Disk from Left to Middle. +26: Move Disk from Left to Right. +27: Move Disk from Middle to Right. +28: Move Disk from Left to Middle. +29: Move Disk from Right to Left. +30: Move Disk from Right to Middle. +31: Move Disk from Left to Middle. +32: Move Disk from Left to Right. +33: Move Disk from Middle to Right. +34: Move Disk from Middle to Left. +35: Move Disk from Right to Left. +36: Move Disk from Middle to Right. +37: Move Disk from Left to Middle. +38: Move Disk from Left to Right. +39: Move Disk from Middle to Right. +40: Move Disk from Middle to Left. +41: Move Disk from Right to Left. +42: Move Disk from Right to Middle. +43: Move Disk from Left to Middle. +44: Move Disk from Right to Left. +45: Move Disk from Middle to Right. +46: Move Disk from Middle to Left. +47: Move Disk from Right to Left. +48: Move Disk from Middle to Right. +49: Move Disk from Left to Middle. +50: Move Disk from Left to Right. +51: Move Disk from Middle to Right. +52: Move Disk from Left to Middle. +53: Move Disk from Right to Left. +54: Move Disk from Right to Middle. +55: Move Disk from Left to Middle. +56: Move Disk from Left to Right. +57: Move Disk from Middle to Right. +58: Move Disk from Middle to Left. +59: Move Disk from Right to Left. +60: Move Disk from Middle to Right. +61: Move Disk from Left to Middle. +62: Move Disk from Left to Right.*/ From 7356f1a3e4ed0a0203dc068faca928274729f505 Mon Sep 17 00:00:00 2001 From: Satyam Chaudhary Date: Mon, 25 May 2020 16:43:54 +0530 Subject: [PATCH 10/19] code added (#2967) --- .../Extended_Euclidean_Algorithm.js | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.js diff --git a/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.js b/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.js new file mode 100644 index 0000000000..cf5d61394b --- /dev/null +++ b/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.js @@ -0,0 +1,39 @@ +// Extended Euclidean Algorithm to find GCD of two numbers + +// Function to return gcd of a and b +function gcdExtended(a, b, x, y) +{ + // Base Case + if (a == 0) + { + x = 0; + y = 1; + return b; + } + + var x1, y1; // To store results of recursive call + var gcd = gcdExtended(b % a, a, x1, y1); + + // Update x and y using results of + // recursive call + x = y1 - (b / a) * x1; + y = x1; + + return gcd; +} + +// Used synchronous readline package to take console input +var readlineSync = require('readline-sync'); + +var a = readlineSync.question('Enter first Number'); +a = parseInt(a); +var b = readlineSync.question('Enter second number'); +b = parseInt(b); + +var result = gcdExtended(a, b); +console.log("The GCD is" + " " + result) + +/* + Sample Input : 9 6 + Sample Output : The GCD is 3 +*/ From 2e415ba77bc1df2ab33293de6d65707dad0a8101 Mon Sep 17 00:00:00 2001 From: Hardev Khandhar <54733460+HardevKhandhar@users.noreply.github.com> Date: Mon, 25 May 2020 16:51:58 +0530 Subject: [PATCH 11/19] Cycle Sort In Ruby (#2970) Updated Changes --- Cycle_Sort/Cycle_Sort.rb | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Cycle_Sort/Cycle_Sort.rb diff --git a/Cycle_Sort/Cycle_Sort.rb b/Cycle_Sort/Cycle_Sort.rb new file mode 100644 index 0000000000..0203c852d2 --- /dev/null +++ b/Cycle_Sort/Cycle_Sort.rb @@ -0,0 +1,93 @@ +# Cycle Sort In Ruby + +# Function definition +def cycleSort!(input) + change = 0 + + # Looping through the input to find cycles to rotate + for cycleStart in 0 .. input.size - 2 + item = input[cycleStart] + + # Finding where to put the item + position = cycleStart + for i in cycleStart + 1 ... input.size + position += 1 if input[i] < item + end + + # If the item is already present, there is no cycle + next if position == cycleStart + + # Otherwise, put the item there or right after any duplicates + position += 1 while item == input[position] + input[position], item = item, input[position] + change += 1 + + # Rotate the rest of the cycle + while position != cycleStart + + # Finding where to put the item + position = cycleStart + for i in cycleStart + 1 ... input.size + position += 1 if input[i] < item + end + + # Putting the item there or right after any duplicates + position += 1 while item == input[position] + input[position], item = item, input[position] + change += 1 + end + end + change +end + +# Taking User Input +puts "Enter the size of array: " +size = gets.to_i +puts +input_array = Array.new(size) + +counter = 0 + +# Input array values using while loop +while(counter != size) + puts "Enter the value at index #{counter}: " + input_array[counter] = gets.to_i + counter += 1 +end + +puts + +puts "Input Array: #{input_array}" +puts "Cycle Sort: " + +cycleSort!(input_array) +puts "#{input_array}" + +=begin + +Sample Input + +Enter the size of array: 15 + +Enter the value at index 0: 0 +Enter the value at index 1: 1 +Enter the value at index 2: 2 +Enter the value at index 3: 2 +Enter the value at index 4: 2 +Enter the value at index 5: 2 +Enter the value at index 6: 1 +Enter the value at index 7: 9 +Enter the value at index 8: 3.5 +Enter the value at index 9: 5 +Enter the value at index 10: 8 +Enter the value at index 11: 4 +Enter the value at index 12: 7 +Enter the value at index 13: 0 +Enter the value at index 14: 6 + +Sample Output + +Input Array: [0, 1, 2, 2, 2, 2, 1, 9, 3, 5, 8, 4, 7, 0, 6] +Cycle Sort: [0, 0, 1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9] + +=end From 0290290a610e5436e13af7e3fc256ca29a70b97d Mon Sep 17 00:00:00 2001 From: shivanidalmia1203 <47073500+shivanidalmia1203@users.noreply.github.com> Date: Mon, 25 May 2020 23:53:41 +0530 Subject: [PATCH 12/19] Updated file (#2989) --- .../Extended_Euclidean_Algorithm.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.ts diff --git a/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.ts b/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.ts new file mode 100644 index 0000000000..5b82e93423 --- /dev/null +++ b/Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.ts @@ -0,0 +1,35 @@ +export {} + +/* +Extended Euclidean Algorithm :- Find greatest common divisor of the two numbers + a*x + b*y = gcd(a,b) + and solves the above linear equation +*/ + +// find greatest common divisor +function gcdExtended(a:number , b:number) : number[] +{ + //base case + if (a==0) + { + return [b,0,1]; + } + //recursive call + let ans: number[] = gcdExtended(b%a , a); + return [ans[0], (ans[2] - (Math.floor(b/a) * ans[1])) , ans[1]]; +} + +let a : number = 12; +let b : number = 16; + +let ans : number[] = gcdExtended(a,b); +console.log(ans[0] , ans[1] , ans[2]); + +/* +INPUT +a = 12 +b = 16 + +OUTPUT +4 -1 1 +*/ From a6923fb562faa90ca54d6e7ae7689b502aefc6f1 Mon Sep 17 00:00:00 2001 From: Satyam Chaudhary Date: Tue, 26 May 2020 00:01:41 +0530 Subject: [PATCH 13/19] Euclidean Algo added (#2966) --- Euclidean_Algorithm/Euclidean_Algorithm.js | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Euclidean_Algorithm/Euclidean_Algorithm.js diff --git a/Euclidean_Algorithm/Euclidean_Algorithm.js b/Euclidean_Algorithm/Euclidean_Algorithm.js new file mode 100644 index 0000000000..d27b6661d8 --- /dev/null +++ b/Euclidean_Algorithm/Euclidean_Algorithm.js @@ -0,0 +1,28 @@ +// Euclidean Algorithm to find GCD of two numbers + +// Function to return gcd of a and b +function gcd(a, b) +{ + // base case + if(b == 0) + { + return a; + } + return gcd(b, a % b); +} + +// Used synchronous readline package to take console input +var readlineSync = require('readline-sync'); + +var a = readlineSync.question('Enter first Number'); +a = parseInt(a); +var b = readlineSync.question('Enter second number'); +b = parseInt(b); + +var result = gcd(a, b); +console.log("The GCD is" + " " + result) + +/* + Sample Input : 9 6 + Sample Output : The GCD is 3 +*/ From 7344243a4db8abb89007ca237347c0f0579f57d0 Mon Sep 17 00:00:00 2001 From: Shriya Bhat <44133832+Shr03mink@users.noreply.github.com> Date: Tue, 26 May 2020 00:05:15 +0530 Subject: [PATCH 14/19] Binary Search in TypeScript (#2941) Correct Indentation fixed indentation --- Binary_Search/Binary search.ts | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Binary_Search/Binary search.ts diff --git a/Binary_Search/Binary search.ts b/Binary_Search/Binary search.ts new file mode 100644 index 0000000000..7ff9a3d907 --- /dev/null +++ b/Binary_Search/Binary search.ts @@ -0,0 +1,59 @@ +/* Implenetation of Binary Search algorithm to find value 6 in a given array of numbers */ + +function BinarySearch( arr:number[] , n:number , val:number ) :number +{ + var start = 0 //starting index of the array + var end = n-1 //ending index + var mid = 0 + var ind = -1 + + while (start <= end) + { + mid = Math.floor((start + end) / 2) + + //checking if value to be found is at the middle + if ( val == arr[mid] ) + { + ind = mid + break + } + + /* if value to be found is greater than the middle then the algorithm + searches to the right of the middle element in the array */ + else if ( val > arr[mid] ) + { + start = mid + 1 + } + /* if value to be found is less than the middle then the algorithm + searches to the left of the middle element in the array */ + else + { + end = mid - 1 + } + } + return ind //returning the index of the value if found, else returns -1 +} +var result = 0 +var n = 0 +var val = 6 //input value +console.log(" value to be found: ", val); +var n: number = 6 //no. of elements in the array +var arr = [1, 2, 3, 4, 5, 6]; //input array +console.log(" Array of numbers is: ", arr); +result = BinarySearch(arr, n, val) +if (result != -1) { + console.log(" value ", val," found at index no. ", result," in the array "); +} +else { + console.log(" value ", val," not found "); +} + + +/* OUTPUT: + + value to be found: 6 + Array of numbers is: [ 1, 2, 3, 4, 5, 6 ] + value 6 found at index no. 5 in the array +*/ + + From 7b31916d83d579bbe73c24d3e5ca40f910dbada6 Mon Sep 17 00:00:00 2001 From: Harshita Date: Tue, 26 May 2020 12:52:31 +0530 Subject: [PATCH 15/19] circle_sort.php added (#2919) --- Circle_Sort/Circle_Sort.php | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Circle_Sort/Circle_Sort.php diff --git a/Circle_Sort/Circle_Sort.php b/Circle_Sort/Circle_Sort.php new file mode 100644 index 0000000000..8545d94700 --- /dev/null +++ b/Circle_Sort/Circle_Sort.php @@ -0,0 +1,85 @@ + $a[$hi]) + { + $temp = $a[$lo]; + $a[$lo] = $a[$hi]; + $a[$hi] = $temp; + $swapped = true; + } + $lo++; + $hi--; + } + + if($lo == $hi) + if($a[$lo] > $a[$hi + 1]) + { + $t = $a[$low]; + $a[$low] = $a[$hi + 1]; + $a[$hi + 1] = $t; + $swapped = true; + } + + $mid = ($high - $low) / 2; + $firstHalf = circleSortRec($a , $low , $low + $mid); + $secondHalf = circleSortRec($a , $low + $mid + 1 , $high); + return $swapped || $firstHalf || $secondHalf; +} + +function circleSort($a , $n) +{ + + while(circleSortRec($a,0,$n - 1)) + { + ; + } +} + +$a = explode(' ',readline()); +$n = sizeof($a) / sizeof($a[0]); + +echo "Unsorted: "; + +for($i = 0; $i < $n; $i++) +{ + echo "$a[$i] "; +} + +circleSort($a , $n); + +echo "\n Sorted: "; + +for($i = 0; $i < $n; $i++) +{ + echo "$a[$i] "; +} + +return 0; + +/* +Input: +7 5 3 1 2 4 6 8 + +Output: +Unsorted: [6, 5, 3, 1, 8, 7, 2, 4] +Sorted: [1, 2, 3, 4, 5, 6, 7, 8] +*/ + +?> From 14da38db41e4166158a6ea011be4771e3e2131c4 Mon Sep 17 00:00:00 2001 From: Sukriti Shah Date: Wed, 27 May 2020 01:24:33 +0530 Subject: [PATCH 16/19] Adding PHP Implementation for Boyer Moore Algo. (#2968) --- Boyer_Moore_Algorithm/Boyer_Moore.php | 81 +++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Boyer_Moore_Algorithm/Boyer_Moore.php diff --git a/Boyer_Moore_Algorithm/Boyer_Moore.php b/Boyer_Moore_Algorithm/Boyer_Moore.php new file mode 100644 index 0000000000..83aba52abc --- /dev/null +++ b/Boyer_Moore_Algorithm/Boyer_Moore.php @@ -0,0 +1,81 @@ += 0 && $pattern[$j] == $text[$s + $j]) + $j--; + + // If the pattern is present at current shift, then index j will become -1, so + // Shift the pattern so that the next character in text aligns + // with the last occurrence of it in pattern. + // Else, Shift the pattern so that the bad character in text aligns with + // the last occurrence of it in pattern. + if ($j < 0){ + $arr[$i++] = $s; + $s += ($s + $patternLength < $textLength) ? + $patternLength - $badChar[ord($text[$s + $patternLength])] : 1; + } + else + $s += max(1, $j - $badChar[ord($text[$s + $j])]); + } + + for ($j = 0; $j < $i; $j++) + $result[$j] = $arr[$j]; + return $result; +} + +// Sample Input +$text = "I scream, you scream, we all scream for ice cream"; +$pattern = "scream"; + +$occ = search($text, $pattern); +echo "Occurrences of pattern in text are as follows:\n"; +foreach ($occ as $res){ + echo "$res \n"; +} + +/* +Sample Input as mentioned in the code- +$text = "I scream, you scream, we all scream for ice cream" +$pattern = "scream" + +Sample Output- +Occurrences of pattern in text are as follows: +2 +14 +29 +8 +*/ +?> + + From 1bda3342aeeec923c1972a845232ac8e07c71a58 Mon Sep 17 00:00:00 2001 From: Mrunal <44579222+mruna7@users.noreply.github.com> Date: Wed, 27 May 2020 18:56:59 +0530 Subject: [PATCH 17/19] Sieve_Of_Eratosthenes (#2904) Sieve_of_eratosthenes.php Delete Binary_To_Octal.java Update Sieve_Of_Eratosthenes.php squashingthecommits Create Decimal_to_Binary Update Decimal_to_Binary Update Decimal_to_Binary Update Decimal_to_Binary Update Decimal_to_Binary Update Decimal_to_Binary Add files via upload Update Binary_To_Octal.java Update Decimal_to_Binary Update Decimal_to_Binary Update Binary_To_Octal.java Update Binary_To_Octal.java Delete Decimal_to_Binary Delete Binary_To_Octal.java --- .../Sieve_Of_Eratosthenes.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Sieve_Of_Eratosthenes/Sieve_Of_Eratosthenes.php diff --git a/Sieve_Of_Eratosthenes/Sieve_Of_Eratosthenes.php b/Sieve_Of_Eratosthenes/Sieve_Of_Eratosthenes.php new file mode 100644 index 0000000000..342c1f0899 --- /dev/null +++ b/Sieve_Of_Eratosthenes/Sieve_Of_Eratosthenes.php @@ -0,0 +1,29 @@ + + +/*Input: +Enter an integer + 10 +Output + 2 3 5 7 +*/ From b7a3d92aa9a6530302b7004afb3ca9508f5e567a Mon Sep 17 00:00:00 2001 From: Monika Jha Date: Wed, 27 May 2020 19:08:31 +0530 Subject: [PATCH 18/19] Depth First Search in Kotlin (#2980) --- Depth_First_Search/Depth_First_Search.kt | 77 ++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Depth_First_Search/Depth_First_Search.kt diff --git a/Depth_First_Search/Depth_First_Search.kt b/Depth_First_Search/Depth_First_Search.kt new file mode 100644 index 0000000000..4e4817db3a --- /dev/null +++ b/Depth_First_Search/Depth_First_Search.kt @@ -0,0 +1,77 @@ +import java.util.LinkedList + +class Dfs(private val vertices: Int) { + private val adj: List> = (0 until vertices).map { LinkedList() } + fun addEdge(vertex: Int, weight: Int) { + adj[vertex].add(weight) // Add weight to vertex's list. + } + + private fun dfsUtil(vertex: Int, visited: BooleanArray) { + visited[vertex] = true // Mark the current node as visited and print it + print("$vertex ") + adj[vertex].filter { !visited[it] }.forEach { dfsUtil(it, visited) } + } + + fun traverse(startVertex: Int) { + dfsUtil(startVertex, BooleanArray(vertices)) // Call the recursive helper function to print DFS traversal + } +} + +fun main(args: Array) { + var read = Scanner(System.`in`) + println("Enter the number of vertices :") + val arrSize = read.nextLine().toInt() + val graph = Dfs(arrSize) + for(i in 0 until arrSize) + { + println("Enter the source vertex :") + val a = read.nextLine().toInt() + println("Enter the destination vertex :") + val b = read.nextLine().toInt() + graph.addEdge(a,b) + } + println("\nFollowing is Depth First Traversal :") + graph.traverse(0) + println() +} + + +/* + + Sample Input : + Enter the number of vertices : + 7 + Enter the source vertex : + 1 + Enter the destination vertex : + 2 + Enter the source vertex : + 2 + Enter the destination vertex : + 3 + Enter the source vertex : + 2 + Enter the destination vertex : + 4 + Enter the source vertex : + 3 + Enter the destination vertex : + 4 + Enter the source vertex : + 1 + Enter the destination vertex : + 5 + Enter the source vertex : + 5 + Enter the destination vertex : + 6 + Enter the source vertex : + 5 + Enter the destination vertex : + 7 + + Following is Depth First Traversal : + 1 2 3 4 5 6 7 + +*/ + From c0b12c5ecee9987eb9b8a946a70854fe06c50216 Mon Sep 17 00:00:00 2001 From: shweta0699 <60976710+shweta0699@users.noreply.github.com> Date: Thu, 12 Mar 2020 18:30:15 +0530 Subject: [PATCH 19/19] Implementation of 0/1 knapsack problem in c++ (updated) Update-1 01_knapsack.cpp Update-2 01_knapsack.cpp Update-3 01_knapsack.cpp --- Knapsack/01_knapsack.cpp | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Knapsack/01_knapsack.cpp diff --git a/Knapsack/01_knapsack.cpp b/Knapsack/01_knapsack.cpp new file mode 100644 index 0000000000..7ef322ca48 --- /dev/null +++ b/Knapsack/01_knapsack.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; + +// Function to return maximum profit +int knapsack(vector profit, vector weight, int capacity, int n) { + + if(capacity == 0 || n == 0 ) { + return 0; + } + if(weight[n - 1] > capacity) { + return knapsack(profit, weight, capacity, n - 1); + } + else { + return max(profit[n - 1] + knapsack(profit, weight, capacity - weight[n - 1], n - 1), knapsack(profit, weight, capacity, n - 1)); + } +} + +int max(int a, int b) { + if(a > b) { + return a; + } + else { + return b; + } +} + +int main() { + int capacity, n; + cout << "Enter the number of elements to be stored: "; + cin >> n; + vector profit(n), weight(n); + cout << "Enter the profit values of all elements: "; + for(int i = 0; i < n; i++) { + cin >> profit[i]; + } + cout << "Enter the weights of all elements: "; + for(int i = 0; i < n; i++) { + cin >> weight[i]; + } + cout << "Enter the capacity: "; + cin >> capacity; + cout << "The maximum profit will be: " << knapsack(profit, weight, capacity, n); + return 0; +} + +/* +SAMPLE INPUT: +Enter the number of elements to be stored: 6 +Enter the profit values of all elements: 10 20 30 44 56 72 +Enter the weights of all elements: 5 10 15 25 20 15 +Enter the capacity: 70 + +SAMPLE OUTPUT: +The maximum profit will be: 192 +*/