diff --git a/ANAGRAM/Main.java b/ANAGRAM/Main.java new file mode 100644 index 0000000..a142f50 --- /dev/null +++ b/ANAGRAM/Main.java @@ -0,0 +1,39 @@ +// code for anagrams in java // +package strings; + +public class Anagrams { + + public static void main(String[] args) { + boolean isAnagram = true; + String a = "aab"; + String b = "abc"; + + int al [] = new int [256]; + int bl [] = new int [256]; + for(char c : a.toCharArray()) { + int index = (int) c; + al[index]++; + + } + + for(char c : b.toCharArray()) { + int index = (int) c; + bl[index]++; + + } + for(int i=0 ; i<256 ; i++) { + if(al[i] == bl[i]) { + isAnagram = true; + break; + } + } + if(isAnagram) { + System.out.println("Anagram"); + }else{ + System.out.println("Not Anagram"); + } + + + } + +} diff --git a/Armstrong.cpp b/Armstrong.cpp new file mode 100644 index 0000000..1371cd8 --- /dev/null +++ b/Armstrong.cpp @@ -0,0 +1,52 @@ +// C++ program to determine whether the number is +// Armstrong number or not +#include +using namespace std; + +/* Function to calculate x raised to the power y */ +int power(int x, unsigned int y) +{ + if (y == 0) + return 1; + if (y % 2 == 0) + return power(x, y / 2) * power(x, y / 2); + return x * power(x, y / 2) * power(x, y / 2); +} + +/* Function to calculate order of the number */ +int order(int x) +{ + int n = 0; + while (x) { + n++; + x = x / 10; + } + return n; +} + +// Function to check whether the given number is +// Armstrong number or not +bool isArmstrong(int x) +{ + // Calling order function + int n = order(x); + int temp = x, sum = 0; + while (temp) { + int r = temp % 10; + sum += power(r, n); + temp = temp / 10; + } + + // If satisfies Armstrong condition + return (sum == x); +} + +// Driver Program +int main() +{ + int x = 153; + cout << boolalpha << isArmstrong(x) << endl; + x = 1253; + cout << boolalpha << isArmstrong(x) << endl; + return 0; +} diff --git a/Array Deque/Main.java b/Array Deque/Main.java new file mode 100644 index 0000000..aa4b1ba --- /dev/null +++ b/Array Deque/Main.java @@ -0,0 +1,69 @@ +// using array deque in java // + +package deque; + +import java.util.ArrayDeque; + +public class MainClass { + + public static void main(String[] args) { + + + ArrayDeque ad = new ArrayDeque<>(); + + ad.push(12); + ad.push(23); + ad.push(34); + System.out.println(ad.pop()); + System.out.println(ad.pop()); + + } + +} + +// implementation of array deque in java // + +package deque; + +public class MyDeque { + + Node head,tail; + + public void addToHead(E data) { + Node toAdd = new Node <>(data); + if(head==null) { + head = tail = toAdd; + return; + } + head.next = toAdd; + toAdd.prev = head; + head = toAdd; + } + + + public E removeLast() { + if(head == null) { + return null; + } + + Node toRemove = tail; + tail = tail.next; + tail.prev = null ; + if(tail == null) { + head = null; + } + return toRemove.data; + + } + public static class Node{ + E data; + Node next, prev; + public Node(E Data) { + this.data = data; + this.next = this.prev = null; + } + } + + + +} diff --git a/BMI_calculator.py b/BMI_calculator.py new file mode 100644 index 0000000..7226a8d --- /dev/null +++ b/BMI_calculator.py @@ -0,0 +1,36 @@ +# calculating BMI by taking height and weight as input + +print("Please press 1 if you want to give your weight and height in lbs and inches respectively \n and press 2 if you want to give your weight and height in kgs and cms respectively:", end=" ") +x= int(input()) + +if x==1: +# calculate in lb and in: + print("Please specify your weight:", end=" ") + weight = float(input()) + print("Please specify your height:", end=" ") + height = float(input()) + + bmi = (weight*703)/(height*height) + +elif x==2: +#calculate in kgs and cms: + + print("Please input your height in cm:", end=" ") + height = float(input()) + print("Please input your weight in kgs:", end= " ") + weight = float(input()) + + bmi = (weight*10000)/(height*height) +else: + print("Wrong input! Exiting...") + exit() + +print("BMI = ", end="") +print(bmi) + +if bmi<17.5: print("You are Underweight") +elif bmi>=17.5 and bmi<25: print("You are Normal weight") +elif bmi>=25 and bmi<30: print("You are Overweight") +elif bmi>=30 and bmi<35: print("You are Obese (class 1)") +elif bmi>=35 and bmi<40: print("You are Obese (class 2)") +elif bmi>=40: print("You are Obese (class 3)") \ No newline at end of file diff --git a/Bernoulli Trial Probability.c b/Bernoulli Trial Probability.c new file mode 100644 index 0000000..9ca8afd --- /dev/null +++ b/Bernoulli Trial Probability.c @@ -0,0 +1,37 @@ +#include +#include +int main() { + float a,n,r,ncr,p; + float x,y,z,fact1,fact2,fact3; + + fact1=1; + for(x=1; x<=n;x++) + fact1=fact1*x; + fact2=1; + for(y=1; y<=r;y++) + fact2=fact2*y; + fact3=3; + for(z=1; z<=n;z++) + fact3=fact3*z; +printf("Provide the probability of success="); +scanf("%f",&a); +if (a>1) +printf ("Input error"); +else{ +printf("Provide number of trials held="); +scanf("%f",&n); +printf("Provide your desired number of success="); +scanf("%f",&r); +if (r>n) +printf("Input Error"); +else +{ +ncr= fact1/((fact2)*(fact3)); +p= ncr*pow(a,r)*pow(1-a,n-r); +if (p>1) +printf("Input Error, Check Again"); +else +printf("Your asked probability=%f",p); +} +} +} diff --git a/Binary-Search.java b/Binary-Search.java index 70cf954..4f76fb8 100644 --- a/Binary-Search.java +++ b/Binary-Search.java @@ -98,11 +98,11 @@ public static void main(String[] args) { if(index == -1){ - System.out.print("not found"); + System.out.println("not found"); }else{ - System.out.print(index); + System.out.println(index); } } diff --git a/Breast_cancer_prediction.ipynb b/Breast_cancer_prediction.ipynb new file mode 100644 index 0000000..323bfe8 --- /dev/null +++ b/Breast_cancer_prediction.ipynb @@ -0,0 +1,369 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
iddiagnosisradius_meantexture_meanperimeter_meanarea_meansmoothness_meancompactness_meanconcavity_meanconcave points_mean...texture_worstperimeter_worstarea_worstsmoothness_worstcompactness_worstconcavity_worstconcave points_worstsymmetry_worstfractal_dimension_worstUnnamed: 32
0842302M17.9910.38122.801001.00.118400.277600.30010.14710...17.33184.602019.00.16220.66560.71190.26540.46010.11890NaN
1842517M20.5717.77132.901326.00.084740.078640.08690.07017...23.41158.801956.00.12380.18660.24160.18600.27500.08902NaN
284300903M19.6921.25130.001203.00.109600.159900.19740.12790...25.53152.501709.00.14440.42450.45040.24300.36130.08758NaN
384348301M11.4220.3877.58386.10.142500.283900.24140.10520...26.5098.87567.70.20980.86630.68690.25750.66380.17300NaN
484358402M20.2914.34135.101297.00.100300.132800.19800.10430...16.67152.201575.00.13740.20500.40000.16250.23640.07678NaN
\n", + "

5 rows × 33 columns

\n", + "
" + ], + "text/plain": [ + " id diagnosis radius_mean texture_mean perimeter_mean area_mean \\\n", + "0 842302 M 17.99 10.38 122.80 1001.0 \n", + "1 842517 M 20.57 17.77 132.90 1326.0 \n", + "2 84300903 M 19.69 21.25 130.00 1203.0 \n", + "3 84348301 M 11.42 20.38 77.58 386.1 \n", + "4 84358402 M 20.29 14.34 135.10 1297.0 \n", + "\n", + " smoothness_mean compactness_mean concavity_mean concave points_mean \\\n", + "0 0.11840 0.27760 0.3001 0.14710 \n", + "1 0.08474 0.07864 0.0869 0.07017 \n", + "2 0.10960 0.15990 0.1974 0.12790 \n", + "3 0.14250 0.28390 0.2414 0.10520 \n", + "4 0.10030 0.13280 0.1980 0.10430 \n", + "\n", + " ... texture_worst perimeter_worst area_worst smoothness_worst \\\n", + "0 ... 17.33 184.60 2019.0 0.1622 \n", + "1 ... 23.41 158.80 1956.0 0.1238 \n", + "2 ... 25.53 152.50 1709.0 0.1444 \n", + "3 ... 26.50 98.87 567.7 0.2098 \n", + "4 ... 16.67 152.20 1575.0 0.1374 \n", + "\n", + " compactness_worst concavity_worst concave points_worst symmetry_worst \\\n", + "0 0.6656 0.7119 0.2654 0.4601 \n", + "1 0.1866 0.2416 0.1860 0.2750 \n", + "2 0.4245 0.4504 0.2430 0.3613 \n", + "3 0.8663 0.6869 0.2575 0.6638 \n", + "4 0.2050 0.4000 0.1625 0.2364 \n", + "\n", + " fractal_dimension_worst Unnamed: 32 \n", + "0 0.11890 NaN \n", + "1 0.08902 NaN \n", + "2 0.08758 NaN \n", + "3 0.17300 NaN \n", + "4 0.07678 NaN \n", + "\n", + "[5 rows x 33 columns]" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "data = pd.read_csv(\"https://raw.githubusercontent.com/pkmklong/Breast-Cancer-Wisconsin-Diagnostic-DataSet/master/data.csv\")\n", + "data.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "data_x = data.iloc[:,2:14]\n", + "data_y = data.iloc[:,1]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.model_selection import train_test_split\n", + "x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, test_size=0.2, random_state=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.preprocessing import StandardScaler\n", + "sc = StandardScaler()\n", + "x_train = sc.fit_transform(x_train)\n", + "x_test = sc.transform(x_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
KNeighborsClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "KNeighborsClassifier()" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.neighbors import KNeighborsClassifier\n", + "model_knn = KNeighborsClassifier(n_neighbors=5, metric='minkowski', p=2)\n", + "model_knn.fit(x_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['M' 'B' 'B' 'B' 'B' 'B' 'B' 'B' 'B' 'B' 'B' 'B' 'B' 'M' 'M' 'M' 'B' 'M'\n", + " 'M' 'M' 'M' 'M' 'B' 'B' 'M' 'B' 'B' 'M' 'B' 'M' 'B' 'M' 'B' 'M' 'B' 'M'\n", + " 'B' 'M' 'B' 'M' 'B' 'B' 'M' 'B' 'B' 'M' 'B' 'B' 'B' 'M' 'M' 'M' 'M' 'B'\n", + " 'B' 'B' 'B' 'B' 'B' 'M' 'M' 'M' 'B' 'B' 'M' 'B' 'M' 'M' 'M' 'B' 'B' 'M'\n", + " 'B' 'B' 'M' 'B' 'B' 'B' 'B' 'B' 'M' 'M' 'M' 'B' 'M' 'B' 'B' 'B' 'M' 'M'\n", + " 'B' 'B' 'B' 'M' 'B' 'B' 'M' 'B' 'B' 'B' 'B' 'B' 'B' 'B' 'M' 'M' 'M' 'B'\n", + " 'B' 'M' 'B' 'M' 'M' 'B']\n" + ] + } + ], + "source": [ + "y_pred = model_knn.predict(x_test)\n", + "print(y_pred)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Confusion Matrix \n", + "[[64 3]\n", + " [ 4 43]]\n", + "Accuracy of the model is: 94 %\n" + ] + } + ], + "source": [ + "from sklearn.metrics import confusion_matrix, accuracy_score\n", + "ac = accuracy_score(y_test, y_pred)\n", + "cm = confusion_matrix(y_test, y_pred)\n", + "print(f'Confusion Matrix \\n{cm}')\n", + "print(f'Accuracy of the model is: {ac*100:.0f} %')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.9.7 ('base')", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "1ff390c49dc86344ce1616d1333123d148d99e4b2158e5b394b29cd8845a560f" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Circular_queue.java b/Circular_queue.java new file mode 100644 index 0000000..836fecd --- /dev/null +++ b/Circular_queue.java @@ -0,0 +1,81 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com; + +/* + * + * @author zenalarifin + */ + +public class Program +{ + public static void main(String[] args) { + CQueue c1 = new CQueue(4); + c1.add(1); + c1.add(2); + c1.add(3); + c1.add(4); + c1.add(5); + c1.display(); + c1.delete(); + c1.add(6); + c1.display(); + } +} +class CQueue { + int capacity, front, end; + int arr[]; + CQueue(int size ) { + front = -1; + end = -1; + capacity = size; + arr = new int[size]; + } + void add(int item) { + if((front==capacity-1 && end==0) || end == front+1) { + System.out.println("Overflow"); + } + else { + if(front== -1 && end == -1) { + front = 0; + end = 0; + } + else { + front++; + if(front == capacity) { + front = front - capacity; + } + } + arr[front] = item; + } + } + void delete() { + if(front==-1 && end==-1) { + System.out.println("underflow"); + } + else { + if(front==end) { + front = -1; + end = -1; + } + else { + end++; + if(end==capacity) { + end=end-capacity; + } + } + } + } + void display() { + if(front==-1 && end==-1) { + System.out.println("Underflow"); + } + else { + for(int i=front;i<=end;i++) { + System.out.println(arr[i]); + } + } + } +} diff --git a/Counting_Sort.cpp b/Counting_Sort.cpp new file mode 100644 index 0000000..8516841 --- /dev/null +++ b/Counting_Sort.cpp @@ -0,0 +1,62 @@ +// Counting sort in C++ programming + +#include +using namespace std; + +void countSort(int array[], int size) { + // The size of count must be at least the (max+1) but + // we cannot assign declare it as int count(max+1) in C++ as + // it does not support dynamic memory allocation. + // So, its size is provided statically. + int output[10]; + int count[10]; + int max = array[0]; + + // Find the largest element of the array + for (int i = 1; i < size; i++) { + if (array[i] > max) + max = array[i]; + } + + // Initialize count array with all zeros. + for (int i = 0; i <= max; ++i) { + count[i] = 0; + } + + // Store the count of each element + for (int i = 0; i < size; i++) { + count[array[i]]++; + } + + // Store the cummulative count of each array + for (int i = 1; i <= max; i++) { + count[i] += count[i - 1]; + } + + // Find the index of each element of the original array in count array, and + // place the elements in output array + for (int i = size - 1; i >= 0; i--) { + output[count[array[i]] - 1] = array[i]; + count[array[i]]--; + } + + // Copy the sorted elements into original array + for (int i = 0; i < size; i++) { + array[i] = output[i]; + } +} + +// Function to print an array +void printArray(int array[], int size) { + for (int i = 0; i < size; i++) + cout << array[i] << " "; + cout << endl; +} + +// Driver code +int main() { + int array[] = {4, 2, 2, 8, 3, 3, 1}; + int n = sizeof(array) / sizeof(array[0]); + countSort(array, n); + printArray(array, n); +} \ No newline at end of file diff --git a/DFS_Visit.c b/DFS_Visit.c new file mode 100644 index 0000000..2926ac7 --- /dev/null +++ b/DFS_Visit.c @@ -0,0 +1,155 @@ +#include +#include +#define MAXNUM_VERTICES 100 + +typedef struct +{ + int n_vertices; + int n_edges; + int adjacency_matrix[MAXNUM_VERTICES][MAXNUM_VERTICES]; +} Graph; +typedef enum +{ + WHITE, + GRAY, + BLACK +} COLOR; + +#define Inf 100000000 +int finish_time[MAXNUM_VERTICES]; +int time = 0; +int topology[MAXNUM_VERTICES]; + +void inisialisasi_graph(Graph *g, int n_v, int n_e) +{ + int i, j; + + g->n_vertices = n_v; + g->n_edges = n_e; + + for (i = 0; i < MAXNUM_VERTICES; i++) + { + for (j = 0; j < MAXNUM_VERTICES; j++) + { + if (i < n_v && j < n_v) + { + g->adjacency_matrix[i][j] = 0; + } + else + { + g->adjacency_matrix[i][j] = -1; + } + } + } +} + +void print_adjacency_matrix(Graph *g) +{ + int i, j; + + for (i = 0; i < g->n_vertices; i++) + { + printf("\t%d", i); + } + printf("\n"); + + for (i = 0; i < g->n_vertices; i++) + { + printf("%d", i); + for (j = 0; j < g->n_vertices; j++) + { + printf("\t%d", g->adjacency_matrix[i][j]); + } + printf("\n"); + } +} + +int cek = 0; +void DFS_visit(Graph *g, COLOR *vertex_colors, int v) +{ + int i; + printf("%d ", v); + + vertex_colors[v] = GRAY; + + for (i = 0; i < g->n_vertices; i++) + { + if (g->adjacency_matrix[v][i] == 1 && vertex_colors[i] != WHITE) + { + cek = 1; + } + + if (g->adjacency_matrix[v][i] == 1 && vertex_colors[i] == WHITE) + { + DFS_visit(g, vertex_colors, i); + } + } + + vertex_colors[v] = BLACK; + time = time + 1; + finish_time[v] = time; + topology[time] = v; +} + +void DFS(Graph *g) +{ + COLOR vertex_colors[MAXNUM_VERTICES]; + int i; + + for (i = 0; i < g->n_vertices; i++) + { + vertex_colors[i] = WHITE; + } + + for (i = 0; i < g->n_vertices; i++) + { + if (vertex_colors[i] == WHITE) + { + DFS_visit(g, vertex_colors, i); + } + } + + printf("\n"); +} + +int cmp(const void *a, const void *b) +{ + return (*(int *)b - *(int *)a); +} + +int main() +{ + int n_v = 0; + int n_e = 0; + int i, j; + scanf("%d %d", &n_v, &n_e); + + Graph g; + inisialisasi_graph(&g, n_v, n_e); + + for (i = 0; i < n_e; i++) + { + int a, b; + scanf("%d %d", &a, &b); + g.adjacency_matrix[a][b] = 1; + } + + print_adjacency_matrix(&g); + DFS(&g); + if (cek == 1) + { + printf("Cycle\n"); + } + else + { + qsort(finish_time, g.n_vertices, sizeof(int), cmp); + printf("Hasil Topology : "); + for (int i = 0; i < g.n_vertices; i++) + { + printf("%d ", topology[finish_time[i]]); + } + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/DoubleLinkedlist.c b/DoubleLinkedlist.c new file mode 100644 index 0000000..9fc91eb --- /dev/null +++ b/DoubleLinkedlist.c @@ -0,0 +1,218 @@ +#include +#include +struct Node +{ + int val; + struct Node *prev; + struct Node *next; +}; +typedef struct Node N; +N *start = NULL,*ptr,*tmp,*t; + +void createList() +{ + ptr = (N *)malloc(sizeof(N)); + printf("Enter the value"); + scanf("%d",&ptr -> val); + ptr -> next = NULL; + if(start == NULL){ + start = ptr; + tmp = ptr; + ptr -> prev = NULL; + } + else{ + tmp -> next = ptr; + ptr -> prev = tmp; + tmp = ptr; + } +} + +void insertFirst(){ + ptr = (N *)malloc(sizeof(N)); + printf("Enter the value"); + scanf("%d",&ptr -> val); + ptr -> prev = NULL; + ptr -> next = start; + start -> prev = ptr; + start = ptr; +} + +void insertLast(){ + tmp = start; + ptr = (N *)malloc(sizeof(N)); + printf("Enter the value"); + scanf("%d",&ptr -> val); + while(tmp -> next != NULL){ + tmp = tmp -> next; + } + ptr -> prev = tmp; + tmp -> next = ptr; + ptr -> next = NULL; +} + +void insertAfter(){ + tmp = start; + int n; + printf("Enter the node"); + scanf("%d",&n); + ptr = (N *)malloc(sizeof(N)); + printf("Enter the value"); + scanf("%d",&ptr -> val); + while(tmp -> val != n){ + tmp = tmp -> next; + } + ptr -> prev = tmp; + ptr -> next = tmp -> next; + tmp -> next -> prev = ptr; + tmp -> next = ptr; +} + +void insertBefore(){ + tmp = start; + int n; + printf("Enter the node"); + scanf("%d",&n); + ptr = (N *)malloc(sizeof(N)); + printf("Enter the value"); + scanf("%d",&ptr -> val); + while(tmp -> val != n){ + t = tmp; + tmp = tmp -> next; + } + ptr -> next = tmp; + ptr -> prev = t; + t -> next = ptr; + tmp -> prev = ptr; +} + +void deleteFirst(){ + if(start -> next == NULL){ + start = NULL; + free(start); + } + else{ + tmp = start; + start = start -> next; + start -> prev = NULL; + free(tmp); + } +} + +void deleteLast() +{ + tmp = start; + while(tmp -> next != NULL){ + tmp = tmp -> next; + } + tmp -> prev -> next = NULL; + free(tmp); +} + +void deleteAny(){ + tmp = start; + int n; + printf("Enter the node"); + scanf("%d",&n); + while(tmp -> val != n){ + tmp = tmp -> next; + } + tmp -> prev -> next = tmp -> next; + tmp -> next -> prev = tmp -> prev; + free(tmp); +} + +void deletebefore(){ + int c = 0,n; + tmp = start; + printf("Enter the node number"); + scanf("%d",&n); + while(tmp != NULL){ + if(c == n){ + ptr = tmp -> prev; + ptr -> prev -> next = tmp; + tmp -> prev = ptr -> prev; + free(ptr); + } + tmp = tmp -> next; + c++; + } +} + +void deleteafter(){ + tmp = start; + int n; + printf("Enter the value"); + scanf("%d",&n); + while (tmp -> val != n) + { + tmp = tmp -> next; + } + ptr = tmp -> next; + tmp -> next = ptr -> next; + ptr -> next -> prev = tmp; + free(ptr); +} +void status() +{ + ptr = start; + if(ptr == NULL){ + printf("Empty List"); + return; + } + while(ptr -> next != NULL){ + printf("%d ->",ptr -> val); + ptr = ptr -> next; + } + while(ptr != NULL){ + printf("%d <-",ptr -> val); + ptr = ptr -> prev; + } +} + + +int main() +{ + int ch; + do{ + printf("\n=======MENU=======\n"); + printf("\n1..........CREATE LIST........"); + printf("\n2..........INSERT BEGINING........"); + printf("\n3..........INSERT LAST........"); + printf("\n4..........INSERT AFTER........"); + printf("\n5..........INSERT BEFORE........"); + printf("\n6..........DELETE FIRST........"); + printf("\n7..........DELETE LAST........"); + printf("\n8..........DELETE BEFORE........"); + printf("\n9..........DELETE AFTER........"); + printf("\n10..........DELETE ANY........"); + printf("\n11..........DISPLAY......."); + printf("\n12..........EXIT......."); + printf("\n Enter the choice"); + scanf("%d",&ch); + switch(ch){ + case 1: createList(); + break; + case 2: insertFirst(); + break; + case 3: insertLast(); + break; + case 4: insertAfter(); + break; + case 5: insertBefore(); + break; + case 6: deleteFirst(); + break; + case 7: deleteLast(); + break; + case 8: deletebefore(); + break; + case 9: deleteafter(); + break; + case 10: deleteAny(); + break; + case 11: status(); + break; + } + } + while(ch != 0); +} diff --git a/Filling Bucket.cpp b/Filling Bucket.cpp new file mode 100644 index 0000000..6f6a9f6 --- /dev/null +++ b/Filling Bucket.cpp @@ -0,0 +1,27 @@ + +#include +using namespace std; +class Solution { + public: + int fillingBucket(int N) { + int mod = 100000000; + long long result=1,a=1,b=1; + for(int i=2;i<=N;i++){ + result =(a+b)%mod; + a=b; + b=result; + } + return result; + } +}; +int main() { + int t; + cin >> t; + while (t--) { + int N; + cin>>N; + Solution ob; + cout << ob.fillingBucket(N) << endl; + } + return 0; +} diff --git a/Heap_Sort.py b/Heap_Sort.py new file mode 100644 index 0000000..dbbdab9 --- /dev/null +++ b/Heap_Sort.py @@ -0,0 +1,35 @@ +#github - akrishna5 + +def heapify(arr, n, i): + largest = i + l = 2 * i + 1 + r = 2 * i + 2 + + if l < n and arr[largest] < arr[l]: + largest = l + + if r < n and arr[largest] < arr[r]: + largest = r + + if largest != i: + arr[i], arr[largest] = arr[largest], arr[i] + + heapify(arr, n, largest) + +def heapSort(arr): + n = len(arr) + + for i in range(n//2 - 1, -1, -1): + heapify(arr, n, i) + + for i in range(n-1, 0, -1): + arr[i], arr[0] = arr[0], arr[i] + heapify(arr, i, 0) + +arr = [45,18,6,1,19,33] +heapSort(arr) +n = len(arr) +print("Sorted array is: ") +for i in range(n): + print("%d" % arr[i]), + diff --git a/Heap_sort_array.java b/Heap_sort_array.java new file mode 100644 index 0000000..4aef619 --- /dev/null +++ b/Heap_sort_array.java @@ -0,0 +1,74 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com; + +/* + * + * @author zenalarifin + */ + +import java.util.Arrays; + +public class HeapSort { + public void sort(int arr[]) + { + int n = arr.length; + + // Build heap + for (int i = n / 2 - 1; i >= 0; i--) + heapify(arr, n, i); + + // Extract element from heap + for (int i = n - 1; i > 0; i--) { + // Move root + int temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + + // Max heapify reduced heap + heapify(arr, i, 0); + } + } + + // To heapify a subtree that has root node i + void heapify(int arr[], int n, int i) + { + int large = i; // Set largest as root + int left = 2 * i + 1; + int right = 2 * i + 2; + + // If left child is larger than root + if (left < n && arr[left] > arr[large]) + large = left; + + // If right child is larger than largest so far + if (right< n && arr[right] > arr[large]) + large = right; + + // If largest is not root + if (large != i) { + int swap = arr[i]; + arr[i] = arr[large]; + arr[large] = swap; + + // Recursively heapify the subtree + heapify(arr, n, large); + } + } + + // Main driver code + public static void main(String args[]) + { + int arr[] = { 132, 44, 5, 42, 45, 167, 4, 52, 31, 16, 42 }; + int n = arr.length; + + HeapSort heap = new HeapSort(); + heap.sort(arr); + + System.out.println("HeapSorted array is:"); + System.out.println(Arrays.toString(arr)); + } +} + diff --git a/HelloWorld.cpp b/HelloWorld.cpp new file mode 100644 index 0000000..deca12e --- /dev/null +++ b/HelloWorld.cpp @@ -0,0 +1,14 @@ +// C++ program to display "Hello World" + +// Header file for input output functions +#include +using namespace std; + +// Main() function: where the execution of program begins +int main() +{ + // prints hello world + cout << "Hello World"; + + return 0; +} \ No newline at end of file diff --git a/Kadane's_Algorithm.cpp b/Kadane's_Algorithm.cpp new file mode 100644 index 0000000..0a7ba00 --- /dev/null +++ b/Kadane's_Algorithm.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +class Solution{ + public: + long long maxSubarraySum(int arr[], int n){ + int sum=0,max_sum=arr[0]; + for(int i=0;i>t; //input testcases + while(t--) //while testcases exist + { + + cin>>n; //input size of array + int a[n]; + for(int i=0;i>a[i]; //inputting elements of array + Solution ob; + cout << ob.maxSubarraySum(a, n) << endl; + } +} diff --git a/Linked List Cycle.cpp b/Linked List Cycle.cpp new file mode 100644 index 0000000..676a38c --- /dev/null +++ b/Linked List Cycle.cpp @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) { + + if(head==NULL) + return false; + + ListNode *fast = head; + ListNode *slow = head; + while(fast!=NULL && fast->next!=NULL){ + fast = fast->next->next; + slow = slow->next; + + if(fast==slow) + return true; + } + return false; + } +}; diff --git a/MergeSrot.c b/MergeSort.c similarity index 100% rename from MergeSrot.c rename to MergeSort.c diff --git a/Minimum Elements to Add to Form a Given Sum.py b/Minimum Elements to Add to Form a Given Sum.py new file mode 100644 index 0000000..0ad0f0c --- /dev/null +++ b/Minimum Elements to Add to Form a Given Sum.py @@ -0,0 +1,16 @@ +class Solution(object): + def minElements(self, nums, limit, goal): + d=abs(goal-sum(nums)) + c=d%limit + if c==0: + return d//limit + else: + return d//limit+1 + + """ + :type nums: List[int] + :type limit: int + :type goal: int + :rtype: int + """ + diff --git a/N Queens problem.cpp b/N Queens problem.cpp new file mode 100644 index 0000000..6e1e712 --- /dev/null +++ b/N Queens problem.cpp @@ -0,0 +1,111 @@ +/* C++ program to solve N Queen Problem using +backtracking */ + +#include +#define N 4 +using namespace std; + +/* A utility function to print solution */ +void printSolution(int board[N][N]) +{ + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) + cout << " " << board[i][j] << " "; + printf("\n"); + } +} + +/* A utility function to check if a queen can +be placed on board[row][col]. Note that this +function is called when "col" queens are +already placed in columns from 0 to col -1. +So we need to check only left side for +attacking queens */ +bool isSafe(int board[N][N], int row, int col) +{ + int i, j; + + /* Check this row on left side */ + for (i = 0; i < col; i++) + if (board[row][i]) + return false; + + /* Check upper diagonal on left side */ + for (i = row, j = col; i >= 0 && j >= 0; i--, j--) + if (board[i][j]) + return false; + + /* Check lower diagonal on left side */ + for (i = row, j = col; j >= 0 && i < N; i++, j--) + if (board[i][j]) + return false; + + return true; +} + +/* A recursive utility function to solve N +Queen problem */ +bool solveNQUtil(int board[N][N], int col) +{ + /* base case: If all queens are placed + then return true */ + if (col >= N) + return true; + + /* Consider this column and try placing + this queen in all rows one by one */ + for (int i = 0; i < N; i++) { + /* Check if the queen can be placed on + board[i][col] */ + if (isSafe(board, i, col)) { + /* Place this queen in board[i][col] */ + board[i][col] = 1; + + /* recur to place rest of the queens */ + if (solveNQUtil(board, col + 1)) + return true; + + /* If placing queen in board[i][col] + doesn't lead to a solution, then + remove queen from board[i][col] */ + board[i][col] = 0; // BACKTRACK + } + } + + /* If the queen cannot be placed in any row in + this column col then return false */ + return false; +} + +/* This function solves the N Queen problem using +Backtracking. It mainly uses solveNQUtil() to +solve the problem. It returns false if queens +cannot be placed, otherwise, return true and +prints placement of queens in the form of 1s. +Please note that there may be more than one +solutions, this function prints one of the +feasible solutions.*/ +bool solveNQ() +{ + int board[N][N] = { { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 } }; + + if (solveNQUtil(board, 0) == false) { + cout << "Solution does not exist"; + return false; + } + + printSolution(board); + return true; +} + +// driver program to test above function +int main() +{ + solveNQ(); + return 0; +} + +// This code is contributed by Aditya Kumar (adityakumar129) diff --git a/PRIME NUMBER/Main (1).java b/PRIME NUMBER/Main (1).java new file mode 100644 index 0000000..b840abe --- /dev/null +++ b/PRIME NUMBER/Main (1).java @@ -0,0 +1,30 @@ +// Prime Number code in java // + +package loops; + +import java.util.Scanner; + +public class PimeNumbers { + + public static void main(String[] args) { + + + Scanner sc= new Scanner(System.in); + + int n= sc.nextInt(); + boolean isprime=true; + for (int i=2;i*i<=n;i++) { + if(n%i==0) { + isprime=false; + break; + } + } + if(n<2) + isprime=false; + + System.out.println(" isprime?"+isprime); + + + } + +} diff --git a/Primefactors b/Primefactors new file mode 100644 index 0000000..cdd45aa --- /dev/null +++ b/Primefactors @@ -0,0 +1,17 @@ +#include +#include +void primefactor(int num) { + printf("Prime factors of the number : "); + for (int i = 2; num > 1; i++) { + while (num % i == 0) { + printf("%d ", i); + num = num / i; + } + } + } +int main() { + int num; + num=12; + primefactor(num); + return 0; +} diff --git a/Quick_sort.java b/Quick_sort.java new file mode 100644 index 0000000..742e01a --- /dev/null +++ b/Quick_sort.java @@ -0,0 +1,80 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com; + +/* + * + * @author zenalarifin + */ + +import java.util.ArrayList; +import java.util.Random; + +public class Program { + static ArrayList list=new ArrayList(); + static boolean swapped=true; + static ArrayList ii=new ArrayList(); + static ArrayList jj=new ArrayList(); + static int temp; + static long count=0; + + public static void main(String[] args) { + + int i=0; + + while (i<300) { + + list.add(new Random().nextInt(500)); + + i++; + } + + System.out.println(list); + int loi=0; + int hii=list.size(); + +sort(list,0,hii-1); + + System.out.println(list); + System.out.println("sort was done "+count+" times");} + + public static void sort(ArrayList sorte,int lo,int hi) { + + int x=sorte.get(lo+(hi-lo)/2); + + int i=lo; + int j= hi; + // System.out.println(list+"begin i= "+i+"wert "+list.get(i)+"j= "+j+"wert "+list.get(j)+"x= "+x+" lo="+lo+" hi= "+hi); + + count++; + + while(i<=j) { + + while(sorte.get(i)hi)i=lo;} + while(sorte.get(j)>x) {j--; if(j +#include +#include +#include +void main() +{ + int i,j,count=0; + char c='\n'; + char ch; + FILE *fp; + char Keyword[32][15]={ + "extern","return","union","const","float","short", + "auto","double","int","struct","break","else","long", + "goto","sizeof","voltile","do","if","static","while", + "unsigned","continue","for","signed","void","default", + "switch","case","enum","register","typedef","char" + }; + fp=fopen("file.txt","w"); + if(fp==NULL) + { + printf("File not exist"); + exit(1); + } + for(i=0;i<32;i++) + { + for(j=0;j +using namespace std; + +int main(){ +cout<<"hi"; + return 0; +} diff --git a/add_numbers.c b/add_numbers.c new file mode 100644 index 0000000..c48f7a1 --- /dev/null +++ b/add_numbers.c @@ -0,0 +1,22 @@ +/* +Rahul Kumar Gupta +https://github.com/rahulshivan05 +*/ + +#include + +int main(int argc, char const *argv[]) +{ + /* Add two numbers as you wish */ + + float a, b; + printf("Enter number a \n"); + scanf("%d", &a); + + printf("Enter number b\n"); + scanf("%d", &b); + + printf("The sum of number is %d\n", a + b); + + return 0; +} diff --git a/arraytobinarytree.cpp b/arraytobinarytree.cpp new file mode 100644 index 0000000..fab45ed --- /dev/null +++ b/arraytobinarytree.cpp @@ -0,0 +1,24 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* sortedArrayToBST(vector& nums) { + return helper(nums, 0, nums.size()-1); + } + + TreeNode* helper(vector& nums, int left, int right){ + + //base case + //If the left pointer crosses right return null; + if(left > right){ + return NULL; + } diff --git a/binary-tree-inorder-traversal.cpp b/binary-tree-inorder-traversal.cpp new file mode 100644 index 0000000..31d9b83 --- /dev/null +++ b/binary-tree-inorder-traversal.cpp @@ -0,0 +1,25 @@ + /** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector vec; + vector inorderTraversal(TreeNode* root) { + if(root==NULL){ + return vec; + }else{ + inorderTraversal(root->left); + vec.push_back(root->val); + inorderTraversal(root->right); + } + return vec; + } +}; diff --git a/binarySearch.py b/binarySearch.py new file mode 100644 index 0000000..3dd51db --- /dev/null +++ b/binarySearch.py @@ -0,0 +1,35 @@ +def binary_search(arr, low, high, x): + + # Check base case + if high >= low: + + mid = (high + low) // 2 + + # If element is present at the middle itself + if arr[mid] == x: + return mid + + # If element is smaller than mid, then it can only + # be present in left subarray + elif arr[mid] > x: + return binary_search(arr, low, mid - 1, x) + + # Else the element can only be present in right subarray + else: + return binary_search(arr, mid + 1, high, x) + + else: + # Element is not present in the array + return -1 + +# Test array +arr = [ 2, 3, 4, 10, 40 ] +x = 10 + +# Function call +result = binary_search(arr, 0, len(arr)-1, x) + +if result != -1: + print("Element is present at index", str(result)) +else: + print("Element is not present in array") diff --git a/bubble_sort.java b/bubble_sort.java new file mode 100644 index 0000000..b83fc59 --- /dev/null +++ b/bubble_sort.java @@ -0,0 +1,38 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com; + +/** + * + * @author zenalarifin + */ +class BubbleSort{ + static int[] arr = new int[10]; + public static void main(String[] args){ + for(int i=0; i<10; i++) + arr[i] = (int)(Math.random()*10); + + System.out.println("Diplay all elements: "); + for(int i : arr) + System.out.print(i+" "); + System.out.println("\n"); + + //Bubble sort + for(int i=arr.length-1; i>1; i--){ + for(int j=0; jarr[j+1]) + exchangeValues(j, j+1); + } + } + System.out.println("Sorted elements: "); + for(int i : arr) + System.out.print(i+" "); + } + static void exchangeValues(int index1, int index2){ + int t = arr[index1]; + arr[index1] = arr[index2]; + arr[index2] = t; + } +} diff --git a/bubblesort.java b/bubblesort.java new file mode 100644 index 0000000..052c336 --- /dev/null +++ b/bubblesort.java @@ -0,0 +1,59 @@ +import java.util.*; +class bubblesort +{ int A[];int n; + public bubblesort(int nn) + { n=nn; + A=new int[n]; + }// contructor to initialize data members + + + public void input() + { + Scanner sc=new Scanner(System.in); + System.out.println("Enter the elements of the array"); + for(int i=0;iA[j+1]) + { + int t=A[j]; + A[j]=A[j+1]; + A[j+1]=t; + } + } + } + }//sorting the array using bubble sort technique + + + public void display() + { sort(); + for(int i=0;i +#include +#include +#include +#include +#include +#include +using namespace std; +class Venue +{ + +public: + +string matchvenue(); + string teamselects(); + string teamselects2(); + string TossDecision(); + void datentime(); + +}; +class display +{ + + + public: + + void display1(); + void display2(); + char* convertIntegerToChar(int N); +}; +class pnode +{ + +public: + + string pname; + int pscore,pout; + pnode* nextp; + pnode() +{ + pname=""; + pscore=0; + pout=0; + nextp=NULL; +} + + + +}; +class score:public Venue +{ + +int tscore,tout,overs,balls;//total score //total out //overs +pnode* head; +pnode* tail; +pnode* head2; +pnode* tail2; + + +public: + + score() + { + head=NULL; + tail=NULL; + head2=NULL; + tail2=NULL; + tscore=0; +tout=0; + overs=0; + } + +string getplayers(); +string getplayers2(); +void dislayt(); +void dislayt2(); +string match(); + + + +}; + +string Venue::matchvenue() +{ + string cricketstadium[5]; + int loop = 0; + string stadium; + + ifstream input("cricketstadium.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + getline(input, stadium); + cricketstadium[loop] = stadium; + loop++; + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + + srand(time(0)); + int randindex = rand() % 4; + //cout << "STADIUM:"<nextp = NULL; + getline(input, player); + ptr->pname = player; + + if (head == NULL) + { + head = ptr; + tail = ptr; + + } + else + { + tail->nextp = ptr; + tail = ptr; + + + } + + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + } + else if (a.teamselects() == "India") + { + + ifstream input("India.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + + if (head == NULL) + { + head = ptr; + tail = ptr; + + } + else + { + tail->nextp = ptr; + tail = ptr; + + + } + + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + } + + else if (a.teamselects() == "England") + { + + ifstream input("England.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + + if (head == NULL) + { + head = ptr; + tail = ptr; + + } + else + { + tail->nextp = ptr; + tail = ptr; + + + } + + } + input.close(); + } + } + else if (a.teamselects() == "Bangladesh") + { + + ifstream input("Bangladesh.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + if (head == NULL) + { + head = ptr; + tail = ptr; + + } + else + { + tail->nextp = ptr; + tail = ptr; + + + } + + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + } + else if (a.teamselects() == "Australia") + { + + ifstream input("Australia.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + if (head == NULL) + { + head = ptr; + tail = ptr; + + } + else + { + tail->nextp = ptr; + tail = ptr; + + + } + + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + } + else + { + cout << "ERRO!"; + } + return player; +} +string score::getplayers2() +{ + + Venue a; + string player; + + if (a.teamselects2() == "Pakistan") + { + + ifstream input("Pakistan.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + + if (head2 == NULL) + { + head2 = ptr; + tail2 = ptr; + + } + else + { + tail2->nextp = ptr; + tail2 = ptr; + + + } + + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + } + else if (a.teamselects2() == "India") + { + + ifstream input("India.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + + if (head2 == NULL) + { + head2 = ptr; + tail2 = ptr; + + } + else + { + tail2->nextp = ptr; + tail2 = ptr; + + + } + + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + } + + else if (a.teamselects2() == "England") + { + + ifstream input("England.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + + if (head2 == NULL) + { + head2 = ptr; + tail2 = ptr; + + } + else + { + tail2->nextp = ptr; + tail2 = ptr; + + + } + + } + input.close(); + } + } + else if (a.teamselects2() == "Bangladesh") + { + + ifstream input("Bangladesh.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + + if (head2 == NULL) + { + head2 = ptr; + tail2 = ptr; + + } + else + { + tail2->nextp = ptr; + tail2 = ptr; + + + } + + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + } + else if (a.teamselects2() == "Australia") + { + + ifstream input("Australia.txt"); + if (input.is_open()) + { + while (!input.eof()) + { + pnode* ptr = new pnode(); + ptr->nextp = NULL; + getline(input, player); + ptr->pname = player; + + if (head2 == NULL) + { + head2 = ptr; + tail2 = ptr; + + } + else + { + tail2->nextp = ptr; + tail2 = ptr; + + + } + + } + input.close(); + } + else + { + cout << "CANT OPEN FILE!!"; + } + } + else + { + cout << "ERROR!"; + } + return player; +} + + +string score::match() +{ + + + + ofstream out("batsman.csv"); + ofstream outb("bowler.csv"); + + string a="LIN"; + srand(time(0)); + int decision;//if team 1 wins + int decision2;//if team 2 wins + decision=rand()%3; + decision2=rand()%3; + + //team 1 wins + if(TossDecision()=="(Heads)") + { + +//team 1 bats first + + + if(decision==1) + { + outtextxy(480,140,"WILL BAT FIRST"); + pnode*batsman=head; + pnode*bowler=head2; +int balls1; + + while(tout<10&&overs<10) + + { + +int outor; +outor=rand()%5; +if(outor==1) +{ + string a;//for batsman name + int b;//for score + + //writing batsman data in file when wickt falls + a=batsman->pname; + b=batsman->pscore; + out<nextp; +batsman->pscore+=0; +bowler->pout+=1; +tout+=1; +balls1+=1; +if(balls1>=6) +{ + string c; + int d; + c=bowler->pname; + d=bowler->pout; + outb<nextp; + overs+=1; + balls1=0; +} + + + +} +else +{ + + int randscore; + randscore=rand()%7; + batsman->pscore+=randscore; +balls1+=1; + if(balls1>=6) +{ + string c; + int d; + c=bowler->pname; + d=bowler->pout; + outb<nextp; +balls1=0; + overs+=1; + +} + +} + + } + } + else + { + + outtextxy(480,140,"WILL BOWL FIRST"); + pnode*batsman=head2; + pnode*bowler=head; + +int balls2; + + while(tout<10&&overs<10) + + { + +int outor; +outor=rand()%5; +if(outor==1) +{ + string a;//for batsman name + int b;//for score + + //writing batsman data in file when wickt falls + a=batsman->pname; + b=batsman->pscore; + out<nextp; +batsman->pscore+=0; +bowler->pout+=1; +tout+=1; +balls2+=1; +if(balls2>=6) +{ + string c; + int d; + c=bowler->pname; + d=bowler->pout; + outb<nextp; + overs+=1; + balls2=0; +} + + + +} +else +{ + + int randscore; + randscore=rand()%7; + batsman->pscore+=randscore; +balls2+=1; + if(balls2>=6) +{ + string c; + int d; + c=bowler->pname; + d=bowler->pout; + outb<nextp; +balls2=0; + overs+=1; + +} + +} + + } + } + + } + + // team 2 wins the toss + else if(TossDecision()=="(Tails)") + { + + + + if(decision2==1) + { + outtextxy(480,140,"WILL BAT FIRST"); + pnode*batsman=head2; + pnode*bowler=head; +int balls3; + + while(tout<10&&overs<10) + + { + +int outor; +outor=rand()%5; +if(outor==1) +{ + string a;//for batsman name + int b;//for score + + //writing batsman data in file when wickt falls + a=batsman->pname; + b=batsman->pscore; + out<nextp; +batsman->pscore+=0; +bowler->pout+=1; +tout+=1; +balls3+=1; +if(balls3>=6) +{ + string c; + int d; + c=bowler->pname; + d=bowler->pout; + outb<nextp; + balls3=0; + overs+=1; +} + + + +} +else +{ + + int randscore; + randscore=rand()%7; + batsman->pscore+=randscore; +balls3+=1; + if(balls3>=6) +{ + string c; + int d; + c=bowler->pname; + d=bowler->pout; + outb<nextp; +balls3=0; + overs+=1; + +} + +} + + } + } + else + { + + outtextxy(480,140,"WILL BOWL FIRST"); + pnode*batsman=head; + pnode*bowler=head2; + +int balls4; + + while(tout<10&&overs<10) + + { + +int outor; +outor=rand()%5; +if(outor==1) +{ + string a;//for batsman name + int b;//for score + + //writing batsman data in file when wickt falls + a=batsman->pname; + b=batsman->pscore; + out<nextp; +batsman->pscore+=0; +bowler->pout+=1; +tout+=1; +balls4+=1; +if(balls4>=6) +{ + string c; + int d; + c=bowler->pname; + d=bowler->pout; + outb<nextp; + overs+=1; + balls4=0; +} + + + +} +else +{ + + int randscore; + randscore=rand()%7; + batsman->pscore+=randscore; +balls4+=1; + if(balls4>=6) +{ + string c; + int d; + c=bowler->pname; + d=bowler->pout; + outb<nextp; +balls4=0; + overs+=1; + +} + +} + + } + } + + } + + return a; + + +} +//Team 1 display function + void score::dislayt() + { + pnode* disp; + disp = head; + + if (disp == NULL) + { + cout << "LIST IS EMPTY!!!"; + } + else + { + while (disp != NULL) + { + cout << disp->pname << "->"; + disp = disp->nextp; + } + } +//Team 2 display function + } + void score::dislayt2() + { + pnode* disp; + disp = head2; + + if (disp == NULL) + { + cout << "LIST IS EMPTY!!!"; + } + else + { + while (disp != NULL) + { + cout << disp->pname << "->"; + disp = disp->nextp; + } + } + + } + + + +int main() +{ + + + int x,y; + +display a; + + initwindow(1350,660);//window + abx: + cleardevice(); + +a.display1(); + + + +while (!ismouseclick(WM_LBUTTONDOWN)) + { + //triangle(maxx/divisor, maxy/divisor); +Venue b; +Venue bb; +score c; +score cc; + + delay(500); + + getmouseclick(WM_LBUTTONDOWN, x, y); + cout << "The mouse was clicked at: "; + cout << "x=" << x; + cout << " y=" << y << endl; + if(x>=220 && x<=694 && y>=200 && y<=240)//match + { + cleardevice(); +readimagefile("sc2.jpg",0,0,1350,660); +a.display2(); + setcolor(WHITE);//Score board + + +settextstyle(10,0,7);//score board +setbkcolor(BLUE); +outtextxy(350,20,"CRICKET SCOREBOARD"); +outtextxy(1190,600,"HOME"); + +settextstyle(10,0,3); +b.matchvenue(); +b.datentime(); +b.teamselects(); +b.teamselects2(); +c.getplayers(); +c.getplayers2(); +b.TossDecision(); +c.match(); + + + + } + else if(x>=220 && x<=644 && y>=270 && y<=310)//BOWLER + { + cleardevice(); + readimagefile("bowler.jpg",0,0,1350,660); + + a.display2(); + setcolor(WHITE);//Score board +settextstyle(10,0,7);//score board +setbkcolor(BLUE); +outtextxy(350,20,"CRICKET SCOREBOARD"); +outtextxy(1190,600,"HOME"); +setcolor(WHITE);//Score board + + + + } + else if(x>=220 && x<=668 && y>=339 && y<=378)//BATSMAN + { + cleardevice(); + readimagefile("batman.jpg",0,0,1350,660); + a.display2(); + setcolor(WHITE);//Score board + settextstyle(10,0,7);//score board + setbkcolor(BLUE); + outtextxy(350,20,"CRICKET SCOREBOARD"); + outtextxy(1190,600,"HOME"); + setcolor(WHITE);//Score board + } + else if(x>=220 && x<=594 && y>=410 && y<=447)//TEAM + { + cleardevice(); + readimagefile("sc5.jpg",0,0,1350,660); + a.display2(); + setcolor(WHITE);//Score board + settextstyle(10,0,7);//score board + setbkcolor(BLUE); + outtextxy(350,20,"CRICKET SCOREBOARD"); + outtextxy(1190,600,"HOME"); + setcolor(WHITE);//Score board + + } + else if(x>=1189 && x<=1345 && y>=601 && y<=660)//HOME + { + + goto abx; + } + + } + + + + +system("pause"); +} +Footer diff --git a/cses/Graph-giantPizza.cpp b/cses/Graph-giantPizza.cpp new file mode 100644 index 0000000..e4214bd --- /dev/null +++ b/cses/Graph-giantPizza.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; +const int maxN = 2e5+1; + +bool vis[maxN]; +char ans[maxN]; +int N, M, K, in[maxN], rt[maxN]; +vector ord, comp, G[maxN], GR[maxN], C[maxN], SCC[maxN]; + +int flip(int x){ + return (x&1 ? x+1 : x-1); +} + +void add_edge(char c1, int a, char c2, int b){ + a = 2*a - (c1 == '-'); + b = 2*b - (c2 == '-'); + G[flip(a)].push_back(b); + G[flip(b)].push_back(a); + GR[a].push_back(flip(b)); + GR[b].push_back(flip(a)); +} + +void dfs1(int u){ + vis[u] = true; + for(int v : G[u]) + if(!vis[v]) + dfs1(v); + ord.push_back(u); +} + +void dfs2(int u){ + vis[u] = true; + comp.push_back(u); + for(int v : GR[u]) + if(!vis[v]) + dfs2(v); +} + +int main(){ + scanf("%d %d", &M, &N); + for(int i = 0, a, b; i < M; i++){ + char c1, c2; + scanf(" %c %d %c %d", &c1, &a, &c2, &b); + add_edge(c1, a, c2, b); + } + + for(int i = 1; i <= 2*N; i++) + if(!vis[i]) + dfs1(i); + + fill(vis+1, vis+2*N+1, false); + reverse(ord.begin(), ord.end()); + for(int u : ord){ + if(!vis[u]){ + dfs2(u); + K++; + for(int v : comp){ + rt[v] = K; + C[K].push_back(v); + } + comp.clear(); + } + } + + // Impossible iff x and not(x) belong to same SCC + for(int i = 1; i <= N; i++){ + if(rt[2*i] == rt[2*i-1]){ + printf("IMPOSSIBLE\n"); + return 0; + } + } + + for(int u = 1; u <= 2*N; u++){ + for(int v : G[u]){ + if(rt[u] != rt[v]){ + SCC[rt[u]].push_back(rt[v]); + in[rt[v]]++; + } + } + } + + queue Q; + ord.clear(); + for(int u = 1; u <= K; u++){ + if(in[u] == 0){ + ord.push_back(u); + Q.push(u); + } + } + + while(!Q.empty()){ + int u = Q.front(); Q.pop(); + for(int v : SCC[u]){ + in[v]--; + if(in[v] == 0){ + ord.push_back(v); + Q.push(v); + } + } + } + + fill(vis+1, vis+N+1, false); + reverse(ord.begin(), ord.end()); + for(int k : ord){ + for(int u : C[k]){ + int i = (u+1)/2; + if(!vis[i]){ + ans[i] = (u&1 ? '-' : '+'); + vis[i] = true; + } + } + } + + for(int i = 1; i <= N; i++) + printf("%c%c", ans[i], (" \n")[i==N]); +} diff --git a/cses/dp/Array_Description.cpp b/cses/dp/Array_Description.cpp new file mode 100644 index 0000000..493f33f --- /dev/null +++ b/cses/dp/Array_Description.cpp @@ -0,0 +1,71 @@ +/* ____ _ __ _ _ _ + | _ \ _ __ ___ _ __ ___ | |/ /__ _ _ __ ___ __ _| | | | __ _(_)_ __ + | |_) | '__/ _ \ '_ ` _ \ | ' // _` | '_ ` _ \ / _` | | _ | |/ _` | | '_ \ + | __/| | | __/ | | | | | | . \ (_| | | | | | | (_| | | | |_| | (_| | | | | | + |_| |_| \___|_| |_| |_| |_|\_\__,_|_| |_| |_|\__,_|_| \___/ \__,_|_|_| |_| +*/ + +#include +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n>>m; + vector arr(n); + for(ll i=0;i>arr[i]; + } + + vector> dp(n,vector(m+1,0)); + + //base case + if(arr[0]==0){ + for(ll i=1;i<=m;i++){ + dp[0][i]=1; + } + } + else { + for(ll i=1;i<=m;i++){ + if(i==arr[0]){ + dp[0][i]=1; + } + else { + dp[0][i]=0; + } + } + } + + // transition + for(ll i=1;i +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<"="<>n>>k; + vector page(n),cost(n); + for(int& it : cost){ + cin>>it; + } + for(int& it : page){ + cin>>it; + } + vector> dp(n+1,vector (k+1,0)); + for(int i=1;i<=n;i++){ + for(int j=0;j<=k;j++){ + dp[i][j]=dp[i-1][j]; + if(j>=cost[i-1]){ + dp[i][j]=max(dp[i][j],dp[i-1][j-cost[i-1]]+page[i-1]); + } + } + } + cout< +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n>>k; + vector v(n); + for(ll i=0;i>v[i]; + } + vector dp(k+1,0); + dp[0]=1; + for(ll i=1;i<=k;i++){ + for(ll j=0;j=v[j]){ + dp[i]+=dp[i-v[j]]; + dp[i]=dp[i]%int(1e9+7); + } + } + } + cout< +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n>>x; + vector coins(n); + for(int i=0;i>coins[i]; + } + ll dp[1000001]; + dp[0]=1; + for(int i=1;i<=n;i++){ + for(int j=0;j<=x;j++){ + if(j-coins[i-1]>=0){ + dp[j]+=dp[j-coins[i-1]]; + dp[j]=dp[j]%hell; + } + } + } + cout< +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n; + vector dp(n+1,0); + dp[0]=1; + for(ll i=1;i<=n;i++){ + for(ll j=1;j<=6;j++){ + if(i-j>=0){ + dp[i]+=dp[i-j]; + dp[i]=dp[i]%mod; + } + } + } + cout< +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n; + vector> grid(n,vector (n)); + for(ll i=0;i>grid[i][j]; + } + } + + if(grid[n-1][n-1]=='*'){ + cout<<"0"<> dp(n,vector (n,0)); + dp[0][0]=1; + for(ll i=0;i0){ + dp[i][j]+=dp[i-1][j]; + } + if(j>0){ + dp[i][j]+=dp[i][j-1]; + } + + if(grid[i][j]=='*'){ + dp[i][j]=0; + } + dp[i][j]%=hell; + } + } + cout< +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n>>k; + vector coins(n),dp(k+1,0); + dp[0]=0; + for(ll i=0;i>coins[i]; + } + + for(ll i=1;i<=k;i++){ + ll key=mod; + for(ll j=0;j=0){ + key=min(key,dp[i-coins[j]]); + } + } + dp[i]=key+1; + // cout<<"i "< +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n; + vector dp(n+1,mod); + dp[0]=0; + for(ll i=1;i<=n;i++){ + ll x=i; + while(x!=0){ + ll a=x%10; + x=x/10; + dp[i]=min(dp[i],dp[i-a]+1); + } + } + cout< +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n; + vector> v(n); + for(int i=0;i>v[i].first; + v[i].second=i+1; + } + sort(v.begin(),v.end()); + ll ans=0; + for(ll i=0;iv[j].second){ + j++; + } + i=j; + ans++; + } + cout<>t; + t=1; + while(t--) + solve(); + return 0; +} diff --git a/cses/greedy/Collecting_Numbers_II.cpp b/cses/greedy/Collecting_Numbers_II.cpp new file mode 100644 index 0000000..e4b0a69 --- /dev/null +++ b/cses/greedy/Collecting_Numbers_II.cpp @@ -0,0 +1,51 @@ +/* ____ _ __ _ _ _ + | _ \ _ __ ___ _ __ ___ | |/ /__ _ _ __ ___ __ _| | | | __ _(_)_ __ + | |_) | '__/ _ \ '_ ` _ \ | ' // _` | '_ ` _ \ / _` | | _ | |/ _` | | '_ \ + | __/| | | __/ | | | | | | . \ (_| | | | | | | (_| | | | |_| | (_| | | | | | + |_| |_| \___|_| |_| |_| |_|\_\__,_|_| |_| |_|\__,_|_| \___/ \__,_|_|_| |_| +*/ + +#include +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n>>m; + vector> v(n); + for(int i=0;i>v[i].first; + v[i].second=i+1; + } + sort(v.begin(),v.end()); + ll ans=0; + for(ll i=0;iv[j].second){ + j++; + } + i=j; + ans++; + } + // cout<>a>>b; + + } + return ; +} + +int main() +{ + int t; + // cin>>t; + t=1; + while(t--) + solve(); + return 0; +} \ No newline at end of file diff --git a/cses/greedy/Distinct_Numbers.cpp b/cses/greedy/Distinct_Numbers.cpp new file mode 100644 index 0000000..d6bf5d5 --- /dev/null +++ b/cses/greedy/Distinct_Numbers.cpp @@ -0,0 +1,36 @@ +/* ____ _ __ _ _ _ + | _ \ _ __ ___ _ __ ___ | |/ /__ _ _ __ ___ __ _| | | | __ _(_)_ __ + | |_) | '__/ _ \ '_ ` _ \ | ' // _` | '_ ` _ \ / _` | | _ | |/ _` | | '_ \ + | __/| | | __/ | | | | | | . \ (_| | | | | | | (_| | | | |_| | (_| | | | | | + |_| |_| \___|_| |_| |_| |_|\_\__,_|_| |_| |_|\__,_|_| \___/ \__,_|_|_| |_| +*/ + +#include +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n; + set s; + for(int i=0;i>x; + s.insert(x); + } + cout<>t; + t=1; + while(t--) + solve(); + return 0; +} \ No newline at end of file diff --git a/cses/hello.cpp b/cses/hello.cpp new file mode 100644 index 0000000..7a41842 --- /dev/null +++ b/cses/hello.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; +typedef long long int ll; + +int main(){ + cout<<"hello world"< +using namespace std; +typedef long long int ll; + +int main(){ + cout<<"hello world 2"< + +using namespace std; +typedef pair pii; +#define x first +#define y second + +const int h[] = {1, -1, 0, 0}, v[] = {0, 0, 1, -1}; + +bool vis[1000][1000]; +char c, par[1000][1000], ans[1000000]; +int N, M, sx, sy, ex, ey, dist[1000][1000]; +queue Q; + + +int main(){ + scanf("%d %d", &N, &M); + for(int i = 0; i < N; i++){ + for(int j = 0; j < M; j++){ + scanf(" %c", &c); + if(c == '#') vis[i][j] = true; + else if(c == 'A'){ + sx = i; sy = j; + } else if(c == 'B'){ + ex = i; ey = j; + } + } + } + + vis[sx][sy] = true; + Q.push({sx, sy}); + while(!Q.empty()){ + pii P = Q.front(); Q.pop(); + for(int i = 0; i < 4; i++){ + int dx = P.x+h[i]; + int dy = P.y+v[i]; + if(0 <= dx && dx < N && 0 <= dy && dy < M && !vis[dx][dy]){ + if(i == 0) par[dx][dy] = 'D'; + else if(i == 1) par[dx][dy] = 'U'; + else if(i == 2) par[dx][dy] = 'R'; + else if(i == 3) par[dx][dy] = 'L'; + dist[dx][dy] = dist[P.x][P.y]+1; + vis[dx][dy] = true; + Q.push({dx, dy}); + } + } + } + + if(!vis[ex][ey]){ + printf("NO\n"); + return 0; + } + + printf("YES\n%d\n", dist[ex][ey]); + pii P = {ex, ey}; + for(int i = dist[ex][ey]; i > 0; i--){ + ans[i] = par[P.x][P.y]; + if(ans[i] == 'D') P = {P.x-1, P.y}; + else if(ans[i] == 'U') P = {P.x+1, P.y}; + else if(ans[i] == 'R') P = {P.x, P.y-1}; + else if(ans[i] == 'L') P = {P.x, P.y+1}; + } + for(int i = 1; i <= dist[ex][ey]; i++) + printf("%c", ans[i]); + printf("\n"); +} diff --git a/ctd.cpp b/ctd.cpp new file mode 100644 index 0000000..2016f38 --- /dev/null +++ b/ctd.cpp @@ -0,0 +1,7 @@ +#include +using namespace std; + +int main(){ + cout << "Hi World"< emails = new ArrayList(); + + // valid email addresses + emails.add("email@example.com"); + emails.add("email.bob@example.com"); + + //invalid email addresses + emails.add("email.example.com"); + emails.add("email..bob@example.com"); + + for (String value : emails) { + System.out.println("The Email address " + value + " is " + (isValidEmail(value) ? "valid" : "invalid")); + } + } +} diff --git a/hel.cpp b/hel.cpp new file mode 100644 index 0000000..e598715 --- /dev/null +++ b/hel.cpp @@ -0,0 +1,7 @@ +#include +using namespace std; + +int main(){ +cout<<"hello world"; + return 0; +} diff --git a/hell1.cpp b/hell1.cpp new file mode 100644 index 0000000..6f64120 --- /dev/null +++ b/hell1.cpp @@ -0,0 +1,7 @@ +#include +using namespace std; + +int main(){ +cout<<"hello world 2"; + return 0; +} diff --git a/hell3.cpp b/hell3.cpp new file mode 100644 index 0000000..2533064 --- /dev/null +++ b/hell3.cpp @@ -0,0 +1,7 @@ +#include +using namespace std; + +int main(){ +cout<<"hello world 3"; + return 0; +} diff --git a/hell4.cpp b/hell4.cpp new file mode 100644 index 0000000..9f684ed --- /dev/null +++ b/hell4.cpp @@ -0,0 +1,7 @@ +#include +using namespace std; + +int main(){ +cout<<"hello world 4"; + return 0; +} diff --git a/helllloooooo.cpp b/helllloooooo.cpp new file mode 100644 index 0000000..f90e6cc --- /dev/null +++ b/helllloooooo.cpp @@ -0,0 +1,7 @@ +#include +using namespace std; + +int main(){ + + cout <<"hello world"; +} diff --git a/hello.cpp b/hello.cpp new file mode 100644 index 0000000..75c8e93 --- /dev/null +++ b/hello.cpp @@ -0,0 +1,7 @@ +#include +using namespace std; + +int main(){ +cout<<"Hi World"< +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n; + vector v(n),one,zero; + for(ll i=0;i>v[i]; + } + for(ll i=0;i>x; + if(v[i]){ + one.push_back(x); + } + else{ + zero.push_back(x); + } + } + sort(one.begin(),one.end()); + sort(zero.begin(),zero.end()); + + reverse(one.begin(),one.end()); + reverse(zero.begin(),zero.end()); + + ll x=min(one.size(),zero.size()); + ll ans=0; + for(ll i=0;i>t; + while(t--) + solve(); + return 0; +} diff --git a/hi.cpp b/hi.cpp new file mode 100644 index 0000000..4358c11 --- /dev/null +++ b/hi.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n; + vector v(n),one,zero; + for(ll i=0;i>v[i]; + } + for(ll i=0;i>x; + if(v[i]){ + one.push_back(x); + } + else{ + zero.push_back(x); + } + } + sort(one.begin(),one.end()); + sort(zero.begin(),zero.end()); + + reverse(one.begin(),one.end()); + reverse(zero.begin(),zero.end()); + + ll x=min(one.size(),zero.size()); + ll ans=0; + for(ll i=0;i>t; + while(t--) + solve(); + return 0; +} diff --git a/hiu.cpp b/hiu.cpp new file mode 100644 index 0000000..4358c11 --- /dev/null +++ b/hiu.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; +typedef long long int ll; +#define debug(x); cout<<#x<<" "<>n; + vector v(n),one,zero; + for(ll i=0;i>v[i]; + } + for(ll i=0;i>x; + if(v[i]){ + one.push_back(x); + } + else{ + zero.push_back(x); + } + } + sort(one.begin(),one.end()); + sort(zero.begin(),zero.end()); + + reverse(one.begin(),one.end()); + reverse(zero.begin(),zero.end()); + + ll x=min(one.size(),zero.size()); + ll ans=0; + for(ll i=0;i>t; + while(t--) + solve(); + return 0; +} diff --git a/insertionsort.cpp b/insertionsort.cpp new file mode 100644 index 0000000..5d20d58 --- /dev/null +++ b/insertionsort.cpp @@ -0,0 +1,50 @@ +// C++ program for insertion sort + +#include +using namespace std; + +// Function to sort an array using +// insertion sort +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i - 1; + + // Move elements of arr[0..i-1], + // that are greater than key, to one + // position ahead of their + // current position + while (j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +// A utility function to print an array +// of size n +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + cout << arr[i] << " "; + cout << endl; +} + +// Driver code +int main() +{ + int arr[] = { 12, 11, 13, 5, 6 }; + int N = sizeof(arr) / sizeof(arr[0]); + + insertionSort(arr, N); + printArray(arr, N); + + return 0; +} +// This is code is contributed by rathbhupendra diff --git a/love.py b/love.py new file mode 100644 index 0000000..b7dc9b6 --- /dev/null +++ b/love.py @@ -0,0 +1,33 @@ +import turtle +obj=turtle.Turtle() + +obj.speed(2) + +window=turtle.Screen() +window.bgcolor("black") + +obj.goto(0,-100) +obj.pensize(5) + +obj.color("red") + +obj.begin_fill() +obj.fillcolor("red") +obj.left(140) +obj.forward(180) +obj.circle(-90, 200) + +obj.setheading(60) +obj.circle(-90,200) +obj.forward(176) + +obj.end_fill() + +obj.setheading(140) +obj.forward(170) +obj.setheading(210) +obj.forward(200) +obj.setheading(-210) +obj.setheading(390) +obj.forward(600) +turtle.done() diff --git a/manojbank.c b/manojbank.c new file mode 100644 index 0000000..2096880 --- /dev/null +++ b/manojbank.c @@ -0,0 +1,188 @@ +#include +#include +#include + +void login(); // maximum 3 attempts +void menu(); +void checkbalance(); +void withdraw(); +void deposit(); +void proceed(); +float read(); +void write(float n); + +float total = 6000; + +void main() +{ + login(); +} + +void write(float n) +{ + FILE *fp; + fp = fopen("balance.txt","a"); + fprintf(fp,"\n%f",n); + fclose(fp); +} + + +float read() +{ + FILE *fp; + float n; + fp = fopen("balance.txt","r"); + if(fp == NULL) + { + printf("Internal Error!"); + exit(0); + } + while(fscanf(fp,"%f",&n) == 1); + fclose(fp); + return n; +} + +void proceed() +{ + char ch; + printf("\nPress Y to continue... "); + scanf(" %c",&ch); + system("cls"); + if(ch == 'Y' || ch == 'y') + { + menu(); + } + else + { + exit(0); + } +} + +void deposit() +{ + float da; + float n = read(); + printf("Enter Amount to Deposit : "); + scanf("%f",&da); + if(da<=10000) + { + total = total + da; + n += da; + printf("\nDeposited Amount = $%.2f\n",da); + printf("\nTotal Balance = $%.2f\n",n); + write(n); + } + else + { + printf("\nAmount must not be greater than 10k!\n"); + } +} + +void withdraw() +{ + float wa; + float n = read(); + printf("Enter Amount to Withdraw : "); + scanf("%f",&wa); + if(wa <= n) + { + if(wa <= 5000) + { + if((int)wa%500 == 0) + { + total = total - wa; + n -= wa; + printf("\nWithdraw Amount = $%.2f\n",wa); + printf("\nTotal Available Balance = $%.2f\n",n); + write(n); + } + else + { + printf("\nAmount must be Multiple of 500!\n"); + } + } + else + { + printf("\nAmount Exceeds Per Transaction Limit!\n"); + } + } + else + { + printf("\nInsufficient Balance!\n"); + } +} + +void checkbalance() +{ + printf("Your Total Balance is $%.2f",read()); +} + +void menu() +{ + int n; + printf("************* Welcome to Manoj International Bank ***************\n"); + printf("Options:\n1. Check Balance\n2. Withdraw\n3. Deposit\n4. Exit\n Select : "); + scanf("%d",&n); + system("cls"); + switch(n) + { + case 1: + + checkbalance(); + break; + case 2: + + withdraw(); + break; + case 3: + + deposit(); + break; + case 4: + + printf("Exit"); + break; + default: + printf("\nInvalid Option!\n"); + } + proceed(); +} + + +void login() +{ + char susr[20] = "manoj"; + char spwd[20] = "manoj@123"; + char usr[20],pwd[20]; + int i,flag=0; + for(i=1;i<=3;i++) + { + printf("Attempt Remaining %d\n",(4-i)); + printf("Enter your user Name : "); + gets(usr); + printf("Enter your user Password : "); + gets(pwd); + + if((strcmp(susr,usr)==0)&&(strcmp(spwd,pwd)==0)) + { + flag = 1; + printf("\nWelcome %s!\n\n\n",usr); + menu(); + } + else + { + flag = 0; + printf("\nLogin Failed!\n"); + } + } + + if(flag == 1) + { + printf("\nWelcome %s\n",usr); + } + else + { + + printf("\nYour Account is Locked!\n"); + } +} diff --git a/matrix_chain_multiplication.cpp b/matrix_chain_multiplication.cpp new file mode 100644 index 0000000..efac80b --- /dev/null +++ b/matrix_chain_multiplication.cpp @@ -0,0 +1,47 @@ +/* +Given a sequence of matrices, find the most efficient way to +multiply these matrices together. The efficient way is the +one that involves the least number of multiplications. + +The dimensions of the matrices are given in an array +arr[] of size N (such that N = number of matrices + 1) where +the ith matrix has the dimensions (arr[i-1] x arr[i]). +*/ +/* A naive recursive implementation that simply +follows the above optimal substructure property */ +#include +using namespace std; + +// Matrix Ai has dimension p[i-1] x p[i] +// for i = 1..n +int MatrixChainOrder(int p[], int i, int j) +{ + if (i == j) + return 0; + int k; + int min = INT_MAX; + int count; + + for (k = i; k < j; k++) + { + count = MatrixChainOrder(p, i, k) + + MatrixChainOrder(p, k + 1, j) + + p[i - 1] * p[k] * p[j]; + + if (count < min) + min = count; + } + + // Return minimum count + return min; +} + +int main() +{ + int arr[] = { 1, 2, 3, 4, 3 }; + int n = sizeof(arr) / sizeof(arr[0]); + + cout << "Minimum number of multiplications is " + << MatrixChainOrder(arr, 1, n - 1); +} + diff --git a/min-max.cpp b/min-max.cpp new file mode 100644 index 0000000..798c19c --- /dev/null +++ b/min-max.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main() { + int a[30], i, n, max, min; + cout << "Enter size of array"; + cin >> n; + cout << "Enter elements of array"; + for(i=0; i> a[i]; + } + min = a[0]; + max = a[n-1]; + for(i=0; i max) + max = a[i]; + } + cout << "The maximum element is " << max << endl; + cout << "The minimum element is " << min << endl; + return 0; +} diff --git a/number_guessing_game.py b/number_guessing_game.py new file mode 100644 index 0000000..69f7c27 --- /dev/null +++ b/number_guessing_game.py @@ -0,0 +1,26 @@ +print('this is a number game , you have to guess the number chosen by me ') +print('you will have 5 chances , else you will be out ') +print('enter a number between 1 to 20 ') +guess=0 +num=1 +while(num!=13): + num=int(input()) + if(num<1 or num>20): + print('invalid number entered ') + guess=guess+1 + elif(num>0 and num<6): + print('too small, try again!') + guess=guess+1 + elif(num<11 and num>5): + print('a lil smaller, try again!') + guess=guess+1 + elif(num==11 or num==12 or num==14 or num==15 ): + print('too close , try again!') + guess=guess+1 + elif(num>15 and num<21): + print('greater number taken, try again!') + guess=guess+1 + elif(num==13): + print('congrats!!!') + if(guess==5): + print('your guesses are over') diff --git a/number_of_set_bits.cpp b/number_of_set_bits.cpp new file mode 100644 index 0000000..c372daa --- /dev/null +++ b/number_of_set_bits.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int getSETBITS_of_one(int x){ + int counter_1=0; + + while(x!=0){ + if(x&1){ + counter_1++; + } + x=x>>1; + } + return counter_1; +} + +int getSETBITS_of_zero(int y){ + int counter_0=0; + + while(y!=0){ + if(y&1){ + } + else{ + counter_0++; + } + y=y>>1; + } + return counter_0; +} + + + +int main(){ + int a,b; + cout<<"ENTER THE 2 NUMBERS"<>a>>b; + /* int no_of_ones_of_a=getSETBITS_of_one(a); + int no_of_zeros_of_a=getSETBITS_of_zero(a); + int no_of_ones_of_b=getSETBITS_of_one(b); + int no_of_zeros_of_b=getSETBITS_of_zero(b);*/ + + cout<<"TOTAL NUMBER OF 1 BITS: "< +#include +using namespace std; +//Function which checks if the block is safe for the rat to go or not. +bool isSafe(int **arr, int x, int y, int n) +{ + if (x < n && y < n && arr[x][y] == 1) + { + return true; + } + else{ + return false; + } +} +bool rat(int **arr, int x, int y, int n, int **solArr) +{ //Applying a base condition wherein rat has arrived at the required position. + if (x == n - 1 && y == n - 1) + { + solArr[x][y] = 1; + return true; + } + //If rat is allowed to go to that block then putting 1 there + else if (isSafe(arr, x, y, n)) + { + solArr[x][y] = 1; + //Moving in right direction and checking if we can get a solution from there. + if (rat(arr, x + 1, y, n, solArr)) + { + return true; + } + //Moving in downward direction to check if the rat is allowed to go on that block + if (rat(arr, x, y + 1, n, solArr)) + { + return true; + } + solArr[x][y] = 0; + return false; + } + else { + return false;} + +} + +int main() +{ + int n; + cin >> n; + + int **arr = new int *[n]; + //Dynamic memory allocation of 2d matrix + for (int i = 0; i < n; i++) + { + arr[i] = new int[n]; + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + int **solArr=new int*[n]; + for (int i = 0; i < n; i++) + { + solArr[i]=new int[n]; + for (int j = 0; j < n; j++) + { + solArr[i][j] = 0; + } + } + + if (rat(arr, 0, 0, n, solArr)) + {// Printing solutions if rat function is true + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cout << solArr[i][j]<<" "; + } cout< +using namespace std; + +template +void bubbleSort(T arr[], int n) +{ + for (int i = 0; i < n - 1; i++) + { + for (int j = 0; j < n - i - 1; j++) + { + if (arr[j] > arr[j + 1]) + { + swap(arr[j], arr[j + 1]); + } + } + } +} + +template +void insertionSort(T arr[], int n) +{ + int i, j; + int key; + + for (int i = 0; i < n; i++) + { + key = arr[i]; + j = i - 1; + + while (j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } +} + +template +void selectionSort(T arr[], int n) +{ + int i, j, min_idx; + + for (i = 0; i < n - 1; i++) + { + min_idx = i; + for (j = i + 1; j < n; j++) + { + if (arr[j] < arr[min_idx]) + { + min_idx = j; + } + } + swap(arr[min_idx], arr[i]); + } +} + +template +void merge(T arr[], int l, int m, int r) +{ + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + + T L[n1], R[n2]; + for (i = 0; i < n1; i++) + { + L[i] = arr[l + i]; + } + for (j = 0; j < n2; j++) + { + R[j] = arr[m + 1 + j]; + } + + i = 0; + j = 0; + k = l; + while (i < n1 && j < n2) + { + if (L[i] <= R[j]) + { + arr[k] = L[i]; + i++; + } + else + { + arr[k] = R[j]; + j++; + } + k++; + } + + while (i < n1) + { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) + { + arr[k] = R[j]; + j++; + k++; + } +} + +template +void mergeSort(T arr[], int l, int r) +{ + if (l < r) + { + int m = l + (r - l) / 2; + + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + + merge(arr, l, m, r); + } +} + +template +T partition(T arr[], int l, int r) +{ + T pivot = arr[l]; + int i = l; + int j = r + 1; + while (true) + { + do + { + i++; + } while (arr[i] < pivot); + do + { + j--; + } while (arr[j] > pivot); + if (i >= j) + { + break; + } + swap(arr[i], arr[j]); + } + swap(arr[l], arr[j]); + return j; +} + +template +void quickSort(T arr[], int l, int r) +{ + if (l < r) + { + int j = partition(arr, l, r); + quickSort(arr, l, j - 1); + quickSort(arr, j + 1, r); + } +} + +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 4, 3, 89, 11}; + int n = sizeof(arr) / sizeof(arr[0]); + + bubbleSort(arr, n); + + for (int i = 0; i < n; i++) + cout << arr[i] << " "; +} \ No newline at end of file diff --git a/std.cpp b/std.cpp new file mode 100644 index 0000000..e5f6ad3 --- /dev/null +++ b/std.cpp @@ -0,0 +1,6 @@ +#include +using namespace std; + +int main(){ + cout << "yout-u-ber"; +} diff --git a/stone_paper_scissors_game.py b/stone_paper_scissors_game.py new file mode 100644 index 0000000..e40eea2 --- /dev/null +++ b/stone_paper_scissors_game.py @@ -0,0 +1,53 @@ +#this is the stone paper scissor game +import random +user=str(input('Enter your name ')) +print(" ") +computer_score=0 +user_score=0 +game_no=0 +list=['stone', 'paper', 'scissors'] +print('you can chose s or p or z') +print('you will be given 5 chances') +print(" ") +print('s : stone') +print('p : paper') +print('z : scissors') +print(" ") +while(game_no!=5): + computer_turn=random.choice(list) + print('your turn ') + game_no+=1 + user_turn = str(input("you : ")) + print("computer : ",computer_turn) + print(" ") + if(user_turn==computer_turn): + user_score+=1 + computer_score+=1 + + elif(user_turn=='s' and computer_turn=='paper'): + computer_score+=1 + + elif(user_turn=='s' and computer_turn=='scissors'): + user_score+=1 + + elif(user_turn=='p' and computer_turn=='stone'): + user_score+=1 + + elif(user_turn=='p' and computer_turn=='scissors'): + computer_score+=1 + + elif(user_turn=='z' and computer_turn=='stone'): + computer_score+=1 + + elif(user_turn=='z' and computer_turn=='paper'): + user_score+=1 + +print('YOUR SCORE : ',user_score) +print('COMPUTER SCORE : ', computer_score) +print(" ") +if(user_score > computer_score): + print("YAY YOU WON !!!!!!!") +elif(user_score < computer_score): + print("COMPUTER WON , BETTER LUCK NEXT TIME ") +elif(user_score == computer_score): + print("ITS A TIE ") diff --git a/subsets_string.cpp b/subsets_string.cpp new file mode 100644 index 0000000..be3e6b7 --- /dev/null +++ b/subsets_string.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +void subset(string s,string curr="",int i=0) +{ + if(i==s.length()) + cout<>s; + subset(curr); + return 0; +} \ No newline at end of file diff --git a/travel_salesman.py b/travel_salesman.py new file mode 100644 index 0000000..75f0537 --- /dev/null +++ b/travel_salesman.py @@ -0,0 +1,32 @@ + +from sys import maxsize +from itertools import permutations +V = 4 +def travellingSalesmanProblem(graph, s): + vertex = [] + for i in range(V): + if i != s: + vertex.append(i) + min_path = maxsize + next_permutation=permutations(vertex) + for i in next_permutation: + + current_pathweight = 0 + k = s + for j in i: + current_pathweight += graph[k][j] + k = j + current_pathweight += graph[k][s] + + # update minimum + min_path = min(min_path, current_pathweight) + + return min_path + +if __name__ == "__main__": + + + graph = [[0, 10, 15, 20], [10, 0, 35, 25], + [15, 35, 0, 30], [20, 25, 30, 0]] + s = 0 + print(travellingSalesmanProblem(graph, s)) diff --git a/tuddler.cpp b/tuddler.cpp new file mode 100644 index 0000000..e11094f --- /dev/null +++ b/tuddler.cpp @@ -0,0 +1,6 @@ +#include +uisng namespace std; + +int main(){ + cout << "yo baby "; +}