diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..48bd760e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "ostream": "cpp", + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/C++/Bubble_Sort.cpp b/C++/Bubble_Sort.cpp new file mode 100644 index 00000000..22e816a9 --- /dev/null +++ b/C++/Bubble_Sort.cpp @@ -0,0 +1,44 @@ +#include +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: "<>n; + cout<<"Enter the elements in the array: "<>arr[i]; + } + bubbleSort(arr, n); + cout << "Sorted array: \n"; + printArray(arr, n); + return 0; +} diff --git a/C++/Bubble_Sort.exe b/C++/Bubble_Sort.exe new file mode 100644 index 00000000..db6e225c Binary files /dev/null and b/C++/Bubble_Sort.exe differ diff --git a/C++/Matrix_Multiplication.cpp b/C++/Matrix_Multiplication.cpp new file mode 100644 index 00000000..1aeb71b5 --- /dev/null +++ b/C++/Matrix_Multiplication.cpp @@ -0,0 +1,52 @@ +#include +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; +} diff --git a/HackerRank-Python-main/Tice Tac Toe.py b/HackerRank-Python-main/Tice Tac Toe.py new file mode 100644 index 00000000..c23dda57 --- /dev/null +++ b/HackerRank-Python-main/Tice Tac Toe.py @@ -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()))