Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch Invalid Response #968

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 66 additions & 17 deletions AreYouOk/AreYouOk.py
Original file line number Diff line number Diff line change
@@ -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("That isn't a valid response, please try again.")
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()
81 changes: 42 additions & 39 deletions Binary Search/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
#include <iostream>
//C++ program to implement Binary search using recursive method
//For this we are hoping that the array is sorted

#include<bits/stdc++.h>

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<count; i++)
{
cout<<"Enter number "<<(i+1)<<": ";
cin>>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<<num<<" found in the array at the location "<<middle+1<<"\n";
break;
}
else {
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<num<<" not found in the array";
}
return 0;
// 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 : "<<isNumberFound;
// Since the returned index is not -1 we print that the number was found and at what index
else
cout<<"The number is not present in the array";
// else part is activated when the function returns -1 indicating that the number is not present

return 0;
}
10 changes: 4 additions & 6 deletions Factorial/Factorial.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
-- Luis L Reyes
-- Simple factorial function
module Factorial where
factorial :: Integer -> Integer
factorial 1 = 1
factorial n = n * (factorial (n-1))
-- Standard inplementation of a factorial in Haskell
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
51 changes: 36 additions & 15 deletions Factorial/Factorial.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
19 changes: 10 additions & 9 deletions FibonacciSeq/Fibonacci.hs
Original file line number Diff line number Diff line change
@@ -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));
-- 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]]
27 changes: 15 additions & 12 deletions FibonacciSeq/Fibonacci.rb
Original file line number Diff line number Diff line change
@@ -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<n do
f3=f1+f2
puts f3
f1=f2
f2=f3
end
end
end

puts "Enter limit : "
limit = gets.to_i

result = fib(limit)

puts "Fib(#{limit}) = #{result}"
obj1=Fibonacci.new
obj1.series
41 changes: 22 additions & 19 deletions Kadane'sAlgorithm/Kadane.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
/*
* Kadane's algorithm in C++
*/

#include <iostream>
#include <algorithm>
#include<vector>
using namespace std;

int Solve(int arr[], int size);
int maxArrSum(vector<int>arr, 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<n;i++)
{
currMax = max(arr[i], currMax+arr[i]);
maxSoFar = max(currMax, maxSoFar);
}
return maxSoFar;
}
int main()
{
int n;

int Solve(int arr[], int size) {

int max=0, cur=0;
for(int i=0; i<size; ++i) {
cur = std::max(arr[i], cur+arr[i]);
max = std::max(cur, max);
vector<int> arr(1000);
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
return max;
}
cout<<maxArrSum(arr, n);
}
27 changes: 19 additions & 8 deletions PalindromeChecker/PalindromeChecker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
num = input('Enter any number : ')
try:
val = int(num)
if num == str(num)[::-1]:
print('The given number is PALINDROME')
def check_palin(string):
n = len(string)
for i in range(int(n/2)):
if(string[i]!=string[n-i-1]):
return False

return True

def main():
print("Input the String: ")
string = input()

if(check_palin(string)):
print("String is a Palindrome")
else:
print('The given number is NOT a palindrome')
except ValueError:
print("That's not a valid number, Try Again !")
print("String is not a Palindrome")

if __name__ == '__main__':
main()

Loading