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

Flappy bird game #1310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
59 changes: 35 additions & 24 deletions BFS/BFS.py
Original file line number Diff line number Diff line change
@@ -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)
81 changes: 39 additions & 42 deletions Binary Search/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
//C++ program to implement Binary search using recursive method
//For this we are hoping that the array is sorted

#include<bits/stdc++.h>

#include <iostream>
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 : "<<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;
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;
}
53 changes: 10 additions & 43 deletions Cyclic Sort/CyclicSort.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
#include <bits/stdc++.h>
#include<iostream>
using namespace std;

void cyclicSort(vector<int>& 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<n){
int correct=arr[i]-1;
if(arr[i]!=correct){
swap(arr[i],arr[correct]);
}
else{i++;}
}
}

void printArray(vector<int> arr)
{
int size=arr.size();
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout<<endl;
}

int main()
{
int n;
cin>>n;//Size of array
vector<int> v(n);
for(int i=0;i<n;i++)
{
cin>>v[i];//Input Array elements
}
cyclicSort(v);//Cyclic sort function
cout<<"Printing sorted array:"<<endl;
printArray(v);//Printing Sorted array
return 0;
}

}
10 changes: 6 additions & 4 deletions Factorial/Factorial.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-- Standard inplementation of a factorial in Haskell
factorial :: Integer -> 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))
51 changes: 15 additions & 36 deletions Factorial/Factorial.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
19 changes: 9 additions & 10 deletions FibonacciSeq/Fibonacci.hs
Original file line number Diff line number Diff line change
@@ -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));
27 changes: 12 additions & 15 deletions FibonacciSeq/Fibonacci.rb
Original file line number Diff line number Diff line change
@@ -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<n do
f3=f1+f2
puts f3
f1=f2
f2=f3
end
def fib(n)
if n <= 2
return 1
else
return fib(n-1) + fib(n-2)
end
end
obj1=Fibonacci.new
obj1.series

puts "Enter limit : "
limit = gets.to_i

result = fib(limit)

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

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

int maxArrSum(vector<int>arr, int n)
{
int currMax = arr[0];
int maxSoFar = arr[0];
int Solve(int arr[], int size);

for(int i=1;i<n;i++)
{
currMax = max(arr[i], currMax+arr[i]);
maxSoFar = max(currMax, maxSoFar);
}
return maxSoFar;
int main() {
int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int size = 8;
std::cout << Solve(arr, size) << std::endl;
return 0;
}
int main()
{
int n;

vector<int> arr(1000);
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
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);
}
cout<<maxArrSum(arr, n);
}
return max;
}
Loading