├── .github └── workflows │ └── generate_docs.yml ├── C# ├── HelloWorld.cs ├── hash.cs └── triangle.cs ├── C++ ├── 0-1_Knapsack.cpp ├── Add_binary_num.cpp ├── BookAllocation.cpp ├── CRC.cpp ├── Caesar Cipher.cpp ├── ChineseRemainderTheorem.cpp ├── Count_divisible_pairs_in_an_array.cpp ├── Counting_sort.cpp ├── DecToBinRecursive.cpp ├── DeleteNodesLL.cpp ├── Design_HashMap.cpp ├── Design_HashSet.cpp ├── Floyds_Warshell_Algo.cpp ├── FormBiggestNumber.cpp ├── GenerateParenthesis.cpp ├── HelloWorld.cpp ├── KDTheorem_subarrays.cpp ├── Last_Occurence.cpp ├── Leetcode-363 Max Sum of Rectangle No Larger Than K.cpp ├── Longest_Common_Substring.cpp ├── Matrix_Chain_Multiplication.cpp ├── OS │ └── sjf.cpp ├── Prime Factors of a number.cpp ├── Queue_using_linked_list.cpp ├── Radix_sort.c++ ├── STACK_reverseSentence.cpp ├── TemperatureConverter.cpp ├── TowerOfHanoi.cpp ├── Travelling-salesman.cpp ├── Validate_Binary_Search_Tree.cpp ├── andrew_algorithm_convex_hull.cpp ├── avl_tree.cpp ├── binary_exponentiation.cpp ├── binomial_heap.cpp ├── butterfly_pattern.cpp ├── check_array.cpp ├── chocolate_distribution.cpp ├── count_nodes_in_circular_linkedlist.cpp ├── count_word.cpp ├── dictOrder.cpp ├── double_hashing.cpp ├── expertsystem.cpp ├── expression_tree.cpp ├── fast_fourier_transform.cpp ├── firstandlastoccurence.cpp ├── generate-random-color.cpp ├── graph_dial.cpp ├── level_order.cpp ├── nth_node.cpp ├── painterspartitionproblem.cpp ├── palindrome_recursion.cpp ├── pangram_check.cpp ├── reverse_array.cpp ├── right_view_of_a_binary tree.cpp ├── rotate_array.cpp ├── snake_game.cpp ├── startPyramid.cpp ├── stepping_number.cpp ├── sudoku_solver.cpp ├── sum_of_nodes_in_top_view_of_a_binary_tree.cpp ├── swap_nodes.cpp ├── top_view_of_a_binary_tree.cpp └── trapRainWater.cpp ├── C ├── AllSortingTechniques.c ├── CheckSum_ErrorDetection.c ├── Contact-Managment-System.c ├── Encryption-Decryption.c ├── Full_Pyramid.c ├── Merge_sortDoublyLinkedList.c ├── Tree traverals │ ├── IdenticalTrees.c │ ├── tree_inOrder.c │ ├── tree_postOrder.c │ └── tree_preOrderTrav.c ├── VRC.c ├── additon.c ├── amstrong.c ├── area of circle.c ├── bubble-sort.c ├── even and odd.c ├── factorial.c ├── fastFoodCLI.c ├── fibonacci.c ├── full pyramid ├── hamming.c ├── inverted full pyramid ├── inverted half pyramid ├── palindrome.c ├── pascals triangle ├── quick_sort.c ├── reverse-linked-list.c ├── starpattern.c └── traingle ├── Go ├── bubbleSort.go └── linearsearch.go ├── Html ├── Class 12th Solutions │ ├── ProfitLoss.html │ ├── functionMathsAll.html │ └── nNaturalNumber.html ├── HelloWorld.html ├── autoChangeColor.html ├── fun project │ ├── index.html │ └── style.css └── onClick.html ├── Java ├── AreaCalculator.java ├── Calculator.java ├── HelloWorld.java ├── HelpdeskApplicationTest.Java ├── IncomeTax.java ├── KnightTourProblem.java ├── LeapYear.java ├── PalindromeTest.java ├── PascalsTriangle.java ├── Pythogorean_Triplets.java ├── QuickSort_on_Singly_Linked_List.java ├── RazorPay │ ├── MagicalBinaryString.java │ └── PairMakesSum.java ├── ReverseStringWordWise.java ├── add_two_numbers.java ├── arrayList │ └── Fruites.java ├── exchangeValues │ └── Main.java ├── fibonacci.java ├── findSingleInArrayOfDoubles.java ├── guessNumber │ └── GuessNumber.java ├── intersection_point_of_two_Linked_Lists.java ├── median_of_two_sorted_array.java ├── merge_sort.java ├── palindrome.java ├── printLarge.java ├── reverse_doubly_linkedlist.java ├── simpleInterest │ └── Si.java ├── spy number in java └── switchCase │ └── Main.java ├── Javascript ├── Calculator │ ├── index.html │ ├── scripts.js │ ├── storage.html │ ├── storage.js │ └── style.css ├── Funny Car project │ ├── background.jpg │ ├── car_body.png │ ├── car_wheel_left.png │ ├── car_wheel_right.png │ ├── index.html │ ├── index.js │ ├── sound.mp3 │ ├── style.css │ ├── track.png │ └── trees.png ├── HelloWorld.js ├── Hollow Square Pattern.js ├── WeaponScavengingGame.js ├── addTwoNumber.js ├── ant smasher │ ├── ant.png │ ├── css │ │ ├── ant.png │ │ ├── reset.css │ │ └── style.css │ ├── index.html │ └── js │ │ ├── ant.js │ │ ├── index.js │ │ └── point.js ├── arrayChecker.js ├── arrowfunction.js ├── bst_to_minheap.js ├── color-flipper │ ├── app.js │ ├── index.html │ └── styles.css ├── fastify │ ├── .gitignore │ ├── index.js │ ├── index.test.js │ ├── package.json │ └── pnpm-lock.yaml ├── function.js ├── merge_two_bst.js ├── palindrome.js ├── pyramidstartpattern.js └── snake game project │ ├── CSS │ └── style.css │ ├── index.html │ ├── main_data │ ├── SnakeGame │ │ ├── SnakeGame.zip │ │ └── SnakeGame │ │ │ ├── Resources.txt │ │ │ ├── css │ │ │ └── style.css │ │ │ ├── img │ │ │ └── bg.jpg │ │ │ ├── index.html │ │ │ ├── js │ │ │ └── index.js │ │ │ └── music │ │ │ ├── food.mp3 │ │ │ ├── gameover.mp3 │ │ │ ├── move.mp3 │ │ │ └── music.mp3 │ ├── img │ │ └── bg.jpg │ └── music │ │ ├── food.mp3 │ │ ├── gameover.mp3 │ │ ├── move.mp3 │ │ └── music.mp3 │ └── script.js │ └── script.js ├── LICENSE ├── PHP ├── forLoop.php ├── helloworld.php ├── koneksi.php └── triangle.php ├── Python ├── 1's complement.py ├── CoffeeMachine.py ├── CombinationSumII.py ├── Compress_The_String.py ├── Count_Sort.py ├── Digital_clock.py ├── Duplicates.py ├── Egg_Catcher_Game.py ├── Fibonacci_recursion.py ├── FidgetSpinner.py ├── HangMan.py ├── Insert_into_a_Binary_Search_Tree.py ├── LMS │ ├── functions.py │ └── main.py ├── Magic_Number.py ├── Monte_hall.py ├── Number Patterns ├── Number_Palindrome.py ├── Pyramidpattern.py ├── Python │ └── maximize.py ├── Queue_LinkedList.py ├── RockPaperScissor.py ├── Rock_Paper_Scissors.py ├── SnakeGame.py ├── Stack_LiskedList.py ├── String_Palindrome.py ├── TCPclient.py ├── TCPserver.py ├── Telephonedirectory.py ├── Tower_of_Hanoi.py ├── Triangle Patter.py ├── Valid Anagram.py ├── YT_video_audio_downloader.py ├── area.py ├── areaOfRectangle.py ├── areaoftriangle.py ├── batman.py ├── battery_saver_notif.py ├── binary_search.py ├── binarytree_fold.py ├── binomial_expansion_series.py ├── bubblesort.py ├── butterfly_pattern.py ├── calendar.py ├── check_prime.py ├── email_sender.py ├── exchangeValuesPython │ └── Main.py ├── fibonacciSequence.py ├── formatting.py ├── hashcracker.py ├── heap_sort.py ├── integer_to_roman.py ├── internetspeedtest.py ├── intersection_point_of_two_Linked_Lists.py ├── katakana2hiragana.py ├── left_view_of_binary_tree.py ├── linear_search.py ├── macspoofer.py ├── mad-libs-gen.py ├── matrix.py ├── median_of_two_sorted_array.py ├── merge_sort.py ├── nmap.py ├── number_guessing_game.py ├── packetflooder.py ├── password_generator.py ├── ping pong game.py ├── python-nmap-port-scanner.py ├── quizGame.py ├── recon.py ├── recursive_factorial.py ├── regression_model │ ├── data.csv │ └── reg.py ├── rev_string.py ├── roadCrossing Game.py ├── screenshot.py ├── secure_password_generator.py ├── sendEmail.py ├── snake game.py ├── spirograph.py ├── subdomain.py ├── sudoku_ManiL-Keo.py ├── suduko_solver.py ├── sum_of_first_n2_natural_numbers.py ├── sum_of_n_natural_numbers.py ├── system_health_check.py ├── temperature_converter.py ├── tictactoe.py ├── top_view.py ├── validation_password_creation_solution.py └── wifi scanner.py ├── README.md ├── Rust ├── binary_search.rs ├── brainfuck_interpreter.rs ├── hello_world.rs ├── rock_paper_scissors │ ├── Cargo.toml │ └── src │ │ └── main.rs └── selection_sort.rs ├── _config.yml ├── docs ├── CNAME ├── README.md ├── css │ └── style.css ├── index.html ├── js │ └── main.js ├── scripts │ └── generate_site_data.py └── site_data.json └── typescript └── palindrome.ts /C#/HelloWorld.cs: -------------------------------------------------------------------------------- 1 | namespace HelloWorld 2 | { 3 | class Hello { 4 | static void Main(string[] args) 5 | { 6 | System.Console.WriteLine("Hello World!"); 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /C#/hash.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Data; 7 | using System.Data.SqlClient; 8 | using System.Security.Cryptography; 9 | using System.Text; 10 | 11 | namespace Desktop3 12 | { 13 | internal class MyClass 14 | { 15 | public static string makeHash(string text) 16 | { 17 | return string.Join("", MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text)).Select(s => s.ToString("x2"))); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /C#/triangle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | namespace Hello_Word 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | int val = 5; 12 | int i, j, k ; 13 | for (i = 1; i <= val; i++) 14 | { 15 | for (j = 1; j <= val-i; j++) 16 | { 17 | // Console.Write(""); 18 | } 19 | for (k = 1; k <= i; k++) 20 | { 21 | Console.Write("*"); 22 | } 23 | Console.WriteLine(""); 24 | } 25 | Console.ReadLine(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /C++/0-1_Knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // A utility function that returns 5 | // maximum of two integers 6 | int max(int a, int b) { return (a > b) ? a : b; } 7 | 8 | // Returns the maximum value that 9 | // can be put in a knapsack of capacity W 10 | int knapSack(int W, int wt[], int val[], int n) 11 | { 12 | 13 | // Base Case 14 | if (n == 0 || W == 0) 15 | return 0; 16 | 17 | // If weight of the nth item is more 18 | // than Knapsack capacity W, then 19 | // this item cannot be included 20 | // in the optimal solution 21 | if (wt[n - 1] > W) 22 | return knapSack(W, wt, val, n - 1); 23 | 24 | // Return the maximum of two cases: 25 | // (1) nth item included 26 | // (2) not included 27 | else 28 | return max( 29 | val[n - 1] 30 | + knapSack(W - wt[n - 1], 31 | wt, val, n - 1), 32 | knapSack(W, wt, val, n - 1)); 33 | } 34 | 35 | // Driver code 36 | int main() 37 | { 38 | int val[] = { 60, 100, 120 }; 39 | int wt[] = { 10, 20, 30 }; 40 | int W = 50; 41 | int n = sizeof(val) / sizeof(val[0]); 42 | cout << knapSack(W, wt, val, n); 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /C++/BookAllocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool possible(int books[] , int nbooks, int stds , int checkmax){ 5 | 6 | 7 | 8 | int currsum = 0 ; 9 | int allocatedto = 1; 10 | for(int i = 0 ; i < nbooks ; i++){ 11 | 12 | if (books[i] > checkmax) { 13 | return false; 14 | } 15 | 16 | if(currsum + books[i] <= checkmax){ 17 | currsum+=books[i]; 18 | }else{ 19 | allocatedto++; 20 | currsum = books[i]; 21 | } 22 | if(allocatedto>stds){ 23 | return false; 24 | } 25 | } 26 | return true; 27 | } 28 | 29 | int allocate(int books[] , int nbooks ,int stds){ 30 | 31 | int res = INT_MAX; 32 | int st = 0 ; 33 | 34 | int sum=0; 35 | for(int i = 0 ; i < nbooks ; i++){ 36 | sum+=books[i]; 37 | } 38 | int en = sum; 39 | 40 | while(st<=en){ 41 | int mid = (st+en)/2; 42 | if(possible(books,nbooks,stds,mid)){ 43 | res = min(mid,res); 44 | en = mid-1; 45 | }else{ 46 | st = mid+1; 47 | } 48 | } 49 | 50 | return res; 51 | 52 | 53 | } 54 | 55 | int main() { 56 | 57 | int t; 58 | cin>>t; 59 | while(t--){ 60 | 61 | int nbooks,stds; 62 | cin>>nbooks>>stds; 63 | 64 | int books[nbooks]; 65 | for(int i = 0 ; i < nbooks; i++){ 66 | cin>>books[i]; 67 | } 68 | 69 | cout< 2 | 3 | using namespace std; 4 | 5 | string ltrim(const string &); 6 | string rtrim(const string &); 7 | 8 | /* 9 | * Complete the 'caesarCipher' function below. 10 | * 11 | * The function is expected to return a STRING. 12 | * The function accepts following parameters: 13 | * 1. STRING s 14 | * 2. INTEGER k 15 | */ 16 | 17 | string caesarCipher(string s, int k) { 18 | int i = 0; 19 | while(s[i]!=NULL) 20 | { 21 | if(s[i] >= 'A' && s[i] <= 'Z') 22 | { 23 | s[i] = char(int((s[i] + k - 65) % 26 + 65)); 24 | } 25 | if(s[i] >= 'a' && s[i] <= 'z') 26 | { 27 | s[i] = char(int((s[i] + k - 97) % 26 + 97)); 28 | } 29 | i++; 30 | } 31 | return s; 32 | } 33 | 34 | int main() 35 | { 36 | ofstream fout(getenv("OUTPUT_PATH")); 37 | 38 | string n_temp; 39 | getline(cin, n_temp); 40 | 41 | int n = stoi(ltrim(rtrim(n_temp))); 42 | 43 | string s; 44 | getline(cin, s); 45 | 46 | string k_temp; 47 | getline(cin, k_temp); 48 | 49 | int k = stoi(ltrim(rtrim(k_temp))); 50 | 51 | string result = caesarCipher(s, k); 52 | 53 | fout << result << "\n"; 54 | 55 | fout.close(); 56 | 57 | return 0; 58 | } 59 | 60 | string ltrim(const string &str) { 61 | string s(str); 62 | 63 | s.erase( 64 | s.begin(), 65 | find_if(s.begin(), s.end(), not1(ptr_fun(isspace))) 66 | ); 67 | 68 | return s; 69 | } 70 | 71 | string rtrim(const string &str) { 72 | string s(str); 73 | 74 | s.erase( 75 | find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), 76 | s.end() 77 | ); 78 | 79 | return s; 80 | } 81 | -------------------------------------------------------------------------------- /C++/Count_divisible_pairs_in_an_array.cpp: -------------------------------------------------------------------------------- 1 | // CPP program to count divisible pairs. 2 | #include 3 | using namespace std; 4 | 5 | int countDivisibles(int arr[], int n) 6 | { 7 | int res = 0; 8 | 9 | // Iterate through all pairs 10 | for (int i=0; i 3 | #include 4 | #define RANGE 255 5 | 6 | // The main function that sort the given string arr[] in 7 | // alphabetical order 8 | void countSort(char arr[]) 9 | { 10 | // The output character array that will have sorted arr 11 | char output[strlen(arr)]; 12 | 13 | // Create a count array to store count of individual 14 | // characters and initialize count array as 0 15 | int count[RANGE + 1], i; 16 | memset(count, 0, sizeof(count)); 17 | 18 | // Store count of each character 19 | for(i = 0; arr[i]; ++i) 20 | ++count[arr[i]]; 21 | 22 | // Change count[i] so that count[i] now contains actual 23 | // position of this character in output array 24 | for (i = 1; i <= RANGE; ++i) 25 | count[i] += count[i-1]; 26 | 27 | // Build the output character array 28 | for (i = 0; arr[i]; ++i) 29 | { 30 | output[count[arr[i]]-1] = arr[i]; 31 | --count[arr[i]]; 32 | } 33 | 34 | // Copy the output array to arr, so that arr now 35 | // contains sorted characters 36 | for (i = 0; arr[i]; ++i) 37 | arr[i] = output[i]; 38 | } 39 | 40 | // Driver program to test above function 41 | int main() 42 | { 43 | char arr[] = "sampleText"; 44 | 45 | countSort(arr); 46 | 47 | printf("Sorted character array is %sn", arr); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /C++/DecToBinRecursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int DecToBin(int n){ 4 | if(n==0){ 5 | return 0; 6 | }else{ 7 | return (n%2+10*(DecToBin(n/2))); 8 | } 9 | } 10 | int main(){ 11 | cout< 2 | using namespace std; 3 | 4 | 5 | class Node{ 6 | 7 | public: 8 | int data; 9 | Node* next; 10 | 11 | Node(int data){ 12 | this->data = data ; 13 | next = NULL; 14 | } 15 | 16 | }; 17 | 18 | Node* remove(Node* head){ 19 | if(head==NULL || head->next ==NULL){ 20 | return head; 21 | } 22 | 23 | head->next = remove(head->next); 24 | 25 | if(head->data >= head->next->data){ 26 | return head; 27 | }else{ 28 | return head->next; 29 | } 30 | } 31 | 32 | 33 | 34 | int main(){ 35 | 36 | int n; 37 | cin>>n; 38 | 39 | Node*head = NULL; 40 | Node*tail = NULL; 41 | 42 | for(int i = 0 ; i < n ; i++){ 43 | int dt; 44 | cin>>dt; 45 | 46 | Node* newnode = new Node(dt); 47 | 48 | if(head==NULL && tail==NULL){ 49 | head=newnode; 50 | tail=newnode; 51 | }else{ 52 | tail->next = newnode; 53 | tail = tail->next; 54 | } 55 | } 56 | 57 | 58 | head = remove(head); 59 | 60 | 61 | Node* temp = head; 62 | while(temp){ 63 | cout<data<<" "; 64 | temp = temp ->next; 65 | } 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /C++/Design_HashMap.cpp: -------------------------------------------------------------------------------- 1 | class MyHashMap { 2 | public: 3 | int arr[1000001]; 4 | MyHashMap() { 5 | fill(arr, arr + 1000000, -1); // initalizing with -1. 6 | } 7 | void put(int key, int val) { 8 | arr[key] = val; 9 | } 10 | int get(int key) { 11 | return arr[key]; 12 | } 13 | void remove(int key) { 14 | arr[key] = -1; 15 | } 16 | }; 17 | /** 18 | * Your MyHashMap object will be instantiated and called as such: 19 | * MyHashMap* obj = new MyHashMap(); 20 | * obj->put(key,value); 21 | * int param_2 = obj->get(key); 22 | * obj->remove(key); 23 | */ 24 | -------------------------------------------------------------------------------- /C++/Design_HashSet.cpp: -------------------------------------------------------------------------------- 1 | class MyHashSet { 2 | public: 3 | vector ans; 4 | MyHashSet() { 5 | ans.resize(1e6+1,false); 6 | } 7 | 8 | void add(int key) { 9 | ans[key]=true; 10 | } 11 | 12 | void remove(int key) { 13 | ans[key]=false; 14 | } 15 | 16 | bool contains(int key) { 17 | return ans[key]; 18 | } 19 | }; 20 | 21 | /** 22 | * Your MyHashSet object will be instantiated and called as such: 23 | * MyHashSet* obj = new MyHashSet(); 24 | * obj->add(key); 25 | * obj->remove(key); 26 | * bool param_3 = obj->contains(key); 27 | */ 28 | -------------------------------------------------------------------------------- /C++/Floyds_Warshell_Algo.cpp: -------------------------------------------------------------------------------- 1 | // C++ Program for Floyd Warshall Algorithm 2 | #include 3 | using namespace std; 4 | 5 | // Number of vertices in the graph 6 | #define V 4 7 | 8 | 9 | for (i = 0; i < V; i++) 10 | for (j = 0; j < V; j++) 11 | dist[i][j] = graph[i][j]; 12 | 13 | for (k = 0; k < V; k++) { 14 | // Pick all vertices as source one by one 15 | for (i = 0; i < V; i++) { 16 | // Pick all vertices as destination for the 17 | // above picked source 18 | for (j = 0; j < V; j++) { 19 | // If vertex k is on the shortest path from 20 | // i to j, then update the value of 21 | // dist[i][j] 22 | if (dist[i][j] > (dist[i][k] + dist[k][j]) 23 | && (dist[k][j] != INF 24 | && dist[i][k] != INF)) 25 | dist[i][j] = dist[i][k] + dist[k][j]; 26 | } 27 | } 28 | } 29 | 30 | // Print the shortest distance matrix 31 | printSolution(dist); 32 | } 33 | 34 | void printSolution(int dist[][V]) 35 | { 36 | cout << "The following matrix shows the shortest " 37 | "distances" 38 | " between every pair of vertices \n"; 39 | for (int i = 0; i < V; i++) { 40 | for (int j = 0; j < V; j++) { 41 | if (dist[i][j] == INF) 42 | cout << "INF" 43 | << " "; 44 | else 45 | cout << dist[i][j] << " "; 46 | } 47 | cout << endl; 48 | } 49 | } 50 | 51 | // Driver's code 52 | 53 | int main() 54 | { 55 | /* Let us create the following weighted graph 56 | 10 57 | (0)------->(3) 58 | | /|\ 59 | 5 | | 60 | | | 1 61 | \|/ | 62 | (1)------->(2) 63 | 3 */ 64 | int graph[V][V] = { { 0, 5, INF, 10 }, 65 | { INF, 0, 3, INF }, 66 | { INF, INF, 0, 1 }, 67 | { INF, INF, INF, 0 } }; 68 | 69 | // Function call 70 | floydWarshall(graph); 71 | return 0; 72 | } 73 | 74 | -------------------------------------------------------------------------------- /C++/FormBiggestNumber.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | bool possible(string a , string b){ 5 | 6 | string ab = a.append(b); 7 | string ba = b.append(a); 8 | int res = 0 ; 9 | for(int i = 0 ; i < ab.size() ; i++){ 10 | if(ab[i]>ba[i]){ 11 | res = 1; 12 | break; 13 | }else if(ab[i] < ba[i]){ 14 | res = -1; 15 | break; 16 | } 17 | } 18 | 19 | if(res>0){ 20 | return false; 21 | }else{ 22 | return true; 23 | } 24 | 25 | } 26 | 27 | int main(){ 28 | int t; 29 | cin>>t; 30 | 31 | while(t--){ 32 | 33 | int n; 34 | cin>>n; 35 | 36 | string arr[n]; 37 | for(int i = 0 ; i>arr[i]; 39 | } 40 | 41 | for(int i = 0 ; i < n-1 ; i++){ 42 | for(int j = 0 ; j < n-1 ; j++){ 43 | 44 | 45 | if(possible(arr[j],arr[j+1])){ 46 | string temp = arr[j]; 47 | arr[j] = arr[j+1]; 48 | arr[j+1] = temp; 49 | } 50 | } 51 | } 52 | 53 | for(int i = 0 ; i 2 | using namespace std; 3 | 4 | void helper(int n , int o , int c, string s){ 5 | 6 | if(c==n){ 7 | cout<>n; 24 | 25 | helper(n,0,0,""); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C++/HelloWorld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout << "Hello World"; 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /C++/KDTheorem_subarrays.cpp: -------------------------------------------------------------------------------- 1 | // Finding the maximum sum of sub array using Kadanes theorem 2 | // Actual KD Theorem(from internet) code: 3 | // #include 4 | // using namespace std; 5 | 6 | // int maxSumSubarray(vector A) { 7 | // int maxsumSoFar = A[0]; 8 | // int maxsumEndingHere = A[0]; 9 | // for (int i = 1; i < A.size(); i = i + 1) 10 | // { 11 | // maxsumEndingHere = max (maxsumEndingHere + A[i], A[i]); 12 | // if (maxsumSoFar < maxsumEndingHere){ 13 | // maxsumSoFar = maxsumEndingHere; 14 | // } 15 | 16 | // } 17 | // return maxsumSoFar; 18 | 19 | // } 20 | 21 | #include 22 | using namespace std; 23 | 24 | int sub_sum(int arr[], int n) 25 | { 26 | int cs = 0, largest = 0; 27 | for (int i = 0; i < n; i++) 28 | { 29 | 30 | cs += arr[i]; 31 | if (cs < 0) 32 | { 33 | cs = 0; 34 | } 35 | largest = max(cs, largest); 36 | } 37 | return largest; 38 | } 39 | int main() 40 | { 41 | int arr[] = {1, -2, 3, 4, 4, -2}; 42 | int n = sizeof(arr) / sizeof(int); 43 | cout << sub_sum(arr, n); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /C++/Last_Occurence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int last_occurance(int a[],int n,int k) 4 | { 5 | int mid,l=0,h=n-1; 6 | while(l<=h) 7 | { 8 | mid=(l+h)/2; 9 | if(a[mid]>k) 10 | h=mid-1; 11 | else if(a[mid]> n; 27 | int k; 28 | cin>>k; 29 | int a[n]; 30 | for(int i=0;i>a[i]; 33 | } 34 | int t=last_occurance(a,n,k); 35 | cout<>& matrix, int k) { 4 | int r = matrix.size(), c = matrix[0].size(); 5 | 6 | int prefix[r+1][c+1]; 7 | memset(prefix,0,sizeof(prefix)); 8 | 9 | for(int i=1; i<=r; ++i) prefix[i][1] = prefix[i-1][1] + matrix[i-1][0]; 10 | for(int i=1; i<=c; ++i) prefix[1][i] = prefix[1][i-1] + matrix[0][i-1]; 11 | 12 | for(int i=2; i<=r; ++i){ 13 | for(int j=2; j<=c; ++j){ 14 | prefix[i][j] = prefix[i-1][j] + prefix[i][j-1] - prefix[i-1][j-1] + matrix[i-1][j-1]; 15 | } 16 | } 17 | 18 | 19 | int ans = INT_MIN; 20 | for (int x1 = 1; x1 <= r; ++x1) { 21 | for (int y1 = 1; y1 <= c; ++y1) { 22 | for (int x2 = x1; x2 <= r; ++x2) { 23 | for (int y2 = y1; y2 <= c; ++y2) { 24 | int sum = prefix[x2][y2] - prefix[x1-1][y2] - prefix[x2][y1-1] + prefix[x1-1][y1-1]; 25 | if (sum <= k) { 26 | ans = max(ans, sum); 27 | } 28 | } 29 | } 30 | 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /C++/Longest_Common_Substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int lcs(string &str1, string &str2){ 4 | int n = str1.size(); 5 | int m = str2.size(); 6 | vector> dp(n+1 , vector(m+1 , 0)); 7 | 8 | int ans = 0; 9 | for(int i = 1 ; i <= n ; i++){ 10 | for(int j = 1 ; j <= m ; j++){ 11 | if(str1[i-1] == str2[j-1]){ 12 | dp[i][j] = 1 + dp[i-1][j-1]; 13 | ans = max(ans , dp[i][j]); 14 | } 15 | else{ 16 | dp[i][j] = 0; 17 | } 18 | } 19 | } 20 | return ans; 21 | } -------------------------------------------------------------------------------- /C++/Matrix_Chain_Multiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int matrixMultiplication(vector &arr, int N) 5 | { 6 | vector> dp(N , vector(N , 0)); 7 | 8 | for(int i = N-1 ; i >= 1 ; i--){ 9 | for(int j = i+1 ; j < N ; j++){ 10 | int mini = 1e9; 11 | for(int k = i ; k < j ; k++){ 12 | int sum = (arr[i-1] * arr[k] * arr[j]) + dp[i][k] + dp[k+1][j]; 13 | mini = min(mini , sum); 14 | } 15 | dp[i][j] = mini; 16 | } 17 | } 18 | return dp[1][N-1]; 19 | } -------------------------------------------------------------------------------- /C++/Prime Factors of a number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int number, c, count = 0; 6 | 7 | cout << "\n\n Print all Prime factors of a number:\n"; 8 | cout << "-----------------------------------\n"; 9 | cout << " Input a number: "; 10 | while (!(cin >> number) || number < 0) 11 | { 12 | cout << "ERROR! Number is invalid.\nEnter a number again."; 13 | cin.clear(); 14 | cin.ignore(123, '\n'); 15 | } 16 | cout << "Prime Factors of " << number << " are as below.\n"; 17 | for (c = 1; c <= number; c++) 18 | { 19 | if (number % c == 0) 20 | { 21 | count = 0; 22 | for (int i = 1; i <= (c / 2); i++) 23 | { 24 | if (c % i == 0) 25 | count++; 26 | } 27 | if (count == 1) 28 | cout << c << " "; 29 | } 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /C++/Queue_using_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // header is used for singly list 3 | using namespace std; 4 | 5 | class Queue{ 6 | int curr_size; 7 | list l; 8 | 9 | public: 10 | Queue(){ 11 | curr_size=0; 12 | } 13 | bool isempty(){ 14 | return curr_size==0; 15 | } 16 | void push(int data){ 17 | l.push_back(data); 18 | curr_size++; 19 | } 20 | void pop(){ 21 | if (!isempty()){ 22 | curr_size--; 23 | l.pop_front(); 24 | } 25 | } 26 | int front(){ 27 | return l.front(); 28 | } 29 | }; 30 | 31 | int main(){ 32 | Queue q; 33 | for (int i=1; i<=10;i++){ 34 | q.push(i); 35 | } 36 | while(!q.isempty()){ 37 | cout< 2 | using namespace std; 3 | #include 4 | 5 | void reverseSentence(string s) 6 | { 7 | stack rs; // stack for storing different words 8 | for (int i = 0; i < s.length(); i++) 9 | { 10 | string word = ""; // string to store each word 11 | while (s[i] != ' ' && i < s.length()) 12 | { 13 | // if(i==s.length()) 14 | // break; 15 | word += s[i]; 16 | i++; 17 | } 18 | rs.push(word); 19 | } 20 | 21 | while (!rs.empty()) 22 | { 23 | cout << rs.top() << " "; 24 | rs.pop(); 25 | } 26 | } 27 | int main() 28 | { 29 | string s = "Hello, how are you?"; 30 | reverseSentence(s); 31 | } -------------------------------------------------------------------------------- /C++/TowerOfHanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //tower of HANOI function implementation 5 | void TOH(int n,char A, char B,char C) 6 | { 7 | if(n==1) 8 | { 9 | cout<<"Move Disk "<>n; 25 | //calling the TOH 26 | TOH(n,'A','B','C'); 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /C++/Travelling-salesman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define V 4 4 | 5 | // implementation of traveling Salesman Problem 6 | int travllingSalesmanProblem(int graph[][V], int s) 7 | { 8 | // store all vertex apart from source vertex 9 | vector vertex; 10 | for (int i = 0; i < V; i++) 11 | if (i != s) 12 | vertex.push_back(i); 13 | 14 | int min_path = INT_MAX; 15 | do 16 | { 17 | 18 | int current_pathweight = 0; 19 | 20 | int k = s; 21 | for (int i = 0; i < vertex.size(); i++) 22 | { 23 | current_pathweight += graph[k][vertex[i]]; 24 | k = vertex[i]; 25 | } 26 | current_pathweight += graph[k][s]; 27 | 28 | // update minimum 29 | min_path = min(min_path, current_pathweight); 30 | 31 | } while ( 32 | next_permutation(vertex.begin(), vertex.end())); 33 | 34 | return min_path; 35 | } 36 | 37 | int main() 38 | { 39 | // matrix representation of graph 40 | int graph[][V] = {{0, 10, 15, 20}, 41 | {10, 0, 35, 25}, 42 | {15, 35, 0, 30}, 43 | {20, 25, 30, 0}}; 44 | int s = 0; 45 | cout << travllingSalesmanProblem(graph, s) << endl; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /C++/Validate_Binary_Search_Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isValidBST(TreeNode* root) { 15 | return isValidBST(root, NULL, NULL); 16 | } 17 | bool isValidBST(TreeNode* root, TreeNode* mini, TreeNode* maxi){ 18 | if(root==NULL) 19 | return true; 20 | if(mini && root->val <= mini->val || maxi && root->val >= maxi->val) 21 | return false; 22 | 23 | return isValidBST(root->left, mini, root) && isValidBST(root->right, root, maxi); 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /C++/andrew_algorithm_convex_hull.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define X first 4 | #define Y second 5 | typedef pair ii; 6 | 7 | int cross(ii O, ii A, ii B) 8 | { 9 | return (((A.X - O.X) * (B.Y - O.Y)) - ((A.Y - O.Y) * (B.X - O.X))); 10 | } 11 | 12 | vector ConvexHull(vector P) 13 | { 14 | if(P.size() <= 1) return P; 15 | vector H(2*P.size()); 16 | int k = 0; 17 | sort(P.begin(), P.end()); 18 | //lower hull 19 | for(int i = 0; i < P.size(); i++) 20 | { 21 | while(k >= 2 and cross(H[k-2], H[k-1], P[i]) < 0) k--; 22 | H[k++] = P[i]; 23 | } 24 | //upper hull 25 | for(int i = P.size()-2, l = k + 1; i >= 0; i--) 26 | { 27 | while(k >= l and cross(H[k-2], H[k-1], P[i]) < 0) k--; 28 | H[k++] = P[i]; 29 | } 30 | H.resize(k-1); 31 | return H; 32 | } 33 | 34 | int main() 35 | { 36 | int n, x, y; 37 | vector P; 38 | 39 | cin >> n; 40 | while(n--) 41 | { 42 | cin >> x >> y; 43 | P.push_back({x, y}); 44 | } 45 | 46 | vector H = ConvexHull(P); 47 | 48 | for(int i = 0; i < H.size(); i++) 49 | cout << H[i].X << ' ' << H[i].Y << '\n'; 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /C++/binary_exponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define F first 10 | #define S second 11 | #define rep(i,j,k) for(ll i=j;i 0) 18 | { 19 | if (b & 1) 20 | res = res * a; 21 | a = a * a; 22 | b >>= 1; 23 | } 24 | return res; 25 | } 26 | 27 | void solve() 28 | {cout<<"enter the number "<>a; 31 | cout<<"enter the power"<>b; 34 | cout<>t; 47 | rep(i,0,t) solve(); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /C++/butterfly_pattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cout<<"Enter the size of pattern : "; 7 | cin>>n; 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(int j=1;j<=i;j++){ 11 | cout<<"*"; 12 | } 13 | int t=2*n-2*i; 14 | for(int j=1;j<=t;j++) 15 | { 16 | cout<<" "; 17 | } 18 | for(int j=1;j<=i;j++) 19 | { 20 | cout<<"*"; 21 | } 22 | cout<=1;i--) 25 | { 26 | for(int j=1;j<=i;j++){ 27 | cout<<"*"; 28 | } 29 | int t=2*n-2*i; 30 | for(int j=1;j<=t;j++) 31 | { 32 | cout<<" "; 33 | } 34 | for(int j=1;j<=i;j++) 35 | { 36 | cout<<"*"; 37 | } 38 | cout< 4 | using namespace std; 5 | 6 | // Returns true if arr1[0..n-1] and arr2[0..m-1] 7 | // contain same elements. 8 | bool areEqual(int arr1[], int arr2[], int N, int M) 9 | { 10 | // If lengths of array are not equal means 11 | // array are not equal 12 | if (N != M) 13 | return false; 14 | 15 | // Sort both arrays 16 | sort(arr1, arr1 + N); 17 | sort(arr2, arr2 + M); 18 | 19 | // Linearly compare elements 20 | for (int i = 0; i < N; i++) 21 | if (arr1[i] != arr2[i]) 22 | return false; 23 | 24 | // If all elements were same. 25 | return true; 26 | } 27 | 28 | // Driver's Code 29 | int main() 30 | { 31 | int arr1[] = { 3, 5, 2, 5, 2 }; 32 | int arr2[] = { 2, 3, 5, 5, 2 }; 33 | int N = sizeof(arr1) / sizeof(int); 34 | int M = sizeof(arr2) / sizeof(int); 35 | 36 | // Function call 37 | if (areEqual(arr1, arr2, N, M)) 38 | cout << "Yes"; 39 | else 40 | cout << "No"; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /C++/chocolate_distribution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a[] = {7, 3, 2, 4, 9, 12, 56} ; 7 | int m = 3; 8 | int n = sizeof(a) / sizeof(a[0]); 9 | int mini = INT_MAX; 10 | 11 | sort(a, a+n); 12 | 13 | for (int i = 0; i + m - 1 < n; i++) { 14 | int diff = a[i + m - 1] - a[i]; 15 | if (diff < mini) 16 | mini = diff; 17 | } 18 | cout << "Minimum difference is "<< mini; 19 | } 20 | -------------------------------------------------------------------------------- /C++/count_nodes_in_circular_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to count number of nodes in a circular 2 | // linked list. 3 | #include 4 | using namespace std; 5 | 6 | /*structure for a node*/ 7 | 8 | struct Node { 9 | int data; 10 | Node* next; 11 | Node(int x) 12 | { 13 | data = x; 14 | next = NULL; 15 | } 16 | }; 17 | /* Function to insert a node at the beginning 18 | of a Circular linked list */ 19 | struct Node* push(struct Node* last, int data) 20 | { 21 | if (last == NULL) { 22 | struct Node* temp 23 | = (struct Node*)malloc(sizeof(struct Node)); 24 | 25 | // Assigning the data. 26 | temp->data = data; 27 | last = temp; 28 | // Note : list was empty. We link single node 29 | // to itself. 30 | temp->next = last; 31 | 32 | return last; 33 | } 34 | 35 | // Creating a node dynamically. 36 | struct Node* temp 37 | = (struct Node*)malloc(sizeof(struct Node)); 38 | 39 | // Assigning the data. 40 | temp->data = data; 41 | 42 | // Adjusting the links. 43 | temp->next = last->next; 44 | last->next = temp; 45 | 46 | return last; 47 | } 48 | 49 | /* Function to count nodes in a given Circular 50 | linked list */ 51 | 52 | int countNodes(Node* head) 53 | { 54 | Node* temp = head; 55 | int result = 0; 56 | if (head != NULL) { 57 | do { 58 | temp = temp->next; 59 | result++; 60 | } while (temp != head); 61 | } 62 | 63 | return result; 64 | } 65 | 66 | /* Driver program to test above functions */ 67 | int main() 68 | { 69 | /* Initialize lists as empty */ 70 | Node* head = NULL; 71 | head = push(head, 12); 72 | head = push(head, 56); 73 | head = push(head, 2); 74 | head = push(head, 11); 75 | cout << countNodes(head); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /C++/count_word.cpp: -------------------------------------------------------------------------------- 1 | // Java program to count number 2 | // of words present in a string 3 | 4 | import java.util.HashSet; 5 | import java.util.regex.Matcher; 6 | import java.util.regex.Pattern; 7 | 8 | public class Test { 9 | static int countOccurrence(String[] word, String str) 10 | { 11 | // counter 12 | int counter = 0; 13 | 14 | // for extracting words 15 | Pattern p = Pattern.compile("[a-zA-Z]+"); 16 | Matcher m = p.matcher(str); 17 | 18 | // HashSet for quick check whether 19 | // a word in str present in word[] or not 20 | HashSet hs = new HashSet(); 21 | 22 | for (String string : word) { 23 | hs.add(string); 24 | } 25 | 26 | while (m.find()) { 27 | if (hs.contains(m.group())) 28 | counter++; 29 | } 30 | 31 | return counter; 32 | } 33 | 34 | public static void main(String[] args) 35 | { 36 | String word[] 37 | = { "welcome", "to", "geeks", "portal" }; 38 | 39 | String str 40 | = "geeksforgeeks is a computer science portal for geeks."; 41 | 42 | System.out.println(countOccurrence(word, str)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /C++/dictOrder.cpp: -------------------------------------------------------------------------------- 1 | //TUSHAR 2 | //Given a set of m words (strings), arrange them in dictionary order. 3 | 4 | #include 5 | #define size 100 6 | using namespace std; 7 | 8 | class dictionary 9 | { 10 | public : 11 | 12 | string words[size]; 13 | int n; 14 | 15 | void createArray(); 16 | 17 | void heapSort(); 18 | void heapify(int sizee , int i); 19 | 20 | }; 21 | 22 | void dictionary::createArray() 23 | { 24 | cout<<"\nEnter no. of strings : "; 25 | cin>>n; 26 | while(n<=0) 27 | { 28 | cout<<"\n[ERROR] Invalid input , TRY AGAIN"; 29 | cout<<"\nEnter no. of strings : "; 30 | cin>>n; 31 | } 32 | 33 | cout<<"\nEnter the "<>words[i]; 37 | } 38 | } 39 | 40 | void dictionary::heapSort() 41 | { 42 | for(int i = n/2-1 ; i>=0 ; i--) 43 | { 44 | heapify(n,i); 45 | } 46 | 47 | for(int i = n-1 ; i>0 ; i--) 48 | { 49 | swap(words[0],words[i]); 50 | heapify(i,0); 51 | } 52 | 53 | 54 | } 55 | 56 | void dictionary::heapify(int sizee , int i) 57 | { 58 | int max = i; 59 | int left = 2*i+1; 60 | int right = 2*i+2; 61 | 62 | if(left < sizee && words[left]>words[max]) 63 | { 64 | max = left; 65 | } 66 | 67 | if(right < sizee && words[right]>words[max]) 68 | { 69 | max = right; 70 | } 71 | 72 | if(max != i) 73 | { 74 | swap(words[i],words[max]); 75 | heapify(sizee , max); 76 | } 77 | } 78 | 79 | int main() 80 | { 81 | dictionary d1; 82 | d1.createArray(); 83 | d1.heapSort(); 84 | cout<<"\ndictionary order of words : "< 2 | #include 3 | using namespace std; 4 | void measels(char,char,char,char,char); 5 | void flu(char,char,char,char,char,char,char,char); 6 | int main() 7 | { 8 | char name[50]; 9 | char a,b,c,d,e,f,g,h,i,j,k; 10 | cout << "Please enter your name.. " << endl; 11 | cin>> name; 12 | cout << "Do you have fever? (y/n)"<< endl; 13 | cin>>a; 14 | cout << "Do you have rashes? (y/n)"<< endl; 15 | cin>>b; 16 | cout << "Do you have headache? (y/n)"<< endl; 17 | cin>>c; 18 | cout << "Do you have running nose? (y/n)"<< 19 | endl; cin>>d; 20 | cout << "Do you have conjunctivities? (y/n)"<< 21 | endl; cin>>e; 22 | cout << "Do you have cough? (y/n)"<< endl; 23 | cin>>f; 24 | cout << "Do you have ache? (y/n)"<< endl; 25 | cin>>g; 26 | cout << "Do you have chills? (y/n)"<< endl; 27 | cin>>h; 28 | cout << "Do you have swollen glands? (y/n)"<< 29 | endl; cin>>i; 30 | cout << "Do you have snezzing? (y/n)"<< endl; 31 | cin>>j; 32 | cout << "Do you have sore throat? (y/n)"<< endl; 33 | cin>>k; 34 | measels(a,f,e,d,b); 35 | flu(a,c,g,e,h,k,f,d); 36 | return 0; 37 | } ; 38 | void measels(char q,char w,char r,char t,char y) 39 | { 40 | if(q=='y'&&w=='y'&& r=='y' && t=='y' && y== 41 | 'y') cout<< "You may have FLU."<< endl; 42 | else 43 | cout<< ""; 44 | } 45 | void flu(char q,char w,char r,char t,char y,char p,char l,char x) 46 | { 47 | if(q=='y'&&w=='y'&& r=='y' && t=='y' && y== 'y'&& p=='y' && l=='y' && 48 | x=='y') cout<< "You may have COVID."<< endl; 49 | else 50 | cout<< ""; 51 | } 52 | -------------------------------------------------------------------------------- /C++/fast_fourier_transform.cpp: -------------------------------------------------------------------------------- 1 | const int mod = 7340033; 2 | const int root = 5; 3 | const int root_1 = 4404020; 4 | const int root_pw = 1 << 20; 5 | 6 | void fft(vector & a, bool invert) { 7 | int n = a.size(); 8 | 9 | for (int i = 1, j = 0; i < n; i++) { 10 | int bit = n >> 1; 11 | for (; j & bit; bit >>= 1) 12 | j ^= bit; 13 | j ^= bit; 14 | 15 | if (i < j) 16 | swap(a[i], a[j]); 17 | } 18 | 19 | for (int len = 2; len <= n; len <<= 1) { 20 | int wlen = invert ? root_1 : root; 21 | for (int i = len; i < root_pw; i <<= 1) 22 | wlen = (int)(1LL * wlen * wlen % mod); 23 | 24 | for (int i = 0; i < n; i += len) { 25 | int w = 1; 26 | for (int j = 0; j < len / 2; j++) { 27 | int u = a[i+j], v = (int)(1LL * a[i+j+len/2] * w % mod); 28 | a[i+j] = u + v < mod ? u + v : u + v - mod; 29 | a[i+j+len/2] = u - v >= 0 ? u - v : u - v + mod; 30 | w = (int)(1LL * w * wlen % mod); 31 | } 32 | } 33 | } 34 | 35 | if (invert) { 36 | int n_1 = inverse(n, mod); 37 | for (int & x : a) 38 | x = (int)(1LL * x * n_1 % mod); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /C++/firstandlastoccurence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int first_occurence(int arr[],int n,int key){ 4 | int s=0,e=n-1; 5 | int mid=s+(e-s)/2; 6 | int ans=-1; 7 | while(s<=e){ 8 | if(arr[mid]==key){ 9 | ans=mid; 10 | e=mid-1; 11 | } 12 | else if(arr[mid]key){ 16 | e=mid-1; 17 | } 18 | mid=s+(e-s)/2; 19 | } 20 | return ans; 21 | } 22 | 23 | int last_occurence(int arr[],int n,int key){ 24 | int s=0,e=n-1; 25 | int mid=s+(e-s)/2; 26 | int ans=-1; 27 | while(s<=e){ 28 | if(arr[mid]==key){ 29 | ans=mid; 30 | s=mid+1; 31 | } 32 | else if(arr[mid]key){ 36 | e=mid-1; 37 | } 38 | mid=s+(e-s)/2; 39 | } 40 | return ans; 41 | } 42 | int main(){ 43 | 44 | int even[11]={1,2,3,4,5,5,5,5,5,6,7}; 45 | cout<<"First Occurence of 5 is at index: "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void rgb_color_code(int rgb[]); 7 | 8 | void rgb_color_code(int rgb[]) 9 | { 10 | int i; 11 | for(i=0;i<3;i++) 12 | { 13 | rgb[i]=rand()%256; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | int rgb[3]; 20 | char hex[6]; 21 | srand(time(0)); 22 | 23 | //RGB color code 24 | rgb_color_code(rgb); 25 | cout<<"Random RGB color code: "; 26 | cout<<"rgb("; 27 | for(int i=0;i<3;i++) 28 | { 29 | cout< 4 | #include 5 | using namespace std; 6 | 7 | // Link list node 8 | class Node { 9 | public: 10 | int data; 11 | Node* next; 12 | }; 13 | 14 | /* Given a reference (pointer to 15 | pointer) to the head of a list 16 | and an int, push a new node on 17 | the front of the list. */ 18 | void push(Node** head_ref, int new_data) 19 | { 20 | 21 | // allocate node 22 | Node* new_node = new Node(); 23 | 24 | // put in the data 25 | new_node->data = new_data; 26 | 27 | // link the old list 28 | // off the new node 29 | new_node->next = (*head_ref); 30 | 31 | // move the head to point 32 | // to the new node 33 | (*head_ref) = new_node; 34 | } 35 | 36 | // Takes head pointer of 37 | // the linked list and index 38 | // as arguments and return 39 | // data at index 40 | int GetNth(Node* head, int index) 41 | { 42 | 43 | Node* current = head; 44 | 45 | // the index of the 46 | // node we're currently 47 | // looking at 48 | int count = 0; 49 | while (current != NULL) { 50 | if (count == index) 51 | return (current->data); 52 | count++; 53 | current = current->next; 54 | } 55 | 56 | /* if we get to this line, 57 | the caller was asking 58 | for a non-existent element 59 | so we assert fail */ 60 | assert(0); 61 | } 62 | 63 | // Driver Code 64 | int main() 65 | { 66 | 67 | // Start with the 68 | // empty list 69 | Node* head = NULL; 70 | 71 | // Use push() to construct 72 | // below list 73 | // 1->12->1->4->1 74 | push(&head, 1); 75 | push(&head, 4); 76 | push(&head, 1); 77 | push(&head, 12); 78 | push(&head, 1); 79 | 80 | // Check the count 81 | // function 82 | cout << "Element at index 3 is " << GetNth(head, 3); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /C++/painterspartitionproblem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int sum(int arr[], int from, int to) 6 | { 7 | int total = 0; 8 | for (int i = from; i <= to; i++) 9 | total += arr[i]; 10 | return total; 11 | } 12 | 13 | 14 | int partition(int arr[], int n, int k) 15 | { 16 | 17 | if (k == 1) // one partition 18 | return sum(arr, 0, n - 1); 19 | if (n == 1) // one board 20 | return arr[0]; 21 | 22 | int best = INT_MAX; 23 | for (int i = 1; i <= n; i++) 24 | best = min(best, max(partition(arr, i, k - 1), 25 | sum(arr, i, n - 1))); 26 | 27 | return best; 28 | } 29 | 30 | int main() 31 | { 32 | int arr[] = { 10, 20, 60, 50, 30, 40 }; 33 | int n = sizeof(arr) / sizeof(arr[0]); 34 | int k = 3; 35 | cout << partition(arr, n, k) << endl; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /C++/palindrome_recursion.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | 6 | bool isPalRec(char str[], 7 | int s, int e) 8 | { 9 | 10 | // If there is only one character 11 | if (s == e) 12 | return true; 13 | 14 | // If first and last 15 | // characters do not match 16 | if (str[s] != str[e]) 17 | return false; 18 | 19 | 20 | if (s < e + 1) 21 | return isPalRec(str, s + 1, e - 1); 22 | 23 | return true; 24 | } 25 | 26 | bool isPalindrome(char str[]) 27 | { 28 | int n = strlen(str); 29 | 30 | // An empty string is 31 | // considered as palindrome 32 | if (n == 0) 33 | return true; 34 | 35 | return isPalRec(str, 0, n - 1); 36 | } 37 | 38 | // Driver Code 39 | int main() 40 | { 41 | char str[] = "geeg"; 42 | 43 | if (isPalindrome(str)) 44 | cout << "Yes"; 45 | else 46 | cout << "No"; 47 | 48 | return 0; 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /C++/pangram_check.cpp: -------------------------------------------------------------------------------- 1 | // A C++ Program to check if the given 2 | // string is a pangram or not 3 | #include 4 | using namespace std; 5 | 6 | // Returns true if the string is pangram else false 7 | bool checkPangram(string& str) 8 | { 9 | // Create a hash table to mark the characters 10 | // present in the string 11 | vector mark(26, false); 12 | // For indexing in mark[] 13 | int index; 14 | // Traverse all characters 15 | for (int i = 0; i < str.length(); i++) { 16 | // If uppercase character, subtract 'A' 17 | // to find index. 18 | if ('A' <= str[i] && str[i] <= 'Z') 19 | index = str[i] - 'A'; 20 | // If lowercase character, subtract 'a' 21 | // to find index. 22 | else if ('a' <= str[i] && str[i] <= 'z') 23 | index = str[i] - 'a'; 24 | // If this character is other than english 25 | // lowercase and uppercase characters. 26 | else 27 | continue; 28 | mark[index] = true; 29 | } 30 | 31 | // Return false if any character is unmarked 32 | for (int i = 0; i <= 25; i++) 33 | if (mark[i] == false) 34 | return (false); 35 | // If all characters were present 36 | return (true); 37 | } 38 | 39 | // Driver Program to test above functions 40 | int main() 41 | { 42 | string str = "The quick brown fox jumps over the" 43 | " lazy dog"; 44 | if (checkPangram(str) == true) 45 | printf(" %s \nis a pangram", str.c_str()); 46 | else 47 | printf(" %s \nis not a pangram", str.c_str()); 48 | return (0); 49 | } 50 | -------------------------------------------------------------------------------- /C++/reverse_array.cpp: -------------------------------------------------------------------------------- 1 | // Basic Java program that reverses an array 2 | 3 | public class reverseArray { 4 | 5 | // function that reverses array and stores it 6 | // in another array 7 | static void reverse(int a[], int n) 8 | { 9 | int[] b = new int[n]; 10 | int j = n; 11 | for (int i = 0; i < n; i++) { 12 | b[j - 1] = a[i]; 13 | j = j - 1; 14 | } 15 | 16 | // printing the reversed array 17 | System.out.println("Reversed array is: \n"); 18 | for (int k = 0; k < n; k++) { 19 | System.out.println(b[k]); 20 | } 21 | } 22 | 23 | public static void main(String[] args) 24 | { 25 | int [] arr = {10, 20, 30, 40, 50}; 26 | reverse(arr, arr.length); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /C++/right_view_of_a_binary tree.cpp: -------------------------------------------------------------------------------- 1 | and a pointer to right child 2 | struct Node 3 | { 4 | int data; 5 | struct Node* left; 6 | struct Node* right; 7 | 8 | Node(int x){ 9 | data = x; 10 | left = right = NULL; 11 | } 12 | }; */ 13 | 14 | // Should return right view of tree 15 | class Solution 16 | { 17 | public: 18 | //Function to return list containing elements of right view of binary tree. 19 | vector rightView(Node *root) 20 | { 21 | // Your Code here 22 | vectorv; 23 | if(!root)return v; 24 | queueq; 25 | q.push(root); 26 | while(!q.empty()){ 27 | // v.push_back(q.front()->data); 28 | int sz=q.size(); 29 | while(sz--){ 30 | Node*t=q.front(); 31 | q.pop(); 32 | if(t->left)q.push(t->left); 33 | if(t->right)q.push(t->right); 34 | if(sz==0)v.push_back(t->data); 35 | } 36 | } 37 | return v; 38 | } 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /C++/rotate_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Fuction to rotate array 5 | void Rotate(int arr[], int d, int n) 6 | { 7 | // Storing rotated version of array 8 | int temp[n]; 9 | 10 | // Keepig track of the current index 11 | // of temp[] 12 | int k = 0; 13 | 14 | // Storing the n - d elements of 15 | // array arr[] to the front of temp[] 16 | for (int i = d; i < n; i++) { 17 | temp[k] = arr[i]; 18 | k++; 19 | } 20 | 21 | // Storing the first d elements of array arr[] 22 | // into temp 23 | for (int i = 0; i < d; i++) { 24 | temp[k] = arr[i]; 25 | k++; 26 | } 27 | 28 | // Copying the elements of temp[] in arr[] 29 | // to get the final rotated array 30 | for (int i = 0; i < n; i++) { 31 | arr[i] = temp[i]; 32 | } 33 | } 34 | 35 | // Function to print elements of array 36 | void PrintTheArray(int arr[], int n) 37 | { 38 | for (int i = 0; i < n; i++) { 39 | cout << arr[i] << " "; 40 | } 41 | } 42 | 43 | // Driver code 44 | int main() 45 | { 46 | int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; 47 | int N = sizeof(arr) / sizeof(arr[0]); 48 | int d = 2; 49 | 50 | // Function calling 51 | Rotate(arr, d, N); 52 | PrintTheArray(arr, N); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /C++/startPyramid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int rows; 7 | 8 | cout << "Enter number of rows: "; 9 | cin >> rows; 10 | 11 | for(int i = 1; i <= rows; ++i) 12 | { 13 | for(int j = 1; j <= i; ++j) 14 | { 15 | cout << "* "; 16 | } 17 | cout << "\n"; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C++/stepping_number.cpp: -------------------------------------------------------------------------------- 1 | // A Java program to find all the Stepping Number in [n, m] 2 | class Main 3 | { 4 | // This Method checks if an integer n 5 | // is a Stepping Number 6 | public static boolean isStepNum(int n) 7 | { 8 | // Initialize prevDigit with -1 9 | int prevDigit = -1; 10 | 11 | // Iterate through all digits of n and compare 12 | // difference between value of previous and 13 | // current digits 14 | while (n > 0) 15 | { 16 | // Get Current digit 17 | int curDigit = n % 10; 18 | 19 | // Single digit is consider as a 20 | // Stepping Number 21 | if (prevDigit != -1) 22 | { 23 | // Check if absolute difference between 24 | // prev digit and current digit is 1 25 | if (Math.abs(curDigit-prevDigit) != 1) 26 | return false; 27 | } 28 | n /= 10; 29 | prevDigit = curDigit; 30 | } 31 | return true; 32 | } 33 | 34 | // A brute force approach based function to find all 35 | // stepping numbers. 36 | public static void displaySteppingNumbers(int n,int m) 37 | { 38 | // Iterate through all the numbers from [N,M] 39 | // and check if it is a stepping number. 40 | for (int i = n; i <= m; i++) 41 | if (isStepNum(i)) 42 | System.out.print(i+ " "); 43 | } 44 | 45 | // Driver code 46 | public static void main(String args[]) 47 | { 48 | int n = 0, m = 21; 49 | 50 | // Display Stepping Numbers in the range [n,m] 51 | displaySteppingNumbers(n,m); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /C++/sum_of_nodes_in_top_view_of_a_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of the approach 2 | #include 3 | using namespace std; 4 | 5 | // Structure of binary tree 6 | struct Node { 7 | Node* left; 8 | Node* right; 9 | int hd; 10 | int data; 11 | }; 12 | 13 | // Function to create a new node 14 | Node* newNode(int key) 15 | { 16 | Node* node = new Node(); 17 | node->left = node->right = NULL; 18 | node->data = key; 19 | return node; 20 | } 21 | 22 | // Function that returns the sum of 23 | // nodes in top view of binary tree 24 | int SumOfTopView(Node* root) 25 | { 26 | if (root == NULL) 27 | return 0; 28 | 29 | queue q; 30 | 31 | map m; 32 | int hd = 0; 33 | 34 | root->hd = hd; 35 | 36 | int sum = 0; 37 | 38 | // Push node and horizontal distance to queue 39 | q.push(root); 40 | 41 | while (q.size()) { 42 | hd = root->hd; 43 | 44 | // Count function returns 1 if the container 45 | // contains an element whose key is equivalent 46 | // to hd, or returns zero otherwise. 47 | if (m.count(hd) == 0) { 48 | m[hd] = root->data; 49 | sum += m[hd]; 50 | } 51 | if (root->left) { 52 | root->left->hd = hd - 1; 53 | q.push(root->left); 54 | } 55 | if (root->right) { 56 | root->right->hd = hd + 1; 57 | q.push(root->right); 58 | } 59 | q.pop(); 60 | root = q.front(); 61 | } 62 | 63 | return sum; 64 | } 65 | 66 | // Driver code 67 | int main() 68 | { 69 | Node* root = newNode(1); 70 | root->left = newNode(2); 71 | root->right = newNode(3); 72 | root->left->right = newNode(4); 73 | root->left->right->right = newNode(5); 74 | root->left->right->right->right = newNode(6); 75 | 76 | cout << SumOfTopView(root); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /C++/top_view_of_a_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to print top 2 | // view of binary tree 3 | 4 | #include 5 | using namespace std; 6 | 7 | // Structure of binary tree 8 | struct Node { 9 | Node* left; 10 | Node* right; 11 | int hd; 12 | int data; 13 | }; 14 | 15 | // function to create a new node 16 | Node* newNode(int key) 17 | { 18 | Node* node = new Node(); 19 | node->left = node->right = NULL; 20 | node->data = key; 21 | return node; 22 | } 23 | 24 | // function should print the topView of 25 | // the binary tree 26 | void topview(Node* root) 27 | { 28 | if (root == NULL) 29 | return; 30 | queue q; 31 | map m; 32 | int hd = 0; 33 | root->hd = hd; 34 | 35 | // push node and horizontal distance to queue 36 | q.push(root); 37 | 38 | cout << "The top view of the tree is : \n"; 39 | 40 | while (q.size()) { 41 | hd = root->hd; 42 | 43 | // count function returns 1 if the container 44 | // contains an element whose key is equivalent 45 | // to hd, or returns zero otherwise. 46 | if (m.count(hd) == 0) 47 | m[hd] = root->data; 48 | if (root->left) { 49 | root->left->hd = hd - 1; 50 | q.push(root->left); 51 | } 52 | if (root->right) { 53 | root->right->hd = hd + 1; 54 | q.push(root->right); 55 | } 56 | q.pop(); 57 | root = q.front(); 58 | } 59 | 60 | for (auto i = m.begin(); i != m.end(); i++) { 61 | cout << i->second << " "; 62 | } 63 | } 64 | 65 | // Driver code 66 | int main() 67 | { 68 | /* Create following Binary Tree 69 | 1 70 | / \ 71 | 2 3 72 | \ 73 | 4 74 | \ 75 | 5 76 | \ 77 | 6 78 | */ 79 | Node* root = newNode(1); 80 | root->left = newNode(2); 81 | root->right = newNode(3); 82 | root->left->right = newNode(4); 83 | root->left->right->right = newNode(5); 84 | root->left->right->right->right = newNode(6); 85 | cout << "Following are nodes in top view of Binary " 86 | "Tree\n"; 87 | topview(root); 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /C++/trapRainWater.cpp: -------------------------------------------------------------------------------- 1 | //PROGRAM TO CALCULATE AMOUNT OF RAIN WATER TRAPPED 2 | #include 3 | 4 | using namespace std; 5 | 6 | class TrapWater{ 7 | 8 | 9 | public: 10 | //Function to find the amount of trapped water between the blocks 11 | int trappingWater(int arr[], int n) 12 | { 13 | int ToatalWaterTrap=0 , Larr[n] , Rarr[n] ; 14 | 15 | Larr[0]=arr[0]; 16 | for(int i = 1 ; i < n ; i++) 17 | { 18 | Larr[i] = (arr[i]>Larr[i-1])? arr[i]:Larr[i-1] ; 19 | } 20 | 21 | Rarr[n-1]=arr[n-1]; 22 | for(int i = n-2 ; i >= 0 ; i--) 23 | { 24 | Rarr[i] = (arr[i]>Rarr[i+1])? arr[i]:Rarr[i+1] ; 25 | } 26 | 27 | for(int i = 0 ; i < n ; i++) 28 | { 29 | ToatalWaterTrap += (Larr[i]>Rarr[i])? (Rarr[i]-arr[i]):(Larr[i]-arr[i]) ; 30 | } 31 | 32 | return ToatalWaterTrap; 33 | 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | int size; 40 | 41 | cout<<"\nEnter size of array : "; //getting size of array from user 42 | cin>>size; 43 | 44 | while(size<1) // size should be >=1 45 | { 46 | cout<<"\n[ERROR] Invalid size ##try again##"; 47 | cout<<"\nEnter size of array : "; 48 | cin>>size; 49 | } 50 | 51 | int arr[size]; 52 | 53 | cout<<"\nEnter height of all the "<>arr[i]; 57 | 58 | } 59 | 60 | for(int i=0;i>arr[i]; 67 | } 68 | } 69 | 70 | TrapWater tw ; 71 | 72 | cout<<"\nTotal Units Of Water Trapped In Blocks : "< 2 | #include 3 | typedef struct contact{ 4 | long long int phoneno; 5 | char name[100]; 6 | }person; 7 | person user[1000]; 8 | int main(){ 9 | int choice,index=1,ch; 10 | char search[100]; 11 | main:printf("**********Welcome to Contacts**********"); 12 | printf("\n*************Main Menu*****************\n[1]Add a new contact \n[2]Search a user \n[3]List contacts \n[4]Exit\n"); 13 | printf("Enter a choice:"); 14 | scanf("%d",&choice); 15 | switch(choice){ 16 | case 1: 17 | printf("Enter name:"); 18 | scanf("%s",&user[index].name); 19 | printf("Enter mobile number:"); 20 | scanf("%lld",&user[index].phoneno); 21 | printf("\n\nContact successfully created!\n%s now successfully added to your contacts.",user[index].name); 22 | index++; 23 | break; 24 | case 2: 25 | printf("Enter name:"); 26 | scanf("%s",&search); 27 | for(int i=0;i<=index;i++){ 28 | if(strcmp(search,user[i].name)==0){ 29 | printf("Mobile Number:%lld",user[i].phoneno); 30 | break; 31 | } 32 | } 33 | break; 34 | case 3: 35 | for(int i=1;i 2 | #include 3 | void main(){ 4 | int x,y,z; 5 | printf("Enter no. of rows: "); 6 | scanf("%x",&x); 7 | for(int i=1;i<=x;i++){ 8 | y=i; 9 | z=0; 10 | while (y!=0) 11 | { 12 | while (z <= x-y) 13 | { 14 | printf(" "); 15 | ++z; 16 | } 17 | printf("x"); 18 | --y; 19 | } 20 | printf("\n"); 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /C/Tree traverals/tree_inOrder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct treeNode 5 | { 6 | int data ; 7 | struct treeNode * left ; 8 | struct treeNode *right ; 9 | } ; 10 | 11 | struct treeNode* createNode(int val) 12 | { 13 | struct treeNode * node = (struct treeNode *) malloc(sizeof(struct treeNode)) ; 14 | 15 | node->data = val ; 16 | node->left = NULL ; 17 | node->right = NULL ; 18 | } 19 | 20 | void inOrderTraversal(struct treeNode * root) // left subtree Root Right Subtree 21 | { 22 | if(root) 23 | { 24 | inOrderTraversal(root->left) ; 25 | printf("%d " , root->data) ; 26 | inOrderTraversal(root->right) ; 27 | } 28 | 29 | } 30 | 31 | signed main() 32 | { 33 | /* Binary Tree is a tree with each node having atmost 2 children . 34 | 35 | 7 36 | 23 6 37 | 9 8 38 | 39 | */ 40 | struct treeNode * root = createNode(7) ; 41 | struct treeNode *n1 = createNode(23) ; 42 | struct treeNode *n2 = createNode(6) ; 43 | struct treeNode *n3 = createNode(9) ; 44 | struct treeNode *n4 = createNode(8) ; 45 | 46 | root->left = n1 ; 47 | root->right = n2 ; 48 | 49 | n1->left = n3 ; 50 | 51 | 52 | n2->right = n4 ; 53 | 54 | 55 | inOrderTraversal(root) ; 56 | 57 | 58 | return 0 ; 59 | } -------------------------------------------------------------------------------- /C/Tree traverals/tree_postOrder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct treeNode 5 | { 6 | int data ; 7 | struct treeNode * left ; 8 | struct treeNode *right ; 9 | } ; 10 | 11 | struct treeNode * createNode(int val) 12 | { 13 | struct treeNode * node = (struct treeNode *) malloc(sizeof(struct treeNode)) ; 14 | 15 | node->data = val ; 16 | node->left = NULL ; 17 | node->right = NULL ; 18 | } 19 | 20 | void postOrderTraversal(struct treeNode * root) // Root left subtree Right Subtree 21 | { 22 | if(root) 23 | { 24 | postOrderTraversal(root->left) ; 25 | postOrderTraversal(root->right) ; 26 | printf("%d " , root->data) ; 27 | } 28 | 29 | } 30 | 31 | signed main() 32 | { 33 | /* Binary Tree is a tree with each node having atmost 2 children . 34 | 35 | 7 36 | 23 6 37 | 9 8 38 | 39 | */ 40 | struct treeNode * root = createNode(7) ; 41 | struct treeNode *n1 = createNode(23) ; 42 | struct treeNode *n2 = createNode(6) ; 43 | struct treeNode *n3 = createNode(9) ; 44 | struct treeNode *n4 = createNode(8) ; 45 | 46 | root->left = n1 ; 47 | root->right = n2 ; 48 | 49 | n1->left = n3 ; 50 | 51 | 52 | n2->right = n4 ; 53 | 54 | 55 | postOrderTraversal(root) ; 56 | 57 | 58 | return 0 ; 59 | } -------------------------------------------------------------------------------- /C/Tree traverals/tree_preOrderTrav.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct treeNode 5 | { 6 | int data ; 7 | struct treeNode * left ; 8 | struct treeNode *right ; 9 | } ; 10 | 11 | struct treeNode * createNode(int val) 12 | { 13 | struct treeNode * node = (struct treeNode *) malloc(sizeof(struct treeNode)) ; 14 | 15 | node->data = val ; 16 | node->left = NULL ; 17 | node->right = NULL ; 18 | } 19 | 20 | void preOrderTraversal(struct treeNode * root) // Root left subtree Right Subtree 21 | { 22 | if(root) 23 | { 24 | printf("%d " , root->data) ; 25 | preOrderTraversal(root->left) ; 26 | preOrderTraversal(root->right) ; 27 | } 28 | 29 | } 30 | 31 | signed main() 32 | { 33 | /* Binary Tree is a tree with each node having atmost 2 children . 34 | 35 | 7 36 | 23 6 37 | 9 8 38 | 39 | */ 40 | struct treeNode * root = createNode(7) ; 41 | struct treeNode *n1 = createNode(23) ; 42 | struct treeNode *n2 = createNode(6) ; 43 | struct treeNode *n3 = createNode(9) ; 44 | struct treeNode *n4 = createNode(8) ; 45 | 46 | root->left = n1 ; 47 | root->right = n2 ; 48 | 49 | n1->left = n3 ; 50 | 51 | 52 | n2->right = n4 ; 53 | 54 | 55 | preOrderTraversal(root) ; 56 | 57 | 58 | return 0 ; 59 | } -------------------------------------------------------------------------------- /C/VRC.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void sender() 6 | { 7 | int arr[4][5]; 8 | printf("Enter a 16 bit binary data in blocks of four:\n"); 9 | for (int i = 0; i < 4; i++) 10 | { 11 | printf("Enter Block%d: ", i + 1); 12 | int pairity = 0; 13 | for (int j = 0; j < 4; j++) 14 | { 15 | scanf("%1d", &arr[i][j]); 16 | pairity ^= arr[i][j]; 17 | } 18 | arr[i][4] = pairity; 19 | } 20 | 21 | printf("\nSender generated dataBlock: "); 22 | for (int i = 0; i < 4; i++) 23 | { 24 | for (int j = 0; j < 5; j++) 25 | { 26 | printf("%d", arr[i][j]); 27 | } 28 | printf(" "); 29 | } 30 | } 31 | 32 | void reciever() 33 | { 34 | int arr[4][5]; 35 | printf("Enter the dataBlock generated by the sender:\n"); 36 | for (int i = 0; i < 4; i++) 37 | { 38 | int pairity = 0; 39 | printf("Enter Block%d: ", i + 1); 40 | for (int j = 0; j < 5; j++) 41 | { 42 | scanf("%1d", &arr[i][j]); 43 | pairity ^= arr[i][j]; 44 | } 45 | if (pairity == 1) 46 | { 47 | printf("Invalid dataBlock!\n"); 48 | exit(0); 49 | } 50 | } 51 | printf("DataBlock Accepted!\n"); 52 | } 53 | int main() 54 | { 55 | printf("Vertical Redundency Check\n\n"); 56 | int n; 57 | printf("Enter 1. for Sender 2. for reciever: "); 58 | scanf("%d", &n); 59 | if (n == 1) 60 | sender(); 61 | else if (n == 2) 62 | reciever(); 63 | else 64 | { 65 | printf("Invalid Input!\n"); 66 | exit(0); 67 | } 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /C/additon.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | 4 | int number1, number2, sum; 5 | 6 | printf("Enter two integers: "); 7 | scanf("%d %d", &number1, &number2); 8 | 9 | 10 | sum = number1 + number2; 11 | 12 | printf("%d + %d = %d", number1, number2, sum); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C/amstrong.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int num, originalNum, remainder, result = 0; 5 | printf("Enter a three-digit integer: "); 6 | scanf("%d", &num); 7 | originalNum = num; 8 | 9 | while (originalNum != 0) { 10 | // remainder contains the last digit 11 | remainder = originalNum % 10; 12 | 13 | result += remainder * remainder * remainder; 14 | 15 | // removing last digit from the orignal number 16 | originalNum /= 10; 17 | } 18 | 19 | if (result == num) 20 | printf("%d is an Armstrong number.", num); 21 | else 22 | printf("%d is not an Armstrong number.", num); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /C/area of circle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | float radius, area; 6 | 7 | printf("Enter the radius of a circle\n"); 8 | 9 | scanf("%f", &radius); 10 | 11 | area = 3.14159*radius*radius; 12 | 13 | printf("Area of the circle = %.2f\n", area); // printing upto two decimal places 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /C/bubble-sort.c: -------------------------------------------------------------------------------- 1 | // C program for implementation of Bubble sort 2 | #include 3 | 4 | void swap(int* xp, int* yp) 5 | { 6 | int temp = *xp; 7 | *xp = *yp; 8 | *yp = temp; 9 | } 10 | 11 | void bubbleSort(int arr[], int n) 12 | { 13 | int i, j; 14 | for (i = 0; i < n - 1; i++) 15 | 16 | for (j = 0; j < n - i - 1; j++) 17 | if (arr[j] > arr[j + 1]) 18 | swap(&arr[j], &arr[j + 1]); 19 | } 20 | 21 | void printArray(int arr[], int size) 22 | { 23 | int i; 24 | for (i = 0; i < size; i++) 25 | printf("%d ", arr[i]); 26 | printf("\n"); 27 | } 28 | 29 | int main() 30 | { 31 | int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; 32 | int n = sizeof(arr) / sizeof(arr[0]); 33 | bubbleSort(arr, n); 34 | printf("Sorted array: \n"); 35 | printArray(arr, n); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /C/even and odd.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i,n,even=0,odd=0; 5 | printf("\nEnter the Ending value:"); 6 | scanf("%d",&n); 7 | printf("\nEven numbers:"); 8 | for(i=0;i<=n;i++) 9 | { 10 | if(i%2==0) 11 | { 12 | printf("\n%d",i); 13 | even++; 14 | } 15 | } 16 | printf("\nOdd numbers:"); 17 | for(i=1;i<=n;i++) 18 | { 19 | if(i%2==1) 20 | { 21 | printf("\n%d",i); 22 | odd++; 23 | } 24 | } 25 | printf("\nTotal even numbers:%d",even); 26 | printf("\nTotal odd numbers:%d",odd); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /C/factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num; 5 | unsigned long long fact = 1; 6 | 7 | printf("Enter a non-negative integer: "); 8 | scanf("%d", &num); 9 | 10 | if (num < 0) { 11 | printf("Factorial is not defined for negative numbers.\n"); 12 | } else { 13 | for (int i = 1; i <= num; i++) { 14 | fact *= i; 15 | } 16 | printf("Factorial of %d = %llu\n", num, fact); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C/fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n1=0,n2=1,n3,i,number; 5 | printf("Enter the number of elements:"); 6 | scanf("%d",&number); 7 | printf("\n%d %d",n1,n2);//printing 0 and 1 8 | for(i=2;i 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | 10 | int data[20],hamming[50] , count=0; 11 | char cdata[20]; 12 | printf("Enter data(bits) : "); 13 | scanf("%s",cdata); 14 | for(int i=0 ; i=strlen(cdata)+n) 22 | { 23 | break; 24 | } 25 | else 26 | { 27 | n++; 28 | } 29 | 30 | } 31 | int h=n+strlen(cdata); 32 | count=strlen(cdata)-1; 33 | 34 | for(int i=0 ; i=0 ;i--) 78 | { 79 | printf("%d",hamming[i]); 80 | } 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /C/inverted full pyramid: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int rows, i, j, space; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = rows; i >= 1; --i) { 7 | for (space = 0; space < rows - i; ++space) 8 | printf(" "); 9 | for (j = i; j <= 2 * i - 1; ++j) 10 | printf("* "); 11 | for (j = 0; j < i - 1; ++j) 12 | printf("* "); 13 | printf("\n"); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /C/inverted half pyramid: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, j, rows; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = rows; i >= 1; --i) { 7 | for (j = 1; j <= i; ++j) { 8 | printf("* "); 9 | } 10 | printf("\n"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /C/palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int n, reversed = 0, remainder, original; 4 | printf("Enter an integer: "); 5 | scanf("%d", &n); 6 | original = n; 7 | 8 | // reversed integer is stored in reversed variable 9 | while (n != 0) { 10 | remainder = n % 10; 11 | reversed = reversed * 10 + remainder; 12 | n /= 10; 13 | } 14 | 15 | // palindrome if orignal and reversed are equal 16 | if (original == reversed) 17 | printf("%d is a palindrome.", original); 18 | else 19 | printf("%d is not a palindrome.", original); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /C/pascals triangle: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int rows, coef = 1, space, i, j; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = 0; i < rows; i++) { 7 | for (space = 1; space <= rows - i; space++) 8 | printf(" "); 9 | for (j = 0; j <= i; j++) { 10 | if (j == 0 || i == 0) 11 | coef = 1; 12 | else 13 | coef = coef * (i - j + 1) / j; 14 | printf("%4d", coef); 15 | } 16 | printf("\n"); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /C/quick_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void quicksort(int number[25],int first,int last){ 3 | int i, j, pivot, temp; 4 | if(firstnumber[pivot]) 12 | j--; 13 | if(i 2 | struct Node { 3 | int data; 4 | struct Node* next; 5 | Node(int data){ 6 | this->data = data; 7 | next = NULL; 8 | } 9 | }; 10 | struct LinkedList { 11 | Node* head; 12 | LinkedList(){ 13 | head = NULL; 14 | } 15 | void interReverseLL(){ 16 | Node* current = head; 17 | Node *prev = NULL, *after = NULL; 18 | while (current != NULL) { 19 | after = current->next; 20 | current->next = prev; 21 | prev = current; 22 | current = after; 23 | } 24 | head = prev; 25 | } 26 | void print() { 27 | struct Node* temp = head; 28 | while (temp != NULL) { 29 | printf("%d ", temp-> data); 30 | temp = temp->next; 31 | } 32 | printf("\n"); 33 | } 34 | void push(int data){ 35 | Node* temp = new Node(data); 36 | temp->next = head; 37 | head = temp; 38 | } 39 | }; 40 | int main() { 41 | LinkedList linkedlist; 42 | linkedlist.push(85); 43 | linkedlist.push(10); 44 | linkedlist.push(65); 45 | linkedlist.push(32); 46 | linkedlist.push(9); 47 | printf("Linked List : \t"); 48 | linkedlist.print(); 49 | linkedlist.interReverseLL(); 50 | printf("Reverse Linked List : \t"); 51 | linkedlist.print(); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /C/starpattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i,j; 5 | for (i=1;i<=5;i++); 6 | { 7 | for (j=5;j>=i;j--); 8 | { 9 | printf("*"); 10 | } 11 | printf("\n"); 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /C/traingle: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int rows = 5; 4 | 5 | // first loop for printing rows 6 | for (int i = 0; i < rows; i++) { 7 | 8 | // second loop for printing character in each rows 9 | for (int j = 0; j <= i; j++) { 10 | printf("* "); 11 | } 12 | printf("\n"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Go/bubbleSort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func BubbleSort(array []int, l int) []int { 6 | for i := 0; i < l-1; i++ { 7 | for j := 0; j < l-i-1; j++ { 8 | if array[j] > array[j+1] { 9 | array[j], array[j+1] = array[j+1], array[j] 10 | } 11 | } 12 | } 13 | return array 14 | } 15 | func main() { 16 | array := []int{11, 40, 3, 38, 18, 70, 43} 17 | l := len(array) 18 | fmt.Println(BubbleSort(array, l)) 19 | } 20 | -------------------------------------------------------------------------------- /Go/linearsearch.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func linearsearch(datalist []int, key int) bool { 6 | for _, item := range datalist { 7 | if item == key { 8 | return true 9 | } 10 | } 11 | return false 12 | } 13 | 14 | func main() { 15 | items := []int{95,78,46,58,45,86,99,251,320} 16 | fmt.Println(linearsearch(items,58)) 17 | } 18 | -------------------------------------------------------------------------------- /Html/Class 12th Solutions/ProfitLoss.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | 25 |

26 | 27 | 28 | -------------------------------------------------------------------------------- /Html/Class 12th Solutions/functionMathsAll.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | 43 |

44 | 45 | 46 | -------------------------------------------------------------------------------- /Html/Class 12th Solutions/nNaturalNumber.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | 13 |

14 | 15 | -------------------------------------------------------------------------------- /Html/HelloWorld.html: -------------------------------------------------------------------------------- 1 |

Hello, World!

2 | -------------------------------------------------------------------------------- /Html/autoChangeColor.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | color change 9 | 18 | 19 | 20 | 21 |

Change Color

22 | 23 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Html/onClick.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | On Click 8 | 39 | 40 | 41 |
42 | 43 | 44 | 50 |
51 | 52 | 53 | -------------------------------------------------------------------------------- /Java/Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Calculator { 4 | public static void main(String[] args){ 5 | System.out.println("Enter your problem"); 6 | Scanner input = new Scanner(System.in); 7 | int num1 =input.nextInt(); 8 | String opt = input.next(); 9 | int num2 = input.nextInt(); 10 | switch (opt) { 11 | case "+" -> { 12 | int sum = num1 + num2; 13 | System.out.println("Sum of two numbers is :" + sum); 14 | } 15 | case "-" -> { 16 | int dif = num1 - num2; 17 | System.out.println("Difference is :" + dif); 18 | } 19 | case "*" -> { 20 | int prod = num1 * num2; 21 | System.out.println("The product is : " + prod); 22 | } 23 | case "/" -> { 24 | int div = num1 / num2; 25 | System.out.println("The division is :" + div); 26 | } 27 | default -> System.out.println("Cannot do anything use + - / or *"); 28 | } 29 | 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Java/HelloWorld.java: -------------------------------------------------------------------------------- 1 | class HelloWorld { 2 | public static void main(String[] args) { 3 | System.out.println("Hello, World!"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /Java/HelpdeskApplicationTest.Java: -------------------------------------------------------------------------------- 1 | package com.brianaubry.helpdesk; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class HelpdeskApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Java/IncomeTax.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner ; 2 | public class IncomeTax { 3 | public static void main(String[] args) 4 | { 5 | float Amount ; 6 | float tax = 0; 7 | System.out.println("Enter the income amount to get info of Income tax"); 8 | Scanner sc = new Scanner(System.in); 9 | Amount = sc.nextFloat(); 10 | if (Amount>2.5f && Amount<=5f) 11 | { 12 | tax = tax + 0.05f*(Amount-2.5f); 13 | } 14 | else if (Amount>5f && Amount<10.f) 15 | { 16 | tax = tax + 0.05f*(5.0f -2.5f); 17 | tax = tax + 0.2f*(Amount-5f); 18 | 19 | } 20 | else if(Amount>10.f) 21 | { 22 | tax = tax + 0.05f*(5.0f -2.5f); 23 | tax = tax + 0.2f*(10.0f - 5f); 24 | tax = tax + 0.3f*(Amount - 10.0f); 25 | } 26 | else 27 | { 28 | tax = 0 ; 29 | } 30 | 31 | System.out.println("Tax paid by employ " + tax ); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Java/LeapYear.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class LeapYear{ 4 | 5 | public static void main(String[] args) { 6 | 7 | System.out.println("Enter Year"); 8 | Scanner obj = new Scanner(System.in); 9 | int year = obj.nextInt(); 10 | boolean leap = false; 11 | 12 | if (year % 4 == 0) { 13 | 14 | if (year % 100 == 0) { 15 | 16 | if (year % 400 == 0) 17 | leap = true; 18 | else 19 | leap = false; 20 | 21 | } 22 | else 23 | leap = true; 24 | } 25 | else 26 | leap = false; 27 | 28 | if (leap) 29 | System.out.println(year + " is a leap year."); 30 | else 31 | System.out.println(year + " is not a leap year."); 32 | } 33 | } -------------------------------------------------------------------------------- /Java/PalindromeTest.java: -------------------------------------------------------------------------------- 1 | public class PalindromeTest { 2 | 3 | public static void main(String args[]) { 4 | System.out.println("Is aaa palindrom?: " + isPalindromString("aaa")); 5 | System.out.println("Is abc palindrom?: " + isPalindromString("abc")); 6 | System.out.println("Is bbbb palindrom?: " + isPalindromString("bbbb")); 7 | System.out.println("Is defg palindrom?: " + isPalindromString("defg")); 8 | } 9 | 10 | public static boolean isPalindromString(String text) { 11 | String reverse = reverse(text); 12 | if (text.equals(reverse)) { 13 | return true; 14 | } 15 | return false; 16 | } 17 | 18 | public static String reverse(String input) { 19 | if (input == null || input.isEmpty()) { 20 | return input; 21 | } 22 | return input.charAt(input.length() - 1) + reverse(input.substring(0, input.length() - 1)); 23 | } 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Java/PascalsTriangle.java: -------------------------------------------------------------------------------- 1 | public class PascalsTriangle { 2 | 3 | static int[] factorialArray = new int[10]; 4 | 5 | public static void main(String[] args) { 6 | initialization(); 7 | computeFactorial(); 8 | 9 | int numberOfLines = 4; 10 | 11 | for (int i = 0; i <= numberOfLines; i++) { 12 | for (int j = 0; j <= numberOfLines - i; j++) { 13 | System.out.print(" "); 14 | } 15 | 16 | for (int j = 0; j <= i; j++) 17 | System.out.print(" " + factorialArray[i] / (factorialArray[i - j] * factorialArray[j])); 18 | 19 | System.out.println(); 20 | } 21 | } 22 | 23 | private static void computeFactorial() { 24 | for (int i = 1; i < factorialArray.length; i++) 25 | factorialArray[i] = i * factorialArray[i - 1]; 26 | } 27 | 28 | private static void initialization() { 29 | factorialArray[0] = 1; 30 | factorialArray[1] = 1; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Java/Pythogorean_Triplets.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class PythogoreanTriplets { 3 | public static void main(String[] args) { 4 | Scanner in = new Scanner(System.in); 5 | 6 | int a = in.nextInt(); 7 | int b = in.nextInt(); 8 | int c = in.nextInt(); 9 | 10 | int max=a; 11 | if(b>a){ 12 | max=b; 13 | } 14 | if(c>a){ 15 | max=c; 16 | } 17 | 18 | if(max==a){ 19 | boolean check = (b*b+c*c)==(a*a); 20 | System.out.println(check); 21 | } 22 | else if(max==b){ 23 | boolean check = (a*a+c*c)==(b*b); 24 | System.out.println(check); 25 | } 26 | else{ 27 | boolean check = (b*b+a*a)==(c*c); 28 | System.out.println(check); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Java/RazorPay/MagicalBinaryString.java: -------------------------------------------------------------------------------- 1 | package RazorPay; 2 | 3 | /* RAZORPAY Q2 : 4 | Program to check below two Cases 5 | 1. Count of 0's and 1's should be equal 6 | 2. Count of 1's should be greater than or equals 0's is every substring 7 | 8 | Example : 9 | Input : 10 | 110010 11 | 12 | Output : 13 | true 14 | 15 | 16 | Explanation : 17 | Case 1 : count of 0's and 1's = 3 each ( 3 0's and 3 1's ), hence First condition True 18 | Case 2 : count of 1's in every substring is greater than or equals to count of 0's. 19 | i.e. 1 : Count of 1's is =1 count of 0's=0 20 | 11 : Count of 1's is =2 count of 0's=0 21 | 110: Count of 1's is =2 count of 0's=1 22 | 1100: Count of 1's is =2 count of 0's=2 23 | 11001: Count of 1's is =3 count of 0's=2 24 | 110010: Count of 1's is =3 count of 0's=3 25 | */ 26 | 27 | import java.util.Scanner; 28 | 29 | public class MagicalBinaryString 30 | { 31 | public static void main(String[] args) 32 | { 33 | Scanner input= new Scanner(System.in); 34 | String s=input.nextLine(); 35 | boolean magicalFirstCase,magicalSecondCase,magical=false; 36 | 37 | magicalFirstCase=s.chars().filter(c->c=='0').count()==s.chars().filter(c->c=='1').count(); 38 | magicalSecondCase=true; 39 | 40 | for(int i=1;ic=='0').count()>temp.chars().filter(c->c=='1').count()) 44 | { 45 | magicalSecondCase=false; 46 | break; 47 | } 48 | } 49 | 50 | System.out.println(magicalFirstCase&magicalSecondCase); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Java/RazorPay/PairMakesSum.java: -------------------------------------------------------------------------------- 1 | package RazorPay; 2 | 3 | /* RAZORPAY Q1 : 4 | Program to find the sum of Pair of numbers in reverse order of given array. 5 | Example : 6 | Input : 7 | Number of elements : 5 8 | Enter elements : 9 | 10 10 | 5 11 | 6 12 | 3 13 | 8 14 | Target sum : 11 15 | 16 | Output : 17 | true 18 | 3,8 19 | 20 | Explanation : given array of 5 elements we can consider the reverse array hence 8 and 3 will be the first pair which 21 | matches target 11 ( i.e. 8+3=11 ) so output will be true and a pair of numbers. 22 | */ 23 | 24 | import java.util.Scanner; 25 | 26 | public class PairMakesSum { 27 | static String result; 28 | public static void main(String[] args) 29 | { 30 | int size,x; 31 | int[] A; 32 | Scanner input=new Scanner(System.in); 33 | System.out.print("Number of elements : "); 34 | size=input.nextInt(); 35 | 36 | A=new int[size]; 37 | System.out.println("Enter elements :"); 38 | for(int i=0;i=0;i--) 60 | { 61 | for(int j=i+1;j=0;i--) 9 | { 10 | if (input.charAt(i) == ' ') 11 | { 12 | str.append(input.substring(i,prevPosition+1)); 13 | prevPosition=i-1; 14 | } 15 | } 16 | return (str.toString()).trim(); 17 | 18 | } 19 | public static void main(String[] args){ 20 | 21 | String str = "Welcome to Github"; 22 | String result = reverseWordWise(str); 23 | System.out.println(result); 24 | } 25 | } -------------------------------------------------------------------------------- /Java/add_two_numbers.java: -------------------------------------------------------------------------------- 1 | class Main { 2 | 3 | public static void main(String[] args) { 4 | 5 | System.out.println("Enter two numbers"); 6 | int first = 10; 7 | int second = 20; 8 | 9 | System.out.println(first + " " + second); 10 | 11 | // add two numbers 12 | int sum = first + second; 13 | System.out.println("The sum is: " + sum); 14 | } 15 | } -------------------------------------------------------------------------------- /Java/arrayList/Fruites.java: -------------------------------------------------------------------------------- 1 | package javaProjects; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Fruites { 6 | 7 | public static void main(String[] args) { 8 | 9 | ArrayList array = new ArrayList(); 10 | 11 | array.add("banana"); 12 | array.add("apple"); 13 | array.add("mango"); 14 | 15 | System.out.println(array); 16 | 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Java/exchangeValues/Main.java: -------------------------------------------------------------------------------- 1 | class Main{ 2 | public static void main(String[] args) { 3 | 4 | //Declaring the variables. 5 | 6 | int a; 7 | int b; 8 | 9 | //We need another variable to shift the values. 10 | int c; 11 | 12 | a = 2; 13 | b = 3; 14 | 15 | //Printing the assigned values. 16 | 17 | System.out.printf("The old value of a was %d.\n",a); 18 | System.out.printf("The old value of b was %d.\n",b); 19 | 20 | System. out. println(); //Printing a empty line. 21 | 22 | //Shifting the values 23 | c = b; //First, we will put the value of b in c (Now the b will be empty). 24 | b = a; //Second, we will put the value of a in b (Now the a will be empty). 25 | a = c; //At beginning we kept the value of b in c. 26 | 27 | /* Hence, the values of both a and b are successfully exchanged. 28 | Lets,print the new values. */ 29 | 30 | System.out.printf("The new value of a is %d.\n",a); 31 | System.out.printf("The new value of b is %d.",b); 32 | } 33 | } -------------------------------------------------------------------------------- /Java/fibonacci.java: -------------------------------------------------------------------------------- 1 | class FibonacciExample1{ 2 | public static void main(String args[]) 3 | { 4 | int n1=0,n2=1,n3,i,count=10; 5 | System.out.print(n1+" "+n2); 6 | 7 | for(i=2;igen) 21 | { 22 | System.out.printf("Please try again Number %d > ? \n",number); 23 | number = scan.nextInt(); 24 | } 25 | 26 | else 27 | { 28 | 29 | System.out.printf("Please try again Number %d < ? \n",number); 30 | 31 | number = scan.nextInt(); 32 | 33 | 34 | } 35 | 36 | }while ( number!=gen); 37 | 38 | 39 | System.out.println("Great you won " + " number is " + gen ); 40 | 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Java/intersection_point_of_two_Linked_Lists.java: -------------------------------------------------------------------------------- 1 | // Java Program to get intersection point of two linked 2 | // lists. 3 | 4 | class GFG { 5 | 6 | static class Node { 7 | int data; 8 | Node next; 9 | Node(int d) 10 | { 11 | data = d; 12 | next = null; 13 | } 14 | } 15 | 16 | /* function to get the intersection point of two linked 17 | lists head1 and head2 */ 18 | public Node getIntersectionNode(Node head1, Node head2) 19 | { 20 | while (head2 != null) { 21 | Node temp = head1; 22 | while (temp != null) { 23 | // if both Nodes are same 24 | if (temp == head2) { 25 | return head2; 26 | } 27 | temp = temp.next; 28 | } 29 | head2 = head2.next; 30 | } 31 | // If intersection is not present between the lists, 32 | // return NULL. 33 | return null; 34 | } 35 | 36 | public static void main(String[] args) 37 | { 38 | GFG list = new GFG(); 39 | 40 | Node head1, head2; 41 | 42 | /* 43 | Create two linked lists 44 | 45 | 1st 3->6->9->15->30 46 | 2nd 10->15->30 47 | 48 | 15 is the intersection point 49 | */ 50 | 51 | head1 = new Node(10); 52 | head2 = new Node(3); 53 | 54 | Node newNode = new Node(6); 55 | head2.next = newNode; 56 | 57 | newNode = new Node(9); 58 | head2.next.next = newNode; 59 | 60 | newNode = new Node(15); 61 | head1.next = newNode; 62 | head2.next.next.next = newNode; 63 | 64 | newNode = new Node(30); 65 | head1.next.next = newNode; 66 | 67 | head1.next.next.next = null; 68 | 69 | Node intersectionPoint 70 | = list.getIntersectionNode(head1, head2); 71 | 72 | if (intersectionPoint == null) { 73 | System.out.print(" No Intersection Point \n"); 74 | } 75 | else { 76 | System.out.print("Intersection Point: " 77 | + intersectionPoint.data); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Java/median_of_two_sorted_array.java: -------------------------------------------------------------------------------- 1 | // Java program for the above approach 2 | import java.io.*; 3 | import java.util.Arrays; 4 | 5 | public class GFG { 6 | public static int Solution(int[] arr) 7 | { 8 | int n = arr.length; 9 | 10 | // If length of array is even 11 | if (n % 2 == 0) 12 | { 13 | int z = n / 2; 14 | int e = arr[z]; 15 | int q = arr[z - 1]; 16 | 17 | int ans = (e + q) / 2; 18 | return ans; 19 | } 20 | 21 | // If length if array is odd 22 | else 23 | { 24 | int z = Math.round(n / 2); 25 | return arr[z]; 26 | } 27 | } 28 | 29 | // Driver Code 30 | public static void main(String[] args) 31 | { 32 | 33 | // TODO Auto-generated method stub 34 | int[] arr1 = { -5, 3, 6, 12, 15 }; 35 | int[] arr2 = { -12, -10, -6, -3, 4, 10 }; 36 | 37 | int i = arr1.length; 38 | int j = arr2.length; 39 | 40 | int[] arr3 = new int[i + j]; 41 | 42 | // Merge two array into one array 43 | System.arraycopy(arr1, 0, arr3, 0, i); 44 | System.arraycopy(arr2, 0, arr3, i, j); 45 | 46 | // Sort the merged array 47 | Arrays.sort(arr3); 48 | 49 | // calling the method 50 | System.out.print("Median = " + Solution(arr3)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Java/merge_sort.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class MergeSort { 4 | void merge(int arr[], int l, int m, int r) 5 | { 6 | // Find sizes of two subarrays to be merged 7 | int n1 = m - l + 1; 8 | int n2 = r - m; 9 | 10 | int L[] = new int[n1]; 11 | int R[] = new int[n2]; 12 | 13 | for (int i = 0; i < n1; ++i) 14 | L[i] = arr[l + i]; 15 | for (int j = 0; j < n2; ++j) 16 | R[j] = arr[m + 1 + j]; 17 | 18 | int i = 0, j = 0; 19 | 20 | int k = l; 21 | while (i < n1 && j < n2) { 22 | if (L[i] <= R[j]) { 23 | arr[k] = L[i]; 24 | i++; 25 | } 26 | else { 27 | arr[k] = R[j]; 28 | j++; 29 | } 30 | k++; 31 | } 32 | 33 | while (i < n1) { 34 | arr[k] = L[i]; 35 | i++; 36 | k++; 37 | } 38 | while (j < n2) { 39 | arr[k] = R[j]; 40 | j++; 41 | k++; 42 | } 43 | } 44 | 45 | void sort(int arr[], int l, int r) 46 | { 47 | if (l < r) { 48 | int m = l + (r - l) / 2; 49 | 50 | sort(arr, l, m); 51 | sort(arr, m + 1, r); 52 | 53 | merge(arr, l, m, r); 54 | } 55 | } 56 | 57 | static void printArray(int arr[]) 58 | { 59 | int n = arr.length; 60 | for (int i = 0; i < n; ++i) 61 | System.out.print(arr[i] + " "); 62 | System.out.println(); 63 | } 64 | 65 | public static void main(String args[]) 66 | { 67 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 68 | 69 | System.out.println("Given Array"); 70 | printArray(arr); 71 | 72 | MergeSort ob = new MergeSort(); 73 | ob.sort(arr, 0, arr.length - 1); 74 | 75 | System.out.println("\nSorted array"); 76 | printArray(arr); 77 | } 78 | } 79 | 80 | -------------------------------------------------------------------------------- /Java/palindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | Scanner s=new Scanner(System.in); 6 | int n=s.nextInt(); 7 | int x=palindrom(n); //getting the returned valued and storing it in x 8 | if(x==1){ // the value which is returned is only present in 0 or 1 9 | System.out.println("it is palindrom"); // x is 1 then it is palindrome 10 | } 11 | else{ 12 | System.out.println("it is not palindrom"); //else it is not palindrome 13 | } 14 | } 15 | static int palindrom(int n){ 16 | int r=n; // stroing the original n value into r to check after 17 | int t=0; //reversing the integer 18 | while(n>0){ 19 | t=t*10+n%10; 20 | n=n/10; 21 | } 22 | 23 | if(r==t){ 24 | return 1; 25 | } 26 | else 27 | return 0; 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Java/printLarge.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.*; 3 | public class printLarge { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner sc=new Scanner(System.in); 8 | System.out.println("Enter 2 very big nos."); 9 | String a=sc.next(); 10 | String b=sc.next(); 11 | BigInteger A,B; 12 | A=new BigInteger(a); 13 | B=new BigInteger(b); 14 | BigInteger add=A.add(B); 15 | // BigInteger prod=A.pow(0) 16 | System.out.println(add); 17 | 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Java/simpleInterest/Si.java: -------------------------------------------------------------------------------- 1 | package javaProjects; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Si { 6 | 7 | public static void main(String [] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | 10 | System.out.print("Enter the principal:$"); 11 | int principal = scanner.nextInt(); 12 | 13 | System.out.print("Enter the time (in year):"); 14 | int time = scanner.nextInt(); 15 | 16 | System.out.print("Enter the rate:"); 17 | int rate = scanner.nextInt(); 18 | 19 | int interest = (principal * time * rate) / 100; 20 | 21 | System.out.printf("Simple interest:%d per annum",interest); 22 | 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Java/spy number in java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class SpyNumberExample1 3 | { 4 | public static void main(String args[]) 5 | { 6 | int num, product=1, sum=0, lastdigit; 7 | // create object of scanner 8 | Scanner sc = new Scanner(System.in); 9 | System.out.print("Enter the number to check: " ); 10 | //reads an integer from the user and stores it in the variable num 11 | num=sc.nextInt(); 12 | //executes untill the condition becomes false 13 | while(num>0) 14 | { 15 | //finds the last digit of the number 16 | lastdigit=num%10; 17 | //adds last digit to the variable sum 18 | sum=sum+lastdigit; 19 | //calculates the product 20 | product=product*lastdigit; 21 | //removes the last digit from the given number 22 | num=num/10; 23 | } 24 | //compares the sum and product 25 | if(sum==product) 26 | //prints if the above condition returns true 27 | System.out.println("The given number is a spy number."); 28 | else 29 | //prints if the above condition returns false 30 | System.out.println("The given number is not a spy number."); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Java/switchCase/Main.java: -------------------------------------------------------------------------------- 1 | package javaProjects; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | 10 | System.out.print("Enter m for male , f for female & o for other:"); //Taking input from user 11 | 12 | String gender = scanner.nextLine(); 13 | 14 | switch(gender){ 15 | 16 | 17 | case "m": 18 | System.out.println("You are a male."); 19 | break; 20 | 21 | case "f": 22 | System.out.println("You are a female."); 23 | break; 24 | 25 | case "o": 26 | System.out.println("You are other."); 27 | break; 28 | 29 | default: 30 | System.out.println("Enter the gender correctly!"); 31 | 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Javascript/Calculator/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | WEB Calculator 8 | 9 | 10 | 11 |
12 |
13 |

0

14 |
15 |
16 |
17 |
7
18 |
8
19 |
9
20 |
+/-
21 |
22 |
23 |
4
24 |
5
25 |
6
26 |
-
27 |
28 |
29 |
1
30 |
2
31 |
3
32 |
+
33 |
34 |
35 |
CE
36 |
0
37 |
=
38 |
39 |
40 | 41 |
42 |
43 | Riwayat Perhitungan 44 |
45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 |
Angka PertamaOperatorAgka KeduaHasil
53 |
54 |
55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Javascript/Calculator/storage.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Session Storage 8 | 9 | 10 |

Anda sudah menekan tombol sebanyak 0 kali

11 | 12 | 13 | 14 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Javascript/Calculator/storage.js: -------------------------------------------------------------------------------- 1 | const cacheKey = "calculation_history"; 2 | 3 | function checkStorage(){ 4 | if(typeof(Storage) != "undefined"){ 5 | return true 6 | }else{ 7 | return false; 8 | } 9 | } 10 | 11 | function putHistory(data){ 12 | if(checkStorage){ 13 | let historyData = null; 14 | if(localStorage.getItem(cacheKey) == null){ 15 | historyData = []; 16 | }else{ 17 | historyData = JSON.parse(localStorage.getItem(cacheKey)); 18 | } 19 | 20 | historyData.unshift(data); 21 | if(historyData.length > 5){ 22 | historyData.pop(); 23 | } 24 | localStorage.setItem(cacheKey, JSON.stringify(historyData)); 25 | } 26 | } 27 | 28 | function showHistory(){ 29 | if(checkStorage){ 30 | return JSON.parse(localStorage.getItem(cacheKey)) || []; 31 | } 32 | else{ 33 | return []; 34 | } 35 | } 36 | 37 | function renderHistory(){ 38 | const historyData = showHistory(); 39 | let historyList = document.querySelector('#historyList'); 40 | historyList.innerHTML = ""; 41 | 42 | for (let history of historyData){ 43 | let row = document.createElement('tr'); 44 | row.innerHTML = "" + history.firstNumber + ""; 45 | row.innerHTML += "" + history.operator + ""; 46 | row.innerHTML += "" + history.secondNumber + ""; 47 | row.innerHTML += "" + history.result + ""; 48 | 49 | historyList.appendChild(row) 50 | } 51 | } 52 | 53 | renderHistory(); -------------------------------------------------------------------------------- /Javascript/Calculator/style.css: -------------------------------------------------------------------------------- 1 | * { 2 | font-family: Arial, Helvetica, sans-serif; 3 | } 4 | 5 | .container { 6 | height: 500px; 7 | width: 500px; 8 | margin: 0 auto; 9 | margin-top: 30px; 10 | 11 | display: flex; 12 | flex-direction: column; 13 | } 14 | 15 | .bg-yellow { 16 | background-color: yellow; 17 | } 18 | 19 | .result { 20 | text-align: right; 21 | font-size: 30px; 22 | color: white; 23 | border-left: 3px solid black; 24 | border-top: 3px solid black; 25 | border-right: 3px solid black; 26 | margin-top: -9px; 27 | background-color: grey; 28 | } 29 | 30 | #number { 31 | margin-right: 20px; 32 | } 33 | 34 | .flex-column { 35 | display: flex; 36 | flex-direction: column; 37 | border: 1px solid black; 38 | height: 100%; 39 | width: 100%; 40 | background-color: rgb(185, 168, 168); 41 | } 42 | 43 | .flex-row { 44 | display: flex; 45 | flex-direction: row; 46 | 47 | height: 30%; 48 | border: 1px solid black; 49 | } 50 | 51 | .box { 52 | display: flex; 53 | flex-basis: 25%; 54 | border: 1px solid black; 55 | font-size: 30px; 56 | justify-content: center; 57 | align-items: center; 58 | } 59 | 60 | .box:hover { 61 | background-color: white; 62 | } 63 | 64 | .double { 65 | flex-basis: 50%; 66 | } 67 | 68 | .card { 69 | box-sizing: border-box; 70 | width: 100%; 71 | height: 500px; 72 | border: 3px solid black; 73 | border-radius: 10px; 74 | margin-top: 20px; 75 | } 76 | 77 | .title{ 78 | font-size: 20px; 79 | margin: 15px; 80 | } 81 | 82 | table { 83 | border: 1px solid black; 84 | width: 95%; 85 | margin-left: 10px; 86 | margin-right: 10px; 87 | margin-bottom: 10px; 88 | } 89 | 90 | th, td{ 91 | border: none; 92 | padding: 10px; 93 | color: black; 94 | text-align: center; 95 | } 96 | th{ 97 | background-color: yellow; 98 | } 99 | tr:nth-child(even){ 100 | background-color: rgb(138, 149, 149); 101 | } 102 | 103 | /* @media screen and (max-width: 513px) { 104 | } */ 105 | -------------------------------------------------------------------------------- /Javascript/Funny Car project/background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/Funny Car project/background.jpg -------------------------------------------------------------------------------- /Javascript/Funny Car project/car_body.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/Funny Car project/car_body.png -------------------------------------------------------------------------------- /Javascript/Funny Car project/car_wheel_left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/Funny Car project/car_wheel_left.png -------------------------------------------------------------------------------- /Javascript/Funny Car project/car_wheel_right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/Funny Car project/car_wheel_right.png -------------------------------------------------------------------------------- /Javascript/Funny Car project/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Moving Car Animation 8 | 9 | 10 | 11 | 12 |
13 |
14 |
15 |
16 |
17 |
18 | 19 |
20 |
21 | 22 |
23 |
24 |
25 |
26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Javascript/Funny Car project/index.js: -------------------------------------------------------------------------------- 1 | var audio = document.createElement('audio') ; 2 | 3 | audio.setAttribute('src' , 'sound.mp3') ; 4 | audio.loop = true ; 5 | 6 | audio.play() ; -------------------------------------------------------------------------------- /Javascript/Funny Car project/sound.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/Funny Car project/sound.mp3 -------------------------------------------------------------------------------- /Javascript/Funny Car project/style.css: -------------------------------------------------------------------------------- 1 | 2 | *{ 3 | margin: 0; 4 | padding: 0; 5 | } 6 | 7 | body 8 | { 9 | overflow: hidden; 10 | } 11 | 12 | .sky 13 | { 14 | height: 100vh; 15 | width: 100vw; 16 | background-image: url(background.jpg); 17 | background-repeat: no-repeat ; 18 | position: absolute; 19 | } 20 | 21 | .trees 22 | { 23 | height: 100vh; 24 | width: 100%; 25 | background-image: url(trees.png); 26 | background-size: cover; 27 | position: absolute; 28 | top: -134px; 29 | } 30 | 31 | .track 32 | { 33 | height: 60vh; 34 | width: 800vw; 35 | background-image: url(track.png); 36 | /* background-size: cover; */ 37 | background-repeat: repeat-x; 38 | position: absolute; 39 | top: 70vh; 40 | animation: carMove 10s linear infinite; 41 | 42 | } 43 | 44 | .car{ 45 | height: 100px; 46 | width: 380px; 47 | background-image: url(car_body.png); 48 | background-repeat: no-repeat; 49 | background-size: cover; 50 | position: absolute; 51 | left: 332px; 52 | bottom: 30vh; 53 | animation: move linear 0.7s infinite; 54 | } 55 | 56 | .wheel img 57 | { 58 | width: 77px; 59 | position: relative; 60 | animation: wheelRotation linear 0.15s infinite ; 61 | } 62 | 63 | .wheel1 img{ 64 | top: 41px; 65 | left: 42px; 66 | } 67 | 68 | 69 | 70 | .wheel2 img{ 71 | top: -52px; 72 | left: 235px; 73 | } 74 | 75 | @keyframes wheelRotation 76 | { 77 | 100%{ 78 | transform: rotate(360deg); 79 | } 80 | } 81 | 82 | @keyframes carMove { 83 | 100% 84 | { 85 | transform:translateX(-500vw) ; 86 | } 87 | 88 | } 89 | 90 | @keyframes move { 91 | 0%{ 92 | transform: translateY(-6px); 93 | 94 | } 95 | 50% 96 | { 97 | transform: translateY(0px); 98 | } 99 | 100%{ 100 | transform: translateY(-6px); 101 | } 102 | 103 | } -------------------------------------------------------------------------------- /Javascript/Funny Car project/track.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/Funny Car project/track.png -------------------------------------------------------------------------------- /Javascript/Funny Car project/trees.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/Funny Car project/trees.png -------------------------------------------------------------------------------- /Javascript/HelloWorld.js: -------------------------------------------------------------------------------- 1 | console.log('Hello World'); 2 | -------------------------------------------------------------------------------- /Javascript/Hollow Square Pattern.js: -------------------------------------------------------------------------------- 1 | let n = 5; // row or column count 2 | // defining an empty string 3 | let string = ""; 4 | 5 | for(let i = 0; i < n; i++) { // external loop 6 | for(let j = 0; j < n; j++) { // internal loop 7 | if(i === 0 || i === n - 1) { 8 | string += "*"; 9 | } 10 | else { 11 | if(j === 0 || j === n - 1) { 12 | string += "*"; 13 | } 14 | else { 15 | string += " "; 16 | } 17 | } 18 | } 19 | // newline after each row 20 | string += "\n"; 21 | } 22 | // printing the string 23 | console.log(string); 24 | -------------------------------------------------------------------------------- /Javascript/addTwoNumber.js: -------------------------------------------------------------------------------- 1 | let firstNumber = 0.1 2 | let secondNumber = 0.2 3 | 4 | let result = firstNumber + secondNumber 5 | console.log(result) 6 | -------------------------------------------------------------------------------- /Javascript/ant smasher/ant.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/ant smasher/ant.png -------------------------------------------------------------------------------- /Javascript/ant smasher/css/ant.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/ant smasher/css/ant.png -------------------------------------------------------------------------------- /Javascript/ant smasher/css/reset.css: -------------------------------------------------------------------------------- 1 | *{ 2 | margin:0px; 3 | padding:0px; 4 | box-sizing: border-box; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /Javascript/ant smasher/css/style.css: -------------------------------------------------------------------------------- 1 | .container{ 2 | display:flex; 3 | width:100%; 4 | height:100%; 5 | overflow:hidden; 6 | align-items:center; 7 | justify-content: center; 8 | border:2px solid red; 9 | } 10 | -------------------------------------------------------------------------------- /Javascript/ant smasher/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Ant Smasher 8 | 9 | 10 | 11 | 12 |
13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Javascript/ant smasher/js/index.js: -------------------------------------------------------------------------------- 1 | var playarea=document.getElementById("playarea") 2 | var ctx = playarea.getContext("2d"); 3 | var ants=[]; 4 | var antCount=10 ; 5 | var offset=100; 6 | var img=new Image(); 7 | img.src="css/ant.png"; 8 | var antArea=70 9 | 10 | function resize(){ 11 | window.WIDTH=window.innerWidth; 12 | window.HEIGHT=window.innerHeight; 13 | ctx.canvas.width=window.WIDTH; 14 | ctx.canvas.height=window.HEIGHT; 15 | } 16 | function update(){ 17 | ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height); 18 | ants.forEach(function (ant){ 19 | ant.collisionUpdate(ants); 20 | ctx.save(); 21 | ctx.translate(ant.pos.x,ant.pos.y); 22 | ctx.drawImage( img,0,0); 23 | ctx.restore(); 24 | }) 25 | requestAnimationFrame(update) 26 | } 27 | 28 | resize(); 29 | 30 | for(var i=0; i { return a + b }; 3 | // calling function 4 | console.log(add(5, 15)); 5 | -------------------------------------------------------------------------------- /Javascript/color-flipper/app.js: -------------------------------------------------------------------------------- 1 | const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"]; 2 | const color = document.querySelector(".color"); 3 | 4 | document.addEventListener("DOMContentLoaded", function () { 5 | setInterval(getColor, 1500); 6 | }); 7 | 8 | function getRandomNumber() { 9 | return Math.floor(Math.random() * hex.length); 10 | } 11 | 12 | function getColor() { 13 | let hexColor = "#"; 14 | for (let i = 0; i < 6; i++) { 15 | hexColor += hex[getRandomNumber()]; 16 | } 17 | 18 | color.textContent = hexColor; 19 | document.body.style.backgroundColor = hexColor; 20 | } -------------------------------------------------------------------------------- /Javascript/color-flipper/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Color Flipper 7 | 8 | 9 | 10 | 11 | 12 | 13 | 17 |
18 |
19 |

Background color : #f1f5f8

20 |
21 |
22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Javascript/color-flipper/styles.css: -------------------------------------------------------------------------------- 1 | @import url("https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,700&display=swap"); 2 | 3 | *, 4 | ::after, 5 | ::before { 6 | margin: 0; 7 | padding: 0; 8 | box-sizing: border-box; 9 | } 10 | body { 11 | font-family: "Open Sans", sans-serif; 12 | background: hsl(205, 100%, 96%); 13 | color: hsl(209, 61%, 16%); 14 | line-height: 1.5; 15 | font-size: 0.875rem; 16 | } 17 | ul { 18 | list-style-type: none; 19 | } 20 | a { 21 | text-decoration: none; 22 | } 23 | 24 | h2, 25 | h4 { 26 | letter-spacing: 0.1rem; 27 | text-transform: capitalize; 28 | line-height: 1.25; 29 | margin-bottom: 0.75rem; 30 | font-family: "Roboto", sans-serif; 31 | } 32 | 33 | h2 { 34 | font-size: 2rem; 35 | } 36 | 37 | h4 { 38 | font-size: 0.875rem; 39 | } 40 | 41 | main { 42 | min-height: 100vh; 43 | display: grid; 44 | place-items: center; 45 | } 46 | 47 | nav { 48 | background: #fff; 49 | height: 3rem; 50 | display: grid; 51 | align-items: center; 52 | box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2); 53 | } 54 | .nav-center { 55 | margin: 20px auto; 56 | display: flex; 57 | align-items: flex-start; 58 | justify-content: space-between; 59 | } 60 | .nav-center h4 { 61 | margin-bottom: 0; 62 | color: black; 63 | } 64 | 65 | main { 66 | min-height: calc(100vh - 3rem); 67 | display: grid; 68 | place-items: center; 69 | } 70 | .container { 71 | text-align: center; 72 | } 73 | .container h2 { 74 | background: #222; 75 | color: #fff; 76 | padding: 1rem; 77 | border-radius: 0.25rem; 78 | margin-bottom: 2.5rem; 79 | } 80 | .color { 81 | color: hsl(210, 22%, 49%); 82 | } -------------------------------------------------------------------------------- /Javascript/fastify/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /Javascript/fastify/index.js: -------------------------------------------------------------------------------- 1 | const fastify = require("fastify")({ logger: true }); 2 | const { customAlphabet } = require("nanoid"); 3 | const alphabets = 4 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 5 | const nanoid = customAlphabet(alphabets, 16); 6 | 7 | fastify.route({ 8 | method: "GET", 9 | url: "/", 10 | schema: { 11 | querystring: { 12 | size: { type: ["string", "null"], nullable: true,}, 13 | }, 14 | response: { 15 | 200: { 16 | type: "object", 17 | properties: { 18 | password: { type: "string" }, 19 | success: { type: "boolean" }, 20 | createdAt: { type: "number" }, 21 | }, 22 | }, 23 | }, 24 | }, 25 | handler: async (request, reply) => { 26 | return { 27 | password: nanoid(request.size), 28 | createdAt: +new Date(), 29 | success: true, 30 | }; 31 | }, 32 | }); 33 | 34 | const start = async () => { 35 | try { 36 | await fastify.listen({ port: 3000 }); 37 | } catch (err) { 38 | fastify.log.error(err); 39 | process.exit(1); 40 | } 41 | }; 42 | start(); 43 | 44 | module.exports = fastify; -------------------------------------------------------------------------------- /Javascript/fastify/index.test.js: -------------------------------------------------------------------------------- 1 | 2 | const { test } = require('tap') 3 | const build = require('./index') 4 | 5 | test('Run test on generate password route: ', async t => { 6 | const response = await build.inject({ 7 | method: 'GET', 8 | url: '/' 9 | }) 10 | t.equal(response.statusCode, 200, 'returns a status code of 200'); 11 | t.equal(JSON.parse(response.body).success, true, "returned success true"); 12 | }) -------------------------------------------------------------------------------- /Javascript/fastify/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fastify", 3 | "version": "1.0.0", 4 | "description": "A fastify API to generate random password with random params.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Shashank", 10 | "license": "ISC", 11 | "dependencies": { 12 | "fastify": "^4.7.0", 13 | "nanoid": "3.0.0", 14 | "tap": "^16.3.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Javascript/function.js: -------------------------------------------------------------------------------- 1 | function ReversedPyramidStarPattern(n){ 2 | let string = ""; 3 | // External loop 4 | for (let i = 0; i < n; i++) { 5 | // printing spaces 6 | for (let j = 0; j < i; j++) { 7 | string += " "; 8 | } 9 | // printing star 10 | for (let k = 0; k < 2 * (n-i) - 1; k++) { 11 | string += "*"; 12 | } 13 | string += "\n"; 14 | } 15 | console.log(string); 16 | } 17 | ReversedPyramidStarPattern(7); 18 | -------------------------------------------------------------------------------- /Javascript/palindrome.js: -------------------------------------------------------------------------------- 1 | // function to check palindrome 2 | function checkPalindrome(str) { 3 | 4 | // applying split, revere and join 5 | const reversedString = str.split('').reverse().join(''); 6 | if(str === reversedString) 7 | return true; 8 | else 9 | return false; 10 | } 11 | -------------------------------------------------------------------------------- /Javascript/pyramidstartpattern.js: -------------------------------------------------------------------------------- 1 | let n = 5; 2 | let string = ""; 3 | // External loop 4 | for (let i = 1; i <= n; i++) { 5 | // printing spaces 6 | for (let j = 1; j <= n - i; j++) { 7 | string += " "; 8 | } 9 | // printing star 10 | for (let k = 0; k < 2 * i - 1; k++) { 11 | string += "*"; 12 | } 13 | string += "\n"; 14 | } 15 | console.log(string); 16 | -------------------------------------------------------------------------------- /Javascript/snake game project/CSS/style.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css2?family=New+Tegomin&display=swap'); 2 | 3 | 4 | *{ 5 | padding: 0; 6 | margin: 0; 7 | box-sizing: border-box; 8 | } 9 | 10 | .container{ 11 | background: url("../main_data/img/bg.jpg"); 12 | min-height: 100vh; 13 | background-size: 100vw 100vh; 14 | /* background-position: center; */ 15 | display: flex; 16 | align-items: center; 17 | justify-content: center; 18 | } 19 | 20 | #scoreBox { 21 | position: absolute; 22 | top: 9px; 23 | right: 200px; 24 | font-size: 39px; 25 | font-weight: bold; 26 | font-family: 'New Tegomin', serif; 27 | } 28 | 29 | #hiscoreBox{ 30 | position: absolute; 31 | top: 59px; 32 | right: 140px; 33 | font-size: 39px; 34 | font-weight: bold; 35 | font-family: 'New Tegomin', serif; 36 | } 37 | 38 | #board{ 39 | display: grid; 40 | grid-template-rows: repeat(18 ,1fr); 41 | grid-template-columns: repeat(18 ,1fr); 42 | background: linear-gradient(rgb(111, 236, 111) , rgb(226, 226, 119)); 43 | height: 92vmin; 44 | width: 90vmin; 45 | border: 2px solid black; 46 | } 47 | 48 | .head{ 49 | background: linear-gradient(rgb(240, 124, 124), rgb(228, 228, 129)); 50 | border: 2px solid rgb(34, 4, 34); 51 | transform: scale(1.02); 52 | border-radius: 9px; 53 | } 54 | 55 | .snake{ 56 | background-color: purple; 57 | border: .25vmin solid white; 58 | border-radius: 12px; 59 | } 60 | 61 | .food{ 62 | background: linear-gradient(red, purple); 63 | border: .25vmin solid black; 64 | border-radius: 8px; 65 | } -------------------------------------------------------------------------------- /Javascript/snake game project/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Snake Game 8 | 9 | 10 | 11 | 12 |
13 |
Score : 0
14 |
HiScore: 0
15 |
16 |
17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/SnakeGame/SnakeGame.zip -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame/Resources.txt: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/38709923/why-is-requestanimationframe-better-than-setinterval-or-settimeout -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame/css/style.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css2?family=New+Tegomin&display=swap'); 2 | *{ 3 | padding: 0; 4 | margin: 0; 5 | } 6 | 7 | .body{ 8 | background: url("../img/bg.jpg"); 9 | min-height: 100vh; 10 | background-size: 100vw 100vh; 11 | background-repeat: no-repeat; 12 | display: flex; 13 | justify-content: center; 14 | align-items: center; 15 | } 16 | 17 | #scoreBox{ 18 | position: absolute; 19 | top: 9px; 20 | right: 200px; 21 | font-size: 39px; 22 | font-weight: bold; 23 | font-family: 'New Tegomin', serif; 24 | } 25 | 26 | #hiscoreBox{ 27 | position: absolute; 28 | top: 59px; 29 | right: 140px; 30 | font-size: 39px; 31 | font-weight: bold; 32 | font-family: 'New Tegomin', serif; 33 | } 34 | 35 | #board{ 36 | background: linear-gradient(rgb(170, 236, 170), rgb(236, 236, 167)); 37 | width: 90vmin; 38 | height: 92vmin; 39 | border: 2px solid black; 40 | display: grid; 41 | grid-template-rows: repeat(18, 1fr); 42 | grid-template-columns: repeat(18, 1fr); 43 | } 44 | 45 | .head{ 46 | background: linear-gradient(rgb(240, 124, 124), rgb(228, 228, 129)); 47 | border: 2px solid rgb(34, 4, 34); 48 | transform: scale(1.02); 49 | border-radius: 9px; 50 | } 51 | 52 | .snake{ 53 | background-color: purple; 54 | border: .25vmin solid white; 55 | border-radius: 12px; 56 | } 57 | 58 | .food{ 59 | background: linear-gradient(red, purple); 60 | border: .25vmin solid black; 61 | border-radius: 8px; 62 | } -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame/img/bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/SnakeGame/SnakeGame/img/bg.jpg -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | SnakeMania - Ek Gaming Katha 8 | 9 | 10 | 11 |
12 |
Score: 0
13 |
HiScore: 0
14 |
15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame/music/food.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/SnakeGame/SnakeGame/music/food.mp3 -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame/music/gameover.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/SnakeGame/SnakeGame/music/gameover.mp3 -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame/music/move.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/SnakeGame/SnakeGame/music/move.mp3 -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/SnakeGame/SnakeGame/music/music.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/SnakeGame/SnakeGame/music/music.mp3 -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/img/bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/img/bg.jpg -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/music/food.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/music/food.mp3 -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/music/gameover.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/music/gameover.mp3 -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/music/move.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/music/move.mp3 -------------------------------------------------------------------------------- /Javascript/snake game project/main_data/music/music.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diwas7777/CodesHub/cdfe7bc68ce8941516bd720d1874b204f3d5d067/Javascript/snake game project/main_data/music/music.mp3 -------------------------------------------------------------------------------- /PHP/forLoop.php: -------------------------------------------------------------------------------- 1 | "; 5 | } 6 | ?> 7 | -------------------------------------------------------------------------------- /PHP/helloworld.php: -------------------------------------------------------------------------------- 1 | 4 | -------------------------------------------------------------------------------- /PHP/koneksi.php: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /PHP/triangle.php: -------------------------------------------------------------------------------- 1 | "; 7 | } 8 | ?> 9 | 10 | -------------------------------------------------------------------------------- /Python/CombinationSumII.py: -------------------------------------------------------------------------------- 1 | # Problem Statement: 2 | # Given a collection/list of conadidate numbers(candidates) and a target number(target) find all unique 3 | # combinations in candidates where the sum of candidate numbers in equal to target. 4 | # Each number from the candidates may be used only once in one combination. 5 | 6 | # Sample Input: 7 | # candidates = [2,5,2,1,2], target = 5 8 | 9 | # Sample Output: 10 | # [ [1,2,2],[ 5 ] ] 11 | ] 12 | # In the solution I have taken a sample input. You can choose any input. 13 | class Solution: 14 | def combinationSum2(self, candidates, target): 15 | candidates.sort() 16 | res=set() 17 | self.findcombination(candidates,target,[],res) 18 | return [list(i) for i in res] 19 | 20 | def findcombination(self,candidates,target,ls,res): 21 | if target==0: 22 | res.add(tuple(ls)) 23 | return 24 | if target<0: 25 | return 26 | for i in range(len(candidates)): 27 | if target Prepare > Python > Itertools > Compress the String! 3 | # url : https://www.hackerrank.com/challenges/compress-the-string/problem?isFullScreen=true 4 | 5 | List = list(map(int, input("Enter the string you want to compress : "))) 6 | Listr = [] 7 | for i in range(len(List)): 8 | count = 1 9 | if i == 0: 10 | for j in range(i, len(List)-1): 11 | if List[j] == List[j+1]: 12 | count += 1 13 | else: 14 | break 15 | Listr.append(str((count, List[i]))) 16 | continue 17 | if List[i] == List[i-1]: 18 | continue 19 | else: 20 | count = 1 21 | for j in range(i, len(List)-1): 22 | if List[j] == List[j+1]: 23 | count += 1 24 | else: 25 | break 26 | Listr.append(str((count, List[i]))) 27 | 28 | print("The compressed string is : ", end="") 29 | print(*Listr) 30 | -------------------------------------------------------------------------------- /Python/Count_Sort.py: -------------------------------------------------------------------------------- 1 | # Counting sort algorithm can be used to sort non-negative integers between a specific range. This post covers a variation of counting sort that can work on negative numbers and can sort numbers in any range. But this version doesn’t produce a stable sort, i.e., the relative order of items with equal keys is not preserved anymore and runs in O(n.log(n)) time, where n is the total number of elements in the input collection. 2 | # The idea remains the same – iterate over the collection and construct a map storing information about the total number of times each key occurs within the input collection. Then loop over the collection again and then use those counts to determine each key value’s final positions in the collection. This takes advantage of the fact that a map is sorted according to the keys’ natural order (ascending order). 3 | # Following is the Python implementation of count sort that takes O(n) space: 4 | 5 | def countSort(nums): 6 | 7 | # create a dictionary to store the frequency of list elements 8 | freq = {} 9 | 10 | # store distinct values in the input list as keys and 11 | # their respective counts as values in the dictionary 12 | for i in nums: 13 | freq[i] = freq.setdefault(i, 0) + 1 14 | 15 | # traverse the dictionary (based on the sorted order of keys) and 16 | # overwrite the input list with sorted elements 17 | i = 0 18 | for key, value in sorted(freq.items()): 19 | while value > 0: 20 | nums[i] = key 21 | value = value - 1 22 | i = i + 1 23 | 24 | 25 | if __name__ == '__main__': 26 | 27 | nums = [4, 2, 1, 4, 1, 4, 2, 1, 10] 28 | 29 | countSort(nums) 30 | print(nums) -------------------------------------------------------------------------------- /Python/Digital_clock.py: -------------------------------------------------------------------------------- 1 | from tkinter import Label, Tk 2 | import time 3 | 4 | app_window = Tk() 5 | app_window.title("Digital Clock") 6 | app_window.geometry("420x150") 7 | app_window.resizable(1,1) 8 | 9 | text_font= ("Boulder", 68, 'bold') 10 | background = "#f2e750" 11 | foreground= "#363529" 12 | border_width = 25 13 | 14 | label = Label(app_window, font=text_font, bg=background, fg=foreground, bd=border_width) 15 | label.grid(row=0, column=1) 16 | 17 | def digital_clock(): 18 | # Put %H instead of %I to change it to 24 hour format 19 | time_live = time.strftime("%I:%M:%S") 20 | label.config(text=time_live) 21 | label.after(200, digital_clock) 22 | 23 | digital_clock() 24 | app_window.mainloop() -------------------------------------------------------------------------------- /Python/Duplicates.py: -------------------------------------------------------------------------------- 1 | # Write a program which acceps % integer values and prints "Duplicates" 2 | # if any of the values entered are duplicates otherwise it print " All Unique" 3 | # Example Let 5 integers are 32,45,90,45,6 then output "Duplicates" to be printed 4 | 5 | if __name__ == "__main__": 6 | l = list(map(int,input("Enter numbers separated by space: ").split())) 7 | s = set(l) 8 | if len(s) == len(l): 9 | print("All unique") 10 | else: 11 | print("Duplicates exist.\n No. of Duplicate elements = ", len(l) - len(s)) 12 | -------------------------------------------------------------------------------- /Python/Fibonacci_recursion.py: -------------------------------------------------------------------------------- 1 | def fibonacci(num): 2 | if num<=1: 3 | return num 4 | else: 5 | return fibonacci(num-1) + fibonacci(num-2) 6 | 7 | toprint = int(input("Enter number of Fibonacci series terms to be printed: ")) 8 | for i in range(0,toprint): 9 | print(fibonacci(i)) -------------------------------------------------------------------------------- /Python/FidgetSpinner.py: -------------------------------------------------------------------------------- 1 | #Fidget spinner game 2 | 3 | from turtle import * 4 | state = {'turn':0} 5 | def spinner(): 6 | clear() 7 | angle = state['turn']/10 8 | right(angle) 9 | forward(100) 10 | dot(115, 'red') 11 | back(105) 12 | right(125) 13 | forward(105) 14 | dot(125, 'greenery') 15 | back(110) 16 | right(125) 17 | forward(105) 18 | dot(125, 'blue') 19 | back(105) 20 | right(120) 21 | update() 22 | def animate(): #animated 23 | if state['turn']>0: 24 | state['turn']-=1 25 | 26 | spinner() 27 | ontimer(animate,20) 28 | def flick(): 29 | state['turn']+=10 30 | 31 | setup(440, 440, 380, 0) 32 | hideturtle() 33 | tracer(False) 34 | width(20) 35 | onkey(flick, 'space') 36 | listen() 37 | animate() 38 | done() -------------------------------------------------------------------------------- /Python/Insert_into_a_Binary_Search_Tree.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | class TreeNode: 3 | def __init__(self, val=0, left=None, right=None): 4 | self.val = val 5 | self.left = left 6 | self.right = right 7 | 8 | class Solution: 9 | def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: 10 | # If the root is None, create a new node with the given value 11 | if not root: 12 | return TreeNode(val) 13 | 14 | # If the value is less than the root, insert it into the left subtree 15 | if val < root.val: 16 | root.left = self.insertIntoBST(root.left, val) 17 | 18 | # If the value is greater than or equal to the root, insert it into the right subtree 19 | else: 20 | root.right = self.insertIntoBST(root.right, val) 21 | 22 | # Return the root node after insertion 23 | return root 24 | 25 | # Sample usage 26 | if __name__ == '__main__': 27 | # Create a binary search tree with root value 4 28 | root = TreeNode(4) 29 | root.left = TreeNode(2) 30 | root.right = TreeNode(7) 31 | root.left.left = TreeNode(1) 32 | root.left.right = TreeNode(3) 33 | 34 | # Insert value 5 into the binary search tree 35 | sol = Solution() 36 | root = sol.insertIntoBST(root, 5) 37 | 38 | # Traverse the binary search tree using in-order traversal 39 | def inorderTraversal(root): 40 | if root: 41 | inorderTraversal(root.left) 42 | print(root.val) 43 | inorderTraversal(root.right) 44 | inorderTraversal(root) 45 | -------------------------------------------------------------------------------- /Python/LMS/main.py: -------------------------------------------------------------------------------- 1 | import functions #importing functions from functions.py 2 | print("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n Welcome to Library Management System \n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n") 3 | functions.library_stock() #calling function library_stock() from functions.py 4 | 5 | '''To show and perform actions on the main menu''' 6 | a=0 7 | while a != 3: 8 | try: 9 | a = int(input( 10 | "Enter '1' to borrow a book \nEnter '2' to return a book \nEnter '3' to exit.\n\nPlease select an option: ")) 11 | if a == 1: 12 | print("You will now borrow a book \n") 13 | functions.borrow_books() #calling function borrow_books() from functions.py 14 | 15 | elif a == 2: 16 | print("You will now return the book \n") 17 | functions.return_books() #calling function return_books() from functions.py 18 | 19 | elif a == 3: 20 | print( 21 | "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n Thank you for using our Library Management System \n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n") 22 | exit 23 | 24 | else: 25 | print( 26 | "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \nInvalid input !!! \nPlease provide value as 1, 2 or 3 \n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n") 27 | except: 28 | print("Please provide input in integer form.\n") #handling exception for non int values 29 | 30 | 31 | -------------------------------------------------------------------------------- /Python/Magic_Number.py: -------------------------------------------------------------------------------- 1 | '''A magic number is that number whose repeated sum of its digits till we get a single digit is equal to 1. 2 | Take a number as input (num). 3 | Step 2: Calculate the sum of digits of the input number. 4 | Repeat step 2 until we get a single digit. 5 | If the resultant sum is equal to 1 then it is a Magic number else not. 6 | Ex - 12345 - 7 | Sum = 1 + 2 + 3 + 4 + 5 = 15 = 1 + 5 = 6 (Not a magic Number) 8 | Ex - 1234 9 | Sum = 1 + 2 + 3 + 4 = 10 = 1 + 0 = 1 (Magic Number)''' 10 | 11 | def check_magic(number): 12 | while len(str(number)) > 1: 13 | sum = 0 14 | for each in str(number): 15 | sum += int(each) 16 | number = sum 17 | 18 | if number == 1: 19 | return "Magic Number" 20 | return "Not a Magic Number" 21 | 22 | print(check_magic(int(input("Enter a number:")))) 23 | -------------------------------------------------------------------------------- /Python/Monte_hall.py: -------------------------------------------------------------------------------- 1 | 2 | import random 3 | doors=[0]*3 4 | goatdoor=[0]*2 5 | swap=0 6 | dont_swap=0 7 | j=0 8 | while(j<10): 9 | x=random.randint(0,2) 10 | doors[x]="BMW" 11 | for i in range (0,3): 12 | if(i==x): 13 | continue 14 | else: 15 | doors[i]="goat" 16 | goatdoor.append[i] 17 | choice=int(input("Enter your choice")) 18 | door_open=random.choice(goatdoor) 19 | while(door_open==choice): 20 | door_open=random.choice(goatdoor) 21 | ch=input("do you want to swap? y/n") 22 | if(ch=='y'): 23 | if(doors[choice]=='goat'): 24 | print("player wins") 25 | swap=swap+1 26 | else: 27 | print("player lost") 28 | else: 29 | if(doors[choice]=='goat'): 30 | print("player lost") 31 | else: 32 | print("player wins") 33 | dont_swap=dont_swap+1 34 | j=j+1 35 | print(swap) 36 | print(dont_swap) 37 | 38 | -------------------------------------------------------------------------------- /Python/Number_Palindrome.py: -------------------------------------------------------------------------------- 1 | x = int(input("Enter any number: ")) 2 | s = abs(x) 3 | rev_num = 0 4 | while s != 0: 5 | last_num = s % 10 6 | rev_num = rev_num * 10 + last_num 7 | s //= 10 8 | if x == rev_num: 9 | print("The number is palindrome") 10 | else: 11 | print("The number is not palindrome") -------------------------------------------------------------------------------- /Python/Pyramidpattern.py: -------------------------------------------------------------------------------- 1 | numRow=int(input('Enter the number of row: ')) 2 | sp=numRow-1 3 | st=1 4 | for i in range(numRow): 5 | for j in range(sp): 6 | print(end=' ') 7 | for j in range(st): 8 | print('*',end=' ') 9 | print() 10 | sp-=1 11 | st+=1 12 | 13 | 14 | 15 | # Output 16 | 17 | # * 18 | # * * 19 | # * * * 20 | # * * * * 21 | # * * * * * 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Python/Python/maximize.py: -------------------------------------------------------------------------------- 1 | # f(X) = X^2 2 | # S = ( f(X1) + f(X2) + ... + f(XK) ) % M 3 | # The Code maximizes S taking the values of f(X) from each of K lines 4 | # Example input: 5 | # 3 1000 6 | # 2 5 4 7 | # 3 7 8 9 8 | # 5 5 7 8 9 10 9 | # output : 206 10 | # Source : Hackerrank > Prepare > PythonItertools > Maximize It! 11 | # url : https://www.hackerrank.com/challenges/maximize-it/problem?isFullScreen=true 12 | 13 | 14 | from itertools import product 15 | K, M = map(int, input("Input K & M : ").split()) 16 | List = [[int(i)**2 for i in input().split()[1:]] for _ in range(K)] 17 | print("The maximum value is :", max([sum(r) % M for r in product(*List)])) 18 | -------------------------------------------------------------------------------- /Python/RockPaperScissor.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def play(): 4 | user = input("What's your choice? 'r' for rock, 'p' for paper, 's' for scissors\n") 5 | computer = random.choice(['r', 'p', 's']) 6 | 7 | if user == computer: 8 | return 'It\'s a tie' 9 | 10 | elif is_win(user, computer): 11 | return 'You won!' 12 | 13 | else: 14 | return 'You lost!' 15 | 16 | def is_win(player, opponent): 17 | if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') \ 18 | or (player == 'p' and opponent == 'r'): 19 | return True 20 | 21 | print(play()) 22 | -------------------------------------------------------------------------------- /Python/Rock_Paper_Scissors.py: -------------------------------------------------------------------------------- 1 | import random 2 | name=input('Enter player name:') 3 | print(name,'v/s Computer') 4 | print('All the best!',name) 5 | p=0 6 | c=0 7 | n=int(input('How many games you want to play?:')) 8 | print('r for ROCK\np for PAPER\ns for SCISSOR') 9 | l=['r','p','s'] 10 | for i in range(1,n+1): 11 | print('Game',i) 12 | ch=input('Enter your choice:') 13 | ch=ch.lower() 14 | x=l.index(ch) 15 | # print(x) 16 | y=random.randint(0,2) 17 | # print(y) 18 | print("Computer's choice:",l[y]) 19 | if (y==0 and x==2) or (y==1 and x==0) or (y==2 and x==1): 20 | c=c+1 21 | elif(x==0 and y==2) or (x==1 and y==0) or (x==2 and y==1): 22 | p=p+1 23 | print('Scores after game',i,':',name,"'s:",p,"Computer's:",c) 24 | print('') 25 | if p>c: 26 | print('Congratulations',name,'You are the winner!!!') 27 | elif p==c: 28 | print("It's a draw") 29 | else: 30 | print('Well played, Better luck next time') 31 | -------------------------------------------------------------------------------- /Python/SnakeGame.py: -------------------------------------------------------------------------------- 1 | # Snake Game 2 | 3 | import turtle 4 | import random 5 | import time 6 | 7 | score = 0 8 | 9 | # screen 10 | screen = turtle.Screen() 11 | screen.bgcolor("black") 12 | screen.title("Snake Game By Atharva") 13 | screen.tracer(0) 14 | 15 | # Snake 16 | snake = turtle.Turtle() 17 | snake.shape("square") 18 | snake.color("green") 19 | snake.penup() 20 | snake_pos = "None" 21 | 22 | # apple 23 | apple = turtle.Turtle() 24 | apple.shape("circle") 25 | apple.color("red") 26 | apple.penup() 27 | apple.goto(0, 100) 28 | 29 | # classes 30 | 31 | 32 | class SnakeMovements: 33 | y = 0 34 | x = 0 35 | snpos = snake_pos 36 | 37 | snpos = None 38 | def main(self): 39 | if self.snpos == "up": 40 | self.y = snake.ycor() 41 | snake.sety(self.y + 15) 42 | 43 | if self.snpos == "down": 44 | self.y = snake.ycor() 45 | snake.sety(self.y - 15) 46 | 47 | if self.snpos == "right": 48 | self.x = snake.xcor() 49 | snake.setx(self.x + 15) 50 | 51 | if self.snpos == "left": 52 | self.x = snake.xcor() 53 | snake.setx(self.x - 15) 54 | 55 | def SnakeUp(self): 56 | self.snpos = "up" 57 | 58 | def SnakeDown(self): 59 | self.snpos = "down" 60 | 61 | def SnakeRight(self): 62 | self.snpos = "right" 63 | 64 | def SnakeLeft(self): 65 | self.snpos = "left" 66 | 67 | instanceVar = SnakeMovements() 68 | 69 | # Keyboard Binding 70 | 71 | screen.listen() 72 | screen.onkeypress(instanceVar.SnakeUp, "Up") 73 | screen.onkeypress(instanceVar.SnakeDown, "Down") 74 | screen.onkeypress(instanceVar.SnakeRight, "Right") 75 | screen.onkeypress(instanceVar.SnakeLeft, "Left") 76 | 77 | # Mainloop of the game 78 | 79 | while True: 80 | if snake.distance(apple) < 20: 81 | score += 1 82 | print(score) 83 | applex = random.randint(-290, 290) 84 | appley = random.randint(-290, 290) 85 | apple.goto(applex, appley) 86 | 87 | time.sleep(0.1) 88 | 89 | instanceVar.main() 90 | screen.update() 91 | -------------------------------------------------------------------------------- /Python/String_Palindrome.py: -------------------------------------------------------------------------------- 1 | str_1 = input ("Enter the string to check if it is a palindrome: ") 2 | str_1 = str_1.casefold () 3 | rev_str = reversed (str_1) 4 | if list (str_1) == list (rev_str): 5 | print ("The string is a palindrome.") 6 | else: 7 | print ("The string is not a palindrome.") -------------------------------------------------------------------------------- /Python/TCPclient.py: -------------------------------------------------------------------------------- 1 | """ 2 | /This is the file for client machine 3 | /The server file is named as TCPserver.py 4 | """ 5 | import socket 6 | 7 | sock_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 | sock_.connect((socket.gethostname(),9337)) 9 | msg = sock_.recv(1024) 10 | sock_.close() 11 | print(msg.decode("ascii")) 12 | -------------------------------------------------------------------------------- /Python/TCPserver.py: -------------------------------------------------------------------------------- 1 | ''' 2 | / This is the server file 3 | / The client file is named as TCPclient.py 4 | ''' 5 | 6 | from email import message 7 | import socket 8 | 9 | host = socket.gethostname() 10 | port = 9337 11 | 12 | sock_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 13 | sock_.bind((host,port)) 14 | sock_.listen(1) 15 | 16 | print("\nServer Started.....\n") 17 | 18 | conn, addr = sock_.accept() 19 | 20 | print("Connected successfully to",str(addr)) 21 | message="\nThank you for connecting to the server"+str(addr) 22 | conn.send(message.encode("ascii")) 23 | conn.close() 24 | -------------------------------------------------------------------------------- /Python/Telephonedirectory.py: -------------------------------------------------------------------------------- 1 | print ("*****TELEPHONE DIRECTORY***") 2 | list1=[] 3 | list2=[] 4 | dict1={} 5 | temp=100 6 | n=int(input("Enter the number of contacts : ")) 7 | for i in range(0,n): 8 | name1=input("Enter your name: ") 9 | num=int(input("Enter your phone number: ")) 10 | list1.extend([name1]) 11 | list2.extend([num]) 12 | dict1=dict(zip(list1,list2))#to convert two list into dictionary 13 | print (dict1) 14 | 15 | print (""" 16 | 1:Add a contact 17 | 2:Search a contact 18 | 3:Delete a contact 19 | 4:Update a contact 20 | 5:View directory 21 | 6:Exit""") 22 | choice=input("Enter your choice") 23 | 24 | def add(dict1): 25 | name3=raw_input("Enter the new name you want to add: ") 26 | num3=input("Enter the number: ") 27 | dict1[name3]=num3 28 | print (dict1) 29 | 30 | 31 | def search(dict1,n,list1,temp): 32 | name2=raw_input("Enter the name whose number is to be found: ") 33 | for i in range(0,n): 34 | if list1[i]==name2: 35 | temp=i 36 | if temp!=100: 37 | print ("Number is : "),list2[temp] 38 | 39 | def delete(dict1): 40 | name4=raw_input("Enter the name you want to delete: ") 41 | del dict1[name4] 42 | print (dict1) 43 | 44 | def update(dict1,n,list1): 45 | name5=raw_input("Enter the name which you want to update: ") 46 | for i in range(0,n): 47 | if list1[i]==name5: 48 | temp=i 49 | if temp!=100: 50 | num5=input("Enter the new number") 51 | dict1[name5]=num5 52 | print (dict1) 53 | def view(dict1): 54 | print (dict1) 55 | 56 | if (choice==1): 57 | add(dict1) 58 | elif (choice==2): 59 | search(dict1,n,list1,temp) 60 | elif (choice==3): 61 | delete(dict1) 62 | elif (choice==4): 63 | update(dict1,n,list1) 64 | else: 65 | view(dict1) -------------------------------------------------------------------------------- /Python/Tower_of_Hanoi.py: -------------------------------------------------------------------------------- 1 | def towerOfHanoi(numrings, from_pole, to_pole, aux_pole): 2 | if numrings == 1: 3 | print('Move ring 1 from', from_pole, 'pole to', to_pole, 'pole') 4 | return 5 | towerOfHanoi(numrings - 1, from_pole, aux_pole, to_pole) 6 | print('Move ring', numrings, 'from', from_pole, 'pole to', to_pole, 'pole') 7 | towerOfHanoi(numrings - 1, aux_pole, to_pole, from_pole) 8 | 9 | 10 | numrings = 2 11 | towerOfHanoi(numrings, 'Left', 'Right', 'Middle') 12 | -------------------------------------------------------------------------------- /Python/Triangle Patter.py: -------------------------------------------------------------------------------- 1 | # PROBLEM STATEMENT: 2 | # Given to you shall be a number, within the range from 0 to 9. Draw a set of triangles which sit inside each other. 3 | # The number of triangles is equal to the input received. Please not that in the example a 0 input implies a 0 triangle. 4 | # 5 | # INPUT: 6 | # The input shall be a single integer from 0 to 9. 7 | # 8 | # OUTPUT: 9 | # The output should be the pattern of triangles inside triangles. 10 | # 11 | # EXAMPLE: 12 | # Input: 13 | # 1 14 | # 15 | # Output: 16 | # 1 17 | # 111 18 | # 19 | # Input: 20 | # 0 21 | # 22 | # Output: 23 | # 0 24 | # 25 | # Input: 26 | # 2 27 | # 28 | # Output: 29 | # 2 30 | # 212 31 | # 21112 32 | # 2222222 33 | 34 | 35 | def triangleloop(num): 36 | rows = [] 37 | if(num == 0): 38 | rows =["0"] 39 | elif(num == 1): 40 | rows = ["1", "111"] 41 | else: 42 | listTmp = triangleloop(num-1) 43 | rows = [num] 44 | for i in listTmp: 45 | rows.append(str(num) + str(i) + str(num)) 46 | rows.append(str(num)* (len(rows[-1])+2)) 47 | return rows 48 | 49 | n = input() 50 | l = triangleloop(int(n)) 51 | if(len(l) != 0): 52 | size = len(l[-1]) 53 | steps = int(size/2) 54 | for i in l: 55 | res = ((str(" ") * steps) + str(i)) 56 | steps= steps - 1 57 | print(res) -------------------------------------------------------------------------------- /Python/Valid Anagram.py: -------------------------------------------------------------------------------- 1 | s = input("Enter a string to be checked from: ") 2 | t = input("Enter string to be checked: ") 3 | 4 | if len(s) != len(t): 5 | print("The string is not valid Anagram") 6 | elif sorted(s, reverse=True) == sorted(t, reverse=True): 7 | print("The string is valid Anagram") 8 | else: 9 | print("The string is not valid Anagram") -------------------------------------------------------------------------------- /Python/area.py: -------------------------------------------------------------------------------- 1 | # Three sides of the triangle is a, b and c: 2 | a = float(input('Enter first side: ')) 3 | b = float(input('Enter second side: ')) 4 | c = float(input('Enter third side: ')) 5 | # calculate the semi-perimeter. 6 | s = (a + b + c) / 2 7 | # calculate the area. 8 | area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 9 | -------------------------------------------------------------------------------- /Python/areaOfRectangle.py: -------------------------------------------------------------------------------- 1 | def area(length: int,breadth: int): 2 | """ 3 | Function to find area of rectangle 4 | """ 5 | return length*breadth 6 | 7 | if __name__ == "__main__": 8 | l = int(input("enter the length of rectangle : ")) 9 | b = int(input("enter the breadth of the rectangle : ")) 10 | print(area(l,b)) -------------------------------------------------------------------------------- /Python/areaoftriangle.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def Area(s1: float, s2: float, s3: float): 5 | """ 6 | Function to find area of triangle 7 | 8 | params: 9 | s1: float: an int type value 10 | s2: float: an int type value 11 | s3: float: an int type value 12 | 13 | return: 14 | area: float: Calculated area of three sided triangle. 15 | """ 16 | area = 0 17 | if(s1 == s2 == s3): 18 | area = (math.sqrt(3)/4) * math.pow(s1,2) 19 | elif(s1 == s2 or s2 == s3 or s1 == s3): 20 | if(s1 == s2): 21 | area = 0.5 * s1 * s3 22 | elif(s2 == s3): 23 | area = 0.5 * s2 * s1 24 | elif(s1 == s3): 25 | area = 0.5 * s3 * s2 26 | else: 27 | sp = (s1 + s2 + s3) / 2 28 | area = math.sqrt(sp*(sp-s1)*(sp-s2)*(sp-s3)) 29 | return area 30 | 31 | 32 | if __name__ == "__main__": 33 | a = float(input('Enter the first side: ')) 34 | b = float(input('Enter the second side: ')) 35 | c = float(input('Enter the third side: ')) 36 | area = Area(a,b,c) 37 | print(f"Area of Triangle is {area:.2f}.") 38 | -------------------------------------------------------------------------------- /Python/battery_saver_notif.py: -------------------------------------------------------------------------------- 1 | import psutil 2 | from plyer import notification 3 | 4 | battery = psutil.sensors_battery() 5 | plugged = battery.power_plugged 6 | percent = battery.percent 7 | 8 | notification.notify(title="Battery percentage",message=str(percent)+"% Battery remain!!") 9 | 10 | if percent < 30 and plugged!=True: 11 | notification.notify(title="Battery Low",message=str(percent) + "% Battery remain!!") 12 | -------------------------------------------------------------------------------- /Python/binary_search.py: -------------------------------------------------------------------------------- 1 | def binary_search(list, target): 2 | first = 0 3 | last = len(list) - 1 4 | 5 | while first <= last: 6 | midpoint = (last - first) // 2 + first 7 | if list[midpoint] == target: 8 | return midpoint 9 | elif list[midpoint] < target: 10 | first = midpoint + 1 11 | else: 12 | last = midpoint - 1 13 | return None 14 | 15 | 16 | def testing(target): 17 | if target is not None: 18 | print("Target found index :", target+1) 19 | else: 20 | print("Target not found") 21 | 22 | 23 | test_1 = binary_search([1, 2, 3, 4, 5, 6, 7, 8, 9], 7) 24 | test_2 = binary_search([1, 2, 3, 4, 5, 6, 7, 8, 9], 11) 25 | testing(test_1) 26 | testing(test_2) 27 | -------------------------------------------------------------------------------- /Python/binarytree_fold.py: -------------------------------------------------------------------------------- 1 | # Python3 program to check foldable binary tree 2 | 3 | # A binary tree node has data, 4 | # pointer to left child and a 5 | # pointer to right child 6 | 7 | 8 | class newNode: 9 | def __init__(self, d): 10 | self.data = d 11 | self.left = None 12 | self.right = None 13 | 14 | # Returns true if the given 15 | # tree is foldable 16 | 17 | 18 | def isFoldable(node): 19 | 20 | # base case 21 | if node == None: 22 | return true 23 | 24 | # convert left subtree to its mirror 25 | mirror(node.left) 26 | 27 | # Compare the structures of the right subtree and mirrored 28 | # left subtree 29 | res = isStructSame(node.left, node.right) 30 | 31 | # Get the original tree back 32 | mirror(node.left) 33 | 34 | return res 35 | 36 | 37 | def isStructSame(a, b): 38 | 39 | if a == None and b == None: 40 | return True 41 | if a != None and b != None and isStructSame(a.left, b.left) and isStructSame(a.right, b.right): 42 | return True 43 | 44 | return False 45 | 46 | 47 | def mirror(node): 48 | 49 | if node == None: 50 | return 51 | else: 52 | 53 | # do the subtrees 54 | mirror(node.left) 55 | mirror(node.right) 56 | 57 | # swap the pointers in this node 58 | temp = node.left 59 | node.left = node.right 60 | node.right = temp 61 | 62 | 63 | # Driver Code 64 | if __name__ == '__main__': 65 | 66 | ''' 67 | The constructed binary tree is 68 | 1 69 | / \ 70 | 2 3 71 | \ / 72 | 4 5 73 | ''' 74 | root = newNode(1) 75 | root.left = newNode(2) 76 | root.right = newNode(3) 77 | root.right.left = newNode(4) 78 | root.left.right = newNode(5) 79 | 80 | if isFoldable(root): 81 | print("tree is foldable") 82 | else: 83 | print("Tree is not foldable") 84 | -------------------------------------------------------------------------------- /Python/binomial_expansion_series.py: -------------------------------------------------------------------------------- 1 | # Code by deepu13 2 | 3 | # Python program to print binomial expansion series 4 | 5 | ''' 6 | Question: 7 | Given three integers, A, X and n, the task is to print terms of below binomial expression series. 8 | (A+X)n = nC0AnX0 + nC1An-1X1 + nC2An-2X2 +….+ nCnA0Xn 9 | ''' 10 | 11 | ''' 12 | Examples: 13 | Input : A = 1, X = 1, n = 5 14 | Output : 1 5 10 10 5 1 15 | Input : A = 1, B = 2, n = 6 16 | Output : 1 12 60 160 240 192 64 17 | ''' 18 | 19 | ''' 20 | Solution: 21 | We know that for each value of n there will be (n+1) term in the binomial series. So now we use a simple approach and calculate the value of each element of the series and print it . 22 | nCr = (n!) / ((n-r)! * (r)!) 23 | Below is value of general term. 24 | Tr+1 = nCn-rAn-rXr 25 | So at each position we have to find the value of the general term and print that term . 26 | ''' 27 | 28 | # Code: 29 | 30 | # Function to calculate factorial of a number 31 | def factorial(n): 32 | f = 1 33 | for i in range(2, n+1): 34 | f *= i 35 | return f 36 | 37 | # Function to print the series 38 | def series(A, X, n): 39 | 40 | # calculating the value of n! 41 | nFact = factorial(n) 42 | 43 | # loop to display the series 44 | for i in range(0, n + 1): 45 | 46 | # For calculating the value of nCr 47 | niFact = factorial(n - i) 48 | iFact = factorial(i) 49 | 50 | # calculating the value of A to the power k and X to the power k 51 | aPow = pow(A, n - i) 52 | xPow = pow(X, i) 53 | 54 | # Display the series 55 | print (int((nFact * aPow * xPow) / (niFact * iFact)), end = " ") 56 | 57 | # Driver Code 58 | A = 3; X = 4; n = 5 59 | series(A, X, n) 60 | 61 | 62 | ''' 63 | Output: 64 | 243 1620 4320 5760 3840 1024 65 | ''' -------------------------------------------------------------------------------- /Python/bubblesort.py: -------------------------------------------------------------------------------- 1 | def bubble_sort(arr): 2 | n = len(arr) 3 | 4 | # Traverse through all elements in the list 5 | for i in range(n): 6 | # Flag to optimize the algorithm by detecting if any swaps were made 7 | swapped = False 8 | 9 | # Last i elements are already in place, so we don't need to compare them 10 | for j in range(0, n-i-1): 11 | # Swap if the element found is greater than the next element 12 | if arr[j] > arr[j+1]: 13 | arr[j], arr[j+1] = arr[j+1], arr[j] # Swap the elements 14 | swapped = True # Set the swapped flag to True 15 | 16 | # If no two elements were swapped in the inner loop, the list is sorted 17 | # and we can break out of the outer loop early 18 | if not swapped: 19 | break 20 | 21 | 22 | arr = [64, 34, 25, 12, 22, 11, 90] 23 | bubble_sort(arr) 24 | print("Sorted array is:", arr) 25 | -------------------------------------------------------------------------------- /Python/butterfly_pattern.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | 4 | lenght_of_butterfly = int(input("Enter the number of rows and make sure its even ")) 5 | 6 | n = lenght_of_butterfly//2 7 | 8 | print("Generated butterfly pattern is:\n") 9 | 10 | for i in range(1,n+1): 11 | for j in range(1, 2*n+1): 12 | if j>i and j< 2*n+1-i: 13 | print(" ", end="") 14 | else: 15 | print(". ", end="") 16 | print("") 17 | 18 | for i in range(n,0,-1): 19 | for j in range(2*n,0,-1): 20 | if j>i and j< 2*n+1-i: 21 | print(" ", end="") 22 | else: 23 | print(". ", end="") 24 | print("") 25 | 26 | -------------------------------------------------------------------------------- /Python/calendar.py: -------------------------------------------------------------------------------- 1 | from calendar import * 2 | year = int(input('Enter Year: ')) 3 | print(calendar(year, 2, 1, 8, 4)) 4 | -------------------------------------------------------------------------------- /Python/check_prime.py: -------------------------------------------------------------------------------- 1 | #code contributed by Akshat-0602 2 | # Program to check if a number is prime or not 3 | from math import sqrt,floor 4 | 5 | def prime_check(number): 6 | if (number == 1): #if number is equal to 1 7 | print("1 is neither a prime number nor a composite number.") 8 | elif (number > 1): #if number is greater than 1 9 | for i in range(2, floor(sqrt(number)) + 1): # 2 is the first prime number 10 | print(number % i) 11 | if ((number % i) == 0): # if the number is divisible by any number other than 1 and itself then it is not a prime number 12 | print(number, "is not a prime number") 13 | print(number, "divided by", i, "=", (number//i)) # print the number of times the number is divisible by the number 14 | break 15 | else: # if the number is not divisible by any number other than 1 and itself 16 | print(number, "is a prime number.") 17 | else: # if the number is less than 1 18 | print(number, "is not a prime number.") 19 | 20 | if __name__ == '__main__': 21 | num = int(input("Enter a number: ")) 22 | prime_check(num) 23 | -------------------------------------------------------------------------------- /Python/exchangeValuesPython/Main.py: -------------------------------------------------------------------------------- 1 | # Classic way of exchanging the variables. 2 | def classic_way_exchange(a, b): 3 | #Printing the assigned values 4 | print(f"The old value of a is {a}") 5 | print(f"The old value of b is {b}\n") 6 | 7 | #To shift the values, we need another variable i.e c 8 | 9 | c = b #First, we will put the value of b in c 10 | b = a #Second, we will put the value of a in b (b is empty due to 'First') 11 | a = c #Now we will put value of c in a 12 | 13 | #Lets print new values 14 | print(f"The new value of a is {a}") 15 | print(f"The new value of b is {b}\n") 16 | 17 | # Much more shorter way - Pythonic Way 18 | def pythonic_way_exchange(a, b): 19 | #Printing the assigned values 20 | print(f"The old value of a is {a}") 21 | print(f"The old value of b is {b}\n") 22 | 23 | b, a = a, b 24 | #Lets print new values 25 | print(f"The new value of a is {a}") 26 | print(f"The new value of b is {b}\n") 27 | 28 | if __name__ == "__main__": 29 | classic_way_exchange(1, 2) 30 | pythonic_way_exchange(1, 2) -------------------------------------------------------------------------------- /Python/fibonacciSequence.py: -------------------------------------------------------------------------------- 1 | # Python Program to print Fibonacci Sequence upto n terms 2 | 3 | n = int(input("Please enter the number of terms : ")) 4 | 5 | # Base Condition(First 2 terms) 6 | n1, n2 = 0, 1 7 | cnt = 0 8 | 9 | # check if the number of terms is valid 10 | if n <= 0: 11 | print("Please enter a positive integer") 12 | # if there is only one term, return n1 13 | elif n == 1: 14 | print("Fibonacci Sequence upto ",n," :") 15 | print(n1) 16 | # Generate Fibonacci Sequence 17 | else: 18 | print("Fibonacci Sequence : ") 19 | while cnt < n: 20 | print(n1) 21 | nth = n1 + n2 22 | # update values 23 | n1 = n2 24 | n2 = nth 25 | cnt += 1 26 | 27 | # So Fibonacci Sequence is what we know a sequence where every term 28 | # is the sum of preceding two terms (Except first two terms : 0 and 1) 29 | # So here we implement this program by using 3 simple conditions : 30 | # 1. If the input terms is less or than equal 0, we output this as wrong input 31 | # 2. If the input is 1 , we output the the first term i.e. n1 32 | # 3. Other wise, 33 | # we use n1 and n2 repeatedly until cnt < n 34 | # print n1 which is the next term in the sequence 35 | # store n1 + n2 in nth 36 | # and update the n1 and n2 as the last two terms accordingly i.e. with n2 and nth 37 | # update cnt by adding 1. -------------------------------------------------------------------------------- /Python/formatting.py: -------------------------------------------------------------------------------- 1 | # Python program showing how to use 2 | # string modulo operator(%) to print 3 | # fancier output 4 | 5 | # print integer and float value 6 | print("Geeks : %2d, Portal : %5.2f" % (1, 05.333)) 7 | 8 | # print integer value 9 | print("Total students : %3d, Boys : %2d" % (240, 120)) 10 | 11 | # print octal value 12 | print("%7.3o" % (25)) 13 | 14 | # print exponential value 15 | print("%10.3E" % (356.08977)) 16 | -------------------------------------------------------------------------------- /Python/hashcracker.py: -------------------------------------------------------------------------------- 1 | import hashlib 2 | import argparse 3 | 4 | parser = argparse.ArgumentParser(description="MD5 Cracker") 5 | parser.add_argument("-md5", dest="hash", help="md5 hash", required = True) 6 | parser.add_argument("-w", fest="wordlist", help="wordlist",required=True) 7 | parsed_args = parser.parse_args() 8 | 9 | def main(): 10 | hash_cracked = "" 11 | with open(parsed_args.wordlist) as file: 12 | for line in file: 13 | line = line.strip() 14 | if hashlib.md5(bytes(line,encoding="utf-8")).hexdigest() == parsed_args.hash: 15 | hash_cracked = line 16 | print("\nMD5-hash has been successfully cracked. The value is %s"%line) 17 | if hash_cracked == "": 18 | print("\n Failed to crack the hash. Try using a different dictionary") 19 | 20 | if __name__ == "__main__": 21 | main() 22 | -------------------------------------------------------------------------------- /Python/heap_sort.py: -------------------------------------------------------------------------------- 1 | from heapq import heappop, heappush 2 | 3 | def heapsort(list1): 4 | heap = [] 5 | for ele in list1: 6 | heappush(heap, ele) 7 | 8 | sort = [] 9 | 10 | # the elements are lift in the heap 11 | while heap: 12 | sort.append(heappop(heap)) 13 | 14 | return sort 15 | 16 | list1 = [27, 21, 55, 15, 60, 4, 11, 17, 2, 87] 17 | print(heapsort(list1)) 18 | -------------------------------------------------------------------------------- /Python/integer_to_roman.py: -------------------------------------------------------------------------------- 1 | def intToRoman(num: int) -> str: 2 | # making a dictionary to make conversion easier 3 | romanChar = { 4 | 1000: 'M', 5 | 900: 'CM', 6 | 500: 'D', 7 | 400: 'CD', 8 | 100: 'C', 9 | 90: 'XC', 10 | 50: 'L', 11 | 40: 'XL', 12 | 10: 'X', 13 | 9: 'IX', 14 | 5: 'V', 15 | 4: 'IV', 16 | 1: 'I' 17 | } 18 | ans = '' 19 | 20 | for i in [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]: 21 | if num // i: 22 | ans += (num//i)*romanChar[i] 23 | num -= (num//i) * i 24 | return ans 25 | 26 | # test 27 | print(intToRoman(984)) 28 | -------------------------------------------------------------------------------- /Python/internetspeedtest.py: -------------------------------------------------------------------------------- 1 | import speedtest 2 | 3 | 4 | def speedtester(): 5 | st = speedtest.Speedtest() 6 | 7 | print("Loading server list...\n") 8 | st.get_servers() 9 | print("Choosing best server...") 10 | best = st.get_best_server() 11 | print(f"Found: {best['host']} located in {best['country']}") 12 | 13 | option = int(input('''What speed do you want to test: 14 | 15 | 1) Download Speed 16 | 17 | 2) Upload Speed 18 | 19 | 3) Ping 20 | 21 | Your Choice: ''')) 22 | 23 | if option == 1: 24 | print("Performing download test...") 25 | downloadresult = st.download() 26 | print(f"Download speed: {downloadresult / 1024 / 1024: .2f} Mbit/s") 27 | 28 | elif option == 2: 29 | print("Performing upload test...") 30 | uploadresult = st.upload() 31 | print(f"Upload result : {uploadresult /1024 /1024:.2f} Mbit/s") 32 | 33 | elif option == 3: 34 | ping_result = st.results.ping 35 | print(f"Ping: {ping_result}ms") 36 | 37 | else: 38 | 39 | print("Please enter the correct choice !") 40 | 41 | 42 | if __name__ == "__main__": 43 | speedtester() -------------------------------------------------------------------------------- /Python/intersection_point_of_two_Linked_Lists.py: -------------------------------------------------------------------------------- 1 | # Python program to get intersection 2 | # point of two linked list 3 | class Node : 4 | def __init__(self, d): 5 | self.data = d; 6 | self.next = None; 7 | 8 | # Function to print the list 9 | def Print(n): 10 | cur = n; 11 | while (cur != None) : 12 | print(cur.data, end=" "); 13 | cur = cur.next; 14 | print(""); 15 | 16 | # Function to find the intersection of two node 17 | def MegeNode(n1, n2): 18 | 19 | # Define hashset 20 | hs = set(); 21 | 22 | while (n1 != None): 23 | hs.add(n1); 24 | n1 = n1.next; 25 | while (n2 != None): 26 | if (n2 in hs): 27 | return n2; 28 | n2 = n2.next; 29 | 30 | return None; 31 | 32 | 33 | # Driver code 34 | 35 | # list 1 36 | n1 = Node(1); 37 | n1.next = Node(2); 38 | n1.next.next = Node(3); 39 | n1.next.next.next = Node(4); 40 | n1.next.next.next.next = Node(5); 41 | n1.next.next.next.next.next = Node(6); 42 | n1.next.next.next.next.next.next = Node(7); 43 | 44 | # list 2 45 | n2 = Node(10); 46 | n2.next = Node(9); 47 | n2.next.next = Node(8); 48 | n2.next.next.next = n1.next.next.next; 49 | 50 | Print(n1); 51 | Print(n2); 52 | 53 | print(MegeNode(n1, n2).data); 54 | -------------------------------------------------------------------------------- /Python/katakana2hiragana.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Japanese words are always written by katakana, but only hiragana can be searched in the dictionary, 3 | this script can convert katakana to hiragana quickly. 4 | ''' 5 | 6 | import re 7 | InputText = 'チョコチョコ' 8 | 9 | 10 | def ConverHina2kata(InputText): 11 | ProcessTexts = [] 12 | for gana in InputText: 13 | if 12448 < int(ord(gana)) < 12543: 14 | hira = chr(int(ord(gana) - 96)) 15 | ProcessTexts.append(hira) 16 | OutputText = ''.join(ProcessTexts) 17 | return OutputText 18 | 19 | 20 | if re.search(r'^[\u30a0-\u30ff]*?$', InputText) != None: 21 | print(ConverHina2kata(InputText)) 22 | -------------------------------------------------------------------------------- /Python/left_view_of_binary_tree.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data, left=None, right=None): 3 | self.data = data 4 | self.left = left 5 | self.right = right 6 | 7 | 8 | def fun(root, d, l): 9 | if root is None: 10 | return 11 | if l not in d: 12 | d[l] = root.data 13 | fun(root.left, d, l + 1) 14 | fun(root.right, d, l + 1) 15 | 16 | 17 | def leftview(root): 18 | d = {} 19 | fun(root, d, 0) 20 | return list(d.values()) 21 | 22 | 23 | root = Node(1) 24 | root.left = Node(2) 25 | root.right = Node(3) 26 | root.left.left = Node(4) 27 | root.left.right = Node(5) 28 | root.right.left = Node(6) 29 | root.right.right = Node(7) 30 | result = leftview(root) 31 | print(result) 32 | -------------------------------------------------------------------------------- /Python/linear_search.py: -------------------------------------------------------------------------------- 1 | lst = [] 2 | i = 0 3 | m = int(input("Enter number of elements to add in list: ")) 4 | while i m): 10 | return self.Median(B, A) # Swapping to make A smaller 11 | 12 | start = 0 13 | end = n 14 | realmidinmergedarray = (n + m + 1) // 2 15 | 16 | while (start <= end): 17 | mid = (start + end) // 2 18 | leftAsize = mid 19 | leftBsize = realmidinmergedarray - mid 20 | 21 | # checking overflow of indices 22 | leftA = A[leftAsize - 1] if (leftAsize > 0) else float('-inf') 23 | leftB = B[leftBsize - 1] if (leftBsize > 0) else float('-inf') 24 | rightA = A[leftAsize] if (leftAsize < n) else float('inf') 25 | rightB = B[leftBsize] if (leftBsize < m) else float('inf') 26 | 27 | # if correct partition is done 28 | if leftA <= rightB and leftB <= rightA: 29 | if ((m + n) % 2 == 0): 30 | return (max(leftA, leftB) + min(rightA, rightB)) / 2.0 31 | return max(leftA, leftB) 32 | 33 | elif (leftA > rightB): 34 | end = mid - 1 35 | else: 36 | start = mid + 1 37 | 38 | 39 | # Driver code 40 | ans = Solution() 41 | arr1 = [-5, 3, 6, 12, 15] 42 | arr2 = [-12, -10, -6, -3, 4, 10] 43 | print("Median of the two arrays is") 44 | print(ans.Median(arr1, arr2)) 45 | 46 | -------------------------------------------------------------------------------- /Python/merge_sort.py: -------------------------------------------------------------------------------- 1 | """ 2 | This is a pure Python implementation of the merge sort algorithm 3 | For doctests run following command: 4 | python -m doctest -v merge_sort.py 5 | or 6 | python3 -m doctest -v merge_sort.py 7 | For manual testing run: 8 | python merge_sort.py 9 | """ 10 | 11 | 12 | def merge_sort(collection: list) -> list: 13 | """Pure implementation of the merge sort algorithm in Python 14 | :param collection: some mutable ordered collection with heterogeneous 15 | comparable items inside 16 | :return: the same collection ordered by ascending 17 | Examples: 18 | >>> merge_sort([0, 5, 3, 2, 2]) 19 | [0, 2, 2, 3, 5] 20 | >>> merge_sort([]) 21 | [] 22 | >>> merge_sort([-2, -5, -45]) 23 | [-45, -5, -2] 24 | """ 25 | 26 | def merge(left: list, right: list) -> list: 27 | """merge left and right 28 | :param left: left collection 29 | :param right: right collection 30 | :return: merge result 31 | """ 32 | 33 | def _merge(): 34 | while left and right: 35 | yield (left if left[0] <= right[0] else right).pop(0) 36 | yield from left 37 | yield from right 38 | 39 | return list(_merge()) 40 | 41 | if len(collection) <= 1: 42 | return collection 43 | mid = len(collection) // 2 44 | return merge(merge_sort(collection[:mid]), merge_sort(collection[mid:])) 45 | 46 | 47 | if __name__ == "__main__": 48 | import doctest 49 | 50 | doctest.testmod() 51 | user_input = input("Enter numbers separated by a comma:\n").strip() 52 | unsorted = [int(item) for item in user_input.split(",")] 53 | print(*merge_sort(unsorted), sep=",") 54 | -------------------------------------------------------------------------------- /Python/nmap.py: -------------------------------------------------------------------------------- 1 | #just a basic port scanner 2 | import nmap 3 | import sys 4 | 5 | target = str(sys.argv[1]) 6 | ports = [21,22,80,443,8080] 7 | 8 | scan_v = nmap.PortScanner() 9 | 10 | print("\nScanning",target,"for ports 21,22,80,443,8080.....\n") 11 | 12 | for port in ports: 13 | portscan = scan_v.scan(target,str(port)) 14 | print("Port",port,"is",portscan['scan'][target]['tcp'][port]['state']) 15 | 16 | print("\nHost",target,"is",portscan['scan'][target]['status']['state']) 17 | -------------------------------------------------------------------------------- /Python/number_guessing_game.py: -------------------------------------------------------------------------------- 1 | import random; 2 | 3 | number = random.randint(1, 100); 4 | 5 | print(".......... GUESS THE NUMBER ! .........."); 6 | print("In this game you will have to guess the number that will lie between 1 & 100"); 7 | readyOrNot = input("So Are you ready ? (y/n) : "); 8 | 9 | def Game() : 10 | previousGuessDistance = 0; 11 | turn = 0; 12 | while 1 : 13 | guess = int(input("Enter your guess : ")); 14 | 15 | if guess == number : 16 | turn+=1; 17 | print("!!! YOU GOT IT !!!"); 18 | print("SCORE : ", turn," | The lesser the better."); 19 | break; 20 | elif abs(guess-number) < previousGuessDistance : 21 | print("Congrat! you are closer"); 22 | previousGuessDistance = abs(guess-number); 23 | turn+=1; 24 | else : 25 | if previousGuessDistance == 0 : 26 | print("Not the number .. try again .."); 27 | previousGuessDistance = abs(guess-number); 28 | turn+=1; 29 | continue; 30 | print("Ough! you went farther away"); 31 | previousGuessDistance = abs(guess-number); 32 | turn+=1; 33 | 34 | 35 | if readyOrNot == 'y' or readyOrNot == 'Y' : 36 | print("Great then Let's play"); 37 | Game(); 38 | else : 39 | print("Doesn't matter .. Let's play anyways ;-)"); 40 | Game(); -------------------------------------------------------------------------------- /Python/packetflooder.py: -------------------------------------------------------------------------------- 1 | from scapy.all import * 2 | 3 | def floodz(source,target): 4 | for source_p in range(100,150): 5 | IPlayer = IP(src=source,dst=target) 6 | TCPlayer = TCP(sport=source_p,dport=600) 7 | pkt = IPlayer / TCPlayer 8 | send(pkt) 9 | 10 | source = "127.0.0.1" 11 | target = "192.168.199.142" 12 | floodz(source,target) 13 | -------------------------------------------------------------------------------- /Python/python-nmap-port-scanner.py: -------------------------------------------------------------------------------- 1 | import nmap 2 | import re 3 | 4 | ip_pattern = re.compile("^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$") 5 | port_pattern = re.compile("([0-9]+)-([0-9]+)") 6 | 7 | port_min = 0 8 | port_max= 65535 9 | 10 | print(r""" 11 | |\---/| 12 | | ,_, | Welcome to 13 | \_`_/-..----. 14 | ___/ ` ' ,""+ \ PYTHON PORT SCANNER 15 | (__...' __\ |`.___.'; 16 | (_,...'(_,.`__)/'.....+ 17 | """) 18 | 19 | 20 | open_ports = [] 21 | 22 | while True: 23 | ip_add_input = input("\n Enter the ip to be scanned:- ") 24 | 25 | if ip_pattern.search(ip_add_input): 26 | print(f"{ip_add_input} is a valid ip address") 27 | break 28 | 29 | while True: 30 | print("Enter the port range you want to scan in format: - (example would be 40-8080)") 31 | port_range = input("Enter port range: ") 32 | port_range_valid = port_pattern.search(port_range.replace(" ","")) 33 | if port_range_valid: 34 | port_min = int(port_range_valid.group(1)) 35 | port_max = int(port_range_valid.group(2)) 36 | break 37 | 38 | nm=nmap.PortScanner() 39 | for port in range(port_min,port_max +1): 40 | try: 41 | result = nm.scan(ip_add_input, str(port)) 42 | port_status = (result['scan'][ip_add_input]['tcp'][port]['state']) 43 | print(f"Port {port} is {port_status}") 44 | except: 45 | print(f"Cannot scan port {port}.") 46 | 47 | -------------------------------------------------------------------------------- /Python/recon.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import requests 3 | import socket 4 | import json 5 | 6 | if len(sys.argv) < 2: 7 | print("Usage: " + sys.argv[0] + "") 8 | sys.exit(1) 9 | 10 | req = requests.get("https://"+sys.argv[1]) 11 | print("\n"+str(req.headers)) 12 | 13 | gethostby_ = socket.gethostbyname(sys.argv[1]) 14 | print("\nThe IP address of "+sys.argv[1]+"is: "+gethostby_ + "\n") 15 | 16 | #ipinfo.io 17 | 18 | req_two = requests.get("https://ipinfo.io/"+gethostby_+"/json") 19 | resp_ = json.loads(req_two.text) 20 | 21 | print("Location: "+resp_["loc"] +"\nRegion: "+resp_["region"] +"\nCity: "+resp_["city"] +"\nCountry: "+resp_["country"]) 22 | -------------------------------------------------------------------------------- /Python/recursive_factorial.py: -------------------------------------------------------------------------------- 1 | def recur_factorial(n): 2 | if n == 1: 3 | return n 4 | else: 5 | return n*recur_factorial(n-1) 6 | 7 | num = 7 8 | 9 | # check if the number is negative 10 | if num < 0: 11 | print("Sorry, factorial does not exist for negative numbers") 12 | elif num == 0: 13 | print("The factorial of 0 is 1") 14 | else: 15 | print("The factorial of", num, "is", recur_factorial(num)) 16 | -------------------------------------------------------------------------------- /Python/regression_model/data.csv: -------------------------------------------------------------------------------- 1 | studytime,score 2 | 1,20 3 | 2,30 4 | 3,56 5 | 4,25 6 | 5,65 7 | 6,54 8 | 7,95 9 | 8,64 10 | 9,75 11 | 10,82 12 | -------------------------------------------------------------------------------- /Python/regression_model/reg.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import matplotlib.pyplot as plt 3 | 4 | data = pd.read_csv('data.csv') 5 | 6 | def meansqerror(m, b, points): 7 | total_error = 0 8 | for i in range(len(points)): 9 | x = points.iloc[i].studytime 10 | y = points.iloc[i].score 11 | total_error += (y-(m*x+b))**2 12 | error = total_error/ float(len(points)) 13 | return error 14 | 15 | def graddescent(m_now, b_now, points, L): 16 | m_gradient = 0 17 | b_gradient = 0 18 | n = len (points) 19 | 20 | for i in range(n): 21 | x = points.iloc[i].studytime 22 | y = points.iloc[i].score 23 | 24 | m_gradient += (-2/n)*x*(y-m_now*x-b_now) 25 | b_gradient += (-2/n)*(y-m_now*x-b_now) 26 | 27 | m = m_now - m_gradient*L 28 | b = b_now - b_gradient*L 29 | return m,b 30 | 31 | m=0 32 | b=0 33 | L=0.001 34 | epos = 300 35 | for i in range(epos): 36 | m,b = graddescent(m,b,data,L) 37 | print("Coefficient : ",m) 38 | print("Intercept : ",b) 39 | print("Mean Squared Error : ",meansqerror(m, b, data)) 40 | 41 | plt.scatter(data.studytime, data.score, color='red') 42 | plt.plot(data.studytime, m*data.studytime+b,color= 'black') 43 | plt.show() -------------------------------------------------------------------------------- /Python/rev_string.py: -------------------------------------------------------------------------------- 1 | def revstring(string): 2 | if len(string) == 0: 3 | return(string) 4 | else: 5 | return revstring(string[1:])+string[0] 6 | 7 | stg = input("Enter a string: ") 8 | print("The reversed string is: ", end=" ") 9 | print(revstring(stg)) -------------------------------------------------------------------------------- /Python/screenshot.py: -------------------------------------------------------------------------------- 1 | import wx 2 | import os 3 | import ftplib 4 | 5 | w = wx.App() 6 | screen = wx.ScreenDC() 7 | size = screen.GetSize() 8 | bmap = wx.Bitmap(size[0],size[1]) 9 | memo = wx.MemoryDC(bmap) 10 | memo.Blit(0,0,size[0],size[1],screen,0,0) 11 | 12 | del memo 13 | bmap.SaveFile("SS.png", wx.BITMAP_TYPE_PNG) 14 | -------------------------------------------------------------------------------- /Python/secure_password_generator.py: -------------------------------------------------------------------------------- 1 | #make sure that your device has Python3 and tkinter installed 2 | from tkinter import * 3 | import random 4 | import string 5 | 6 | root = Tk() 7 | 8 | def generate(): 9 | char = list(string.ascii_letters + string.digits + string.punctuation) 10 | 11 | 12 | for i in range (25): 13 | if i == 0: 14 | password = char[random.randint(0,len(char)-1)] 15 | else: 16 | password = password + char[random.randint(0,len(char)-1)] 17 | 18 | l_output = Label(root, text="password") 19 | l_output.pack() 20 | 21 | output= Entry(root) 22 | output.insert(10,password) 23 | output.pack() 24 | 25 | button = Button(root, text="Generate", command=generate) 26 | button.pack() 27 | 28 | root.mainloop() 29 | -------------------------------------------------------------------------------- /Python/sendEmail.py: -------------------------------------------------------------------------------- 1 | import smtplib 2 | import getpass 3 | from email.mime.text import MIMEText 4 | 5 | def send_email(senders_address,recipients): 6 | password = getpass.getpass() 7 | subject = 'About Me' 8 | msg = ''' 9 | Hello Everyone! 10 | This is Rajdeep Kumar. Nice to meet you. 11 | ThankYou. 12 | ''' 13 | #server initialisation 14 | server = smtplib.SMTP('smtp.gmail.com',587) 15 | server.starttls() 16 | server.login(senders_address,password) 17 | 18 | #draft my message body 19 | msg = MIMEText(msg) 20 | msg['Subject'] = subject 21 | msg['From'] = senders_address 22 | msg['To'] = ", ".join(recipients) 23 | #msg.set_param('importance','high value') 24 | 25 | server.sendmail(senders_address,recipients,msg.as_string()) 26 | 27 | send_email(input('From: '),input('To: ').split()) 28 | -------------------------------------------------------------------------------- /Python/spirograph.py: -------------------------------------------------------------------------------- 1 | """Spirograph""" 2 | import random 3 | import turtle as t 4 | 5 | t.colormode(255) 6 | t.Screen().bgcolor('black') 7 | t.Screen().title("Spirograph") 8 | t.hideturtle() 9 | def rcolor(): 10 | r = random.randint(0, 255) 11 | g = random.randint(0, 255) 12 | b = random.randint(0, 255) 13 | rc = (r, g, b) 14 | return rc 15 | 16 | 17 | t.pensize(2) 18 | t.speed("fastest") 19 | 20 | 21 | def spiro(num): 22 | for _ in range(int(360 / num)): 23 | t.color(rcolor()) 24 | t.circle(120) 25 | t.setheading(t.heading() + 10) 26 | screen = t.Screen() 27 | screen.exitonclick() 28 | 29 | 30 | spiro(4) -------------------------------------------------------------------------------- /Python/subdomain.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import sys 3 | 4 | sub_list = open("subdomains.txt").read() 5 | subs = sub_list.splitlines() 6 | 7 | for sub in subs: 8 | url_ = f"http://{sub}.{sys.argv[1]}" 9 | 10 | try: 11 | requests.get(url_) 12 | 13 | except requests.ConnectionError: 14 | pass 15 | 16 | else: 17 | print("Valid domain: ", url_) 18 | -------------------------------------------------------------------------------- /Python/suduko_solver.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | grid=[[1,0,4,2,0,7,9,8,0], 4 | [0,8,0,0,0,0,6,0,2], 5 | [6,0,0,8,0,3,0,0,7], 6 | [0,0,0,9,4,1,0,6,0], 7 | [3,0,0,0,0,0,0,0,9], 8 | [0,7,0,6,3,5,0,0,0], 9 | [4,0,0,5,0,2,0,0,1], 10 | [2,0,7,0,0,0,0,9,0], 11 | [0,5,9,1,0,4,3,0,6]] 12 | def possible(row,column,number): 13 | global grid 14 | #checks the row 15 | for i in range(0,9): 16 | if grid[row][i]==number: 17 | return False 18 | #checks the column 19 | for i in range(0,9): 20 | if grid[i][column]==number: 21 | return False 22 | row=row//3 23 | column=column//3 24 | if row==1: 25 | row=3 26 | elif row==2: 27 | row=6 28 | if column==1: 29 | column=3 30 | elif column==2: 31 | column=6 32 | #checks 3X3 square grid 33 | for i in range(row,row+3): 34 | for j in range(column,column+3): 35 | if grid[i][j]==number: 36 | return False 37 | 38 | return True 39 | 40 | def solve(): 41 | global grid 42 | for row in range(0,9): 43 | for column in range(0,9): 44 | if grid[row][column]==0: 45 | for number in range(1,10): 46 | if possible(row,column,number): 47 | grid[row][column]=number 48 | solve() 49 | grid[row][column]=0 50 | return 51 | print(np.matrix(grid)) 52 | 53 | 54 | solve() 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Python/sum_of_first_n2_natural_numbers.py: -------------------------------------------------------------------------------- 1 | def get_sum_upto_number_square(last_num): 2 | 3 | total = last_num * (last_num + 1) * (2 * last_num + 1) 4 | return total // 6 5 | 6 | 7 | if __name__ == "__main__": 8 | 9 | n = input("Enter a Number to find the sum of squares, upto that number: ") 10 | 11 | try: 12 | n = int(n) 13 | except ValueError: 14 | print("ValueError: Input is not a valid Integer.") 15 | exit() 16 | 17 | if n > 0: 18 | result = get_sum_upto_number_square(n) 19 | print("...") 20 | print(f"Sum of first {n}^2 natural numbers is {result}.") 21 | else: 22 | print("ValueError: Input is a non-positive Integer.") 23 | -------------------------------------------------------------------------------- /Python/sum_of_n_natural_numbers.py: -------------------------------------------------------------------------------- 1 | def get_sum_upto_number(last_num): 2 | # we will update this variable in each iteration, with the total sum. 3 | total = 0 4 | 5 | # iterating from 'last_num' to '0' using while loop. 6 | # we add each number in 'total' and deduct 1 from 'last_num' 7 | while True: 8 | if last_num == 0: 9 | break 10 | total += last_num 11 | last_num -= 1 12 | 13 | return total 14 | 15 | 16 | if __name__ == "__main__": 17 | # asks for an Input from User, raises an error if input is not a Valid Number. 18 | n = input("Enter a Number to find the sum, upto that number: ") 19 | 20 | try: 21 | n = int(n) 22 | except ValueError: 23 | print("ValueError: Input is not a valid Integer.") 24 | exit() 25 | 26 | # run, if input is a positive integer. 27 | if n > 0: 28 | # calling the function above, with users's input as argument. 29 | result = get_sum_upto_number(n) 30 | print("...") 31 | print(f"Sum of first {n} natural numbers is {result}.") 32 | else: 33 | print("ValueError: Input is a non-positive Integer.") 34 | -------------------------------------------------------------------------------- /Python/system_health_check.py: -------------------------------------------------------------------------------- 1 | import socket,shutil,psutil 2 | 3 | def check_localhost(): 4 | localhost=socket.gethostbyname('localhost') 5 | return localhost=='127.0.0.1' 6 | 7 | def check_disk_usage(disk): 8 | '''Verifies that there's enough free space on disk''' 9 | du=shutil.disk_usage(disk) 10 | free=du.free / du.total*100 11 | return free>20 12 | 13 | def check_memory_usage(): 14 | '''Verifies that there's enough free space on disk''' 15 | mu=psutil.virtual_memory().available 16 | total=mu/(1024.0**2) 17 | return total>500 18 | 19 | def check_cpu_usage(): 20 | '''Verifies that there's enough unused CPU''' 21 | usage=psutil.cpu_percent(1) 22 | return usage<80 23 | 24 | if not check_cpu_usage(): 25 | print('⚠️ - CPU usage is over 80%') 26 | else: 27 | print('CPU is operating within normal parameters') 28 | 29 | if not check_memory_usage(): 30 | print('⚠️ - Available memory is less than 500MB') 31 | else: 32 | print('Memory is operating within normal parameters') 33 | 34 | if not check_disk_usage('/'): 35 | print('⚠️ - Available disk space is less than 20%') 36 | else: 37 | print('Disk is operating within normal parameters') 38 | 39 | if not check_localhost(): 40 | print('⚠️ - localhost cannot be resolved to 127.0.0.1') 41 | else: 42 | print('Localhost is operating within normal parameters') 43 | -------------------------------------------------------------------------------- /Python/temperature_converter.py: -------------------------------------------------------------------------------- 1 | suhu = input("Input temperature? (ex: 30C, 20F, 21K, 44R): ") 2 | drjt = int(suhu[:-1]) 3 | inputan = suhu[-1] 4 | 5 | if inputan.upper() == "C": 6 | hasil1 = float((9 * drjt) / 5 + 32) 7 | hasil2 = float(drjt + 273.15) 8 | hasil3 = float(4/5 * drjt) 9 | jenisX = "Celcius" 10 | jenis1 = "Fahrenheit" 11 | jenis2 = "Kelvin" 12 | jenis3 = "Reamur" 13 | 14 | elif inputan.upper() == "F": 15 | hasil1 = float((drjt - 32) * 5 / 9) 16 | hasil2 = float(((drjt - 32) * 5 / 9) + 273.15) 17 | hasil3 = float(4/9 * (drjt-32)) 18 | jenisX = "Fahrenheit" 19 | jenis1 = "Celsius" 20 | jenis2 = "Kelvin" 21 | jenis3 = "Reamur" 22 | elif inputan.upper() == "K": 23 | hasil1 = float(drjt - 273.15) 24 | hasil2 = float(((drjt - 273.15) * 9 / 5)+32) 25 | hasil3 = float(4/5 * (drjt-273)) 26 | jenisX = "Kelvin" 27 | jenis1 = "Celcius" 28 | jenis2 = "Fahrenheit" 29 | jenis3 = "Reamur" 30 | elif inputan.upper() == "R": 31 | hasil1 = float((5/4) * drjt) 32 | hasil2 = float((9/4 * drjt) + 32) 33 | hasil3 = float((5/4 * drjt) + 273) 34 | jenisX = "Reamur" 35 | jenis1 = "Celcius" 36 | jenis2 = "Fahrenheit" 37 | jenis3 = "Kelvin" 38 | else: 39 | print("Invalid input!! Attention to Input Writing") 40 | 41 | print(drjt,jenisX,"=","{:.1f}".format(hasil1),jenis1) 42 | print(drjt,jenisX,"=","{:.1f}".format(hasil2),jenis2) 43 | print(drjt,jenisX,"=","{:.1f}".format(hasil3),jenis3) 44 | -------------------------------------------------------------------------------- /Python/top_view.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.data = data 7 | self.left = None 8 | self.right = None 9 | self.horiz_dist = 0 10 | 11 | 12 | def topview(root): 13 | if not root: 14 | return [] 15 | 16 | d = {} 17 | queue = deque() 18 | root.horiz_dist = 0 19 | queue.append(root) 20 | 21 | while queue: 22 | cur = queue.popleft() 23 | horiz_dist = cur.horiz_dist 24 | if horiz_dist not in d: 25 | d[horiz_dist] = cur.data 26 | if cur.left: 27 | cur.left.horiz_dist = horiz_dist - 1 28 | queue.append(cur.left) 29 | if cur.right: 30 | cur.right.horiz_dist = horiz_dist + 1 31 | queue.append(cur.right) 32 | top_view_nodes = [d[key] for key in sorted(d)] 33 | return top_view_nodes 34 | 35 | 36 | root = Node(1) 37 | root.left = Node(2) 38 | root.right = Node(3) 39 | root.left.left = Node(4) 40 | root.left.right = Node(5) 41 | root.right.left = Node(6) 42 | root.right.right = Node(7) 43 | 44 | result = topview(root) 45 | print(result) 46 | -------------------------------------------------------------------------------- /Python/wifi scanner.py: -------------------------------------------------------------------------------- 1 | from scapy.all import * 2 | import os 3 | 4 | iface = "wlan0" 5 | 6 | def h_packet(packet): 7 | if packet.haslayer(Dot11ProbeReq) or packet.haslayer(Dot11ProbeResp) or packet.haslayer(Dot11AssoReq): 8 | print ("SSID identified") + packet.info 9 | 10 | os.system("iwconfig" + iface + "mode monitor") 11 | 12 | print ("Sniffing traffic on interface") + iface 13 | sniff(iface=iface, prn=h_packet) 14 | -------------------------------------------------------------------------------- /Rust/binary_search.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 3 | let target: i32 = 9; 4 | testing(&arr, &target); //Given array contains 10 elements and 9 found at index 10 5 | } 6 | 7 | 8 | fn testing(arr: &[i32], &target: &i32) { 9 | if let Some(result) = binary_search(&arr, &target) { 10 | println!("Given array contains {} elements and {} found at index {}", arr.len(), target, result); 11 | } else { 12 | println!("{} not found.", target); 13 | } 14 | } 15 | 16 | fn binary_search(arr: &[i32], target: &i32) -> Option { 17 | let mut first: usize = 0; 18 | let mut last: usize = arr.len() - 1; 19 | 20 | while first <= last { 21 | let midpoint = ((last - first) / 2) + first; 22 | let midpoint_index = midpoint as usize; 23 | let val = &arr[midpoint_index]; 24 | 25 | if val == target { 26 | return Some(midpoint_index+1); 27 | } 28 | 29 | if val < target { 30 | first = midpoint + 1; 31 | } 32 | 33 | if val > target { 34 | last = midpoint - 1; 35 | } 36 | } 37 | None 38 | } 39 | -------------------------------------------------------------------------------- /Rust/hello_world.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | -------------------------------------------------------------------------------- /Rust/rock_paper_scissors/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | rand= "0.8.5" 10 | -------------------------------------------------------------------------------- /Rust/selection_sort.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut vec:Vec = vec![3, 2, 5, 1, 4]; 3 | testing(&mut vec); //The sorted array is: [1, 2, 3, 4, 5] 4 | } 5 | 6 | 7 | fn testing(vec: &mut Vec) { 8 | println!("The sorted array is: {:?}", selection_sort(vec)); 9 | } 10 | 11 | fn selection_sort(vec: &mut Vec) -> Vec { 12 | let mut min: usize = 0; 13 | let mut temp: i32 = 0; 14 | for i in 0..vec.len() { 15 | min = i; 16 | for j in i+1..vec.len() { 17 | if vec[j] < vec[min] { 18 | min = j; 19 | } 20 | } 21 | temp = vec[i]; 22 | vec[i] = vec[min]; 23 | vec[min] = temp; 24 | } 25 | return vec.to_vec(); 26 | } 27 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | remote_theme: pages-themes/cayman@v0.2.0 2 | show_downloads: false 3 | title: CodesHub 4 | plugins: 5 | - jekyll-relative-links 6 | description: CodesHub is made to help students find codes in different languages easily for every usecase. 7 | author: Diwas Shrestha 8 | -------------------------------------------------------------------------------- /docs/CNAME: -------------------------------------------------------------------------------- 1 | codeshub.diwas.info.np -------------------------------------------------------------------------------- /typescript/palindrome.ts: -------------------------------------------------------------------------------- 1 | const checkPalindrome = (str: string): boolean => { 2 | let reverseStr: string = ""; 3 | for (let i = str.length - 1; i >= 0; i--) { 4 | reverseStr += str[i]; 5 | } 6 | if (str === reverseStr) { 7 | return true; 8 | } else { 9 | return false; 10 | } 11 | }; 12 | --------------------------------------------------------------------------------