From 8a28b3c01a87e363137726ac4226609b94fac6c6 Mon Sep 17 00:00:00 2001 From: DualityStudios Date: Mon, 3 Oct 2022 09:52:47 -0700 Subject: [PATCH 1/2] Added Error Loop --- AreYouOk/AreYouOk.py | 83 ++++++++++++++++++++------ Binary Search/BinarySearch.cpp | 81 +++++++++++++------------ Factorial/Factorial.hs | 10 ++-- Factorial/Factorial.java | 51 +++++++++++----- FibonacciSeq/Fibonacci.hs | 19 +++--- FibonacciSeq/Fibonacci.rb | 27 +++++---- Kadane'sAlgorithm/Kadane.cpp | 41 +++++++------ PalindromeChecker/PalindromeChecker.py | 27 ++++++--- Tower_of_Hanoi/Tower_Of_Hanoi.cpp | 47 +++++---------- VolumeOfCylinder/volumeOfCylinder.rb | 19 ++++-- bubble_sort/BubbleSort.kt | 43 ++++--------- 11 files changed, 254 insertions(+), 194 deletions(-) diff --git a/AreYouOk/AreYouOk.py b/AreYouOk/AreYouOk.py index 7b3c30e0..d0beecab 100644 --- a/AreYouOk/AreYouOk.py +++ b/AreYouOk/AreYouOk.py @@ -1,43 +1,92 @@ import webbrowser import time +QUIT_REQUEST_RESPONSES = [ + "quit", + "exit", + "leave", + "stop", + "cancel", + "cease", + "kill", + "go away"] + VALID_YES_RESPONSES = [ "y", "yes", "yeah", "yep", "ye" -] - + "yea" + "yae" + "mhm" + 'yas' + 'yup' + 'sure'] VALID_NO_RESPONSES = [ "n", "no", "nah", - "nope" -] + "nope"] + +def printellipsis(time = 2.0): + interval = time / 3 + for loop in range (3): + print(".", end = '') + time.sleep(interval) + def print_ok(): print(":D") print("Well, everything actually seems ok with you") + time.sleep(1.5) + print("") def print_not_ok(): print(":(") print("Hope you get better my friend, help is on it's way...") time.sleep(3) - webbrowser.open('https://giphy.com/search/puppy') + webbrowser.open('https://www.bing.com/images/search?q=puppy') def print_definitely_not_ok(): + print("You're definitely not ok.") -print("Oh, hey there, are u ok buddy?") -is_okay_response = input() -if is_okay_response in VALID_NO_RESPONSES: - print_not_ok() -elif is_okay_response in VALID_YES_RESPONSES: - print("Really? oh that's great") - print("Hey have you ever searched for 'sea doggos' videos?") - has_searched_response = input() - if has_searched_response in VALID_NO_RESPONSES: - print_definitely_not_ok() - elif has_searched_response in VALID_YES_RESPONSES: - print_ok() + +def main(): + while True: + try: + help() + except: + for loop in range (2): + print("...") + print("oh dear god what is that") + print() + +def help(): + print("Oh, hey there, are u ok buddy?") + is_okay_response = input() + if is_okay_response in QUIT_REQUEST_RESPONSES: + print("okay, exiting the program", end='') + for loop in range(3): + print(".", end='') + time.sleep(1) + print() + SystemExit() + elif is_okay_response in VALID_NO_RESPONSES: + print_not_ok() + elif is_okay_response in VALID_YES_RESPONSES: + print("Really? oh that's great") + print("Hey have you ever searched for 'sea doggos' videos?") + has_searched_response = input() + if has_searched_response in VALID_NO_RESPONSES: + print_definitely_not_ok() + time.sleep(15) + webbrowser.open("https://www.bing.com/images/search?q=sea+doggos") + elif has_searched_response in VALID_YES_RESPONSES: + print_ok() + + else: + raise Exception("invalid answer") + +main() \ No newline at end of file diff --git a/Binary Search/BinarySearch.cpp b/Binary Search/BinarySearch.cpp index 26616d8c..dc302f77 100644 --- a/Binary Search/BinarySearch.cpp +++ b/Binary Search/BinarySearch.cpp @@ -1,42 +1,45 @@ -#include +//C++ program to implement Binary search using recursive method +//For this we are hoping that the array is sorted + +#include + using namespace std; -int main() -{ - int count, i, arr[30], num, first, last, middle; - cout<<"how many elements would you like to enter?:"; - cin>>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< 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 : "< Integer - factorial 1 = 1 - factorial n = n * (factorial (n-1)) \ No newline at end of file +-- Standard inplementation of a factorial in Haskell +factorial :: Integer -> Integer +factorial 0 = 1 +factorial n = n * factorial (n - 1) diff --git a/Factorial/Factorial.java b/Factorial/Factorial.java index d830abf0..c8546aa7 100644 --- a/Factorial/Factorial.java +++ b/Factorial/Factorial.java @@ -1,19 +1,40 @@ import java.util.Scanner; -class Factorial{ +/*This a example of Factorization*/ +public class Main { + public static long factorial(int n){ + long accumulator = 1; - 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; + 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"); + } } - System.out.println("Factorial of the number is "+fact); - } } diff --git a/FibonacciSeq/Fibonacci.hs b/FibonacciSeq/Fibonacci.hs index 4600d87b..9a1d006c 100644 --- a/FibonacciSeq/Fibonacci.hs +++ b/FibonacciSeq/Fibonacci.hs @@ -1,9 +1,10 @@ --- 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 +-- 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]] diff --git a/FibonacciSeq/Fibonacci.rb b/FibonacciSeq/Fibonacci.rb index 7c4ee3e9..914be7a3 100644 --- a/FibonacciSeq/Fibonacci.rb +++ b/FibonacciSeq/Fibonacci.rb @@ -1,14 +1,17 @@ -def fib(n) - if n <= 2 - return 1 - else - return fib(n-1) + fib(n-2) +class Fibonacci + def series + puts "Enter the Fibonacci value" + n=gets.to_i + f1=0 + f2=1 + f3=0 + while f3 -#include +#include +using namespace std; -int Solve(int arr[], int size); +int maxArrSum(vectorarr, int n) +{ + int currMax = arr[0]; + int maxSoFar = arr[0]; -int main() { - int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}; - int size = 8; - std::cout << Solve(arr, size) << std::endl; - return 0; + for(int i=1;i arr(1000); + cin>>n; + for(int i=0;i>arr[i]; } - return max; -} + cout< -#define lli long long +#include using namespace std; -lli dp[202]; +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); + } +} int main() { - 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 + 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; +} diff --git a/VolumeOfCylinder/volumeOfCylinder.rb b/VolumeOfCylinder/volumeOfCylinder.rb index 5005740c..682c1113 100644 --- a/VolumeOfCylinder/volumeOfCylinder.rb +++ b/VolumeOfCylinder/volumeOfCylinder.rb @@ -1,7 +1,16 @@ -puts "Enter the radius of the cylinder: " -r = gets.chomp.to_f -puts "Enter the height of the cylinder: " -h = gets.chomp.to_f +include Math -puts "The volume of the cylinder is: " + (Math::PI * h * r ** 2).round(4).to_s +include Math + +def get_cylinder_volume(radius, height) + return Math::PI * radius ** 2 * height +end + + +print "Enter the radius of the cylinder: " +r = Float gets +print "Enter the height of the cylinder: " +h = Float gets + +puts("The volume of the cylinder is:", get_cylinder_volume(r, h)) diff --git a/bubble_sort/BubbleSort.kt b/bubble_sort/BubbleSort.kt index d81fa7d8..d74129df 100644 --- a/bubble_sort/BubbleSort.kt +++ b/bubble_sort/BubbleSort.kt @@ -1,36 +1,13 @@ - -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) +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 + } } } } From 610c56dc26123f33ae9ca5982246af89a27d2176 Mon Sep 17 00:00:00 2001 From: DualityStudios Date: Mon, 3 Oct 2022 10:05:02 -0700 Subject: [PATCH 2/2] Added very minor changes to make the program easier to use - changed the invalid input response to make sense, and added '>>> ' before any input prompt so the user knows where and when to input data. --- AreYouOk/AreYouOk.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AreYouOk/AreYouOk.py b/AreYouOk/AreYouOk.py index d0beecab..63fff398 100644 --- a/AreYouOk/AreYouOk.py +++ b/AreYouOk/AreYouOk.py @@ -60,12 +60,12 @@ def main(): except: for loop in range (2): print("...") - print("oh dear god what is that") + print("That isn't a valid response, please try again.") print() def help(): print("Oh, hey there, are u ok buddy?") - is_okay_response = input() + is_okay_response = input(">>> ") if is_okay_response in QUIT_REQUEST_RESPONSES: print("okay, exiting the program", end='') for loop in range(3): @@ -78,7 +78,7 @@ def help(): elif is_okay_response in VALID_YES_RESPONSES: print("Really? oh that's great") print("Hey have you ever searched for 'sea doggos' videos?") - has_searched_response = input() + has_searched_response = input(">>> ") if has_searched_response in VALID_NO_RESPONSES: print_definitely_not_ok() time.sleep(15)