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

Bubble-Sort #884

Open
wants to merge 3 commits into
base: main
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
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"ostream": "cpp",
"iostream": "cpp"
}
}
44 changes: 44 additions & 0 deletions C++/Bubble_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;

void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
}

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

int main()
{
cout<<"Enter the number of elements in the array: "<<endl;
int n;
cin>>n;
cout<<"Enter the elements in the array: "<<endl;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
bubbleSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
Binary file added C++/Bubble_Sort.exe
Binary file not shown.
52 changes: 52 additions & 0 deletions C++/Matrix_Multiplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <bits/stdc++.h>
using namespace std;
#define R1 2
#define C1 2
#define R2 2
#define C2 2

void mulMat(int mat1[][C1], int mat2[][C2])
{
int rslt[R1][C2];

cout << "Multiplication of given two matrices is:\n";

for (int i = 0; i < R1; i++) {
for (int j = 0; j < C2; j++) {
rslt[i][j] = 0;

for (int k = 0; k < R2; k++) {
rslt[i][j] += mat1[i][k] * mat2[k][j];
}

cout << rslt[i][j] << "\t";
}

cout << endl;
}
}

int main()
{
int mat1[R1][C1] = { { 1, 1 },
{ 2, 2 } };

int mat2[R2][C2] = { { 1, 1 },
{ 2, 2 } };

if (C1 != R2) {
cout << "The number of columns in Matrix-1 must "
"be equal to the number of rows in "
"Matrix-2"
<< endl;
cout << "Please update MACROs according to your "
"array dimension in #define section"
<< endl;

exit(EXIT_FAILURE);
}

mulMat(mat1, mat2);

return 0;
}
99 changes: 99 additions & 0 deletions HackerRank-Python-main/Tice Tac Toe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import numpy as np
import random
from time import sleep

def create_board():
return(np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))

def possibilities(board):
l = []

for i in range(len(board)):
for j in range(len(board)):

if board[i][j] == 0:
l.append((i, j))
return(l)

def random_place(board, player):
selection = possibilities(board)
current_loc = random.choice(selection)
board[current_loc] = player
return(board)

def row_win(board, player):
for x in range(len(board)):
win = True

for y in range(len(board)):
if board[x, y] != player:
win = False
continue

if win == True:
return(win)
return(win)

def col_win(board, player):
for x in range(len(board)):
win = True

for y in range(len(board)):
if board[y][x] != player:
win = False
continue

if win == True:
return(win)
return(win)

def diag_win(board, player):
win = True
y = 0
for x in range(len(board)):
if board[x, x] != player:
win = False
if win:
return win
win = True
if win:
for x in range(len(board)):
y = len(board) - 1 - x
if board[x, y] != player:
win = False
return win

def evaluate(board):
winner = 0

for player in [1, 2]:
if (row_win(board, player) or
col_win(board,player) or
diag_win(board,player)):

winner = player

if np.all(board != 0) and winner == 0:
winner = -1
return winner

def play_game():
board, winner, counter = create_board(), 0, 1
print(board)
sleep(2)

while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + " move")
print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return(winner)

print("Winner is: " + str(play_game()))