diff --git a/BFS/BFS.py b/BFS/BFS.py index 472d6267..d6d563b9 100644 --- a/BFS/BFS.py +++ b/BFS/BFS.py @@ -1,27 +1,38 @@ -graph = { - '5' : ['3','7'], - '3' : ['2', '4'], - '7' : ['8'], - '2' : [], - '4' : ['8'], - '8' : [] -} +from collections import defaultdict + + +class Graph: + + def __init__(self): + self.graph = defaultdict(list) -visited = [] -queue = [] + def addEdge(self,u,v): + self.graph[u].append(v) -def bfs(visited, graph, node): - visited.append(node) - queue.append(node) + def BFS(self, s): + visited = [False] * (len(self.graph)) + queue = [] + queue.append(s) + visited[s] = True + + while queue: + s = queue.pop(0) + print (s, end = " ") + for i in self.graph[s]: + if visited[i] == False: + queue.append(i) + visited[i] = True + +# Driver code - while queue: - m = queue.pop(0) - print (m, end = " ") - - for neighbour in graph[m]: - if neighbour not in visited: - visited.append(neighbour) - queue.append(neighbour) - -print("Following is the Breadth-First Search") -bfs(visited, graph, '5') +# g = Graph() +# g.addEdge(0, 1) +# g.addEdge(0, 2) +# g.addEdge(1, 2) +# g.addEdge(2, 0) +# g.addEdge(2, 3) +# g.addEdge(3, 3) + +# print ("Following is Breadth First Traversal" +# " (starting from vertex 2)") +# g.BFS(2) \ No newline at end of file diff --git a/Binary Search/BinarySearch.cpp b/Binary Search/BinarySearch.cpp index dc302f77..26616d8c 100644 --- a/Binary Search/BinarySearch.cpp +++ b/Binary Search/BinarySearch.cpp @@ -1,45 +1,42 @@ -//C++ program to implement Binary search using recursive method -//For this we are hoping that the array is sorted - -#include - +#include using namespace std; -// A function that return the index of the element if found -// If the number is not there then it will return -1 - -int bSearch(int binarySearchArray[], int low, int high, int searchingNumber){ - int mid = (low + high)/2; - // When the array is initialized then low represents 0th index whereas high represents the last index of the array - if(low > high) - return -1; - // we return -1 when low becomes greater than high denoting that the number is not present - if(binarySearchArray[mid] == searchingNumber) - return mid; - // If the number is found we are returning the index of the number where it was found - else if(searchingNumber > binarySearchArray[mid]) - bSearch(binarySearchArray, mid + 1, high, searchingNumber); - // Since the number is greater than the mid element in the array so we increase the index of low by mid+1 - // becuase the number before do not contain the number we were searching for - else - bSearch(binarySearchArray, low, mid-1, searchingNumber); - // Since the number is less than the mid elemet in the array so we decrease the index of high to mid-1 - // because the number after the middle element do not contain the number we were searching for -} - -int main(){ - int sizeofArray = 10; // Taking the size of array - int binSearchArray[sizeofArray] = {5, 8 ,12, 34, 36, 40, 45, 50, 56, 61}; // Array containing the elements - int searchNumber = 40; - - int isNumberFound = bSearch(binSearchArray, 0, sizeofArray - 1, searchNumber); - - if(isNumberFound != -1) - cout<<"The number is found at the index : "<>count; + + for (i=0; i>arr[i]; + } + cout<<"Enter the number that you want to search:"; + cin>>num; + first = 0; + last = count-1; + middle = (first+last)/2; + while (first <= last) + { + if(arr[middle] < num) + { + first = middle + 1; + + } + else if(arr[middle] == num) + { + cout< last) + { + cout< +#include using namespace std; - -void cyclicSort(vector& arr) -{ - int n=arr.size(); - int i = 0; - while(i < n) - { - int correct = arr[i] - 1 ; - if(arr[i] != arr[correct]) - { - swap(arr[i], arr[correct]) ; - } - else{ - i++ ; - } +void cyclicSort(int &arr[],int n){ + int i=0; + while(i arr) -{ - int size=arr.size(); - for (int i = 0; i < size; i++) - { - cout << arr[i] << " "; - } - cout<>n;//Size of array - vector v(n); - for(int i=0;i>v[i];//Input Array elements - } - cyclicSort(v);//Cyclic sort function - cout<<"Printing sorted array:"< Integer -factorial 0 = 1 -factorial n = n * factorial (n - 1) +-- Luis L Reyes +-- Simple factorial function +module Factorial where + factorial :: Integer -> Integer + factorial 1 = 1 + factorial n = n * (factorial (n-1)) \ No newline at end of file diff --git a/Factorial/Factorial.java b/Factorial/Factorial.java index c8546aa7..d830abf0 100644 --- a/Factorial/Factorial.java +++ b/Factorial/Factorial.java @@ -1,40 +1,19 @@ import java.util.Scanner; -/*This a example of Factorization*/ -public class Main { - public static long factorial(int n){ - long accumulator = 1; +class Factorial{ - for(; n > 0; n--){ - accumulator *= n; - } - - return accumulator; - } - - public static boolean isInteger(String text){ - try{ - Integer.parseInt(text); - return true; - }catch(Exception e){ - return false; - } - } - - public static void main(String[] args){ - InputStreamReader reader = new InputStreamReader(System.in); - BufferedReadLine br = new BufferedReadLine(reader); - System.out.println("Enter a number to factorialize: "); - int input = Integer.parseInt(); - - if(isInteger(input)){ - try{ - int num = Integer.parseInt(input); - System.out.println("The factorial of " + num + " is " + factorial(num)); - }catch(NumberFormatException e){ - System.out.println("What happened there ?"); - } - }else { - System.out.println("The input was not a number"); - } + public static void main(String[] args){ + + Scanner s=new Scanner(System.in); + System.out.println("Enter a non negative number"); + int n=s.nextInt(); + int fact=1; + if(n==0 || n==1){ + fact=1; + }else{ + + for(int i=2;i<=n;i++) + fact*=i; } + System.out.println("Factorial of the number is "+fact); + } } diff --git a/FibonacciSeq/Fibonacci.hs b/FibonacciSeq/Fibonacci.hs index 9a1d006c..4600d87b 100644 --- a/FibonacciSeq/Fibonacci.hs +++ b/FibonacciSeq/Fibonacci.hs @@ -1,10 +1,9 @@ --- Inplementation of a fibonacci sequence in Haskell --- Calculating the nth number in the sequence -fibonacci :: Integer -> Integer -fibonacci 0 = 0 -fibonacci 1 = 1 -fibonacci n = fibonacci (n-1) + fibonacci (n-2) - --- Generating the first n numbers in the sequence -fibseq :: Integer -> [Integer] -fibseq n = [fibonacci x | x <- [1..n]] +-- Luis L Reyes +-- fibonacci function takes in the nth number +-- of the sequence you want where sequence +-- is 1 while n = 0 or 1 +module Fibonacci where + fibonacci :: Integer -> Integer + fibonacci 0 = 1 + fibonacci 1 = 1 + fibonacci n = (fibonacci (n-1)) + (fibonacci(n-2)); \ No newline at end of file diff --git a/FibonacciSeq/Fibonacci.rb b/FibonacciSeq/Fibonacci.rb index 914be7a3..7c4ee3e9 100644 --- a/FibonacciSeq/Fibonacci.rb +++ b/FibonacciSeq/Fibonacci.rb @@ -1,17 +1,14 @@ -class Fibonacci - def series - puts "Enter the Fibonacci value" - n=gets.to_i - f1=0 - f2=1 - f3=0 - while f3 -#include -using namespace std; +#include -int maxArrSum(vectorarr, int n) -{ - int currMax = arr[0]; - int maxSoFar = arr[0]; +int Solve(int arr[], int size); - for(int i=1;i arr(1000); - cin>>n; - for(int i=0;i>arr[i]; +int Solve(int arr[], int size) { + + int max=0, cur=0; + for(int i=0; i +#include +#define lli long long using namespace std; -void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) -{ - if (n > 0) - { - towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); - cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; - towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); - } -} +lli dp[202]; int main() { - int n; - cout<<" enter number of disks "; - cin>>n; //user defined number of disks; - towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods - return 0; -} + int t,n; + lli x,y; + cin >> t; + assert(t<=10); + while ( t-- ) { + cin >> n; + assert(n<=200); + vector < pair > v; + for ( int i = 0; i < n; i++ ) { + cin >> x >> y; + assert(x<=1000000000); + assert(y<=1000000000); + v.push_back(make_pair(x,y)); + } + sort(v.begin(),v.end()); + dp[0] = v[0].second; + lli ans = v[0].second; + for ( int i = 1; i < n; i++ ) { + dp[i] = v[i].second; + for ( int j = 0; j < i; j++ ) { + if ( v[i].second > v[j].second && v[i].first > v[j].first ) dp[i] = max(dp[i], dp[j]+v[i].second); + } + ans = max(ans, dp[i]); + } + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/bubble_sort/BubbleSort.kt b/bubble_sort/BubbleSort.kt index d74129df..d81fa7d8 100644 --- a/bubble_sort/BubbleSort.kt +++ b/bubble_sort/BubbleSort.kt @@ -1,13 +1,36 @@ -fun bubbleSort(numbers: IntArray) { - for (pass in 0 until (numbers.size - 1)) { - // A single pass of bubble sort - for (currentPosition in 0 until (numbers.size - 1)) { - // This is a single step - if (numbers[currentPosition] > numbers[currentPosition + 1]) { - val tmp = numbers[currentPosition] - numbers[currentPosition] = numbers[currentPosition + 1] - numbers[currentPosition + 1] = tmp - } + +import java.util.* +import java.lang.* +import java.io.* + +class BubbleSort { + internal fun bubbleSort(arr: IntArray) { + val n = arr.size + for (i in 0 until n - 1) + for (j in 0 until n - i - 1) + if (arr[j] > arr[j + 1]) { + val temp = arr[j] + arr[j] = arr[j + 1] + arr[j + 1] = temp + } + } + + internal fun printArray(arr: IntArray) { + val n = arr.size + for (i in 0 until n) + print(arr[i].toString() + " ") + println() + } + + companion object { + + @JvmStatic + fun main(args: Array) { + val ob = BubbleSort() + val arr = intArrayOf(120, 24, 29, 172, 2, 1, 5) + ob.bubbleSort(arr) + println("Sorted array") + ob.printArray(arr) } } } diff --git a/magic8ball/magic8ball.py b/magic8ball/magic8ball.py new file mode 100644 index 00000000..9d5a2824 --- /dev/null +++ b/magic8ball/magic8ball.py @@ -0,0 +1,25 @@ +import random + +def magic_8_ball(): + responses = [ + "Yes, definitely.", + "No, not a chance.", + "I'm not sure, try again later.", + "Ask again later.", + "Yes, but proceed with caution.", + "No, it's not advisable.", + "Absolutely!", + "I wouldn't count on it." + ] + + print("Welcome to the Magic 8-Ball!") + while True: + question = input("Ask a yes or no question (or type 'quit' to exit): ") + if question.lower() == 'quit': + print("Goodbye!") + break + else: + print(random.choice(responses)) + +if __name__ == "__main__": + magic_8_ball() diff --git a/magic8ball/readme.md b/magic8ball/readme.md new file mode 100644 index 00000000..a52533ac --- /dev/null +++ b/magic8ball/readme.md @@ -0,0 +1,7 @@ +Welcome to the Magic 8-Ball! +Ask a yes or no question (or type 'quit' to exit): Will it rain tomorrow? +Yes, definitely. +Ask a yes or no question (or type 'quit' to exit): Should I take that job offer? +No, not a chance. +Ask a yes or no question (or type 'quit' to exit): quit +Goodbye!