├── README.md ├── Recursion_for_coding_interviews ├── .vscode │ ├── c_cpp_properties.json │ ├── launch.json │ └── settings.json ├── README.md ├── factorial.c ├── iterative_to_recursive.c ├── iterative_to_recursive2.c ├── problem_using_recursion │ ├── README.md │ ├── count_vowel_letters.c │ ├── decimal_to_binary.c │ ├── modulo_operation.c │ ├── nodeSum.c │ ├── palindrome.c │ ├── reverse_string.c │ ├── string_length.c │ └── sum_of_natural_number.c ├── tail_recursion_optimatization.c └── types_of_recursion │ ├── README.md │ ├── binary_recursion.c │ ├── head_recursion.c │ ├── indirect_recursion.c │ ├── nested_recursion.c │ └── tail_recursion.c ├── algorithms ├── .gitignore ├── README.md ├── dynamicProgramming │ ├── README.md │ ├── bottom-Up-Approach.c │ └── top-Down-approach.c ├── graphTraversal │ ├── BFS │ │ ├── BFS.c │ │ ├── README.md │ │ ├── makefile │ │ ├── queue.c │ │ └── queue.h │ ├── DFS │ │ ├── DFS.c │ │ ├── README.md │ │ ├── makefile │ │ ├── stack.c │ │ └── stack.h │ └── README.md ├── greedyAlgorithms │ ├── README.md │ ├── findContentChildren.py │ └── minimumCoinExchange.c ├── hashingAndCollision │ ├── README.md │ ├── linearProbing.c │ └── separateChaining.c ├── pascal_triangle.c ├── searchingAlgorithm │ ├── README.md │ ├── binarySearch.c │ ├── jumpSerach.c │ └── linearSearch.c └── sortingAlgorithms │ ├── README.md │ ├── bubbleSort.c │ ├── heapSort.c │ ├── insertionSort.c │ ├── mergeSort.c │ ├── quickSort.c │ └── selectionSort.c ├── c-programming ├── README.md ├── array │ ├── 1D_array.c │ ├── 2D_array.c │ ├── 2D_array_func.c │ └── arr_func.c ├── bit_field │ ├── bit.c │ └── bit_calc.c ├── bitwise_operator_in_c │ ├── a.exe │ ├── check_bit.c │ ├── clear_bit.c │ ├── get_nth_bit.c │ ├── is_even.c │ ├── least_significant_bit.c │ ├── most_significant_bit.c │ ├── set_bit.c │ ├── set_nth_bit.c │ ├── swap.c │ └── toggled_bit.c ├── decision_making_in_c │ ├── bmi.calc.c │ └── simple_calc.c ├── enum │ ├── enum.c │ └── enum_variable.c ├── loop_in_c │ ├── nested_loop.c │ ├── print_digit.c │ ├── sum_of_digit.c │ ├── sum_of_even_and_odd.c │ └── sum_of_odd_and_even_position.c ├── string │ ├── strcat.c │ ├── strchr.c │ ├── strcpy.c │ └── strlen.c ├── structure │ ├── array_struct.c │ ├── distance.c │ ├── struct.c │ ├── struct2.c │ ├── struct_func.c │ ├── struct_within_struct.c │ ├── student_data.c │ └── student_mark.c └── union │ ├── union.c │ └── union_struct.c ├── coding_interview_pattern ├── cyclic_sort │ ├── 0-main.c │ ├── 1-main.c │ ├── 2-main.c │ ├── README.md │ ├── cyclic_sort.c │ ├── cyclic_sort.h │ ├── duplicateNumber.c │ ├── missingNumber.c │ ├── print_array.c │ └── swap.c ├── fast_and_slow_pointers │ ├── a.out │ ├── fast_slow.h │ ├── findLoopStart.c │ ├── get_middle_node.c │ ├── happyNumber.c │ ├── hasLoop.c │ ├── loopLength.c │ ├── main.c │ ├── printNode.c │ └── startoftheloop.png ├── sliding_window │ ├── C │ │ ├── max_sub_array_sum.c │ │ ├── small_length_subarray_sum.c │ │ └── sub_array_sum.c │ ├── README.md │ └── python │ │ ├── maxSubArraySum.py │ │ ├── smallLengthSubarraySum.py │ │ └── subArraySum.py ├── tree_breadth_first-search │ └── printLevelOrder.cpp └── two_pointers │ ├── README.md │ ├── c │ ├── a.out │ ├── moveZeroes.c │ ├── pair_with_target_sum.c │ ├── remove_duplicate.c │ ├── remove_duplicates.c │ └── remove_element.c │ └── python │ ├── has_pair_with_target.py │ ├── moveZeroes.py │ ├── remove_duplicate.py │ ├── remove_duplicates.py │ └── remove_element.py ├── data_structure ├── README.md ├── array │ ├── README.md │ ├── insert_array_element.c │ ├── remove_array_element.c │ └── search_array_element.c ├── binary_heap │ ├── .vscode │ │ ├── c_cpp_properties.json │ │ ├── launch.json │ │ ├── settings.json │ │ └── tasks.json │ ├── README.md │ ├── a.out │ ├── heapify │ └── heapify.c ├── binary_search_tree │ ├── README.md │ ├── bts.h │ ├── delete_node.c │ ├── get_new_node.c │ ├── inorder.c │ ├── insert_node.c │ ├── main.c │ ├── makefile │ └── search_node.c ├── circular_linked_list │ ├── README.md │ ├── a.out │ ├── add_at_beginning.c │ ├── add_at_end.c │ ├── delete_node.c │ ├── lists.h │ ├── main.c │ ├── print_node.c │ ├── search_node.c │ └── test │ │ └── simple_version.c ├── doubly_linked_list │ ├── README.md │ ├── a.out │ ├── delete_node.c │ ├── insert_at_beginning.c │ ├── insert_at_end.c │ ├── lists.h │ ├── main.c │ ├── print_node.c │ ├── search_node.c │ └── test │ │ └── test.c ├── graph │ ├── .vscode │ │ ├── c_cpp_properties.json │ │ ├── launch.json │ │ └── settings.json │ ├── README.md │ ├── adjacentList.c │ ├── adjacentMatrixOfDirectedGraph.c │ └── adjacentMatrixOfUndirectedGraph.c ├── queue │ ├── README.md │ ├── array.c │ ├── linked_list.c │ └── linked_list2.c ├── singly_linked_list │ ├── README.md │ ├── delete_node.c │ ├── insert_at_beginning.c │ ├── insert_at_end.c │ ├── lists.h │ ├── main.c │ ├── print_node.c │ ├── search_node.c │ ├── simple_version.c │ └── singly_linked_list.c └── stack │ ├── README.md │ ├── array.c │ └── linked_list.c ├── java ├── .idea │ ├── .gitignore │ ├── misc.xml │ ├── modules.xml │ └── vcs.xml ├── creatingClass │ ├── Dog.class │ ├── Dog.java │ ├── Person.class │ ├── Person.java │ ├── Student.java │ ├── personTester.class │ └── personTester.java ├── javaDocProject │ ├── javaDocProject.iml │ └── src │ │ └── code │ │ ├── javaDocExample.class │ │ └── javaDocExample.java ├── javaExcercise │ ├── ArrayExcercise.class │ ├── ArrayExcercise.java │ ├── FindTheArea.class │ ├── FindTheArea.java │ ├── HelloWorld.class │ ├── HelloWorld.java │ ├── MethodExcercise.class │ ├── MethodExcercise.java │ ├── ServiceTest.class │ └── ServiceTest.java ├── lesson3 │ └── src │ │ ├── abstractClass │ │ ├── Car.java │ │ ├── Vehicle.java │ │ └── VehicleTester.java │ │ ├── inheritanceExample │ │ ├── Person.java │ │ ├── PersonTester.java │ │ ├── Student.java │ │ └── StudentEmployee.java │ │ ├── interfaceExample │ │ ├── Car.java │ │ └── Vehicle.java │ │ └── polymorphismExample │ │ ├── Boat.java │ │ ├── Car.java │ │ ├── Plane.java │ │ ├── Vehicle.java │ │ └── VehicleTester.java ├── lesson4 │ └── src │ │ ├── DateAndCalender │ │ ├── DatesAndCalenders.java │ │ ├── Transaction.java │ │ └── TransactionTester.java │ │ ├── RegexExample │ │ ├── Person.java │ │ ├── PersonTester.java │ │ └── regEXTester.java │ │ ├── ScannerExample │ │ └── UserInputTester.java │ │ ├── StringMethods │ │ └── StringMethods.java │ │ ├── UserInput │ │ └── UserInput.java │ │ ├── enumerations │ │ ├── Light.java │ │ ├── LightChanger.java │ │ └── StopLight.java │ │ ├── exceptionExample │ │ ├── Phone.java │ │ └── PhoneExceptionTester.java │ │ └── scanner │ │ ├── ScannerMethods.java │ │ └── SnackSurvey.java ├── lesson5 │ └── src │ │ ├── CollectionsExample │ │ └── CollectionExcercise.java │ │ ├── GenericExample │ │ └── GenericExcercise.java │ │ ├── IteratorExample │ │ └── IteratorExample.java │ │ └── SortingCollection │ │ ├── Person.java │ │ ├── PersonSort.java │ │ ├── StringSorting.java │ │ └── WrapperSorting.java └── lesson6 │ └── src │ ├── mapsExample │ ├── MapsExcercise.java │ └── Person.java │ ├── overridingHashAndEqual │ └── People.java │ ├── queueExample │ └── queueExcercise.java │ └── setsExample │ └── SetExcercise.java ├── mysql_practice ├── alias.sql ├── clause.sql ├── crossjoin.sql ├── crossjoin_init.sql ├── distinct.sql ├── earthquake.csv ├── init_join.sql ├── join.sql ├── mysqlsampledatabase.sql ├── operators.sql ├── order_by.sql ├── select.sql └── where.sql ├── pointer_in_c ├── README.md ├── array_of_pointer.c ├── file_pointer │ ├── file_copy.c │ ├── read_char.c │ ├── read_string.c │ ├── write_char.c │ └── write_str.c ├── function_pointer │ ├── a.out │ ├── func_pointer.c │ ├── func_pointer_with_typdef.c │ ├── qsort_comparator.1.c │ ├── qsort_comparator.c │ ├── qsort_comparator2.c │ ├── simple_calculator.c │ ├── simple_calculator2.c │ └── swap.c ├── null_pointer.c ├── pointer.c ├── pointer_comparison1.c ├── pointer_to_array.c ├── string_and_pointer │ ├── a.out │ ├── array_of_pointer_str.c │ ├── print_str.c │ ├── str_pointer.c │ └── str_pointer2.c └── struct_and_pointer │ ├── a.out │ ├── struct_pointer.c │ ├── struct_pointer_func.c │ └── student_struct.c ├── problem_solving_for_beginners ├── Number_Problems │ ├── README.md │ ├── c_programming │ │ ├── a.out │ │ ├── absolute_number.c │ │ ├── armstrong_number.c │ │ ├── even_odd.c │ │ ├── factor_of_number.c │ │ ├── fibonnaci.c │ │ ├── palindrome_number.c │ │ ├── perfect_number.c │ │ ├── positive_negative.c │ │ ├── prime_number.c │ │ ├── reverse_number.c │ │ ├── split_number.c │ │ ├── sum_of_digit.c │ │ ├── sum_of_natural_number.c │ │ ├── sum_of_natural_number2.c │ │ └── swap.c │ └── python │ │ ├── absolute_number.py │ │ ├── armstrong_number.py │ │ ├── even_odd.py │ │ ├── factor_of_number.py │ │ ├── fibonnaci.py │ │ ├── palindrome_number.py │ │ ├── perfect_number.py │ │ ├── positive_negative.c │ │ ├── positive_negative.py │ │ ├── prime_number.py │ │ ├── reverse_number.py │ │ ├── split_number.py │ │ ├── sum_of_digit.py │ │ ├── sum_of_natural_number.py │ │ ├── sum_of_natural_number2.py │ │ └── swap.py ├── README.md ├── array_problems │ ├── README.md │ ├── c-programming │ │ ├── count_even_and_odd_number.c │ │ ├── find_max.c │ │ ├── min_and_max.c │ │ ├── reverse_array.c │ │ ├── search_element.c │ │ ├── sum_of_2darray.c │ │ └── sum_of_array.c │ └── python │ │ ├── count_even_and_odd_number.py │ │ ├── find_max.py │ │ ├── min_and_max.py │ │ ├── reverse_array.py │ │ ├── search_element.py │ │ ├── sum_of_2Darray.py │ │ └── sum_of_array.py ├── bitwise │ ├── README.md │ ├── c-programming │ │ ├── README.md │ │ ├── check_nth_bit.c │ │ ├── disable_nth_bit.c │ │ ├── enable_nth_bit.c │ │ ├── even_or_odd.c │ │ ├── find_equal.c │ │ ├── odd_occuring_number_in_array.c │ │ ├── swap.c │ │ └── toggle_nth_bit.c │ └── python │ │ ├── README.md │ │ ├── check_nth_bit.py │ │ ├── disable_nth_bit.py │ │ ├── enable_nth_bit.py │ │ ├── even_or_odd.py │ │ ├── find_equal.py │ │ ├── odd_occuring_number.py │ │ ├── swap.py │ │ └── toggle_nth_bit.py ├── patterns │ ├── README.md │ ├── c-programming │ │ ├── diagonal.c │ │ ├── hallow_box.c │ │ ├── print_box.c │ │ └── pyramid.c │ └── python │ │ ├── diagonal.py │ │ ├── hallow_box.py │ │ ├── print_box.py │ │ └── pyramid.py └── string_problems │ ├── README.md │ ├── c-programming │ ├── a.out │ ├── change_case.c │ ├── count_vowel.c │ ├── palindrome.c │ ├── reverse_str.c │ ├── str_comp.c │ ├── str_concat.c │ ├── strcpy.c │ └── strlen.c │ └── python │ ├── change_case.py │ ├── count_vowel.py │ ├── len.py │ ├── palindrome.py │ ├── rev_str.py │ ├── str_comp.py │ ├── str_concat.py │ └── strcpy.py ├── problem_solving_with_DSA ├── .vscode │ ├── settings.json │ └── tasks.json ├── BTS │ └── C │ │ ├── BST.c │ │ ├── BST.h │ │ ├── doubleTree.c │ │ ├── identicalTree.c │ │ ├── isBST.c │ │ ├── maxDepth.c │ │ ├── minValueInBTS.c │ │ ├── mirror.c │ │ └── sizeOfBST.c ├── README.md ├── array │ ├── C │ │ ├── arrayRotate.c │ │ ├── removeDuplicate.c │ │ ├── reverse.c │ │ └── reverseWords.c │ ├── README.md │ └── python │ │ ├── arrayRotate.py │ │ ├── removeDuplicate.py │ │ ├── reverse.py │ │ └── reverseWords.py ├── hashing │ ├── README.md │ ├── checkForSubArrayWithSumZero.c │ ├── checkforSubArray.c │ ├── disjoint.c │ ├── hash.h │ └── linearProbing.c ├── linkedList │ ├── C │ │ ├── PrintInReverse.c │ │ ├── a.out │ │ ├── getMiddleNode.c │ │ ├── getNthFromLast.c │ │ ├── getNthNode.c │ │ └── hasLoop.c │ └── python │ │ ├── getMiddleNode.py │ │ ├── getNthNode.py │ │ └── printInReverse.py └── stack │ ├── C │ ├── implement-two-stack-in-single-array-method1.c │ ├── implement-two-stack-in-single-array-method2.c │ ├── postFixEval.c │ ├── stack.c │ ├── stack.h │ ├── stringRev.c │ └── validParentheses.c │ └── Python │ ├── implement-two-stack-in-single-array-method1.py │ ├── implement-two-stack-in-single-array-method2.py │ ├── postFixEval.py │ ├── stringRev.py │ └── validParentheses.py └── python ├── README.md ├── class ├── class_and_instance_variable.py ├── constructor.py ├── data.py ├── inheritance.py ├── inheritance2.py ├── multilevel_inheritance.py ├── multiple_inheritance.py ├── number.py └── person.py ├── decorators ├── calculation.py └── decorator1.py ├── dictionary ├── app.py └── modify_dic.py ├── error_and_exceptions ├── error_handling.py ├── finally.py ├── new.txt └── try_and_except.py ├── file ├── __pycache__ │ └── copy.cpython-39.pyc ├── append.py ├── copy.py ├── copy.txt ├── delete.py ├── file.txt ├── new.txt ├── read.py └── write.py ├── function ├── arbitrage_args.py ├── check.py ├── display_date.py ├── factorial.py └── lambda.py ├── if_statmenet ├── app.py ├── app2.c └── if_else.py ├── list └── list.py ├── loop ├── break.py ├── check_vowel.py ├── continue.py ├── else_loop.py ├── loop.py ├── nested_loop.py ├── pass.py ├── pattern.py ├── print_digit.py ├── print_sum.py ├── range.py └── while.py ├── module ├── module.py └── util.py ├── set ├── diff_symmetric_diff.py ├── find_vowel.py ├── set.py ├── set_union.py └── subset_superset_disjoin.py ├── string ├── slice_str.py ├── str.py ├── str_method.py └── strlen.py └── tuple └── tuple.py /README.md: -------------------------------------------------------------------------------- 1 | # Log2Base2 2 | **Log2Base2** is a visual learning platform to learn `programming`, `data structures` & `algorithms`, 3 | `Coding Interview Patterns`, `Recursion`, and `Dynamic Programming`. 4 | 5 | ## Programming Languages 6 | - C programming language 7 | - Python 8 | - Java 9 | 10 | 11 | ## Pointer in C programming language 12 | - Array and Pointer 13 | - String and Pointer 14 | - Function Pointer 15 | - Struct and Pointer 16 | - File Pointer 17 | 18 | 19 | ## Problem solving for beginners 20 | - Number problems 21 | - Array problems 22 | - String problems 23 | - Patterns 24 | - Bitwise 25 | 26 | 27 | ## Data structure & Algorithms 28 | - Data structure & Algorithms in C 29 | - Data structure & Algorithms in Python 30 | - Data structure & Algorithms in Java 31 | 32 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "linux-gcc-x64", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "compilerPath": "/usr/bin/gcc", 9 | "cStandard": "${default}", 10 | "cppStandard": "${default}", 11 | "intelliSenseMode": "linux-gcc-x64", 12 | "compilerArgs": [ 13 | "" 14 | ] 15 | } 16 | ], 17 | "version": 4 18 | } -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "C/C++ Runner: Debug Session", 6 | "type": "cppdbg", 7 | "request": "launch", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "externalConsole": false, 11 | "cwd": "/home/belovedtech/beloved/Log2Base2/Recursion_for_coding_interviews", 12 | "program": "/home/belovedtech/beloved/Log2Base2/Recursion_for_coding_interviews/build/Debug/outDebug", 13 | "MIMode": "gdb", 14 | "miDebuggerPath": "gdb", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | } 21 | ] 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp_Runner.cCompilerPath": "gcc", 3 | "C_Cpp_Runner.cppCompilerPath": "g++", 4 | "C_Cpp_Runner.debuggerPath": "gdb", 5 | "C_Cpp_Runner.cStandard": "", 6 | "C_Cpp_Runner.cppStandard": "", 7 | "C_Cpp_Runner.msvcBatchPath": "", 8 | "C_Cpp_Runner.useMsvc": false, 9 | "C_Cpp_Runner.warnings": [ 10 | "-Wall", 11 | "-Wextra", 12 | "-Wpedantic" 13 | ], 14 | "C_Cpp_Runner.enableWarnings": true, 15 | "C_Cpp_Runner.warningsAsError": false, 16 | "C_Cpp_Runner.compilerArgs": [], 17 | "C_Cpp_Runner.linkerArgs": [], 18 | "C_Cpp_Runner.includePaths": [], 19 | "C_Cpp_Runner.includeSearch": [ 20 | "*", 21 | "**/*" 22 | ], 23 | "C_Cpp_Runner.excludeSearch": [ 24 | "**/build", 25 | "**/build/**", 26 | "**/.*", 27 | "**/.*/**", 28 | "**/.vscode", 29 | "**/.vscode/**" 30 | ] 31 | } -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/README.md: -------------------------------------------------------------------------------- 1 | # Recursion for coding interview 2 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * factorial - find a factorial of a number 5 | * @num: number to find its factorial 6 | * return: factorial of the number 7 | */ 8 | 9 | int factorial(int num) 10 | { 11 | if (num <= 1) 12 | return (1); 13 | return num * factorial(num - 1); 14 | } 15 | 16 | /** 17 | * main - Entry level 18 | * return: 0 always (success) 19 | */ 20 | int main(void) 21 | { 22 | int n, ans; 23 | 24 | printf("Enter a number "); 25 | scanf("%d", &n); 26 | 27 | if (n > 0) 28 | { 29 | ans = factorial(n); 30 | printf("%d\n", ans); 31 | } 32 | 33 | return (0); 34 | } -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/iterative_to_recursive.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * iterative- print number up to 1 using iteration 5 | * @num: number to print 6 | */ 7 | 8 | void iterative(int num) 9 | { 10 | while (num > 0) 11 | { 12 | printf("%d\n", num); 13 | num--; 14 | } 15 | } 16 | 17 | /** 18 | * recurcive- print number up to 1 using recursion 19 | * @num: number to print 20 | */ 21 | void recurcive(int num) 22 | { 23 | // if (num == 0) 24 | if (num < 1) 25 | return; 26 | 27 | printf("%d\n", num); 28 | recurcive(--num); 29 | } 30 | 31 | /** 32 | * main - Entry level 33 | * return: 0 always (success) 34 | */ 35 | int main(void) 36 | { 37 | 38 | iterative(5); 39 | printf("________________\n"); 40 | recurcive(5); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/problem_using_recursion/README.md: -------------------------------------------------------------------------------- 1 | # Problem solving using Recursion 2 | 3 | - Count Vowel letter 4 | - Decimal to binary 5 | - Modulo operator 6 | - Sum of a singly linked list 7 | - Palindrome 8 | - Reverse string 9 | - Length of a string 10 | - Sum of natural number 11 | - 12 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/problem_using_recursion/count_vowel_letters.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int isVowel(char ch) 6 | { 7 | if (ch == 'a' || ch == 'A' || 8 | ch == 'e' || ch == 'E' || 9 | ch == 'i' || ch == 'I' || 10 | ch == 'o' || ch == 'O' || 11 | ch == 'u' || ch == 'U') { 12 | return 1; 13 | } 14 | return 0; 15 | } 16 | int countVowels(char str[], int n) 17 | { 18 | if (n < 0) 19 | return 0; 20 | 21 | if (isVowel(str[n])) 22 | return countVowels(str, n-1) + 1; 23 | else 24 | return countVowels(str, n-1) + 0; 25 | } 26 | 27 | int main(int ac, char **av) 28 | { 29 | if (ac > 1) 30 | { 31 | int res = countVowels(av[1], strlen(av[1])); 32 | 33 | printf("%d\n", res); 34 | } 35 | 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/problem_using_recursion/decimal_to_binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void decimalToBinary(int num) 4 | { 5 | if (num <= 0) 6 | return; 7 | int rem = num % 2; 8 | decimalToBinary(num/2); 9 | printf("%d", rem); 10 | } 11 | 12 | int main(void) 13 | { 14 | int num; 15 | scanf("%d", &num); 16 | 17 | decimalToBinary(num); 18 | printf("\n"); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/problem_using_recursion/modulo_operation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * mod - Find modulo 5 | * 6 | * @num: Numerator 7 | * @den: Denomerator 8 | * Return: The modulo 9 | */ 10 | int mod(int num, int den) 11 | { 12 | if (num < den) 13 | return num; 14 | return mod(num-den, den); 15 | } 16 | 17 | 18 | 19 | int main(void) 20 | { 21 | int num, den; 22 | scanf("%d %d", &num, &den); 23 | 24 | printf("%d\n", mod(num, den)); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/problem_using_recursion/reverse_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void reverse(char str[], int i, int n) 6 | { 7 | if (i == n/2) 8 | return; 9 | char temp = str[i]; 10 | str[i] = str[n-1-i]; 11 | str[n-1-i] = temp; 12 | reverse(str, i+1, n); 13 | } 14 | 15 | 16 | 17 | int main(int ac, char **av) 18 | { 19 | if (ac > 1) 20 | { 21 | printf("%s\n", av[1]); 22 | reverse(av[1], 0, strlen(av[1])); 23 | printf("%s\n", av[1]); 24 | } 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/problem_using_recursion/sum_of_natural_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int sum(int n) 4 | { 5 | if (n <= 1) 6 | return 1; 7 | return n + sum(n-1); 8 | } 9 | 10 | int main(void) 11 | { 12 | int num; 13 | scanf("%d", &num); 14 | 15 | printf("%d\n", sum(num)); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/types_of_recursion/README.md: -------------------------------------------------------------------------------- 1 | # Types of Recursion 2 | 3 | - Tail Recursion 4 | 5 | - Head Recursion 6 | 7 | - Nested Recursion 8 | 9 | - Binary Recursion 10 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/types_of_recursion/binary_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * fib - fibonacci series 5 | * @num: positive integer 6 | * 7 | * Return: fibonacci of nth term 8 | */ 9 | int fib(int n) 10 | { 11 | if (n <= 1) 12 | return n; 13 | 14 | return fib(n-2) + fib(n-1); 15 | } 16 | 17 | 18 | int main(void) 19 | { 20 | int num = 8; 21 | 22 | printf("%d\n", fib(num)); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/types_of_recursion/head_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * head_recursion - print string 5 | * 6 | * @str: String to print 7 | */ 8 | void head_recursion(char *str) 9 | { 10 | if (*str == '\0') 11 | return; 12 | 13 | head_recursion(str+1); 14 | printf("%c ", *str); 15 | } 16 | 17 | 18 | int main(void) 19 | { 20 | char *str = "Head Recursion!"; 21 | 22 | head_recursion(str); 23 | printf("\n"); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/types_of_recursion/indirect_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void even(); 4 | void odd(); 5 | 6 | int num = 1; 7 | 8 | void even() 9 | { 10 | if (num == 10) 11 | return; 12 | 13 | printf(" ODD: %d\n", num); 14 | num++; 15 | odd(); 16 | 17 | } 18 | 19 | void odd() 20 | { 21 | if (num == 10) 22 | return; 23 | 24 | printf("EVEN: %d\n", num); 25 | num++; 26 | even(); 27 | } 28 | 29 | 30 | int main(void) 31 | { 32 | even(); 33 | 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/types_of_recursion/nested_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * fun - nested recursion 5 | * @num: positive integer 6 | * 7 | * Return: positive integer 8 | */ 9 | int fun(int num) 10 | { 11 | if (num > 10) 12 | return num -1; 13 | 14 | return fun(fun(num + 2)); 15 | } 16 | 17 | 18 | int main(void) 19 | { 20 | int num = 6; 21 | 22 | printf("%d\n", fun(num)); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /Recursion_for_coding_interviews/types_of_recursion/tail_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * tail_recursion - print string 5 | * 6 | * @str: String to print 7 | */ 8 | void tail_recursion(char *str) 9 | { 10 | if (*str == '\0') 11 | return; 12 | 13 | printf("%c ", *str); 14 | tail_recursion(str+1); 15 | } 16 | 17 | 18 | int main(void) 19 | { 20 | char *str = "Tail Recursion!"; 21 | 22 | tail_recursion(str); 23 | printf("\n"); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /algorithms/.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ -------------------------------------------------------------------------------- /algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Agorithms 2 | 3 | - Searching Algorithms 4 | - Sorting Algorithms 5 | - Hashing and Collision 6 | - Greedy Algorithms 7 | - Dynamic Programming 8 | - Graph Traversal 9 | -------------------------------------------------------------------------------- /algorithms/dynamicProgramming/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic programming 2 | 3 | Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of subproblems, so that we do not have to re-compute them when needed later. This simple optimization reduces time complexities from exponential to polynomial. 4 | 5 | ## Approaches of Dynamic programming 6 | 7 | - Bottom-Up Approach - Solving problem step by step from base case 8 | 9 | - Top-Down Approach - Breaking Big problem into smaller ones recursively 10 | 11 | ##### Memoization - Is the effective way of avoiding recomputation issue in Top-Down approach. It is a technique used for storing all the solved sub-problems so that we do ot need to recalculate the soli=ution of the already solved subproblems. 12 | -------------------------------------------------------------------------------- /algorithms/dynamicProgramming/bottom-Up-Approach.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * fibonacci - nth fibonacci using Bottom-Up approach 5 | * 6 | * @N: Nth of fibonacci 7 | * Return: the result 8 | */ 9 | int fibonacci(int N) 10 | { 11 | int fib[N]; 12 | 13 | fib[0] = 0; 14 | fib[1] = 1; 15 | 16 | for (int i = 2; i <= N; i++) 17 | fib[i] = fib[i - 2] + fib[i - 1]; 18 | 19 | return fib[N]; 20 | } 21 | 22 | int main(void) 23 | { 24 | int n; 25 | 26 | printf("Enter number of fibonacci\n"); 27 | scanf("%d", &n); 28 | 29 | if (n <= 1) 30 | printf("Fib[%d] = %d\n", n, n); 31 | else 32 | printf("Fib[%d] = %d\n", n, fibonacci(n)); 33 | 34 | return (0); 35 | } -------------------------------------------------------------------------------- /algorithms/graphTraversal/BFS/README.md: -------------------------------------------------------------------------------- 1 | # BFS 2 | 3 | Breadth First Search graph traversal - it uses Queue data structure to ensure that no node is visited more than once. 4 | -------------------------------------------------------------------------------- /algorithms/graphTraversal/BFS/makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean oclean fclean re 2 | 3 | CC=gcc 4 | SRC=queue.c BFS.c 5 | OBJ=$(SRC:%.c=%.o) 6 | NAME=BFS 7 | RM=rm -f 8 | CFLAGS= -Wall -Werror -Wextra 9 | 10 | all: $(OBJ) 11 | $(CC) $(CFLAGS) $(SRC) -o $(NAME) 12 | 13 | clean: 14 | $(RM) *~ $(NAME) 15 | 16 | oclean: 17 | $(RM) $(OBJ) 18 | 19 | fclean: clean oclean 20 | 21 | re: fclean all -------------------------------------------------------------------------------- /algorithms/graphTraversal/BFS/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef _QUEUE_H 2 | #define _QUEUE_H 3 | 4 | #include 5 | 6 | int isQueueEmpty(); 7 | int isQueueFull(); 8 | void enqueue(int value); 9 | int dequeue(); 10 | 11 | #endif /*_QUEUE_H*/ -------------------------------------------------------------------------------- /algorithms/graphTraversal/DFS/README.md: -------------------------------------------------------------------------------- 1 | # DFS 2 | 3 | Depth First Search graph traversal - it uses Stack data structure to ensure that no node is visited more than once. 4 | -------------------------------------------------------------------------------- /algorithms/graphTraversal/DFS/makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean oclean fclean re 2 | 3 | CC=gcc 4 | SRC=stack.c DFS.c 5 | OBJ=$(SRC:%.c=%.o) 6 | NAME=DFS 7 | CFLAGS= -Wall -Wextra -Werror 8 | RM=rm -f 9 | 10 | all: $(OBJ) 11 | $(CC) $(CFLAGS) $(SRC) -o $(NAME) 12 | 13 | clean: 14 | $(RM) *~ $(NAME) 15 | 16 | oclean: 17 | $(RM) $(OBJ) 18 | 19 | fclean: clean oclean 20 | 21 | re: fclean all -------------------------------------------------------------------------------- /algorithms/graphTraversal/DFS/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _STACK_H 2 | #define _STACK_H 3 | 4 | #include 5 | int isStackEmpty(); 6 | int isStackFull(); 7 | void push(int value); 8 | int pop(); 9 | 10 | #endif /*_STACK_H*/ -------------------------------------------------------------------------------- /algorithms/graphTraversal/README.md: -------------------------------------------------------------------------------- 1 | # Graph Traversal 2 | 3 | #### BFS 4 | 5 | Breadth First Search (BFS) - Uses queue data structure 6 | 7 | #### DFS 8 | 9 | Depth First Search (DFS) - Uses stack data structure 10 | -------------------------------------------------------------------------------- /algorithms/greedyAlgorithms/README.md: -------------------------------------------------------------------------------- 1 | # Greedy Algorithm 2 | -------------------------------------------------------------------------------- /algorithms/hashingAndCollision/README.md: -------------------------------------------------------------------------------- 1 | # hashing And Collision 2 | -------------------------------------------------------------------------------- /algorithms/pascal_triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void pascalTriangle(int num) 5 | { 6 | int i, j, c; 7 | 8 | for (i = 1; i < num+1; i++) 9 | { 10 | for (j = 0; j < num - i; j++) 11 | printf(" "); 12 | 13 | c = 1; 14 | 15 | for (j = 1; j < i + 1; j++) 16 | { 17 | printf("%d ", c); 18 | c = c * (i - j) / j; 19 | 20 | } 21 | printf("\n"); 22 | } 23 | } 24 | 25 | 26 | int main(int ac, char **av) 27 | { 28 | 29 | if (ac > 1) 30 | { 31 | int num = atoi(av[1]); 32 | pascalTriangle(num); 33 | } 34 | 35 | 36 | return (0); 37 | } -------------------------------------------------------------------------------- /algorithms/searchingAlgorithm/README.md: -------------------------------------------------------------------------------- 1 | # Searching Algorithms 2 | 3 | - Linear Search 4 | - Binary Search 5 | -------------------------------------------------------------------------------- /algorithms/searchingAlgorithm/linearSearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SIZE 5 4 | 5 | /** 6 | * linearSearch - Search for a key in array element 7 | * 8 | * @arr: Array of integer 9 | * @key: Key to search for 10 | * @size: SIze of the array 11 | * return: If found return (1) otherwise (0) 12 | */ 13 | 14 | int linearSearch(int arr[], int key, int size) 15 | { 16 | int i; 17 | 18 | for (int i = 0; i < size; i++) 19 | { 20 | if (arr[i] == key) 21 | return (1); 22 | } 23 | return (0); 24 | } 25 | 26 | 27 | int main(void) 28 | { 29 | int bookPage[SIZE] = {20, 30, 50, 70, 50}; 30 | 31 | int key = 1; 32 | 33 | if (linearSearch(bookPage, key, SIZE)) 34 | printf("Key found\n"); 35 | else 36 | printf("Key not found\n"); 37 | 38 | return (0); 39 | } -------------------------------------------------------------------------------- /c-programming/README.md: -------------------------------------------------------------------------------- 1 | # C- programming language 2 | -------------------------------------------------------------------------------- /c-programming/array/1D_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - creates and prints element of 1D array using for loop 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int size, i; 12 | 13 | printf("Enter size of array: "); 14 | scanf("%d", &size); 15 | 16 | int arr[size]; 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | printf("Enter Element of array of size %d: ", size); 21 | scanf("%d", &arr[i]); 22 | } 23 | 24 | for (i = 0; i < size; i++) 25 | printf("%d ", arr[i]); 26 | 27 | printf("\n"); 28 | return (0); 29 | } -------------------------------------------------------------------------------- /c-programming/array/2D_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ROW 3 4 | #define COL 3 5 | 6 | /** 7 | * main - creates and prints element of 2D array using for loop 8 | * 9 | * Return: 0 always (success) 10 | */ 11 | 12 | int main(void) 13 | { 14 | int arr[ROW][COL], i, j; 15 | 16 | for (i = 0; i < ROW; i++) 17 | { 18 | for (j = 0; j < COL; j++) 19 | { 20 | printf("Enter the element of arr[%d][%d]: ", ROW, COL); 21 | scanf("%d", &arr[i][j]); 22 | } 23 | } 24 | 25 | for (i = 0; i < ROW; i++) 26 | for (j = 0; j < COL; j++) 27 | printf("arr[%d][%d] = %d\n", i, j, arr[i][j]); 28 | 29 | printf("\n"); 30 | return (0); 31 | } -------------------------------------------------------------------------------- /c-programming/array/2D_array_func.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ROW 2 4 | #define COL 2 5 | 6 | /** 7 | * display_array - displays element of 2D array 8 | * @arr: 2D array to display 9 | */ 10 | 11 | void display_array(int arr[][COL]) 12 | { 13 | int i, j; 14 | 15 | for (i = 0; i < ROW; i++) 16 | for (j = 0; j < COL; j++) 17 | printf("%d ", arr[i][j]); 18 | } 19 | 20 | int main(void) 21 | { 22 | int arr[ROW][COL] = {20, 50, 30, 23}; 23 | 24 | display_array(arr); 25 | return (0); 26 | } -------------------------------------------------------------------------------- /c-programming/array/arr_func.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * display_array - displays element of 1D array 4 | * @arr: 2D array to display 5 | */ 6 | 7 | void display_array(int arr[], int size) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < size; i++) 12 | printf("%d \n", arr[i]); 13 | 14 | } 15 | 16 | int main(void) 17 | { 18 | int arr[5] = {20, 50, 30, 23, 50}; 19 | 20 | display_array(arr, 5); 21 | return (0); 22 | } -------------------------------------------------------------------------------- /c-programming/bit_field/bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * struct status - sets bit of two integers 4 | * @ON: 1 bit 5 | * @OFF: 1 bit 6 | */ 7 | struct status 8 | { 9 | unsigned int ON : 1; 10 | unsigned int OFF : 1; 11 | }; 12 | 13 | /** 14 | * main - prints size of struct status 15 | * 16 | * * Return: 0 always (success) 17 | */ 18 | 19 | int main(void) 20 | { 21 | struct status s; 22 | 23 | s.ON = 1; 24 | s.OFF = 0; 25 | 26 | printf("%d\n",s.OFF); 27 | printf("%d\n",s.ON); 28 | printf("%ld\n", sizeof(struct status)); 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /c-programming/bit_field/bit_calc.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * struct date - sets bit of date of the year 4 | * @day: day of week 5 | * @month: month of the year 6 | * @year: the year 7 | */ 8 | struct date 9 | { 10 | unsigned int day : 5; 11 | unsigned int month : 4; 12 | unsigned int year : 23; 13 | }; 14 | 15 | 16 | /** 17 | * main - prints size of struct date 18 | * 19 | * * Return: 0 always (success) 20 | */ 21 | 22 | int main(void) 23 | { 24 | struct date d; 25 | 26 | d.day = 2; 27 | d.month = 11; 28 | d.year = 1996; 29 | 30 | printf("%d %d %d\n", d.day, d.month, d.year); 31 | printf("%ld\n", sizeof(struct date)); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/c-programming/bitwise_operator_in_c/a.exe -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/check_bit.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | /** 5 | * main - checks whether the 2nd bit is still on. 6 | * 7 | * Return: if 2nd bit is on print "Yes" otherwise print "No" 8 | */ 9 | 10 | 11 | int main(void) 12 | { 13 | int status, mask, output; 14 | 15 | status = mask = output = 0; 16 | status |= 2; //enabling 2nd bit 17 | 18 | //Write your logic here 19 | mask = 7; 20 | output = status & mask; 21 | 22 | if (output == 2) 23 | printf("Yes\n"); 24 | else 25 | printf("No\n"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/clear_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - unsets nth bit of a given number using Bitwise 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int num, n, newNum; 10 | 11 | printf("Enter a positive integer: "); 12 | scanf("%d", &num); 13 | 14 | /* Input bit position you want to check */ 15 | printf("Enter nth bit to check (0-31): "); 16 | scanf("%d", &n); 17 | 18 | /** 19 | * Left shifts 1 to n times 20 | * Perform complement of above 21 | * finally perform bitwise AND with num and result of above 22 | */ 23 | newNum = num & (~ (1 << n)); 24 | 25 | printf("Bit cleared successfully.\n\n"); 26 | printf("Number before setting %d bit: %d (in decimal)\n", n, num); 27 | printf("Number after setting %d bit: %d (in decimal)\n", n, newNum); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/get_nth_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - gets nth bit of a given number using Bitwise 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int num, n, bitStatus; 12 | 13 | printf("Enter a positive integer: "); 14 | scanf("%d", &num); 15 | 16 | /* Input bit position you want to check */ 17 | printf("Enter nth bit to check (0-31): "); 18 | scanf("%d", &n); 19 | 20 | /* Right shift num, n times and perform bitwise AND with 1 */ 21 | bitStatus = (num >> n) & 1; 22 | 23 | printf("The %d bit is set to %d", n, bitStatus); 24 | 25 | return (0); 26 | } -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/least_significant_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - find least signficant bit of a number using bitwise 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int num; 12 | int mask = 1; 13 | 14 | printf("Enter a positive integer: "); 15 | scanf("%d", &num); 16 | 17 | /*if (num & 1) evaluates to 1 */ 18 | if (num & mask) 19 | printf("LSB of %d is set to (1).\n", num); 20 | else 21 | printf("LSB of %d is set to (0).\n", num); 22 | 23 | return(0); 24 | } -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/most_significant_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define BITS sizeof(int) * 8 // Total bits required to represent integer 4 | 5 | /** 6 | * main - find most signficant bit of a number using bitwise 7 | * 8 | * Return: 0 always (success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int num, msb; 14 | 15 | printf("Enter a positive integer: "); 16 | scanf("%d", &num); 17 | 18 | /* Move first bit of 1 to highest order */ 19 | msb = 1 << (BITS - 1); 20 | 21 | /* Perform bitwise AND with msb and num */ 22 | if (num & msb == 1) 23 | printf("MSB of %d is (1). \n", num); 24 | else 25 | printf("MSB of %d is (0). \n", num); 26 | 27 | return (0); 28 | } -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/set_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - enable the second bit of the 3 bits 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int status, mask, output; 12 | 13 | status = 0; 14 | mask = 2; 15 | 16 | //enable 2bit here 17 | output = status | mask; 18 | 19 | printf("%d", output); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/set_nth_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - sets nth bit of a given number using Bitwise 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | unsigned int num, n, newNum; 10 | 11 | printf("Enter a positive integer: "); 12 | scanf("%d", &num); 13 | 14 | /* Input bit position you want to check */ 15 | printf("Enter nth bit to check (0-31): "); 16 | scanf("%d", &n); 17 | 18 | /* Left shift 1, n times and perform bitwise OR with num */ 19 | if (n >= 0 && n <= 31) 20 | { 21 | newNum = (1 << n) | num; 22 | printf("Bit set successfully.\n\n"); 23 | printf("Number before setting %d bit: %d (in decimal)\n", n, num); 24 | printf("Number after setting %d bit: %d (in decimal)\n", n, newNum); 25 | } 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - swap two integer 4 | * Return: 0 always (success) 5 | */ 6 | 7 | int main(void) 8 | { 9 | int a, b, tmp; 10 | 11 | printf("Enter two positive integers: "); 12 | scanf("%d %d", &a, &b); 13 | 14 | printf("Before swap a = %d b = %d\n", a, b); 15 | tmp = a ^ b; 16 | a = tmp ^ a; 17 | b = tmp ^ b; 18 | printf("After swap a = %d b = %d\n", a, b); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /c-programming/bitwise_operator_in_c/toggled_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - toggles nth bit of a given number using Bitwise 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int num, n, newNum; 10 | 11 | printf("Enter a positive integer: "); 12 | scanf("%d", &num); 13 | 14 | /* Input bit position you want to check */ 15 | printf("Enter nth bit to check (0-31): "); 16 | scanf("%d", &n); 17 | 18 | /** 19 | * Left shifts 1 to n times 20 | * finally perform bitwise XOR with num and result of above 21 | */ 22 | newNum = num ^ (1 << n); 23 | 24 | printf("Bit toggled successfully.\n\n"); 25 | printf("Number before setting %d bit: %d (in decimal)\n", n, num); 26 | printf("Number after setting %d bit: %d (in decimal)\n", n, newNum); 27 | return (0); 28 | } -------------------------------------------------------------------------------- /c-programming/decision_making_in_c/simple_calc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b, choice; 6 | 7 | printf("Enter two digits: \n"); 8 | scanf("%d %d", &a, &b); 9 | 10 | printf("Enter your choice\n"); 11 | printf("1 for addition 2 for subtraction, 3 for multplication and for 4 for division.\n"); 12 | scanf("%d", &choice); 13 | 14 | switch(choice) 15 | { 16 | case 1: 17 | printf("%d\n", a + b); 18 | break; 19 | case 2: 20 | printf("%d\n", a - b); 21 | break; 22 | case 3: 23 | printf("%d\n", a * b); 24 | break; 25 | case 4: 26 | printf("%d\n", a / b); 27 | break; 28 | default: 29 | printf("Invalid\n"); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /c-programming/enum/enum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - set the value of status using enum 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | int main(void) 9 | { 10 | enum status{OFF, ON}; 11 | 12 | printf("ON = %d OFF = %d\n", ON, OFF); 13 | 14 | return (0); 15 | } -------------------------------------------------------------------------------- /c-programming/enum/enum_variable.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - initializing and naming of variable using enum 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | // initializing and naming of variable 12 | enum status{sun, mon, tue, wed, thur, fri, sat} day; 13 | 14 | // initializing the declare variable 15 | enum status{sun, mon, tue, wed, thur, fri, sat}; 16 | enum status day; 17 | 18 | day = sat; 19 | printf("%d\n", day); 20 | 21 | return (0); 22 | } -------------------------------------------------------------------------------- /c-programming/loop_in_c/nested_loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints N X N empty box. 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int count, i, j; 11 | 12 | printf("Enter positive integers: "); 13 | scanf("%d", &count); 14 | 15 | for (i = 1; i <= count; i++) 16 | { 17 | for (j = 1; j <= count; j++) 18 | { 19 | if ((i > 1 && j > 1) && (i != count && j != count)) 20 | printf(" "); 21 | else 22 | printf("*"); 23 | } 24 | printf("\n"); 25 | } 26 | 27 | return (0); 28 | } -------------------------------------------------------------------------------- /c-programming/loop_in_c/print_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - get a positive integer from the user. And print it's digits one by one. 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n, l; 12 | 13 | printf("Enter positive integers: "); 14 | scanf("%d", &n); 15 | 16 | while (n >= 1) 17 | { 18 | l = n % 10; 19 | printf("%d\n", l); 20 | n = n / 10; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /c-programming/loop_in_c/sum_of_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the sum of all the digits. 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | 11 | int num, n, sum = 0; 12 | 13 | printf("Enter positive integers: "); 14 | scanf("%d", &num); 15 | 16 | n = num; 17 | while (n >= 1) 18 | { 19 | sum += n % 10; 20 | n = n / 10; 21 | } 22 | printf("The sum of %d is %d\n", num, sum); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /c-programming/loop_in_c/sum_of_even_and_odd.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the sum of odd and even numbers separately. 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int n, l, evenSum = 0, oddSum = 0; 11 | 12 | printf("Enter positive integers: "); 13 | scanf("%d", &n); 14 | 15 | while (n >= 1) 16 | { 17 | l = n % 10; 18 | if (l % 2 == 0) 19 | evenSum += l; 20 | else 21 | oddSum += l; 22 | n = n / 10; 23 | } 24 | printf("Odd Digit Sum = %d\n", oddSum); 25 | printf("Even Digit Sum = %d\n", evenSum); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /c-programming/loop_in_c/sum_of_odd_and_even_position.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the sum of odd and even position digits. 4 | * (Note: Position count starts from the right side of the number) 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n, l, count = 0, oddPostSum = 0, evenPostSum = 0; 12 | 13 | printf("Enter positive integers: "); 14 | scanf("%d", &n); 15 | 16 | while (n >= 1) 17 | { 18 | ++count; 19 | l = n % 10; 20 | if (count % 2 == 0) 21 | evenPostSum += l; 22 | else 23 | oddPostSum += l; 24 | n = n / 10; 25 | } 26 | printf ("Odd Position Sum = %d\n", oddPostSum); 27 | printf ("Even Position Sum = %d\n", evenPostSum); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /c-programming/string/strchr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strchr - locates first occurence of a character in a string 5 | * @str: string 6 | * @c: character to find 7 | * 8 | * Return: pointer to the character 9 | */ 10 | 11 | char *_strchr(char str[], int c) 12 | { 13 | int i; 14 | 15 | for (i = 0; str[i] != '\0'; i++) 16 | { 17 | if (str[i] == c) 18 | return (&str[i]); 19 | else 20 | return (NULL); 21 | } 22 | } 23 | 24 | int main(void) 25 | { 26 | char str[20]; 27 | char chr, *pos; 28 | 29 | printf("Enter a string: "); 30 | fgets(str, 20, stdin); 31 | 32 | printf("Enter a character to find its first occurrence: "); 33 | scanf("%c", &chr); 34 | 35 | pos = _strchr(str, chr); 36 | printf("The first occurrence of %c in %s is at %c\n", chr, str, *pos); 37 | return (0); 38 | } -------------------------------------------------------------------------------- /c-programming/string/strcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strcpy - copy a string into another string 5 | * @dest: destination to copy to 6 | * @src: source to copy from 7 | * 8 | * Return: pointer to the newstring 9 | */ 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i; 13 | 14 | for (i = 0; src[i]; i++) 15 | dest[i] = src[i]; 16 | dest[i] = '\0'; 17 | 18 | return (dest); 19 | } 20 | 21 | int main(void) 22 | { 23 | char str[20]; 24 | char newStr[20]; 25 | 26 | printf("Enter a string: "); 27 | fgets(str, 20, stdin); 28 | 29 | _strcpy(newStr, str); 30 | printf("The new string is %s\n", newStr); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /c-programming/string/strlen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strlen - finds legnth of a string 5 | * @str: string to fins its length 6 | * Return: length of the string 7 | */ 8 | int _strlen(char str[]) 9 | { 10 | int len = 0; 11 | while (str[len]) 12 | len++; 13 | 14 | return (len); 15 | } 16 | 17 | int main(void) 18 | { 19 | char str[20]; 20 | int len; 21 | 22 | printf("Enter a string: "); 23 | scanf("%s", str); 24 | len = _strlen(str); 25 | 26 | printf("The length of %s = %d\n", str, len); 27 | return (0); 28 | } -------------------------------------------------------------------------------- /c-programming/structure/struct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * struct car - car description 5 | * @name: name of the car 6 | * @price: price of the car 7 | */ 8 | struct car 9 | { 10 | char name[5]; 11 | float price; 12 | }; 13 | 14 | int main(void) 15 | { 16 | struct car car1 = {"Benz", 1234.56}; 17 | 18 | printf("Name of car1 %s\n", car1.name); 19 | printf("Price of car1 %f\n", car1.price); 20 | 21 | return (0); 22 | } -------------------------------------------------------------------------------- /c-programming/structure/struct2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #pragma pack(1); 4 | 5 | /** 6 | * struct mobile - mobile details 7 | * @name: name of the mobile 8 | * @price: price of the mobile 9 | * @colour: colour of the mobile 10 | */ 11 | struct mobile 12 | { 13 | char name[10]; 14 | float price; 15 | char colour[20]; 16 | }; //__attribute__((packed)); 17 | 18 | int main(void) 19 | { 20 | struct mobile mobile1; 21 | 22 | printf("Enter mobile name: "); 23 | scanf("%s", mobile1.name); 24 | printf("Enter mobile price: "); 25 | scanf("%f", &mobile1.price); 26 | printf("Enter mobile colour: "); 27 | scanf("%s", mobile1.colour); 28 | 29 | printf("Mobile name: %s\n", mobile1.name); 30 | printf("Mobile price: %f\n", mobile1.price); 31 | printf("Mobile colour: %s\n", mobile1.colour); 32 | 33 | printf("%ld\n", sizeof(struct mobile)); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /c-programming/structure/struct_within_struct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * struct one - struct within struct 5 | */ 6 | struct one 7 | { 8 | int i; 9 | struct two 10 | { 11 | int j; 12 | struct three 13 | { 14 | int k; 15 | }var3; 16 | }var2; 17 | }var1; 18 | 19 | int main() 20 | { 21 | var1.i = 5; 22 | var1.var2.j = 100; 23 | var1.var2.var3.k = 15; 24 | 25 | printf("%d %d %d\n", var1.i, var1.var2.j, var1.var2.var3.k); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /c-programming/union/union.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * union value - union of char and int variable 4 | * @c: union member of char type 5 | * @i: union member of int type 6 | */ 7 | 8 | union value 9 | { 10 | char c; 11 | int i; 12 | }; 13 | 14 | /** 15 | * main - union 16 | * 17 | * Return: 0 always (success) 18 | */ 19 | 20 | int main(void) 21 | { 22 | union value v; 23 | 24 | v.i = 100; 25 | v.c = 'a'; 26 | 27 | printf("%c %d\n", v.c, v.i); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /c-programming/union/union_struct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum{INT, CHAR, FLOAT, DOUBLE}; 4 | 5 | struct 6 | { 7 | int type; 8 | union 9 | { 10 | int i; 11 | char c; 12 | float f; 13 | double d; 14 | }u; 15 | }s; 16 | 17 | /** 18 | * main - print the corresponding data type 19 | * 20 | * Return: 0 always (success) 21 | */ 22 | 23 | int main(void) 24 | { 25 | s.type = CHAR; 26 | s.u.c = 'a'; 27 | 28 | switch (s.type) 29 | { 30 | case 0: 31 | printf("%d\n",s.u.i); 32 | break; 33 | case 1: 34 | printf("%c\n",s.u.c); 35 | break; 36 | case 2: 37 | printf("%f\n",s.u.f); 38 | break; 39 | default: 40 | printf("%lf\n",s.u.d); 41 | } 42 | 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /coding_interview_pattern/cyclic_sort/0-main.c: -------------------------------------------------------------------------------- 1 | #include "cyclic_sort.h" 2 | 3 | int main(void) 4 | { 5 | int size = 5; 6 | int arr[] = {5, 3, 1, 4, 2}; 7 | 8 | printf("Before Cyclic Sort\n"); 9 | printArray(arr, size); 10 | cyclicSort2(arr, size); 11 | printf("After Cyclic Sort\n"); 12 | printArray(arr, size); 13 | 14 | return (1); 15 | } 16 | -------------------------------------------------------------------------------- /coding_interview_pattern/cyclic_sort/1-main.c: -------------------------------------------------------------------------------- 1 | #include "cyclic_sort.h" 2 | 3 | int main(void) 4 | { 5 | int size = 5; 6 | int arr[] = {2, 4, 1, 3, 2}; 7 | 8 | printf("Before Cyclic Sort\n"); 9 | printArray(arr, size); 10 | 11 | int res = duplicateNumber(arr, size); 12 | 13 | printf("%d\n", res); 14 | 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /coding_interview_pattern/cyclic_sort/2-main.c: -------------------------------------------------------------------------------- 1 | #include "cyclic_sort.h" 2 | 3 | int main(void) 4 | { 5 | int size = 5; 6 | // int arr[] = {0, 1, 5, 3, 4}; 7 | int arr[] = {1,3,5,4,2,6}; 8 | 9 | printf("Before Cyclic Sort\n"); 10 | printArray(arr, size); 11 | 12 | int res = missingNumber(arr, size); 13 | printf("\nRESULT: %d\n", res); 14 | 15 | int res2 = missingNumber2(arr, size); 16 | printf("\nRESULT: %d\n", res2); 17 | 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /coding_interview_pattern/cyclic_sort/README.md: -------------------------------------------------------------------------------- 1 | # Cyclic Sort 2 | 3 | Cyclic sort is a technique use to sort arrays containing a range of integer such e.g range(0, N), range(1, N). 4 | 5 | ### procedures 6 | 7 | 1. initialize i to equal to zero (0) 8 | 2. find index of the current integer (arr[i] -1). supposed arr[i] is 5 then it should be at index arr[i] - 1 => 5 - 1 = index 4 9 | 3. if arr[i] not equal to arr[index] 10 | - swap the two integers swap(arr[i], arr[index]) 11 | 4. else 12 | - Increment i 13 | -------------------------------------------------------------------------------- /coding_interview_pattern/cyclic_sort/cyclic_sort.h: -------------------------------------------------------------------------------- 1 | #ifndef _CYCLIC_SORT_H_ 2 | #define _CYCLIC_SORT_H_ 3 | 4 | #include 5 | 6 | void swap(int *a, int *b); 7 | void cyclicSort(int arr[], int size); 8 | void cyclicSort2(int arr[], int size); 9 | void printArray(int arr[], int size); 10 | int duplicateNumber(int arr[], int size); 11 | int missingNumber(int arr[], int size); 12 | int missingNumber2(int arr[], int size); 13 | 14 | #endif /*_CYCLIC_SORT_H_*/ 15 | 16 | -------------------------------------------------------------------------------- /coding_interview_pattern/cyclic_sort/duplicateNumber.c: -------------------------------------------------------------------------------- 1 | #include "cyclic_sort.h" 2 | 3 | int duplicateNumber(int arr[], int size) 4 | { 5 | int i = 0; 6 | 7 | while (i < size) 8 | { 9 | if (arr[i] != i + 1) 10 | { 11 | int index = arr[i] - 1; 12 | if (arr[i] == arr[index]) 13 | return arr[i]; 14 | else 15 | swap(&arr[i], &arr[index]); 16 | } 17 | else 18 | i++; 19 | 20 | } 21 | return -1; 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /coding_interview_pattern/cyclic_sort/print_array.c: -------------------------------------------------------------------------------- 1 | #include "cyclic_sort.h" 2 | /** 3 | * printArray - print array 4 | * 5 | * @arr: array to print 6 | * @size: size of the array 7 | */ 8 | void printArray(int arr[], int size) 9 | { 10 | printf("{"); 11 | for (int i = 0; i < size; i++) 12 | { 13 | if (i != size-1) 14 | printf("%d, ", arr[i]); 15 | else 16 | printf("%d", arr[i]); 17 | } 18 | printf("}\n"); 19 | } 20 | -------------------------------------------------------------------------------- /coding_interview_pattern/cyclic_sort/swap.c: -------------------------------------------------------------------------------- 1 | #include "cyclic_sort.h" 2 | 3 | /** 4 | * swap - swap two integers 5 | * 6 | * @a: address of the first integer 7 | * @b: address of the second integer 8 | */ 9 | void swap(int *a, int *b) 10 | { 11 | int temp = *a; 12 | *a = *b; 13 | *b = temp; 14 | } 15 | -------------------------------------------------------------------------------- /coding_interview_pattern/fast_and_slow_pointers/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/coding_interview_pattern/fast_and_slow_pointers/a.out -------------------------------------------------------------------------------- /coding_interview_pattern/fast_and_slow_pointers/fast_slow.h: -------------------------------------------------------------------------------- 1 | #ifndef _FAST_SLOW_H_ 2 | #define _FAST_SLOW_H_ 3 | #include 4 | #include 5 | /* 6 | ** Definition for a linked list node. 7 | */ 8 | typedef struct node 9 | { 10 | int data; 11 | struct node *next; 12 | }lNode; 13 | 14 | void printNode(lNode *head); 15 | int hasLoop(lNode *head); 16 | int getMiddleNode(lNode *head); 17 | int loopLength(lNode *head); 18 | int calculateLoopLength(lNode *slow); 19 | lNode *findLoopStart(lNode *head); 20 | lNode *getLoopStart(lNode *head, lNode *slow, lNode *fast); 21 | int isHappyNumber(int num); 22 | 23 | #endif /*_FAST_SLOW_H_*/ 24 | -------------------------------------------------------------------------------- /coding_interview_pattern/fast_and_slow_pointers/findLoopStart.c: -------------------------------------------------------------------------------- 1 | #include "fast_slow.h" 2 | 3 | /** 4 | * findLoopStart - find the loop start in the linked list 5 | * 6 | * @head: Pointer to the head of the linked list 7 | * Return: pointer to the start of the loop 8 | */ 9 | lNode *findLoopStart(lNode *head) 10 | { 11 | lNode *slow, *fast; 12 | slow = fast = head; 13 | 14 | while (slow != NULL && fast != NULL && fast->next != NULL) 15 | { 16 | slow = slow->next; 17 | fast = fast->next->next; 18 | 19 | if (slow == fast) 20 | { 21 | return getLoopStart(head, slow, fast); 22 | } 23 | } 24 | } 25 | 26 | lNode *getLoopStart(lNode *head, lNode *slow, lNode *fast) 27 | { 28 | slow = head; 29 | while (slow != fast) 30 | { 31 | slow = slow->next; 32 | fast = fast->next; 33 | } 34 | return slow; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /coding_interview_pattern/fast_and_slow_pointers/get_middle_node.c: -------------------------------------------------------------------------------- 1 | #include "fast_slow.h" 2 | /** 3 | * getMiddleNode - Get the Middle Node object 4 | * 5 | * @head: head of the linked list 6 | * Return: data of the middle node otherwise -1 7 | */ 8 | int getMiddleNode(struct node *head) 9 | { 10 | if (head == NULL) return -1; 11 | 12 | struct node *slow, *fast; 13 | slow = fast = head; 14 | 15 | while (fast != NULL && fast->next != NULL) 16 | { 17 | slow = slow->next; 18 | fast = fast->next->next; 19 | } 20 | return slow->data; 21 | } 22 | -------------------------------------------------------------------------------- /coding_interview_pattern/fast_and_slow_pointers/printNode.c: -------------------------------------------------------------------------------- 1 | #include "fast_slow.h" 2 | 3 | void printNode(struct node *head) 4 | { 5 | struct node *temp = head; 6 | 7 | while (temp != NULL) 8 | { 9 | printf("%d ", temp->data); 10 | temp = temp->next; 11 | } 12 | printf("\n"); 13 | } 14 | -------------------------------------------------------------------------------- /coding_interview_pattern/fast_and_slow_pointers/startoftheloop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/coding_interview_pattern/fast_and_slow_pointers/startoftheloop.png -------------------------------------------------------------------------------- /coding_interview_pattern/sliding_window/C/max_sub_array_sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int maxSubarraySum(int arr[], int n, int k) 4 | { 5 | int i; 6 | int wstart = 0; 7 | int subsum = 0; 8 | int max = 0; 9 | 10 | for (i = 0; i < n; i++) 11 | { 12 | subsum += arr[i]; 13 | 14 | if (i >= k-1) 15 | { 16 | max = max > subsum? max : subsum; 17 | subsum -= arr[wstart]; 18 | wstart++; 19 | } 20 | } 21 | return max; 22 | } 23 | 24 | 25 | int main(void) 26 | { 27 | int arr[6] = {1, 5, -1, 6, 3, 2}; 28 | int size = sizeof(arr) / sizeof(arr[0]); 29 | int K = 3; 30 | 31 | int max = maxSubarraySum(arr, size, K); 32 | printf("%d\n", max); 33 | 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /coding_interview_pattern/sliding_window/README.md: -------------------------------------------------------------------------------- 1 | # Sliding window Technique 2 | 3 | ## How to identify sliding window problems 4 | 5 | - Problem with sub-array, sub-string 6 | - Examples 7 | - Smallest subarray with a given sum 8 | - maximum subarray of size K 9 | - Longest substring with K distinct characters 10 | -------------------------------------------------------------------------------- /coding_interview_pattern/sliding_window/python/maxSubArraySum.py: -------------------------------------------------------------------------------- 1 | def maxSubArraySum(arr, n, K): 2 | """Compute subarray sum of size K 3 | """ 4 | wstart = 0 5 | subSum = 0 6 | max = 0 7 | 8 | for i in range(n): 9 | subSum += arr[i] 10 | if i >= K-1: 11 | if max < subSum: 12 | max = subSum 13 | subSum -= arr[wstart] 14 | wstart += 1 15 | return max 16 | 17 | if __name__ == "__main__": 18 | arr = [1, 5, -1, 6, 3, 2] 19 | size = len(arr) 20 | K = 3 21 | 22 | max = maxSubArraySum(arr, size, K) 23 | print(max) 24 | -------------------------------------------------------------------------------- /coding_interview_pattern/sliding_window/python/smallLengthSubarraySum.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def smallLengthSubarraySum(arr, n, S): 4 | """Find the smallest length of subarray >= the value of S 5 | """ 6 | wstart = 0 7 | subSum = 0 8 | length = sys.maxsize 9 | for i in range(n): 10 | 11 | subSum += arr[i] 12 | while subSum >= S: 13 | currentWindowSize = i - wstart + 1 14 | if length > currentWindowSize: 15 | length = currentWindowSize 16 | 17 | subSum -= arr[wstart] 18 | wstart += 1 19 | 20 | length if length == sys.maxsize else length 21 | 22 | return length 23 | 24 | 25 | if __name__ == "__main__": 26 | arr = [4, 1, 5, 2, 4, 1, 7] 27 | size = len(arr) 28 | S = 7 29 | 30 | min = smallLengthSubarraySum(arr, size, S) 31 | print(min) 32 | -------------------------------------------------------------------------------- /coding_interview_pattern/sliding_window/python/subArraySum.py: -------------------------------------------------------------------------------- 1 | def subArraySum(arr, n, K): 2 | """Compute subarray sum of size K 3 | """ 4 | wstart = 0 5 | res = [] 6 | subSum = 0 7 | 8 | for i in range(n): 9 | subSum += arr[i] 10 | if i >= K-1: 11 | res.append(subSum) 12 | subSum -= arr[wstart] 13 | wstart += 1 14 | return res 15 | 16 | if __name__ == "__main__": 17 | arr = [1, 5, -1, 6, 3, 2] 18 | size = len(arr) 19 | K = 2 20 | 21 | res = subArraySum(arr, size, K) 22 | print(res) 23 | -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/README.md: -------------------------------------------------------------------------------- 1 | # Two Pointers 2 | 3 | How to identify two pointers problems 4 | 5 | - Remove duplicate 6 | - Pair/triplet/subarray with a target 7 | - sorted array / linkedlist 8 | -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/c/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/coding_interview_pattern/two_pointers/c/a.out -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/c/pair_with_target_sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int hasPairWithTarget(int arr[], int n, int target) 4 | { 5 | int start, end, sum = 0; 6 | 7 | start = 0; 8 | end = n - 1; 9 | 10 | while (start < end) 11 | { 12 | sum = arr[start] + arr[end]; 13 | 14 | if (sum == target) 15 | return 1; 16 | else if (sum < target) 17 | start++; 18 | else 19 | end--; 20 | } 21 | 22 | return 0; 23 | } 24 | 25 | 26 | int main(void) 27 | { 28 | int arr[] = {1,5,10,20,80}; 29 | int target = 30; 30 | int size = sizeof(arr) / sizeof(arr[0]); 31 | 32 | int res = hasPairWithTarget(arr, size, target); 33 | 34 | printf("%d\n", res); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/c/remove_element.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * removeElement - remove occurence of val in array 5 | * 6 | * @arr: array 7 | * @n: size of the array 8 | * @val: occurrence of value 9 | * Return - the new length 10 | */ 11 | int removeElement(int arr[], int n, int val) 12 | { 13 | int i, newLen = 0; 14 | for (i = 0; i < n; i++) 15 | { 16 | if (arr[i] != val) 17 | { 18 | arr[newLen++] = arr[i]; 19 | } 20 | } 21 | return newLen; 22 | } 23 | 24 | 25 | int main(void) 26 | { 27 | int arr[] = {4,7,6,7,8,7,7}; 28 | int val = 7; 29 | int size = sizeof(arr) / sizeof(arr[0]); 30 | 31 | int newLen = removeElement(arr, size, val); 32 | 33 | printf("%d\n", newLen); 34 | 35 | for (int i = 0; i < newLen; i++) 36 | printf("%d ", arr[i]); 37 | printf("\n"); 38 | 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/python/has_pair_with_target.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Check if a sorted array has a pair whose sum equal to target 3 | """ 4 | from typing import List 5 | 6 | 7 | def hasPairWithTarget(arr: List[int], n: int, target: int) -> int: 8 | start = 0 9 | end = n - 1 10 | sum = 0 11 | 12 | while start < end: 13 | sum = arr[start] + arr[end] 14 | 15 | if sum == target: 16 | return 1 17 | elif sum < target: 18 | start += 1 19 | else: 20 | end -= 1 21 | return 0 22 | 23 | 24 | if __name__ == "__main__": 25 | arr = [1,5,10,20,80] 26 | size = len(arr) 27 | target = 90 28 | 29 | res = hasPairWithTarget(arr, size, target) 30 | print(res) 31 | -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/python/moveZeroes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """remove occurence of value in array 3 | then return the new length 4 | """ 5 | from typing import List 6 | 7 | def moveZeroes(arr: List[int], n: int) -> None: 8 | newLen = 0 9 | 10 | for i in range(n): 11 | if arr[i] != 0: 12 | arr[newLen] = arr[i] 13 | newLen += 1 14 | 15 | while newLen < n: 16 | arr[newLen] = 0 17 | newLen += 1 18 | 19 | 20 | 21 | if __name__ == "__main__": 22 | arr = [1, 0, 1, 0, 1, 1] 23 | size = len(arr) 24 | 25 | print(arr) 26 | moveZeroes(arr, size) 27 | print(arr) 28 | -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/python/remove_duplicate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """remove duplicate in a sorted array 3 | """ 4 | from typing import List 5 | 6 | def removeDuplicate(arr: List[int], n: int) -> int: 7 | 8 | if n == 0 or n == 1: return n 9 | 10 | newLen = 0 11 | 12 | for i in range(n-1): 13 | if arr[i] != arr[i+1]: 14 | arr[newLen] = arr[i] 15 | newLen += 1 16 | arr[newLen] = arr[n-1] 17 | newLen += 1 18 | 19 | return newLen 20 | 21 | 22 | 23 | if __name__ == "__main__": 24 | arr = [1,1,1,3,3,5] 25 | size = len(arr) 26 | 27 | print(arr) 28 | 29 | res = removeDuplicate(arr, size) 30 | print(res) 31 | 32 | print(arr[:res]) 33 | -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/python/remove_duplicates.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """remove duplicate at most twice in a sorted array 3 | """ 4 | from typing import List 5 | 6 | def removeDuplicate(arr: List[int], n: int) -> int: 7 | 8 | if n <= 2: return n 9 | 10 | Len = 0 11 | arr[Len] = arr[0] 12 | Len += 1 13 | 14 | for i in range(1, n-1): 15 | if arr[i-1] != arr[i+1]: 16 | arr[Len] = arr[i] 17 | Len += 1 18 | arr[Len] = arr[n-1] 19 | Len += 1 20 | 21 | return Len 22 | 23 | 24 | 25 | if __name__ == "__main__": 26 | arr = [1,1,1,3,3,3,5,5] 27 | size = len(arr) 28 | 29 | print(arr) 30 | 31 | res = removeDuplicate(arr, size) 32 | print(res) 33 | 34 | print(arr[:res]) 35 | -------------------------------------------------------------------------------- /coding_interview_pattern/two_pointers/python/remove_element.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """remove occurence of value in array 3 | then return the new length 4 | """ 5 | from typing import List 6 | 7 | def removeElement(arr: List[int], n: int, val: int) -> int: 8 | newLen = 0 9 | 10 | for i in range(n): 11 | if arr[i] != val: 12 | arr[newLen] = arr[i] 13 | newLen += 1 14 | return newLen 15 | 16 | 17 | 18 | if __name__ == "__main__": 19 | arr = [4,7,6,7,8,7,7] 20 | size = len(arr) 21 | val = 7 22 | 23 | print(arr) 24 | 25 | res = removeElement(arr, size, val) 26 | print(res) 27 | 28 | print(arr[:res]) 29 | -------------------------------------------------------------------------------- /data_structure/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures 2 | 3 | A data structure is a predefined format for efficiently storing, 4 | accessing, and processing data in a computer program. 5 | Some data structures are a programming language built-in component, 6 | and others may require the inclusion of a library or module before the structure can be used. 7 | 8 | ## Types of data structure 9 | - Array data structure 10 | - Linked list data structure 11 | - Singly linked list 12 | - Doubly linked list 13 | - Circular linked list 14 | - Stack data structure 15 | - Queue data structure 16 | - Binary search tree (BTS) 17 | -------------------------------------------------------------------------------- /data_structure/array/README.md: -------------------------------------------------------------------------------- 1 | # Array Data Structure -------------------------------------------------------------------------------- /data_structure/array/search_array_element.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int search_element(int arr[], int size, int key) 4 | { 5 | int i, flag = -1; 6 | 7 | for (i = 0; i < size; i++) 8 | { 9 | if (arr[i] == key) 10 | return (i); 11 | } 12 | return (flag); 13 | } 14 | 15 | 16 | int main(void) 17 | { 18 | int arr[] = {1, 2, 3, 4, 5}; 19 | int size = 5, res, key; 20 | 21 | printf("Enter a key: "); 22 | scanf("%d", &key); 23 | 24 | res = search_element(arr, size, key); 25 | 26 | if (res >= 0) 27 | printf("Found at index %d\n", res); 28 | else 29 | printf("Not Found\n"); 30 | 31 | return (0); 32 | } -------------------------------------------------------------------------------- /data_structure/binary_heap/.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "linux-gcc-x64", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "compilerPath": "/usr/bin/gcc", 9 | "cStandard": "${default}", 10 | "cppStandard": "${default}", 11 | "intelliSenseMode": "linux-gcc-x64", 12 | "compilerArgs": [ 13 | "" 14 | ], 15 | "configurationProvider": "ms-vscode.makefile-tools" 16 | } 17 | ], 18 | "version": 4 19 | } -------------------------------------------------------------------------------- /data_structure/binary_heap/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "C/C++ Runner: Debug Session", 6 | "type": "cppdbg", 7 | "request": "launch", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "externalConsole": false, 11 | "cwd": "/home/belovedtech/beloved/Log2Base2/data_structure/binary_heap", 12 | "program": "/home/belovedtech/beloved/Log2Base2/data_structure/binary_heap/build/Debug/outDebug", 13 | "MIMode": "gdb", 14 | "miDebuggerPath": "gdb", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | } 21 | ] 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /data_structure/binary_heap/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: gcc-11 build active file", 6 | "command": "/usr/bin/gcc-11", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /data_structure/binary_heap/README.md: -------------------------------------------------------------------------------- 1 | # Binary heap 2 | 3 | Binary heap is a priority queue. binary heap is a complete tree which satisfying the heap ordering priority 4 | 5 | A complete tree ia a full tree in which the last level may contain lesser nodes but should be as left as possible. 6 | 7 | ## Two kinds of heap 8 | 9 | - Min heap - The node with the minimum value will be at the root so it can be accessible at constant time O(1) 10 | 11 | - Max heap - The node with the maximum value will be at the root o it can be accessible at constant time O(1) 12 | 13 | ## Formular to find left and right node 14 | 15 | - left node = `2 * i + 1` 16 | - right node = `2 * i + 2` 17 | ( i == index 18 | ) 19 | -------------------------------------------------------------------------------- /data_structure/binary_heap/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/data_structure/binary_heap/a.out -------------------------------------------------------------------------------- /data_structure/binary_heap/heapify: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/data_structure/binary_heap/heapify -------------------------------------------------------------------------------- /data_structure/binary_search_tree/README.md: -------------------------------------------------------------------------------- 1 | # Binary Search Tree (BTS) 2 | 3 | A binary search tree is a rooted binary tree in which the nodes are arranged in total order 4 | in which the nodes with keys/data greater than any particular node is stored on the right sub-trees 5 | and the ones with equal to or less than are stored on the left sub-tree satisfying the binary search property 6 | 7 | ## Files in the directory 8 | 9 | | Files | Description | 10 | ----------------|-------------- 11 | | bts.h | Header file for BTS 12 | | main.c | Main function 13 | | get_node.c | Allocate space in heap for BTS nodes 14 | | insert_node.c | Insert value in BTS nodes 15 | | inorder.c | Print BTS nodes value 16 | | search_node.c | Search for key in BTS nodes 17 | | delete_node.c | Delete nodes from BTS nodes 18 | 19 | -------------------------------------------------------------------------------- /data_structure/binary_search_tree/bts.h: -------------------------------------------------------------------------------- 1 | #ifndef BTS_H_ 2 | #define BTS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct node - binary search tree 9 | * @key: value of binary search tree 10 | * @left: left children/nodes of bts 11 | * @right: right children/nodes of bts 12 | */ 13 | struct node 14 | { 15 | int key; 16 | struct node *left; 17 | struct node *right; 18 | }; 19 | 20 | struct node *getNewNode(int val); 21 | struct node *insertNode(struct node *root, int val); 22 | void inorder(struct node *root); 23 | int searchNode(struct node *root, int key); 24 | struct node *deleteNode(struct node *root, int key); 25 | int getRightMin(struct node *root); 26 | #endif /*BTS_H_*/ -------------------------------------------------------------------------------- /data_structure/binary_search_tree/get_new_node.c: -------------------------------------------------------------------------------- 1 | #include "bts.h" 2 | 3 | /** 4 | * getNewNode - create new node of bts 5 | * @val: Value of the node created 6 | * Return: pointer to the newNode 7 | */ 8 | struct node *getNewNode(int val) 9 | { 10 | struct node *newNode; 11 | 12 | newNode = malloc(sizeof(struct node)); 13 | if (newNode == NULL) 14 | return (NULL); 15 | 16 | newNode->key = val; 17 | newNode->left = NULL; 18 | newNode->right = NULL; 19 | 20 | return (newNode); 21 | } 22 | -------------------------------------------------------------------------------- /data_structure/binary_search_tree/inorder.c: -------------------------------------------------------------------------------- 1 | #include "bts.h" 2 | /** 3 | * inorder - print bts node value in ascending order 4 | * @root: pointer to the root 5 | */ 6 | 7 | void inorder(struct node *root) 8 | { 9 | if (root == NULL) 10 | return; 11 | inorder(root->left); 12 | printf("%d ", root->key); 13 | inorder(root->right); 14 | } 15 | -------------------------------------------------------------------------------- /data_structure/binary_search_tree/insert_node.c: -------------------------------------------------------------------------------- 1 | #include "bts.h" 2 | 3 | /** 4 | * insertNode - insert node to binary search tree 5 | * @node: pointer to the struct node 6 | * @val: node value 7 | * Return: pointer to the root 8 | */ 9 | struct node *insertNode(struct node *root, int val) 10 | { 11 | if (root == NULL) 12 | return getNewNode(val); 13 | 14 | if (root->key < val) 15 | root->right = insertNode(root->right, val); 16 | else if (root->key > val) 17 | root->left = insertNode(root->left, val); 18 | return (root); 19 | } -------------------------------------------------------------------------------- /data_structure/binary_search_tree/main.c: -------------------------------------------------------------------------------- 1 | #include "bts.h" 2 | 3 | int main(void) 4 | { 5 | struct node *root = NULL; 6 | int res; 7 | int key = 100; 8 | 9 | root = insertNode(root, 100); 10 | root = insertNode(root, 50); 11 | root = insertNode(root, 150); 12 | root = insertNode(root, 125); 13 | root = insertNode(root, 10); 14 | 15 | inorder(root); 16 | printf("\n"); 17 | 18 | printf("Deleting key.....%d\n", key); 19 | deleteNode(root, key); 20 | 21 | res = searchNode(root, 150); 22 | if (res) 23 | printf("Key found\n"); 24 | else 25 | printf("Key not found\n"); 26 | 27 | inorder(root); 28 | printf("\n"); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /data_structure/binary_search_tree/makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean oclean fclean re 2 | 3 | CC = gcc 4 | SRC = main.c get_new_node.c delete_node.c insert_node.c search_node.c inorder.c 5 | OBJ = $(SRC:%.c=%.o) 6 | NAME = bts 7 | RM = rm -f 8 | CFLAGS = -Wall -Werror -Wextra 9 | 10 | all: $(OBJ) 11 | $(CC) $(CFLAGS) $(SRC) -o $(NAME) 12 | 13 | clean: 14 | $(RM) *~ $(NAME) 15 | 16 | oclean: 17 | $(RM) $(OBJ) 18 | 19 | fclean: clean oclean 20 | 21 | re: fclean all -------------------------------------------------------------------------------- /data_structure/binary_search_tree/search_node.c: -------------------------------------------------------------------------------- 1 | #include "bts.h" 2 | /** 3 | * searchNode - Search for the key in BTS nodes 4 | * @root: Pointer to the root 5 | * @key: Value to find 6 | * Return: 1 if found otherwise 0 7 | */ 8 | 9 | int searchNode(struct node *root, int key) 10 | { 11 | if (root == NULL) 12 | return (0); 13 | if (root->key == key) 14 | return (1); 15 | if (root->key < key) 16 | return searchNode(root->right, key); 17 | return searchNode(root->left, key); 18 | } 19 | -------------------------------------------------------------------------------- /data_structure/circular_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Circular Linked List -------------------------------------------------------------------------------- /data_structure/circular_linked_list/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/data_structure/circular_linked_list/a.out -------------------------------------------------------------------------------- /data_structure/circular_linked_list/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | 7 | typedef struct snode 8 | { 9 | int data; 10 | struct snode *next; 11 | }s_lists; 12 | 13 | typedef struct dnode 14 | { 15 | int data; 16 | struct dnode *prev; 17 | struct dnode *next; 18 | }d_lists; 19 | 20 | void print_lists(s_lists *head); 21 | s_lists *insert_at_beginning1(s_lists **head, int val); 22 | s_lists *insert_at_end1(s_lists **head, int val); 23 | 24 | void print_listd(d_lists *head); 25 | d_lists *insert_at_beginning2(d_lists **head, int val); 26 | d_lists *insert_at_end2(d_lists **head, int val); 27 | void search_node(d_lists *head, int key); 28 | void delete_node(d_lists **head, int key); 29 | 30 | #endif /*LISTS_H*/ -------------------------------------------------------------------------------- /data_structure/circular_linked_list/main.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | int main(void) 4 | { 5 | d_lists *head = NULL; 6 | 7 | // insert_at_beginning2(&head, 50); 8 | // insert_at_beginning2(&head, 40); 9 | // insert_at_beginning2(&head, 30); 10 | // insert_at_beginning2(&head, 20); 11 | // insert_at_beginning2(&head, 10); 12 | 13 | insert_at_end2(&head, 100); 14 | insert_at_end2(&head, 200); 15 | insert_at_end2(&head, 300); 16 | insert_at_end2(&head, 400); 17 | insert_at_end2(&head, 500); 18 | 19 | 20 | delete_node(&head, 500); 21 | 22 | // search_node(head, 500); 23 | // search_node(head, 600); 24 | print_listd(head); 25 | 26 | return (0); 27 | } -------------------------------------------------------------------------------- /data_structure/circular_linked_list/print_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_lists - print circular singly linked list data 4 | * 5 | * @head: pointer to the head 6 | */ 7 | void print_lists(s_lists *head) 8 | { 9 | if (head == NULL) 10 | return; 11 | 12 | s_lists *tmp = head; 13 | 14 | do 15 | { 16 | printf("%d ", tmp->data); 17 | tmp = tmp->next; 18 | 19 | } while (tmp != head); 20 | printf("\n"); 21 | } 22 | 23 | /** 24 | * print_listd - print circular doubly linked list data 25 | * 26 | * @head: pointer to the head 27 | */ 28 | void print_listd(d_lists *head) 29 | { 30 | if (head == NULL) 31 | return; 32 | 33 | d_lists *tmp = head; 34 | 35 | do 36 | { 37 | printf("%d ", tmp->data); 38 | tmp = tmp->next; 39 | 40 | } while (tmp != head); 41 | printf("\n"); 42 | } 43 | 44 | -------------------------------------------------------------------------------- /data_structure/circular_linked_list/search_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * search_node - search node in doubly linked list 5 | * @key: key to find 6 | * 7 | * Return: Found otherwise Not found 8 | */ 9 | void search_node(d_lists *head, int key) 10 | { 11 | d_lists *temp = head; 12 | int flag = 0, idx = 0; 13 | 14 | do 15 | { 16 | if (temp->data == key) 17 | { 18 | flag = 1; 19 | break; 20 | } 21 | temp = temp->next; 22 | idx++; 23 | 24 | } while (temp != head); 25 | 26 | if (flag) 27 | printf("Found at index %d\n", idx); 28 | else 29 | printf("Not Found\n"); 30 | } -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Doubly linked list -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/data_structure/doubly_linked_list/a.out -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/insert_at_beginning.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * insert_at_beginning - insert data to singly linked list 4 | * @head: pointer to pointer 5 | * @val: data to add. 6 | * 7 | * Return: pointer to new node 8 | */ 9 | struct node *add_node_front(struct node **head, int val) 10 | { 11 | struct node *newNode; 12 | 13 | newNode = malloc(sizeof(struct node)); 14 | if (newNode == NULL) 15 | return (NULL); 16 | 17 | newNode->data = val; 18 | 19 | if (*head == NULL) 20 | { 21 | newNode->prev = NULL; 22 | newNode->next = NULL; 23 | *head = newNode; 24 | } 25 | else 26 | { 27 | newNode->prev = NULL; 28 | newNode->next = *head; 29 | (*head)->prev = newNode; 30 | *head = newNode; 31 | } 32 | 33 | return (newNode); 34 | } -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/insert_at_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node_end - insert data to the end of singly linked list 4 | * @head: pointer to pointer 5 | * @val: data to add. 6 | * 7 | * Return: pointer to new node 8 | */ 9 | struct node *add_node_end(struct node **head, int val) 10 | { 11 | struct node *newNode, *last; 12 | 13 | newNode = malloc(sizeof(struct node)); 14 | if (newNode == NULL) 15 | return (NULL); 16 | 17 | newNode->data = val; 18 | 19 | if (*head == NULL) 20 | { 21 | newNode->prev = NULL; 22 | newNode->next = NULL; 23 | *head = newNode; 24 | } 25 | else 26 | { 27 | last = *head; 28 | while (last->next != NULL) 29 | last = last->next; 30 | 31 | newNode->prev = last; 32 | newNode->next = NULL; 33 | last->next = newNode; 34 | } 35 | return (newNode); 36 | } -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | struct node 7 | { 8 | int data; 9 | struct node *prev; 10 | struct node *next; 11 | }; 12 | 13 | int print_node(struct node *head); 14 | struct node *add_node_front(struct node **head, int val); 15 | struct node *add_node_end(struct node **head, int val); 16 | int search_node(struct node *head, int key); 17 | int delete_node(struct node **head, int key); 18 | #endif /*LISTS_H*/ -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/main.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | 4 | int main(void) 5 | { 6 | struct node *head = NULL; 7 | int res; 8 | 9 | 10 | add_node_front(&head, 10); 11 | add_node_front(&head, 20); 12 | add_node_front(&head, 30); 13 | add_node_end(&head, 40); 14 | add_node_end(&head, 50); 15 | add_node_end(&head, 60); 16 | add_node_end(&head, 70); 17 | 18 | print_node(head); 19 | delete_node(&head, 30); 20 | delete_node(&head, 70); 21 | delete_node(&head, 700); 22 | delete_node(&head, 60); 23 | delete_node(&head, 10); 24 | print_node(head); 25 | // search_node(head, 5590); 26 | 27 | return (0); 28 | } -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/print_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | int print_node(struct node *head) 4 | { 5 | int counter = 0; 6 | 7 | while (head != NULL) 8 | { 9 | printf("%d ", head->data); 10 | head = head->next; 11 | counter++; 12 | } 13 | printf("\n"); 14 | return (counter); 15 | } -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/search_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * search_node - search node in circular linked list 4 | * @key: key to find 5 | * 6 | * Return: Found otherwise Not found 7 | */ 8 | int search_node(struct node *head, int key) 9 | { 10 | struct node *tmp = head; 11 | int flag = 0, idx = 0; 12 | 13 | while (tmp != NULL) 14 | { 15 | if (tmp->data == key) 16 | { 17 | flag = 1; 18 | break; 19 | } 20 | tmp = tmp->next; 21 | idx++; 22 | } 23 | 24 | if (flag) 25 | printf("Found at %d\n", idx); 26 | else 27 | printf("Not found\n"); 28 | 29 | return (idx); 30 | } 31 | -------------------------------------------------------------------------------- /data_structure/doubly_linked_list/test/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node* prev; 8 | struct node* next; 9 | }; 10 | 11 | int main() 12 | { 13 | 14 | struct node *head, *mid, *last; 15 | 16 | head = malloc(sizeof(struct node)); 17 | mid = malloc(sizeof(struct node)); 18 | last = malloc(sizeof(struct node)); 19 | 20 | head->data = 10; 21 | mid->data = 20; 22 | last->data = 30; 23 | 24 | //Implement task 1 here 25 | head->prev = NULL; 26 | head->next = mid; 27 | mid->prev = head; 28 | mid->next = last; 29 | last->prev = mid; 30 | last->next = NULL; 31 | 32 | //Implement task 2 here 33 | struct node *temp; 34 | 35 | temp = last; 36 | 37 | while(temp != NULL) 38 | { 39 | printf("%d ", temp->data); 40 | temp = temp->prev; 41 | } 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /data_structure/graph/.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "linux-gcc-x64", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "compilerPath": "/usr/bin/gcc", 9 | "cStandard": "${default}", 10 | "cppStandard": "${default}", 11 | "intelliSenseMode": "linux-gcc-x64", 12 | "compilerArgs": [ 13 | "" 14 | ] 15 | } 16 | ], 17 | "version": 4 18 | } -------------------------------------------------------------------------------- /data_structure/graph/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "C/C++ Runner: Debug Session", 6 | "type": "cppdbg", 7 | "request": "launch", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "externalConsole": false, 11 | "cwd": "/home/belovedtech/beloved/Log2Base2/data_structure/graph", 12 | "program": "/home/belovedtech/beloved/Log2Base2/data_structure/graph/build/Debug/outDebug", 13 | "MIMode": "gdb", 14 | "miDebuggerPath": "gdb", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | } 21 | ] 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /data_structure/graph/README.md: -------------------------------------------------------------------------------- 1 | # Graph Data Structure 2 | 3 | A graph data structure is a collection of edges and vertices. Vertices also called Nodes. G = (V , E) 4 | 5 | ## Types of Graphs direction 6 | 7 | - Directed Graph 8 | - Unidirected Graph 9 | 10 | ## Two types of Degree 11 | 12 | - In-degree - edges going to of vertex 13 | - Out-degree - edges coming out of vertex 14 | 15 | ## Two Methods of Graph representation 16 | 17 | - Adjacency Matrix 18 | - Adjacency List 19 | 20 | ## Types of Graphs and suitable graph methods 21 | 22 | - Dense Graph - Adjacency Matrix 23 | - Sparse Graph - Adjacency List 24 | -------------------------------------------------------------------------------- /data_structure/queue/README.md: -------------------------------------------------------------------------------- 1 | # Queue Data Structure 2 | 3 | Implementation of `queue data structure` using: 4 | - Array data structure 5 | - linked list data structure 6 | 7 | 8 | ## Queue Functions 9 | - Enqueue 10 | - Dequeue 11 | - is_empty 12 | - is_full -------------------------------------------------------------------------------- /data_structure/singly_linked_list/README.md: -------------------------------------------------------------------------------- 1 | # Singly Linked List -------------------------------------------------------------------------------- /data_structure/singly_linked_list/delete_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * delete_node - Delete key in the linked list node 4 | * @key: data to delete 5 | */ 6 | void delete_node(struct node **head, int key) 7 | { 8 | struct node *tmp; 9 | 10 | if ((*head)->data == key) 11 | { 12 | tmp = *head; 13 | *head = (*head)->next; 14 | free(tmp); 15 | } 16 | else 17 | { 18 | struct node *current = *head; 19 | while (current->next != NULL) 20 | { 21 | if (current->next->data == key) 22 | { 23 | tmp = current->next; 24 | current->next = current->next->next; 25 | free(tmp); 26 | break; 27 | } 28 | else 29 | { 30 | current = current->next; 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /data_structure/singly_linked_list/insert_at_beginning.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_at_beginning - insert data to singly linked list 5 | * @head: pointer to pointer 6 | * @val: data to add. 7 | * 8 | * Return: pointer to new node 9 | */ 10 | struct node *insert_at_beginning(struct node **head, int val) 11 | { 12 | struct node *newNode; 13 | 14 | newNode = malloc(sizeof(struct node)); 15 | if (newNode == NULL) 16 | return (NULL); 17 | 18 | newNode->data = val; 19 | newNode->next = *(head); 20 | 21 | *(head) = newNode; 22 | 23 | return (newNode); 24 | } 25 | -------------------------------------------------------------------------------- /data_structure/singly_linked_list/insert_at_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_at_end - insert data at end of singly linked list 5 | * @head: pointer to pointer 6 | * @val: data to add. 7 | * 8 | * Return: pointer to new node 9 | */ 10 | 11 | struct node *insert_at_end(struct node **head, int val) 12 | { 13 | struct node *newNode; 14 | 15 | newNode = malloc(sizeof(struct node)); 16 | if (newNode == NULL) 17 | return (NULL); 18 | 19 | newNode->data = val; 20 | newNode->next = NULL; 21 | 22 | if (*(head) == NULL) 23 | { 24 | *(head) = newNode; 25 | return (newNode); 26 | } 27 | 28 | struct node *lastNode = *head; 29 | while (lastNode->next != NULL) 30 | { 31 | lastNode = lastNode->next; 32 | } 33 | 34 | lastNode->next = newNode; 35 | 36 | return (newNode); 37 | } 38 | -------------------------------------------------------------------------------- /data_structure/singly_linked_list/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H_ 2 | #define LISTS_H_ 3 | 4 | #include 5 | #include 6 | 7 | struct node 8 | { 9 | int data; 10 | struct node *next; 11 | }; 12 | int print_node(struct node *head); 13 | struct node *insert_at_beginning(struct node **head, int val); 14 | struct node *insert_at_end(struct node **head, int val); 15 | void delete_node(struct node **head, int key); 16 | int search_node(struct node *head, int key); 17 | 18 | #endif /* LISTS_H_ */ -------------------------------------------------------------------------------- /data_structure/singly_linked_list/main.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | int main(void) 4 | { 5 | struct node *head = NULL; 6 | int res; 7 | 8 | insert_at_beginning(&head, 10); 9 | insert_at_beginning(&head, 20); 10 | insert_at_beginning(&head, 30); 11 | insert_at_beginning(&head, 40); 12 | insert_at_beginning(&head, 50); 13 | insert_at_end(&head, 20); 14 | insert_at_end(&head, 30); 15 | insert_at_end(&head, 40); 16 | delete_node(&head, 10); 17 | delete_node(&head, 20); 18 | delete_node(&head, 20); 19 | delete_node(&head, 50); 20 | print_node(head); 21 | 22 | res = search_node(head, 40); 23 | 24 | if (res) 25 | printf("Found\n"); 26 | else 27 | printf("Not found\n"); 28 | return (0); 29 | } -------------------------------------------------------------------------------- /data_structure/singly_linked_list/print_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_node - print singly linked list data 4 | * 5 | * @head: pointer to the head 6 | */ 7 | 8 | int print_node(struct node *head) 9 | { 10 | struct node *tmp = head; 11 | int counter = 0; 12 | 13 | while (tmp != NULL) 14 | { 15 | printf("%d\n", tmp->data); 16 | tmp = tmp->next; 17 | counter++; 18 | } 19 | return (counter); 20 | } 21 | -------------------------------------------------------------------------------- /data_structure/singly_linked_list/search_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * search_node - search node in singly linked list 4 | * @key: key to find 5 | * 6 | * Return: Found otherwise Not found 7 | */ 8 | int search_node(struct node *head, int key) 9 | { 10 | struct node *tmp; 11 | int flag = 0; 12 | 13 | tmp = head; 14 | while (tmp != NULL) 15 | { 16 | if (tmp->data == key) 17 | { 18 | flag = 1; 19 | break; 20 | } 21 | tmp = tmp->next; 22 | } 23 | return (flag); 24 | } -------------------------------------------------------------------------------- /data_structure/stack/README.md: -------------------------------------------------------------------------------- 1 | # Stack Data Structure 2 | 3 | Implementation of `stack data structure` using: 4 | - Array data structure 5 | - linked list data structure 6 | 7 | 8 | ## Queue Functions 9 | - Push 10 | - pop 11 | - is_empty 12 | - is_full 13 | - print_stack -------------------------------------------------------------------------------- /java/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /java/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /java/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /java/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /java/creatingClass/Dog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/creatingClass/Dog.class -------------------------------------------------------------------------------- /java/creatingClass/Person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/creatingClass/Person.class -------------------------------------------------------------------------------- /java/creatingClass/Student.java: -------------------------------------------------------------------------------- 1 | public class Student { 2 | private final String id; 3 | private final String firstName; 4 | private final String lastName; 5 | 6 | public Student(String id, String firstName, String lastName) { 7 | this.id = id; 8 | this.firstName = firstName; 9 | this.lastName = lastName; 10 | } 11 | 12 | public String getId() { 13 | return id; 14 | } 15 | 16 | public String getFirstName() { 17 | return firstName; 18 | } 19 | 20 | public String getLastName() { 21 | return lastName; 22 | } 23 | 24 | public void setStudent(String id, String firstName, String lastName) { 25 | this.id = id; 26 | this.firstName = firstName; 27 | this.lastName = lastName; 28 | } 29 | } -------------------------------------------------------------------------------- /java/creatingClass/personTester.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/creatingClass/personTester.class -------------------------------------------------------------------------------- /java/creatingClass/personTester.java: -------------------------------------------------------------------------------- 1 | public class personTester { 2 | 3 | public static void main(String[] args) { 4 | Person mike = new Person("Mike", "No2 Kangu, Ilorin", "Teacher"); 5 | Person bob = new Person("Bob", "Sanrab Tanke Ilorin", "Farmer"); 6 | System.out.println(mike); 7 | System.out.println(bob); 8 | } 9 | } -------------------------------------------------------------------------------- /java/javaDocProject/javaDocProject.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /java/javaDocProject/src/code/javaDocExample.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/javaDocProject/src/code/javaDocExample.class -------------------------------------------------------------------------------- /java/javaDocProject/src/code/javaDocExample.java: -------------------------------------------------------------------------------- 1 | package code; 2 | 3 | /** 4 | * This is my first javaDoc 5 | * 6 | * @author: Abeeb Raheem 7 | */ 8 | public class javaDocExample { 9 | /** 10 | * Say hi to the caller 11 | * 12 | * @name: Name of the user 13 | * @age: Age of the user 14 | * @return Greeting message with user's details 15 | */ 16 | public String greet(String name, int age) { 17 | return "Hey!"; 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /java/javaExcercise/ArrayExcercise.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/javaExcercise/ArrayExcercise.class -------------------------------------------------------------------------------- /java/javaExcercise/ArrayExcercise.java: -------------------------------------------------------------------------------- 1 | 2 | public class ArrayExcercise { 3 | 4 | public int[] fillArray() { 5 | int[] arr = new int[5]; 6 | 7 | for (int i = 0; i < arr.length; i++) { 8 | arr[i] = 5 - i; 9 | } 10 | return arr; 11 | } 12 | 13 | public static void main(String[] args) { 14 | ArrayExcercise arrayExcercise = new ArrayExcercise(); 15 | int[] arr = arrayExcercise.fillArray(); 16 | String[] words = {"Ignition sequence start!", "Liftoff!"}; 17 | 18 | System.out.println(words[0]); 19 | for (int i =0 ; i < arr.length; i++) { 20 | System.out.println(arr[i]); 21 | } 22 | System.out.println(words[1]); 23 | } 24 | } -------------------------------------------------------------------------------- /java/javaExcercise/FindTheArea.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/javaExcercise/FindTheArea.class -------------------------------------------------------------------------------- /java/javaExcercise/FindTheArea.java: -------------------------------------------------------------------------------- 1 | public class FindTheArea { 2 | 3 | public static void main(String[] args) { 4 | System.out.println(findTheArea(4.5, 3.2)); 5 | } 6 | 7 | public static double findTheArea(double height, double width) { 8 | double area = height * width; 9 | return area; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /java/javaExcercise/HelloWorld.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/javaExcercise/HelloWorld.class -------------------------------------------------------------------------------- /java/javaExcercise/HelloWorld.java: -------------------------------------------------------------------------------- 1 | public class HelloWorld { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("Hello World!"); 5 | } 6 | 7 | } -------------------------------------------------------------------------------- /java/javaExcercise/MethodExcercise.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/javaExcercise/MethodExcercise.class -------------------------------------------------------------------------------- /java/javaExcercise/MethodExcercise.java: -------------------------------------------------------------------------------- 1 | public class MethodExcercise { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("The sum is: " + MethodExcercise.addNumbers(5, 6)); 5 | System.out.println("The product is: " + MethodExcercise.multiplyNumbers(5, 6)); 6 | } 7 | 8 | public static int addNumbers(int x, int y) { 9 | return x + y; 10 | } 11 | 12 | public static int multiplyNumbers(int x, int y) { 13 | return x * y; 14 | } 15 | } -------------------------------------------------------------------------------- /java/javaExcercise/ServiceTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/java/javaExcercise/ServiceTest.class -------------------------------------------------------------------------------- /java/javaExcercise/ServiceTest.java: -------------------------------------------------------------------------------- 1 | public class ServiceTest { 2 | private String serviceId; 3 | 4 | private String generateServiceId() { 5 | if (serviceId == null) { 6 | return "1111"; 7 | } 8 | return serviceId; 9 | } 10 | 11 | public String getServiceId() { 12 | return generateServiceId(); 13 | } 14 | 15 | // Entry point 16 | public static void main(String[] args) { 17 | ServiceTest serviceTest = new ServiceTest(); 18 | 19 | System.out.println(serviceTest.getServiceId()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/lesson3/src/abstractClass/Car.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.abstractClass; 2 | 3 | public class Car extends Vehicle { 4 | 5 | public Car() { 6 | super("Car start", "Car stop", "Car direction"); 7 | } 8 | 9 | @Override 10 | public void speed() { 11 | System.out.println("55"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java/lesson3/src/abstractClass/Vehicle.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.abstractClass; 2 | 3 | public abstract class Vehicle { 4 | protected String start; 5 | protected String stop; 6 | protected String direction; 7 | 8 | public Vehicle(String start, String stop, String direction) { 9 | this.start = start; 10 | this.stop = stop; 11 | this.direction = direction; 12 | } 13 | 14 | public abstract void speed(); 15 | 16 | } -------------------------------------------------------------------------------- /java/lesson3/src/abstractClass/VehicleTester.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.abstractClass; 2 | 3 | public class VehicleTester { 4 | 5 | public static void main(String[] args) { 6 | Car benz = new Car(); 7 | 8 | benz.speed(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/lesson3/src/inheritanceExample/Person.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.inheritanceExample; 2 | 3 | public class Person { 4 | private String firstName; 5 | private String lastName; 6 | 7 | public Person(String firstName, String lastName) { 8 | super(); 9 | this.firstName = firstName; 10 | this.lastName = lastName; 11 | } 12 | 13 | public String getFirstName() { 14 | return firstName; 15 | } 16 | 17 | public void setFirstName(String firstname) { 18 | this.firstName = firstname; 19 | } 20 | 21 | public String getLastName() { 22 | return lastName; 23 | } 24 | 25 | public void setLastName(String lastName) { 26 | this.lastName = lastName; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "Name: " + firstName + " " + lastName; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /java/lesson3/src/inheritanceExample/PersonTester.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.inheritanceExample; 2 | 3 | public class PersonTester { 4 | public static void main(String[] args) { 5 | Person john = new Person("James", "John"); 6 | Student james = new Student("James", "Daniel", "00123"); 7 | StudentEmployee mike = new StudentEmployee("James", "Daniel", "00123", 45.6, "emp001"); 8 | 9 | System.out.println(john); 10 | System.out.println(james); 11 | System.out.println(mike); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java/lesson3/src/inheritanceExample/Student.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.inheritanceExample; 2 | 3 | public class Student extends Person{ 4 | private String studentId; 5 | 6 | public Student (String firstName, String lastName, String studentId) { 7 | super(firstName, lastName); 8 | this.studentId = studentId; 9 | } 10 | 11 | public String getStudentId() { 12 | return studentId; 13 | } 14 | 15 | public void setStudentId(String studentId) { 16 | this.studentId = studentId; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return super.toString() + " Student ID:" + studentId; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/lesson3/src/interfaceExample/Car.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.interfaceExample; 2 | 3 | public class Car implements Vehicle { 4 | private String type; 5 | private String speed; 6 | private String color; 7 | 8 | public Car(String type, String speed, String color) { 9 | super(); 10 | this.type = type; 11 | this.speed = speed; 12 | this.color = color; 13 | } 14 | 15 | @Override 16 | public String getType() { 17 | return type; 18 | 19 | } 20 | 21 | @Override 22 | public String getSpeed() { 23 | return speed; 24 | 25 | } 26 | 27 | @Override 28 | public String getColor() { 29 | return color; 30 | 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /java/lesson3/src/interfaceExample/Vehicle.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.interfaceExample; 2 | 3 | public interface Vehicle { 4 | 5 | public String getType(); 6 | public String getSpeed(); 7 | public String getColor(); 8 | } 9 | -------------------------------------------------------------------------------- /java/lesson3/src/polymorphismExample/Boat.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.polymorphismExample; 2 | 3 | public class Boat extends Vehicle { 4 | public Boat() { 5 | super("Boat start", "Boat stop", "Boat speed", "Boat direction"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /java/lesson3/src/polymorphismExample/Car.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.polymorphismExample; 2 | 3 | public class Car extends Vehicle { 4 | 5 | public Car() { 6 | super("Car start", "Car stop", "Car speed", "Car direction"); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /java/lesson3/src/polymorphismExample/Plane.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.polymorphismExample; 2 | 3 | public class Plane extends Vehicle{ 4 | public Plane() { 5 | super("Plane start", "Plane stop", "Plane speed", "Plane direction"); 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /java/lesson3/src/polymorphismExample/Vehicle.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.polymorphismExample; 2 | 3 | public class Vehicle { 4 | protected String start; 5 | protected String stop; 6 | protected String speed; 7 | protected String direction; 8 | 9 | public Vehicle(String start, String stop, String speed, String direction) { 10 | this.start = start; 11 | this.stop = stop; 12 | this.speed = speed; 13 | this.direction = direction; 14 | } 15 | 16 | public void start() { 17 | System.out.println(start); 18 | } 19 | 20 | public void stop() { 21 | System.out.println(stop); 22 | } 23 | 24 | public void speed() { 25 | System.out.println(speed); 26 | } 27 | 28 | public void direction() { 29 | System.out.println(direction); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /java/lesson3/src/polymorphismExample/VehicleTester.java: -------------------------------------------------------------------------------- 1 | package lesson3.src.polymorphismExample; 2 | 3 | public class VehicleTester { 4 | 5 | public static void main(String[] args) { 6 | Vehicle[] vehicles = new Vehicle[3]; 7 | 8 | vehicles[0] = new Car(); 9 | vehicles[1] = new Boat(); 10 | vehicles[2] = new Plane(); 11 | 12 | for (int i = 0; i < vehicles.length; i++) { 13 | vehicles[i].speed(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/lesson4/src/DateAndCalender/Transaction.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.DateAndCalender; 2 | 3 | import java.util.Date; 4 | 5 | public class Transaction { 6 | 7 | private String description; 8 | private Date date; 9 | private String account; 10 | private double amount; 11 | 12 | public Transaction(String description, Date date, String account, double amount) { 13 | this.description = description; 14 | this.date = date; 15 | this.account = account; 16 | this.amount = amount; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "Transaction{" + 22 | "description='" + description + '\'' + 23 | ", date='" + date + 24 | ", account='" + account + '\'' + 25 | ", amount=" + amount + 26 | '}'; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /java/lesson4/src/DateAndCalender/TransactionTester.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.DateAndCalender; 2 | 3 | import java.util.Date; 4 | 5 | public class TransactionTester { 6 | 7 | public static void main(String[] args) { 8 | Transaction transaction = new Transaction("Withdrawal", new Date(), "1111", 567.50); 9 | 10 | System.out.println(transaction); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /java/lesson4/src/RegexExample/Person.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.RegexExample; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class Person { 6 | private String name; 7 | private String email; 8 | private final String emailRegex = "^(.+)@(.+).com$"; 9 | private final Pattern pattern = Pattern.compile(emailRegex); 10 | 11 | public Person(String name, String email) { 12 | if (!pattern.matcher(email).matches()) { 13 | throw new IllegalArgumentException("Error, Invalid email!"); 14 | } 15 | this.name = name; 16 | this.email = email; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "Person{"+ 22 | "Name:'" + name + '\'' + 23 | ", Email:'" + email + '\'' + 24 | "}"; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /java/lesson4/src/RegexExample/PersonTester.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.RegexExample; 2 | 3 | public class PersonTester { 4 | 5 | public static void main(String[] args) { 6 | Person person = new Person("Abeeb", "abeeb@gmail.com"); 7 | 8 | System.out.println(person); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/lesson4/src/RegexExample/regEXTester.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.RegexExample; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class regEXTester { 6 | 7 | 8 | public static void main(String[] args) { 9 | String emailRegex = "^(.+)@(.+).com$"; 10 | Pattern pattern = Pattern.compile(emailRegex); 11 | String email = "jeffy@gmail.com"; 12 | 13 | System.out.println(pattern.matcher(email).matches()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java/lesson4/src/ScannerExample/UserInputTester.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.ScannerExample; 2 | 3 | import java.util.*; 4 | 5 | public class UserInputTester { 6 | 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | try { 10 | System.out.println("Enter a string"); 11 | String userInput = scanner.nextLine(); 12 | System.out.println("The user input: " + userInput); 13 | } catch (Exception ex) { 14 | ex.getLocalizedMessage(); 15 | } finally { 16 | scanner.close(); 17 | } 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /java/lesson4/src/StringMethods/StringMethods.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.StringMethods; 2 | 3 | public class StringMethods { 4 | 5 | 6 | public static void main(String[] args) { 7 | String text = "Hello"; 8 | System.out.println(text.charAt(2)); 9 | 10 | System.out.println(text.equalsIgnoreCase("hello")); 11 | 12 | System.out.println(text.contains("el")); 13 | 14 | String commaSeparatedText = "This, is, a, goal!"; 15 | String[] stringArray = commaSeparatedText.split(","); 16 | 17 | for (int i = 0; i < stringArray.length; i++) { 18 | System.out.println(stringArray[i]); 19 | } 20 | 21 | System.out.println(commaSeparatedText.substring(0, 4)); 22 | 23 | System.out.println(text.replace('e', 'a')); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/lesson4/src/enumerations/Light.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.enumerations; 2 | 3 | public class Light { 4 | public Light(){}; 5 | 6 | enum StopLight { 7 | GREEN, YELLOW, RED; 8 | } 9 | 10 | public void changeLight(StopLight currentLight) { 11 | if (currentLight == StopLight.GREEN) { 12 | System.out.println("Green means GO!"); 13 | } else if (currentLight == StopLight.YELLOW) { 14 | System.out.println("Yellow means SLOW DOWN!"); 15 | } else if (currentLight == StopLight.RED) { 16 | System.out.println("Red means STOP!"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/lesson4/src/enumerations/LightChanger.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.enumerations; 2 | 3 | import lesson4.src.enumerations.Light.StopLight; 4 | 5 | public class LightChanger { 6 | 7 | public static void main(String[] args) { 8 | Light light = new Light(); 9 | light.changeLight(StopLight.RED); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /java/lesson4/src/enumerations/StopLight.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.enumerations; 2 | 3 | public enum StopLight { 4 | GREEN, YELLOW, RED; 5 | } 6 | -------------------------------------------------------------------------------- /java/lesson4/src/exceptionExample/PhoneExceptionTester.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.exceptionExample; 2 | 3 | public class PhoneExceptionTester { 4 | 5 | public static void main(String[] args) { 6 | 7 | String[] numbers = new String[] {"123-456", null, "567-897", "123-095"}; 8 | 9 | for (int i = 0; i < numbers.length; i++) { 10 | try { 11 | System.out.println(new Phone("redmi", numbers[i])); 12 | } catch (IllegalArgumentException ex) { 13 | System.out.println(ex.getLocalizedMessage()); 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/lesson4/src/scanner/ScannerMethods.java: -------------------------------------------------------------------------------- 1 | package lesson4.src.scanner; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ScannerMethods { 6 | public static void main(String[] args) { 7 | try (Scanner scanner = new Scanner("This is a scanner")) { 8 | while (scanner.hasNext()) { 9 | System.out.println(scanner.next()); 10 | } 11 | } 12 | 13 | // System.out.println(scanner.nextLine()); 14 | // System.out.println(scanner.nextInt()); 15 | // System.out.println(scanner.next()); 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/lesson5/src/CollectionsExample/CollectionExcercise.java: -------------------------------------------------------------------------------- 1 | package lesson5.src.CollectionsExample; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | public class CollectionExcercise { 7 | public static void main(String[] args) { 8 | List listOfItems = new LinkedList(); 9 | 10 | listOfItems.add("Mike"); 11 | listOfItems.add("Bob"); 12 | listOfItems.add("Alice"); 13 | 14 | for (Object list: listOfItems) { 15 | System.out.println(list); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/lesson5/src/GenericExample/GenericExcercise.java: -------------------------------------------------------------------------------- 1 | package lesson5.src.GenericExample; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class GenericExcercise { 6 | 7 | public static void main(String[] args) { 8 | ArrayList variables = new ArrayList(); 9 | 10 | Double doubleNumber = 123.45; 11 | String stringText = "Hey there!"; 12 | Integer integerNumber = 1234; 13 | Character letter = 'Z'; 14 | 15 | variables.add(doubleNumber); 16 | variables.add(stringText); 17 | variables.add(integerNumber); 18 | variables.add(letter); 19 | 20 | for (Object variable: variables) { 21 | GenericExcercise.displayClassName(variable); 22 | } 23 | } 24 | 25 | static void displayClassName(T variable) { 26 | System.out.println(variable.getClass().getName()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java/lesson5/src/IteratorExample/IteratorExample.java: -------------------------------------------------------------------------------- 1 | package lesson5.src.IteratorExample; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | 8 | public class IteratorExample { 9 | 10 | public static void main(String[] args) { 11 | List names = new ArrayList(); 12 | 13 | names.add("Mike"); 14 | names.add("Bob"); 15 | names.add("Alice"); 16 | 17 | // Convert List of strings to an iterator 18 | Iterator iterator = names.iterator(); 19 | 20 | while(iterator.hasNext()) { 21 | System.out.println(iterator.next()); 22 | } 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/lesson5/src/SortingCollection/Person.java: -------------------------------------------------------------------------------- 1 | package lesson5.src.SortingCollection; 2 | 3 | public class Person implements Comparable{ 4 | 5 | public String name; 6 | 7 | public Person(String name) { 8 | this.name = name; 9 | } 10 | 11 | public int compareTo(Person person) { 12 | return this.name.compareTo(person.name); 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "Name: " + name; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/lesson5/src/SortingCollection/PersonSort.java: -------------------------------------------------------------------------------- 1 | package lesson5.src.SortingCollection; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class PersonSort { 7 | public static void main(String[] args) { 8 | ArrayList people = new ArrayList(); 9 | 10 | 11 | people.add(new Person("James")); 12 | people.add(new Person("Bob")); 13 | people.add(new Person("Michael")); 14 | 15 | Collections.sort(people); 16 | 17 | for (Person person: people) { 18 | System.out.println(person); 19 | } 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/lesson5/src/SortingCollection/StringSorting.java: -------------------------------------------------------------------------------- 1 | package lesson5.src.SortingCollection; 2 | 3 | import java.util.Collections; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | 7 | public class StringSorting { 8 | public static void main(String[] args) { 9 | List names = new LinkedList(); 10 | 11 | names.add("Zen"); 12 | names.add("james"); 13 | names.add("Bob"); 14 | 15 | for (String name: names) { 16 | System.out.println(name); 17 | } 18 | 19 | // Sorting 20 | Collections.sort(names); 21 | System.out.println("................Sorting.............."); 22 | for (String name: names) { 23 | System.out.println(name); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/lesson5/src/SortingCollection/WrapperSorting.java: -------------------------------------------------------------------------------- 1 | package lesson5.src.SortingCollection; 2 | 3 | import java.util.Collections; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | 7 | public class WrapperSorting { 8 | 9 | public static void main(String[] args) { 10 | List nums = new LinkedList(); 11 | 12 | nums.add(302); 13 | nums.add(102); 14 | nums.add(123); 15 | 16 | for (Integer num: nums) { 17 | System.out.println(num); 18 | } 19 | 20 | // Sorting 21 | Collections.sort(nums); 22 | System.out.println("................Sorting.............."); 23 | 24 | for (Integer num: nums) { 25 | System.out.println(num); 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java/lesson6/src/mapsExample/Person.java: -------------------------------------------------------------------------------- 1 | package lesson6.src.mapsExample; 2 | 3 | public class Person { 4 | private String name; 5 | private String email; 6 | 7 | 8 | public Person(String name, String email) { 9 | this.name = name; 10 | this.email = email; 11 | } 12 | 13 | public String getName() { return name; } 14 | 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | 19 | public String getEmail() { return email; } 20 | 21 | public void setEmail(String email) { 22 | this.email = email; 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return "{" + 28 | "Name='" + name + '\'' + 29 | ", Email='" + email + '\'' + 30 | "}"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /java/lesson6/src/overridingHashAndEqual/People.java: -------------------------------------------------------------------------------- 1 | package lesson6.src.overridingHashAndEqual; 2 | 3 | import java.util.Objects; 4 | 5 | public class People { 6 | // private String name; 7 | private String address; 8 | // private String number; 9 | 10 | @Override 11 | public boolean equals(Object o) { 12 | if (this == o) return true; 13 | if (this == null || getClass() != o.getClass()) return false; 14 | People people = (People) o; 15 | return address.equals(people.address); 16 | } 17 | 18 | @Override 19 | public int hashCode() { 20 | return Objects.hash(address); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/lesson6/src/queueExample/queueExcercise.java: -------------------------------------------------------------------------------- 1 | package lesson6.src.queueExample; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class queueExcercise { 7 | 8 | public static void main(String[] args) { 9 | Queue queuedCustomer = new LinkedList<>(); 10 | 11 | queuedCustomer.add("1230"); 12 | queuedCustomer.add("3421"); 13 | queuedCustomer.add("4231"); 14 | queuedCustomer.add("0123"); 15 | 16 | while (!queuedCustomer.isEmpty()) { 17 | System.out.println("Customer " + queuedCustomer.poll() + " is getting helped"); 18 | } 19 | 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /mysql_practice/alias.sql: -------------------------------------------------------------------------------- 1 | -- ALIAS STATEMENT 2 | -- MySQL alias for columns 3 | 4 | SELECT 5 | CONCAT_WS(', ', firstName, lastName) AS fullName 6 | FROM 7 | employees; 8 | 9 | SELECT "--"; 10 | SELECT 11 | CONCAT_WS(', ', firstName, lastName) AS fullName 12 | FROM 13 | employees 14 | ORDER BY 15 | fullName; 16 | 17 | 18 | SELECT "--"; 19 | -- selects the orders whose total amount is greater than 60000 20 | SELECT 21 | orderNumber `order no`, 22 | SUM(quantityOrdered * priceEach) total 23 | FROM 24 | orderdetails 25 | GROUP BY 26 | `order no`; 27 | HAVING total > 60000; -------------------------------------------------------------------------------- /mysql_practice/crossjoin.sql: -------------------------------------------------------------------------------- 1 | -- List all sales 2 | 3 | -- SELECT 4 | -- product_name, 5 | -- store_name, 6 | -- quantity, 7 | -- FROM 8 | -- sales 9 | -- INNER JOIN 10 | -- products ON products.id = sales.product_id 11 | -- INNER JOIN 12 | -- stores ON stores.id = sales.store_id 13 | -- GROUP BY 14 | -- store_names, product_name; 15 | 16 | SELECT 17 | store_name, 18 | product_name, 19 | SUM(quantity * price) AS revenue 20 | FROM 21 | sales 22 | INNER JOIN 23 | products ON products.id = sales.product_id 24 | INNER JOIN 25 | stores ON stores.id = sales.store_id 26 | GROUP BY 27 | store_name, product_name 28 | ORDER BY 29 | store_name; 30 | 31 | SELECT "--"; 32 | SELECT 33 | store_name, product_name 34 | FROM 35 | stores AS a 36 | CROSS JOIN 37 | products AS b; 38 | 39 | -------------------------------------------------------------------------------- /mysql_practice/distinct.sql: -------------------------------------------------------------------------------- 1 | -- SELECT DISTINCT STATEMENT 2 | 3 | -- List all employee lastname 4 | SELECT 5 | lastName 6 | FROM 7 | employees 8 | ORDER BY lastName; 9 | 10 | SELECT "---"; 11 | 12 | -- List distinct all employee lastName 13 | SELECT 14 | DISTINCT lastName 15 | FROM 16 | employees 17 | ORDER BY lastName; 18 | 19 | SELECT "---"; 20 | 21 | -- List all distinct customer state and city 22 | SELECT 23 | DISTINCT state, city 24 | FROM 25 | customers 26 | WHERE 27 | state IS NOT NULL 28 | ORDER BY 29 | state, city; 30 | 31 | SELECT "---"; 32 | -- List all customer state and city 33 | 34 | SELECT 35 | state, city 36 | FROM 37 | customers 38 | WHERE 39 | state IS NOT NULL 40 | ORDER BY 41 | state, city; 42 | -------------------------------------------------------------------------------- /mysql_practice/init_join.sql: -------------------------------------------------------------------------------- 1 | -- Create member and committee table 2 | 3 | DROP TABLE IF EXISTS members; 4 | 5 | CREATE TABLE IF NOT EXISTS members( 6 | member_id INT AUTO_INCREMENT, 7 | name VARCHAR(255), 8 | PRIMARY KEY (member_id) 9 | ); 10 | 11 | DROP TABLE IF EXISTS committees; 12 | 13 | CREATE TABLE IF NOT EXISTS committees( 14 | committee_id INT AUTO_INCREMENT, 15 | name VARCHAR(255), 16 | PRIMARY KEY (committee_id) 17 | ); 18 | 19 | INSERT INTO members (name) 20 | VALUES ('Jane'),('Kane'),('Ken'),('Bob'),('Mark'),('Mike'); 21 | 22 | INSERT INTO committees (name) 23 | VALUES ('Bob'),('Ken'),('Jane'),('Ben'); -------------------------------------------------------------------------------- /mysql_practice/order_by.sql: -------------------------------------------------------------------------------- 1 | -- ORDER BY STATEMENT 2 | 3 | SELECT contactLastName, contactFirstName 4 | FROM customers 5 | ORDER BY contactLastName DESC, contactFirstName ASC; 6 | 7 | 8 | SELECT 9 | orderNumber, 10 | orderLineNumber, 11 | quantityOrdered * priceEach AS subtotal 12 | FROM orderdetails 13 | ORDER BY subtotal DESC; 14 | 15 | 16 | SELECT 17 | orderNumber, status 18 | FROM 19 | orders 20 | ORDER BY FIELD(status, 21 | 'In Process', 22 | 'On Hold', 23 | 'Cancelled', 24 | 'Resolved', 25 | 'Disputed', 26 | 'Shipped'); 27 | 28 | SELECT 29 | firstname, 30 | lastname, 31 | reportsTo 32 | FROM 33 | employees 34 | ORDER BY reportsTo; -------------------------------------------------------------------------------- /mysql_practice/select.sql: -------------------------------------------------------------------------------- 1 | -- select statement 2 | 3 | SELECT * FROM employees; 4 | 5 | SELECT firstname, lastname, jobtitle 6 | FROM employees; 7 | 8 | SELECT 1 + 1; 9 | 10 | SELECT NOW(); 11 | 12 | SELECT CONCAT("Abeeb ", "Raheem"); 13 | 14 | SELECT CONCAT("Abeeb ", "Raheem") AS fullname; -------------------------------------------------------------------------------- /pointer_in_c/README.md: -------------------------------------------------------------------------------- 1 | # Advanced pointer in c 2 | 3 | The Pointer in C, is a variable that stores address of another variable. 4 | A pointer can also be used to refer to another pointer function. 5 | A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. 6 | The purpose of pointer is to save memory space and achieve faster execution time. 7 | 8 | ## Pointer concepts 9 | - Array and Pointer 10 | - String and Pointer 11 | - Function Pointer 12 | - Struct and Pointer 13 | - File Pointer 14 | - Null Pointer 15 | - Pointer comparison -------------------------------------------------------------------------------- /pointer_in_c/array_of_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - assigns and prints value to array of pointer 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | /* Pointer to entire array */ 10 | int *arr[5]; 11 | int a, b, c, d, e; 12 | 13 | // initialize 5 integers 14 | a = 5; b = 10; c = 15; d = 20; e = 25; 15 | 16 | // initialize arr of 5 pointer 17 | arr[0] = &a; 18 | arr[1] = &b; 19 | arr[2] = &c; 20 | arr[3] = &d; 21 | arr[4] = &e; 22 | 23 | for (int i = 0; i < 5; i++) 24 | printf("arr[%d] = %p \t *(arr[%d]) = %d\n", i, arr[i], i, *(arr[i])); 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /pointer_in_c/file_pointer/file_copy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define size 20 3 | /** 4 | * main - reads and copy to another file 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | int main(void) 9 | { 10 | FILE *fr = fopen("text.txt", "r"); 11 | FILE *fw = fopen("file_copy.txt", "w"); 12 | char str[size]; 13 | 14 | if (fr && fw) 15 | { 16 | while (fgets(str, size, fr) != NULL) 17 | { 18 | fputs(str, fw); 19 | } 20 | fclose(fr); 21 | fclose(fw); 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /pointer_in_c/file_pointer/read_char.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - reads from the file character by character 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | FILE *fp; 12 | 13 | fp = fopen("text.txt", "r"); 14 | 15 | if (fp != NULL) 16 | { 17 | char letter = fgetc(fp); 18 | 19 | while (letter != EOF) 20 | { 21 | printf("%c", letter); 22 | 23 | letter = fgetc(fp); 24 | } 25 | } 26 | fclose(fp); 27 | printf("\n"); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /pointer_in_c/file_pointer/read_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define size 10 3 | /** 4 | * main - reads from the file string by string 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | int main(void) 9 | { 10 | FILE *fp = fopen("new.txt", "r"); 11 | char str[size]; 12 | 13 | 14 | if (fp != NULL) 15 | { 16 | while (fgets(str, size, fp) != NULL) 17 | { 18 | printf("%s", str); 19 | } 20 | fclose(fp); 21 | } 22 | 23 | printf("\n"); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /pointer_in_c/file_pointer/write_char.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define size 10 3 | /** 4 | * main - writes to the file string by string 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | int main(void) 9 | { 10 | FILE *fp = fopen("new.txt", "w"); 11 | char str[size]; 12 | 13 | if (fp != NULL) 14 | { 15 | fputc('H', fp); 16 | fputc('e', fp); 17 | fputc('y', fp); 18 | 19 | fclose(fp); 20 | } 21 | 22 | printf("\n"); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /pointer_in_c/file_pointer/write_str.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define size 20 3 | /** 4 | * main - writes to the file string by string 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | int main(void) 9 | { 10 | FILE *fp = fopen("new.txt", "w"); 11 | char str[size]; 12 | 13 | 14 | if (fp != NULL) 15 | { 16 | fputs("Hello ", fp); 17 | 18 | printf("Enter your name: "); 19 | scanf("%s", str); 20 | 21 | fputs(str, fp); 22 | fclose(fp); 23 | } 24 | 25 | printf("\n"); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /pointer_in_c/function_pointer/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/pointer_in_c/function_pointer/a.out -------------------------------------------------------------------------------- /pointer_in_c/function_pointer/func_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int a, int b); 4 | /** 5 | * main - read and prints element of arr using pointer 6 | * 7 | * Return: 0 always (success) 8 | */ 9 | 10 | int main(void) 11 | { 12 | // Declare function pointer 13 | int (*fptr)(int, int); 14 | 15 | // Assign value to function pointer 16 | fptr = add; 17 | 18 | // Invoke function pointer 19 | int sum = (*fptr)(2, 4); 20 | 21 | printf("%d\n", sum); 22 | 23 | return (0); 24 | } 25 | 26 | 27 | /** 28 | * add - add two integers 29 | * @a: first operand 30 | * @b: second operand 31 | * Return: 32 | */ 33 | 34 | int add(int a, int b) 35 | { 36 | return (a+b); 37 | } -------------------------------------------------------------------------------- /pointer_in_c/function_pointer/swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | typedef void (*FP)(char *, char *); 3 | 4 | 5 | /** 6 | * swap - swaps two characters 7 | * @a: first operand 8 | * @b: second operand 9 | * Return: answer 10 | */ 11 | 12 | void swap(char *a, char *b) 13 | { 14 | char tmp; 15 | 16 | tmp = *a; 17 | *a = *b; 18 | *b = tmp; 19 | } 20 | 21 | /** 22 | * main - prints the resukt of swap using function pointer 23 | * 24 | * Return: 0 always (success) 25 | */ 26 | 27 | int main(void) 28 | { 29 | //Edit below this line 30 | FP fptr; 31 | char ch1 = 'a'; 32 | char ch2 = 'z'; 33 | 34 | fptr = swap; 35 | (*fptr)(&ch1, &ch2); 36 | 37 | printf("%c-%c\n", ch1, ch2); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /pointer_in_c/null_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - checks null pointer 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int a[5] = {1, 4, 8, 12, 16}; 12 | 13 | 14 | int *ptr = NULL; 15 | int *ptr2 = a; 16 | 17 | if (ptr == NULL) 18 | printf("ptr points to Nothing!\n"); 19 | 20 | if (ptr2 != NULL) 21 | printf("ptr2 points to one memory address!\n"); 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /pointer_in_c/pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - read and prints element of arr using pointer 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i, arr[5]; 12 | 13 | for (i = 0; i < 5; i++) 14 | scanf("%d", arr+i); 15 | 16 | for (i = 0; i < 5; i++) 17 | printf("%p %d\n", arr+i, *(arr+i)); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /pointer_in_c/pointer_comparison1.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - compares the address of elemnt of array 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int a[5] = {1, 4, 8, 12, 16}; 10 | 11 | int *x = &a[2]; 12 | int *y = &a[3]; 13 | 14 | if (x > y) 15 | printf("Y is ahead of X\n"); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /pointer_in_c/pointer_to_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - pointer to array elements 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int arr[5] = {1, 2, 3, 4, 5}; 10 | int (*ptr)[5]; 11 | 12 | ptr = &arr; 13 | 14 | for (int i = 0; i < 5; i++) 15 | printf("arr[%d]-> *(ptr+i) = %p ptr+i = %p *(*ptr+i) = %d\n", i, *(ptr+i), ptr+i, *(*ptr+i)); 16 | 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /pointer_in_c/string_and_pointer/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/pointer_in_c/string_and_pointer/a.out -------------------------------------------------------------------------------- /pointer_in_c/string_and_pointer/array_of_pointer_str.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints base address and value of array of string 6 | * 7 | * Return: 0 always (success) 8 | */ 9 | int main(void) 10 | { 11 | char *names[6] = {"Joe", "John", "James", "Mark", "Doe"}; 12 | int i; 13 | 14 | for(i = 0; names[i] != NULL; i++) 15 | printf("&names[%d] = %p = %s\n", i, names[i], names[i]); 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /pointer_in_c/string_and_pointer/print_str.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints string starting from the last character 6 | * 7 | * Return: 0 always (success) 8 | */ 9 | int main(void) 10 | { 11 | char str[50]; 12 | int len; 13 | 14 | printf("Enter string (<= 50 characters): "); 15 | scanf("%s", str); 16 | 17 | len = strlen(str); 18 | len = len -1; 19 | 20 | for(len; len >= 0; len--) 21 | printf("%s\n", str+len); 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /pointer_in_c/string_and_pointer/str_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints address of array of character and the value at the address 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | char *str = "Hello World"; 12 | int i; 13 | 14 | for (i = 0; str[i]; i++) 15 | printf("&str[%d] = %p \t *(str+%d) = %c\n", i, str+i, i, *(str+i)); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /pointer_in_c/string_and_pointer/str_pointer2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints address of array of character and the value at the address 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | char str[11] = "Hello World"; 12 | char *ptr; 13 | int i; 14 | 15 | ptr = str; 16 | for (i = 0; ptr[i] != '\0'; i++) 17 | printf("&ptr[%d] = %p \t *(ptr+%d) = %c\n", i, ptr+i, i, *(ptr+i)); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /pointer_in_c/struct_and_pointer/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/pointer_in_c/struct_and_pointer/a.out -------------------------------------------------------------------------------- /pointer_in_c/struct_and_pointer/struct_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * struct employee - employee description 5 | * @name: name of the employeee 6 | * @age: price of the employeee 7 | * @weight: weight of the employeee 8 | */ 9 | 10 | struct employee 11 | { 12 | char name[20]; 13 | int age; 14 | float weight; 15 | }; 16 | 17 | /** 18 | * main - prints address of array of character and the value at the address 19 | * 20 | * Return: 0 always (success) 21 | */ 22 | 23 | int main(void) 24 | { 25 | 26 | struct employee e = {"John", 25, 52.76}; 27 | struct employee *ptr = &e; 28 | 29 | printf("ptr->name = %s\n", ptr->name); 30 | printf("ptr->age = %d\n", ptr->age); 31 | printf("ptr->weight = %.2f\n", ptr->weight); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /pointer_in_c/struct_and_pointer/student_struct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * struct student - student description 5 | * @name: name of the student 6 | * @age: price of the student 7 | * @roll_number: weight of the student 8 | * @dob: dob of the student 9 | */ 10 | struct student 11 | { 12 | char name[20]; 13 | int age; 14 | int roll_number; 15 | char dob[10]; 16 | }; 17 | 18 | /** 19 | * main - prints student info using struct pointer 20 | * 21 | * Return: 0 always (success) 22 | */ 23 | 24 | int main(void) 25 | { 26 | struct student s; 27 | struct student *ptr; 28 | 29 | ptr = &s; 30 | //Edit below this line 31 | printf("Enter student name age and roll number: "); 32 | scanf("%s %d %d %s", ptr->name, &ptr->age, &ptr->roll_number, ptr->dob); 33 | 34 | printf("%s %d %d %s\n", ptr->name, ptr->age, ptr->roll_number, ptr->dob); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/README.md: -------------------------------------------------------------------------------- 1 | # Number problems solution in python and c programming language -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/problem_solving_for_beginners/Number_Problems/c_programming/a.out -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/absolute_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints absolute number of a number 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int num; 10 | printf("Enter a number: "); 11 | scanf("%d", &num); 12 | 13 | //Write your code here 14 | if (num >= 0) 15 | printf("%d", num); 16 | else 17 | { 18 | num *= -1; 19 | printf("%d", num); 20 | } 21 | 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/even_odd.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - checks if num is even or odd number 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int num; 10 | printf("Enter a number: ") 11 | scanf("%d", &num); 12 | 13 | //Write your code here 14 | if (num % 2 == 0) 15 | printf("Even"); 16 | else 17 | printf("Odd"); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/factor_of_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - finds a factor of a number 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int N,X; 10 | printf("Enter a number: "); 11 | printf("Enter a divisor: "); 12 | scanf("%d%d", &N, &X); 13 | 14 | //Write your code here 15 | if (N % X == 0) 16 | printf("Yes"); 17 | else 18 | printf("No"); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/fibonnaci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * fibonacci - finds a fibonnaci of any given number 4 | * 5 | * @num: number to find its fibonnaci 6 | */ 7 | void fibonnaci(int num) 8 | { 9 | int a, b, c, i; 10 | 11 | a = 0; 12 | b = 1; 13 | for (i = 1; i <= num; i++) 14 | { 15 | printf("%d ", a); 16 | c = a + b; 17 | a = b; 18 | b = c; 19 | } 20 | } 21 | 22 | int main(void) 23 | { 24 | int n; 25 | 26 | printf("Enter a number: "); 27 | scanf("%d", &n); 28 | 29 | fibonnaci(n); 30 | putchar(10); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/palindrome_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * palindrome_number - checks wheather a number is a palindrom or not 4 | * @num: number to check 5 | */ 6 | 7 | void palindrome_number(int num) 8 | { 9 | int tmp, mod, ans = 0; 10 | 11 | tmp = num; 12 | while (num >= 1) 13 | { 14 | mod = num % 10; 15 | ans = ans * 10 + mod; 16 | num = num / 10; 17 | } 18 | 19 | if (tmp == ans) 20 | printf("Palindrome\n"); 21 | else 22 | printf("Not Palindrome\n"); 23 | } 24 | 25 | /** 26 | * main - Entry point 27 | * 28 | * Return: 0 always (success) 29 | */ 30 | int main(void) 31 | { 32 | int num; 33 | printf("Enter a positive number: "); 34 | scanf("%d", &num); 35 | 36 | palindrome(num); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/perfect_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * isperfect - checks for a perfect number 5 | * 6 | * @num: number to check 7 | * 8 | * Return: 0 (prime), 1 (Not Prime) 9 | */ 10 | int isperfect(int num) 11 | { 12 | int i, res = 1; 13 | for (i = 2; i <= sqrt(num); i++) 14 | { 15 | if (num % i == 0) 16 | { 17 | if (num / i == i) 18 | res = res + i; 19 | else 20 | res = res + i + (num / i); 21 | } 22 | } 23 | return (res); 24 | } 25 | 26 | int main(void) 27 | { 28 | int n, res; 29 | printf("Enter a positive number: "); 30 | scanf("%d", &n); 31 | 32 | res = isperfect(n); 33 | if (res == n) 34 | printf("Yes\n"); 35 | else 36 | printf("No\n"); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/positive_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - checks for positive or negative number 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int num; 11 | printf("Enter a number: "); 12 | scanf("%d", &num); 13 | 14 | //Write your code here 15 | if (num == 0) 16 | printf("Neither positive nor negative"); 17 | else if (num < 0) 18 | printf("Negative"); 19 | else 20 | printf("Positive"); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/split_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - splits number 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int num, res; 10 | printf("Enter a number: "); 11 | scanf("%d", &num); 12 | 13 | //Write your code here 14 | while (num >= 1) 15 | { 16 | res = num % 10; 17 | printf("%d\n", res); 18 | num = num / 10; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/sum_of_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - sum of digit 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int num, sum, res; 10 | printf("Enter a positive number: "); 11 | scanf("%d", &num); 12 | 13 | //Write your code here 14 | while (num >= 1) 15 | { 16 | res = num % 10; 17 | sum += res; 18 | num = num / 10; 19 | } 20 | 21 | printf("%d", sum); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/sum_of_natural_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - sum of natural number O(N) 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int N, sum; 10 | printf("Enter a number: "); 11 | scanf("%d", &N); 12 | 13 | //Write your code here 14 | int i; 15 | sum = 0; 16 | 17 | if (N <= 0) 18 | return (N); 19 | 20 | for (i = 1; i <= N; i++) 21 | sum += i; 22 | 23 | printf("%d", sum); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/sum_of_natural_number2.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - sum of natural number O(1) 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int N, sum; 10 | printf("Enter a number: "); 11 | scanf("%d", &N); 12 | 13 | //Write your code here 14 | sum = (N * (N + 1)) / 2; 15 | 16 | printf("%d", sum); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/c_programming/swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - swaps two integers 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | int a, b; 10 | printf("Enter two integers (a b): "); 11 | scanf("%d%d", &a, &b); 12 | 13 | //Write your code here 14 | int tmp; 15 | 16 | tmp = a; 17 | a = b; 18 | b = tmp; 19 | 20 | printf("%d %d\n", a, b); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/absolute_number.py: -------------------------------------------------------------------------------- 1 | num = int(input("Enter a number: ")) 2 | #write your code here 3 | if num >= 0: 4 | print(num) 5 | else: 6 | num *= -1 7 | print(num) -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/armstrong_number.py: -------------------------------------------------------------------------------- 1 | # armstrong_number - checks if a number is armstrong number or not 2 | 3 | def armStrong(num): 4 | sum = 0 5 | tmp = num 6 | 7 | while num >= 1: 8 | mod = num % 10 9 | sum = sum + mod * mod * mod 10 | num = num // 10 11 | 12 | if sum == tmp: 13 | print("Armstrong Number") 14 | else: 15 | print("Not Armstrong Number") 16 | 17 | 18 | num = int(input("Enter a positive number: ")) 19 | #write your code here 20 | armStrong(num) -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/even_odd.py: -------------------------------------------------------------------------------- 1 | num = int(input("Enter a number: ")) 2 | #write your code here 3 | 4 | if num % 2 == 0: 5 | print("Even") 6 | else: 7 | print("Odd") -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/factor_of_number.py: -------------------------------------------------------------------------------- 1 | N = int(input("Enter a number :")) 2 | X = int(input("Enter a divisor: ")) 3 | #write your code here 4 | if N % X == 0: 5 | print("Yes") 6 | else: 7 | print("No") -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/fibonnaci.py: -------------------------------------------------------------------------------- 1 | def fibonnaci(n): 2 | a = 0 3 | b = 1 4 | 5 | for i in range (0, n, 1): 6 | print(a, end=" ") 7 | c = a + b 8 | a = b 9 | b = c 10 | 11 | 12 | n = int(input("Enter a number: ")) 13 | #write your code here 14 | fibonnaci(n) 15 | print() -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/palindrome_number.py: -------------------------------------------------------------------------------- 1 | def palindrome_number(num): 2 | ans = 0 3 | tmp = num 4 | 5 | while num >= 1: 6 | mod = num % 10 7 | ans = ans * 10 + mod 8 | num = num // 10 9 | 10 | if tmp == ans: 11 | print("Palindrome") 12 | else: 13 | print("Not Palindrome") 14 | 15 | num = int(input("Enter a positive number: ")) 16 | palindrome_number(num) 17 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/perfect_number.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def isPerfect(n): 4 | root = int(math.sqrt(n)) 5 | res = 1 6 | for i in range (2, root+1): 7 | if n % i == 0: 8 | if n // i == i: 9 | res = res + i 10 | else: 11 | res = res + i + n // i 12 | return res 13 | 14 | 15 | n = int(input("Enter a positive number: ")) 16 | 17 | res = isPerfect(n) 18 | 19 | if res == n: 20 | print("Yes") 21 | else: 22 | print("No") 23 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/positive_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - checks for positive or negative number 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int num; 11 | printf("Enter a number: ") 12 | scanf("%d", &num); 13 | 14 | //Write your code here 15 | if (num == 0) 16 | printf("Neither positive nor negative"); 17 | else if (num < 0) 18 | printf("Negative"); 19 | else 20 | printf("Positive"); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/positive_negative.py: -------------------------------------------------------------------------------- 1 | num = int(input("Enter a number: ")) 2 | #write your code here 3 | 4 | if num == 0: 5 | print(" Neither positive nor negative") 6 | elif num < 0: 7 | print("Negative") 8 | else: 9 | print("Positive") -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/prime_number.py: -------------------------------------------------------------------------------- 1 | # checks for a prime number 2 | def isprime(num): 3 | flag = 0 4 | for i in range(2, num): 5 | if num % i == 0: 6 | flag = 1 7 | break 8 | return flag 9 | 10 | def print_prime(flag): 11 | if flag == 0: 12 | print("Prime number") 13 | else: 14 | print("Not a Prime number") 15 | 16 | 17 | num = int(input("Enter a positive number: ")) 18 | flag = isprime(num) 19 | print_prime(flag) 20 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/reverse_number.py: -------------------------------------------------------------------------------- 1 | num = int(input("Enter a positive number: ")) 2 | #write your code here 3 | ans = 0 4 | while num >= 1: 5 | mod = num % 10 6 | ans = ans * 10 + mod 7 | num = num // 10 8 | 9 | print(ans) -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/split_number.py: -------------------------------------------------------------------------------- 1 | num = int(input("Enter a number: ")) 2 | #write your code here 3 | 4 | while num >= 1: 5 | res = num % 10 6 | print(res) 7 | num = num // 10 -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/sum_of_digit.py: -------------------------------------------------------------------------------- 1 | num = int(input("Enter any positive number")) 2 | #write your code here 3 | sum = 0 4 | while (num >= 1): 5 | res = num % 10 6 | sum += res 7 | num = num // 10 8 | 9 | print(sum) -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/sum_of_natural_number.py: -------------------------------------------------------------------------------- 1 | N = int(input("Enter a number: ")) 2 | #write your code here 3 | 4 | sum = 0 5 | for x in range(1, N+1, 1): 6 | print(x) 7 | sum += x 8 | 9 | print(sum) 10 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/sum_of_natural_number2.py: -------------------------------------------------------------------------------- 1 | N = int(input("Enter a number: ")) 2 | #write your code here 3 | sum = (N * (N + 1)) // 2 4 | 5 | print(sum) -------------------------------------------------------------------------------- /problem_solving_for_beginners/Number_Problems/python/swap.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter the value of a: ")) 2 | b = int(input("Enter the value of b: ")) 3 | #write your code here 4 | 5 | tmp = a 6 | a = b 7 | b = tmp 8 | 9 | print(a, b) -------------------------------------------------------------------------------- /problem_solving_for_beginners/README.md: -------------------------------------------------------------------------------- 1 | # Problem Solving for Beginners Using C Programming Language and Python -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/README.md: -------------------------------------------------------------------------------- 1 | # Array problems solving in C programming language and Python -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/c-programming/find_max.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Find max elements in array 4 | * 5 | * Return: 0 Always (success) 6 | */ 7 | int main(void) 8 | { 9 | int arr[10], n, i, max, cur, next; 10 | 11 | printf("Enter the size of array: "); 12 | scanf("%d", &n); 13 | 14 | for(i = 0; i < n; i++) 15 | { 16 | printf("Enter the number of element: "); 17 | scanf("%d", &arr[i]); 18 | } 19 | 20 | max = arr[0]; 21 | for (i = 0; i < n; i++) 22 | { 23 | if (arr[i] > max) 24 | max = arr[i]; 25 | } 26 | 27 | printf("Max = %d\n", max); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/c-programming/min_and_max.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Find min and max elements in array 4 | * 5 | * Return: 0 Always (success) 6 | */ 7 | int main(void) 8 | { 9 | int arr[10], n, i, max, min; 10 | printf("Enter the size of array: "); 11 | scanf("%d", &n); 12 | 13 | for(i = 0; i < n; i++) 14 | { 15 | printf("Enter the element of array: "); 16 | scanf("%d", &arr[i]); 17 | } 18 | 19 | //Write your code here 20 | max = arr[0]; 21 | min = arr[0]; 22 | 23 | for (i = 0; i < n; i++) 24 | { 25 | if (arr[i] > max) 26 | max = arr[i]; 27 | 28 | if (arr[i] < min) 29 | min = arr[i]; 30 | } 31 | 32 | printf("Max = %d\nMin = %d\n", max, min); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/c-programming/reverse_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Reverse elements of array 4 | * 5 | * Return: 0 Always (success) 6 | */ 7 | int main(void) 8 | { 9 | int arr[10], n, i; 10 | printf("Enter the size of array: "); 11 | scanf("%d", &n); 12 | 13 | for(i = 0; i < n; i++) 14 | { 15 | printf("Enter the size of array: "); 16 | scanf("%d", &arr[i]); 17 | } 18 | 19 | //Write your code here 20 | int tmp, lastindex, half; 21 | 22 | lastindex = n - 1; 23 | half = n / 2; 24 | for (i = 0; i < half; i++) 25 | { 26 | tmp = arr[i]; 27 | arr[i] = arr[lastindex - i]; 28 | arr[lastindex - i] = tmp; 29 | } 30 | 31 | for (i = 0; i < n; i++) 32 | printf("%d", arr[i]); 33 | 34 | printf("\n"); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/c-programming/search_element.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - search key in element of array. Print "Key found" otherwise "Key not found" 4 | * 5 | * Return: 0 Always (success) 6 | */ 7 | int main(void) 8 | { 9 | int arr[10], n, i, key; 10 | printf("Enter the size of array: "); 11 | scanf("%d", &n); 12 | 13 | for(i = 0; i < n; i++) 14 | { 15 | printf("Enter the element of array: "); 16 | scanf("%d", &arr[i]); 17 | } 18 | 19 | printf("Enter the key: "); 20 | scanf("%d", &key); 21 | 22 | int flag = 0; 23 | for (i = 0; i < n; i++) 24 | { 25 | if (arr[i] == key) 26 | { 27 | flag = 1; 28 | break; 29 | } 30 | } 31 | 32 | if (flag == 1) 33 | printf("Key found\n"); 34 | else 35 | printf("Key not found\n"); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/c-programming/sum_of_2darray.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Sum of elements of 2D array 4 | * 5 | * Return: 0 Always (success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int n, i, j; 11 | printf("Enter the size of array: "); 12 | scanf("%d", &n); 13 | 14 | int arr[n][n]; 15 | 16 | for(i = 0; i < n; i++) 17 | { 18 | for(j = 0; j < n; j++) 19 | { 20 | printf("Enter the element of array: "); 21 | scanf("%d", &arr[i][j]); 22 | } 23 | } 24 | 25 | int sum = 0; 26 | for (i = 0; i < n; i++) 27 | { 28 | for (j = 0; j < n; j++) 29 | sum += arr[i][j]; 30 | } 31 | 32 | printf("%d\n", sum); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/c-programming/sum_of_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Sum of the elements of array 5 | * 6 | * Return: 0 Always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int arr[10], n, i, sum = 0; 12 | printf("Enter the size of array: "); 13 | scanf("%d", &n); 14 | 15 | for(i = 0; i < n ; i++) 16 | { 17 | printf("Enter the number of element: "); 18 | scanf("%d", &arr[i]); 19 | } 20 | 21 | //Write your code here 22 | for (i = 0; i < n; i++) 23 | sum += arr[i]; 24 | 25 | printf("Sum = %d\n", sum); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/python/count_even_and_odd_number.py: -------------------------------------------------------------------------------- 1 | # Count even and odd number in the element of array 2 | arr = [] 3 | size = int(input("Enter size of array: ")) 4 | even_number_count = odd_number_count = 0 5 | 6 | for i in range(size): 7 | i = int(input("Enter Element of array: ")) 8 | arr.append(i) 9 | 10 | #write your code here 11 | for i in arr: 12 | if i % 2 == 0: 13 | even_number_count += 1 14 | else: 15 | odd_number_count += 1 16 | 17 | print(f"Even Number Count = {even_number_count:d}") 18 | print(f"Odd Number Count = {odd_number_count:d}") 19 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/python/find_max.py: -------------------------------------------------------------------------------- 1 | # Find max elements in array 2 | 3 | arr = [] 4 | n = int(input("Enter the size of array: ")) 5 | max = 0 6 | 7 | for i in range(n): 8 | i = int(input("Enter element of array: ")) 9 | arr.append(i) 10 | 11 | #write your code here 12 | max = arr[0] 13 | for i in arr: 14 | if i > max: 15 | max = i 16 | 17 | print(f"Max = {max}") -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/python/min_and_max.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Find min and max in elements of array""" 3 | arr = [] 4 | n = int(input("Enter a size of array: ")) 5 | 6 | for i in range(n): 7 | i = int(input("Enter element of array: ")) 8 | arr.append(i) 9 | 10 | #write your code here 11 | max = arr[0] 12 | min = arr[0] 13 | 14 | for i in arr: 15 | if i > max: 16 | max = i 17 | 18 | if i < min: 19 | min = i 20 | 21 | print(f"Max = {max}") 22 | print(f"Min = {min}") -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/python/reverse_array.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Reverse array elements""" 3 | arr = [] 4 | n = int(input("Enter a size of array: ")) 5 | 6 | for i in range(n): 7 | i = int(input("Enter element of array: ")) 8 | arr.append(i) 9 | 10 | #write your code here 11 | half = n // 2 12 | lastindex = n - 1 13 | 14 | for i in range(half): 15 | tmp = arr[i] 16 | arr[i] = arr[lastindex - i] 17 | arr[lastindex - i] = tmp 18 | 19 | for i in arr: 20 | print(i) -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/python/search_element.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Search a key in elements of array""" 3 | arr = [] 4 | n = int(input("Enter a size of array: ")) 5 | 6 | for i in range(n): 7 | i = int(input("Enter element of array: ")) 8 | arr.append(i) 9 | 10 | 11 | key = int(input("Enter a key: ")) 12 | flag = 0 13 | 14 | for i in range(n): 15 | if arr[i] == key: 16 | flag = 1 17 | break 18 | 19 | if flag == 1: 20 | print("Key found") 21 | else: 22 | print("Key not found") -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/python/sum_of_2Darray.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Sum of elements of 2D array""" 3 | arr = [] 4 | n = int(input("Enter a size of array: ")) 5 | 6 | for i in range(n): 7 | col = [] 8 | for j in range(n): 9 | j = int(input("Enter element of array: ")) 10 | col.append(j) 11 | arr.append(col) 12 | 13 | #write your code here 14 | sum = 0 15 | for i in range(n): 16 | for j in range(n): 17 | sum += arr[i][j] 18 | 19 | print(sum) -------------------------------------------------------------------------------- /problem_solving_for_beginners/array_problems/python/sum_of_array.py: -------------------------------------------------------------------------------- 1 | # Sum of the elements of array 2 | 3 | arr = [] 4 | n = int(input("Enter the size of array: ")) 5 | sum = 0 6 | 7 | for i in range(n): 8 | i = int(input("Enter element of array: ")) 9 | arr.append(i) 10 | 11 | for i in arr: 12 | sum += i; 13 | 14 | print(f"Sum = {sum:d}") -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/README.md: -------------------------------------------------------------------------------- 1 | # Bitwise Operators -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/c-programming/README.md: -------------------------------------------------------------------------------- 1 | # C - Bitwise Operator -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/c-programming/check_nth_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /*** 3 | * main -Checks nth bit if it's set or unset using bitwise 4 | * 5 | * description: Bitwise LEFT SHIFT is used to shift to nth bit to check 6 | * then bitwise AND to check. zero (0) is return if it's unset 7 | * otherwise any other number aside from zero 8 | * 9 | * Return: prints ON otherwise OFF 10 | */ 11 | 12 | int main(void) 13 | { 14 | int num, n, res; 15 | 16 | printf("Enter number and nth bit you want to enable: "); 17 | scanf("%d%d", &num, &n); 18 | 19 | 20 | 21 | res = num & (1 << (n - 1)); 22 | 23 | if (res) 24 | printf("ON\n"); 25 | else 26 | printf("OFF\n"); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/c-programming/disable_nth_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /*** 3 | * main - Disable nth bit of a given number using bitwise 4 | */ 5 | int main(void) 6 | { 7 | int num, n, res; 8 | 9 | printf("Enter number and nth bit you want to disable: "); 10 | scanf("%d%d", &num,&n); 11 | 12 | //Write your code here 13 | res = num & ~(1 << (n - 1)); 14 | 15 | printf("%d\n", res); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/c-programming/enable_nth_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /*** 3 | * main -Enable nth bit using bitwise OR and LEFT SHIFT operators 4 | * Return: prints the new value 5 | */ 6 | 7 | int main(void) 8 | { 9 | int num, n, res; 10 | 11 | printf("Enter number and nth bit you want to enable: "); 12 | scanf("%d%d", &num, &n); 13 | 14 | /* 15 | * shift the bit to the nth number - 1 16 | * and enable it with bitwise OR 17 | */ 18 | res = num | (1 << (n - 1)); 19 | 20 | printf("%d\n", res); 21 | return (0); 22 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/c-programming/find_equal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /*** 4 | * main - Finds equal or not using bitwise XOR operator 5 | * 6 | * Return: prints Equal otherwise Unequal 7 | */ 8 | 9 | int main(void) 10 | { 11 | int num1, num2; 12 | 13 | printf("Enter two positive integers: "); 14 | scanf("%d%d", &num1, &num2); 15 | 16 | if ((num1 ^ num2) == 0) 17 | printf("Equal\n"); 18 | else 19 | printf("Unequal\n"); 20 | 21 | return (0); 22 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/c-programming/odd_occuring_number_in_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | /*** 3 | * main - Find odd occuring of a number in a given array using bitwise 4 | */ 5 | int main(void) 6 | { 7 | int size = 8, i, res; 8 | 9 | int arr[10] = {7, 1, 1, 1, 1, 6, 4, 4}; 10 | 11 | res = arr[0]; 12 | for (i = 0; i < size; i++) 13 | res = res ^ arr[i]; 14 | 15 | printf("%d\n", res); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/c-programming/swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /*** 4 | * main - Swap two integers using bitwise XOR operator 5 | * 6 | * Return: prints Equal otherwise Unequal 7 | */ 8 | 9 | int main(void) 10 | { 11 | int a, b, tmp; 12 | 13 | printf("Enter two positive integers: "); 14 | scanf("%d%d", &a, &b); 15 | 16 | printf("Before.... %d %d\n", a, b); 17 | 18 | tmp = a ^ b; 19 | b = tmp ^ b; 20 | a = tmp ^ a; 21 | 22 | printf("After..... %d %d\n", a, b); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/c-programming/toggle_nth_bit.c: -------------------------------------------------------------------------------- 1 | #include 2 | /*** 3 | * main - Toggle nth bit of a given number using bitwise 4 | */ 5 | int main(void) 6 | { 7 | int num, n, res; 8 | 9 | printf("Enter number and nth bit you want to disable: "); 10 | scanf("%d%d", &num,&n); 11 | 12 | //Write your code here 13 | res = num ^ (1 << (n - 1)); 14 | 15 | printf("%d\n", res); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/README.md: -------------------------------------------------------------------------------- 1 | # Python - Bitwise Operator -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/check_nth_bit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | Checks nth bit if it's set or unset using 4 | bitwise OR and LEFT SHIFT operators 5 | """ 6 | 7 | num, n = input("Enter Number and nth bit to enable: ").split() 8 | num = int(num) 9 | n = int(n) - 1 10 | 11 | res = num & (1 << n) 12 | 13 | if res: 14 | print("ON") 15 | else: 16 | print("OFF") -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/disable_nth_bit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | Disable nth bit of a given number using 4 | bitwise AND and LEFT SHIFT operators 5 | """ 6 | num, n = input("Enter Number and nth bit to disable: ").split() 7 | num = int(num) 8 | n = int(n) - 1 9 | 10 | res = num & ~(1 << n) 11 | 12 | print(res) -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/enable_nth_bit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Enable nth bit using bitwise OR and LEFT SHIFT operators""" 3 | 4 | num, n = input("Enter Number and nth bit to enable: ").split() 5 | num = int(num) 6 | n = int(n) - 1 7 | 8 | res = num | (1 << n) 9 | 10 | print(res) -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/even_or_odd.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Checks if num is Even or Odd number.""" 3 | 4 | num = int(input("Enter a positive integer: ")) 5 | 6 | if (num & 1) == 0: 7 | print("Even") 8 | else: 9 | print("Odd") 10 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/find_equal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Finds equal or not equal using bitwise XOR operator""" 3 | 4 | num1, num2 = input("Enter two positive integers: ").split() 5 | 6 | if (int(num1) ^ int(num2)) == 0: 7 | print("Equal") 8 | else: 9 | print("Unequal") -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/odd_occuring_number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | Find odd occuring of a number 4 | in a given array using bitwise 5 | """ 6 | arr = [7, 1, 1, 1, 1, 6, 4, 4] 7 | size = 8 8 | 9 | res = arr[0] 10 | for i in range(size): 11 | res = res ^ arr[i] 12 | 13 | print(res) 14 | 15 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/swap.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Swap two integers using bitwise XOR operator""" 3 | 4 | num1, num2 = input("Enter two positive integers: ").split() 5 | 6 | print("............Before swapping..........") 7 | print(num1, num2) 8 | 9 | num1 = int(num1) 10 | num2 = int(num2) 11 | 12 | num1 = num1 ^ num2 13 | num2 = num1 ^ num2 14 | num1 = num1 ^ num2 15 | 16 | 17 | print("............After swapping..........") 18 | print(num1, num2) -------------------------------------------------------------------------------- /problem_solving_for_beginners/bitwise/python/toggle_nth_bit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | Toggle nth bit of a given number using 4 | bitwise XOR and LEFT SHIFT operators 5 | """ 6 | num = int(input("Enter a number: ")) 7 | n = int(input("Enter nth bit to toggle: ")) 8 | 9 | n = n - 1 10 | res = num ^ (1 << n) 11 | 12 | print(res) -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/README.md: -------------------------------------------------------------------------------- 1 | # Pattern -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/c-programming/diagonal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints diagonal pattern 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | int main(void) 9 | { 10 | int n, i, j; 11 | 12 | printf("Enter a positive number: "); 13 | scanf("%d", &n); 14 | 15 | //Write your Code here 16 | for (i = 1; i <= n; i++) 17 | { 18 | for (j = 1; j <= n; j++) 19 | { 20 | if (i == j || i+j == n+1) 21 | printf("* "); 22 | else 23 | printf(" "); 24 | } 25 | printf("\n"); 26 | } 27 | 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/c-programming/hallow_box.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints hallow pattern 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n, i, j; 12 | 13 | printf("Enter a positive number: "); 14 | scanf("%d", &n); 15 | 16 | for (i = 1; i <= n; i++) 17 | { 18 | for (j = 1; j <= n; j++) 19 | { 20 | if (j == 1 || i == 1 || j == n || i == n) 21 | printf("* "); 22 | else 23 | printf(" "); 24 | } 25 | 26 | printf("\n"); 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/c-programming/print_box.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints n x n box pattern 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n, i, j; 12 | 13 | printf("Enter a positive number: "); 14 | scanf("%d", &n); 15 | 16 | for (i = 1; i <= n; i++) 17 | { 18 | for (j = 1; j <= n; j++) 19 | printf("* "); 20 | 21 | printf("\n"); 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/c-programming/pyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints pyramid pattern 5 | * 6 | * Return: 0 always (success) 7 | */ 8 | int main(void) 9 | { 10 | int n, i, j; 11 | 12 | printf("Enter a positive number: "); 13 | scanf("%d", &n); 14 | 15 | //Write your Code here 16 | for (i = 1; i <= n; i++) 17 | { 18 | for (j = 1; j <= i; j++) 19 | printf("* "); 20 | 21 | printf("\n"); 22 | } 23 | 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/python/diagonal.py: -------------------------------------------------------------------------------- 1 | 2 | n = int(input("Enter a positive number: ")) 3 | #write your code here 4 | for i in range(n): 5 | for j in range(n): 6 | if i == j or i+j == n-1: 7 | print("*", end=" ") 8 | else: 9 | print(" ", end=" ") 10 | print() -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/python/hallow_box.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter a positive number: ")) 2 | #write your code here 3 | for i in range(n): 4 | for j in range(n): 5 | if j == 0 or i == 0 or j == n-1 or i == n-1: 6 | print("*", end=" ") 7 | else: 8 | print(" ", end=" ") 9 | print() -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/python/print_box.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter a positive number: ")) 2 | #write your code here 3 | for i in range(n): 4 | for j in range(n): 5 | print("*", end=" ") 6 | print() -------------------------------------------------------------------------------- /problem_solving_for_beginners/patterns/python/pyramid.py: -------------------------------------------------------------------------------- 1 | # prints pyramid pattern 2 | 3 | n = int(input("Enter a positive number: ")) 4 | #write your code here 5 | for i in range(1, n+1): 6 | for j in range(i): 7 | print("*", end=" ") 8 | print() -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/README.md: -------------------------------------------------------------------------------- 1 | # String problems solution in python and c programming language -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/c-programming/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/problem_solving_for_beginners/string_problems/c-programming/a.out -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/c-programming/change_case.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Change uppercase to lowercase and vice versa 4 | * Return: 0 always (success) 5 | */ 6 | int main(void) 7 | { 8 | char str[10]; 9 | printf("Enter a string: "); 10 | scanf("%s", str); 11 | 12 | //Write your code here 13 | int i; 14 | for (i = 0; str[i] != '\0'; i++) 15 | { 16 | if (str[i] >= 65 && str[i] <= 90) 17 | str[i] += 32; 18 | else 19 | str[i] -= 32; 20 | } 21 | 22 | printf("%s\n", str); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/c-programming/count_vowel.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Count vowels in a string 4 | * 5 | * Return: 0 Always (success) 6 | */ 7 | int main(void) 8 | { 9 | char str[10]; 10 | printf("Enter a string: "); 11 | scanf("%s", str); 12 | 13 | //Write your code here 14 | char vowels[] = {'a', 'e', 'i', 'o', 'u', '\0'}; 15 | int count = 0, i, j; 16 | 17 | for (i = 0; str[i] != '\0'; i++) 18 | { 19 | for (j = 0; vowels[j] != '\0'; j++) 20 | { 21 | if (str[i] == vowels[j]) 22 | count++; 23 | } 24 | } 25 | printf("%d\n", count); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/c-programming/str_comp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strcmp - Compares two strings 5 | * @str1: first string 6 | * @str2: seconf string 7 | * 8 | * Return: Print Yes otherwise 9 | */ 10 | 11 | void _strcmp(char *str1, char *str2) 12 | { 13 | int idx = 0; 14 | 15 | while(str1[idx] != '\0' && str2[idx] != '\0' && str1[idx] == str2[idx]) 16 | { 17 | idx++; 18 | } 19 | if (str1[idx] == '\0' && str2[idx] == '\0') 20 | printf("Yes\n"); 21 | else 22 | printf("No\n"); 23 | } 24 | 25 | int main(void) 26 | { 27 | char str1[10], str2[10]; 28 | printf("Enter two strings to compare: "); 29 | scanf("%s%s", str1, str2); 30 | 31 | //Write your code here 32 | _strcmp(str1, str2); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/c-programming/str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Concatenates two strings 4 | * 5 | * Return: 0 always (success) 6 | */ 7 | int main(void) 8 | { 9 | char str1[10], str2[10], str3[20]; 10 | 11 | printf("Enter two strings: "); 12 | scanf("%s%s", str1, str2); 13 | 14 | int i, len = 0; 15 | 16 | for (i = 0; str1[i] != '\0'; i++) 17 | str3[len++] = str1[i]; 18 | 19 | for (i = 0; str2[i] != '\0'; i++) 20 | str3[len++] = str2[i]; 21 | 22 | str3[len] = '\0'; 23 | 24 | printf("%s\n", str3); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/c-programming/strcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strcpy - Copies string to another string 5 | * @src: source 6 | * @dest: destination 7 | * 8 | * Return: Pointer to the destination 9 | */ 10 | char *_strcpy(char *src, char *dest) 11 | { 12 | int i; 13 | 14 | for (i = 0; src[i] != '\0'; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | dest[i] = '\0'; 19 | 20 | return (dest); 21 | } 22 | 23 | int main(void) 24 | { 25 | char str[10], copy_str[10]; 26 | 27 | 28 | printf("Enter a string: "); 29 | scanf("%s", str); 30 | 31 | _strcpy(str, copy_str); 32 | 33 | printf("Copied string = %s\n",copy_str); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/c-programming/strlen.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Find the length of a string 4 | * 5 | * Return: 0 Always (success) 6 | */ 7 | int _strlen(char *str) 8 | { 9 | int len = 0; 10 | 11 | while (str[len] != '\0') 12 | len++; 13 | 14 | return (len); 15 | } 16 | int main(void) 17 | { 18 | char str[1024]; 19 | printf("Enter a string: "); 20 | scanf("%s", str); 21 | 22 | //Write your code here 23 | int len = 0; 24 | 25 | len = _strlen(str); 26 | 27 | printf("%d %d\n", len); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/python/change_case.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | str1 = input("Enter a string: ") 3 | strlen = 0 4 | newstr = "" 5 | 6 | for i in str1: 7 | strlen += 1 8 | 9 | """Change uppercase to lowercase and vice versa""" 10 | # for i in range(strlen): 11 | for ch in str1: 12 | if (ch >= 'A') and (ch <= 'Z'): 13 | newstr += chr(ord(ch) + 32) 14 | elif (ch >= 'a') and (ch <= 'z'): 15 | newstr += chr(ord(ch) - 32) 16 | 17 | print(newstr) 18 | 19 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/python/count_vowel.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Count vowels in a string""" 3 | 4 | str1 = input("Enter a string: ") 5 | length = 0 6 | 7 | for i in str1: 8 | length += 1 9 | #write your code here 10 | 11 | count = 0 12 | 13 | for ch in str1: 14 | if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u': 15 | count += 1 16 | 17 | print(count) -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/python/len.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Length of a string""" 3 | str1 = input("Enter a string: ") 4 | #write your code here 5 | len = 0 6 | 7 | for i in str1: 8 | len += 1 9 | 10 | print(len) -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/python/rev_str.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | def length(string): 3 | """Length of a string""" 4 | count = 0 5 | for i in string: 6 | count += 1 7 | return count 8 | 9 | def rev_str(string): 10 | """Reverse string""" 11 | rev_str = '' 12 | strlen = length(string) 13 | for i in range(1, strlen + 1): 14 | rev_str += string[-i] 15 | print(rev_str) 16 | 17 | def rev_str2(string): 18 | """Reverse string""" 19 | rev_str = str1[::-1] 20 | print(rev_str) 21 | 22 | def rev_str3(string): 23 | """Reverse string""" 24 | rev_str = '' 25 | strlen = length(string) 26 | for i in range(strlen): 27 | rev_str = string[i] + rev_str 28 | print(rev_str) 29 | 30 | str1 = input("Enter a string: ") 31 | print(str1) 32 | rev_str(str1) 33 | rev_str2(str1) 34 | rev_str3(str1) 35 | 36 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/python/str_comp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | str1 = input("Enter first string: ") 3 | str2 = input("Enter second string: ") 4 | 5 | len1 = 0 6 | len2 = 0 7 | flag = 0 8 | 9 | for i in str1: 10 | len1 += 1 11 | 12 | for i in str2: 13 | len2 += 1 14 | 15 | """Compares two string""" 16 | if len1 == len2: 17 | for i in range(len1): 18 | if str1[i] != str2[i]: 19 | flag += 1 20 | print("No") 21 | break 22 | else: 23 | print("Yes") 24 | else: 25 | print("No") 26 | 27 | 28 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/python/str_concat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | str1 = input("Enter first string: ") 3 | str2 = input("Enter second string: ") 4 | str3 = '' 5 | 6 | """String concatenation""" 7 | for ch in str1: 8 | str3 += ch 9 | 10 | for ch2 in str2: 11 | str3 += ch2 12 | 13 | print(str3) 14 | -------------------------------------------------------------------------------- /problem_solving_for_beginners/string_problems/python/strcpy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | str1 = input("Enter a string: ") 3 | copy_str1 = 'Hey ' 4 | 5 | """Copy one string to another string""" 6 | for ch in str1: 7 | copy_str1 += ch 8 | 9 | print("Copied string = {:s}".format(copy_str1)) -------------------------------------------------------------------------------- /problem_solving_with_DSA/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp.errorSquiggles": "disabled" 3 | } 4 | -------------------------------------------------------------------------------- /problem_solving_with_DSA/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: gcc-11 build active file", 6 | "command": "/usr/bin/gcc-11", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /problem_solving_with_DSA/BTS/C/BST.h: -------------------------------------------------------------------------------- 1 | #ifndef _BST_H 2 | #define _BST_H 3 | 4 | #include 5 | #include 6 | 7 | struct node 8 | { 9 | int data; 10 | struct node *left; 11 | struct node *right; 12 | }; 13 | 14 | struct node *getNewNode(int data); 15 | struct node *insertNode(struct node *root, int value); 16 | void preOrder(struct node *root); 17 | 18 | 19 | #endif /*_BST_H*/ -------------------------------------------------------------------------------- /problem_solving_with_DSA/README.md: -------------------------------------------------------------------------------- 1 | # Problem Solving With DSA 2 | -------------------------------------------------------------------------------- /problem_solving_with_DSA/array/README.md: -------------------------------------------------------------------------------- 1 | # Array 2 | -------------------------------------------------------------------------------- /problem_solving_with_DSA/hashing/README.md: -------------------------------------------------------------------------------- 1 | # HASHING 2 | 3 | Hashing in the data structure is a technique of mapping a large chunk of data into small tables using a hashing function. 4 | -------------------------------------------------------------------------------- /problem_solving_with_DSA/hashing/hash.h: -------------------------------------------------------------------------------- 1 | #ifndef _HASH_H_ 2 | #define _HASH_H_ 3 | 4 | #include 5 | int insert(int value); 6 | int search(int value); 7 | 8 | #endif /*_HASH_H_*/ 9 | -------------------------------------------------------------------------------- /problem_solving_with_DSA/linkedList/C/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/problem_solving_with_DSA/linkedList/C/a.out -------------------------------------------------------------------------------- /problem_solving_with_DSA/stack/C/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _STACK_H 2 | #define _STACK_H 3 | 4 | #include 5 | #include 6 | 7 | void push(char c); 8 | char pop(); 9 | int isStackEmpty(); 10 | int isStackFull(); 11 | 12 | #endif /*_STACK_H*/ -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | # Python -------------------------------------------------------------------------------- /python/class/class_and_instance_variable.py: -------------------------------------------------------------------------------- 1 | class Example: 2 | # class variable (Common) 3 | common = 50 4 | def __init__(self, val): 5 | # instance variable (Unique) 6 | self.local = val 7 | 8 | A = Example(10) 9 | B = Example(30) 10 | 11 | print(A.common, A.local) 12 | print(B.common, B.local) 13 | -------------------------------------------------------------------------------- /python/class/constructor.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | def __init__(self, name, age, dob): 3 | self.name = name 4 | self.age = age 5 | self.dob = dob 6 | 7 | def getDetails(self): 8 | print(f"Name: {self.name} \nAge: {self.age} \nDob: {self.dob}") 9 | 10 | 11 | obj = Person("Bob", 25, "20-11-1995") 12 | obj.getDetails() 13 | 14 | 15 | -------------------------------------------------------------------------------- /python/class/data.py: -------------------------------------------------------------------------------- 1 | # create Data class 2 | class Data: 3 | def setData(self, x, y, z): 4 | self.x = x 5 | self.y = y 6 | self.z = z 7 | 8 | def getData(self): 9 | print(f"x={self.x} \ny={self.y} \nz={self.z}") 10 | 11 | # create an instance of object 12 | obj = Data() 13 | obj.setData(10, 20, 30) 14 | obj.getData() 15 | -------------------------------------------------------------------------------- /python/class/inheritance.py: -------------------------------------------------------------------------------- 1 | class Student: 2 | def __init__(self, name, age): 3 | self.name = name 4 | self.age = age 5 | 6 | 7 | class Library(Student): 8 | pass 9 | 10 | 11 | lib = Library("Bob", 25) 12 | 13 | print(f"Name: {lib.name} \nAge: {lib.age}") -------------------------------------------------------------------------------- /python/class/inheritance2.py: -------------------------------------------------------------------------------- 1 | # Student class 2 | class Student: 3 | def __init__(self, name, age): 4 | self.name = name 5 | self.age = age 6 | # private variable 7 | self.__phone = "08109211864" 8 | 9 | def getPhone(self): 10 | return self.__phone 11 | 12 | def getDetails(self): 13 | print(f"Name: {self.name} \nAge: {self.age} \nFine: {self.fine}") 14 | 15 | # Library Class inherit Student properties and methods 16 | class Library(Student): 17 | def __init__(self, name, age, fine): 18 | Student.__init__(self, age, name) 19 | self.fine = fine 20 | 21 | 22 | 23 | lib = Library("Bob", 25, 500) 24 | lib.getDetails() 25 | print(f"Phone Number: {lib.getPhone()}") -------------------------------------------------------------------------------- /python/class/multilevel_inheritance.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __init__(self, a): 3 | self.a = a 4 | 5 | class B(A): 6 | def __init__(self, a, b): 7 | A.__init__(self, a) 8 | self.b = b 9 | 10 | # multLevel inheritance 11 | class C(B): 12 | def __init__(self, a, b, c): 13 | B.__init__(self, a, b) 14 | self.c = c 15 | 16 | obj = C(10, 20, 30) 17 | print(obj.a, obj.b, obj.c) -------------------------------------------------------------------------------- /python/class/multiple_inheritance.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __init__(self): 3 | self.a = 10 4 | 5 | class B: 6 | def __init__(self): 7 | self.b = 20 8 | 9 | # multiple inheritance 10 | class C(A, B): 11 | def __init__(self): 12 | A.__init__(self) 13 | B.__init__(self) 14 | self.c = 30 15 | 16 | obj = C() 17 | print(obj.a, obj.b, obj.c) -------------------------------------------------------------------------------- /python/class/number.py: -------------------------------------------------------------------------------- 1 | class Numbers: 2 | def __init__(self): 3 | self.n = [] 4 | 5 | def add_item(self, num): 6 | self.n.append(num) 7 | 8 | 9 | odd = Numbers() 10 | odd.add_item(1) 11 | odd.add_item(3) 12 | print(odd.n) 13 | 14 | even = Numbers() 15 | even.add_item(2) 16 | even.add_item(4) 17 | print(even.n) -------------------------------------------------------------------------------- /python/class/person.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | def setDetails(self, name, age, dob): 3 | self.name = name 4 | self.age = age 5 | self.dob = dob 6 | 7 | def getDetails(self): 8 | print(f"Name: {self.name} \nAge: {self.age} \nDob: {self.dob}") 9 | 10 | 11 | p1 = Person() 12 | p1.setDetails("Bob", 25, "20-11-1995") 13 | p1.getDetails() 14 | 15 | 16 | -------------------------------------------------------------------------------- /python/decorators/calculation.py: -------------------------------------------------------------------------------- 1 | def add(a, b): 2 | return a + b 3 | 4 | def mul(a, b): 5 | return a * b 6 | 7 | def calculation(fun, a, b): 8 | print(fun(a, b)) 9 | 10 | calculation(add, 5, 10) 11 | calculation(mul, 5, 10) -------------------------------------------------------------------------------- /python/decorators/decorator1.py: -------------------------------------------------------------------------------- 1 | def name(): 2 | print("Beloved,", end=" ") 3 | 4 | 5 | def decorator(fun): 6 | def greeting(): 7 | print("Hello", end=" ") 8 | fun() 9 | print("How are you doing today?") 10 | return greeting 11 | 12 | 13 | new = decorator(name) 14 | 15 | new() 16 | -------------------------------------------------------------------------------- /python/dictionary/app.py: -------------------------------------------------------------------------------- 1 | data = {'name': 'Benz', 'price': 250, 'color':'White'} 2 | # find = data.get('name') 3 | # find2 = data['price'] 4 | 5 | # key = 'Benz' 6 | # if key in data: 7 | # print(data[key]) 8 | # else: 9 | # print('Key not found') 10 | 11 | # print key 12 | # for x in data: 13 | # print(x) 14 | # for x in data.keys(): 15 | # print(x) 16 | 17 | # # print value 18 | # for x in data: 19 | # print(data[x]) 20 | # for x in data.values(): 21 | # print(x) 22 | 23 | # print key and value 24 | for x,y in data.items(): 25 | print(x, y) 26 | 27 | # print(find2) -------------------------------------------------------------------------------- /python/dictionary/modify_dic.py: -------------------------------------------------------------------------------- 1 | data = {'name': 'Benz', 'price': 250, 'color':'White'} 2 | 3 | # modify data 4 | data['price'] = 10000 5 | 6 | # add new item 7 | data['model'] = 2021 8 | 9 | # remove item 10 | # data.pop('price') 11 | # del data('color') 12 | 13 | # clear and delete 14 | # data.clear() 15 | # del data 16 | 17 | # copy 18 | copy = data.copy() 19 | copy2 = dict(data) 20 | 21 | print(data) 22 | print(copy) 23 | print(copy2) -------------------------------------------------------------------------------- /python/error_and_exceptions/error_handling.py: -------------------------------------------------------------------------------- 1 | try: 2 | 5 / 0 3 | except NameError as err: 4 | print("Name Error: ", err) 5 | except TypeError as err: 6 | print("Type Error: ", err) 7 | except ValueError as err: 8 | print("Value Error: ", err) 9 | except: 10 | print("Something Went Wrong!") -------------------------------------------------------------------------------- /python/error_and_exceptions/finally.py: -------------------------------------------------------------------------------- 1 | try: 2 | f = open("new.txt", "w") 3 | f.write("Hello Python") 4 | except: 5 | print("Something Went Wrong!") 6 | finally: 7 | f.close() -------------------------------------------------------------------------------- /python/error_and_exceptions/new.txt: -------------------------------------------------------------------------------- 1 | Hello Python -------------------------------------------------------------------------------- /python/error_and_exceptions/try_and_except.py: -------------------------------------------------------------------------------- 1 | def div(a, b): 2 | try: 3 | print(a / b) 4 | except: 5 | print("Something Went Wrong!") 6 | else: 7 | print("Division completed succesfully") 8 | 9 | div(5, 10) 10 | div(5, 0) -------------------------------------------------------------------------------- /python/file/__pycache__/copy.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/belovetech/Log2Base2/717582693b8621dfb5720864f35a7780ebf0b5bf/python/file/__pycache__/copy.cpython-39.pyc -------------------------------------------------------------------------------- /python/file/append.py: -------------------------------------------------------------------------------- 1 | # append 2 | f = open("new.txt", "a") 3 | f.write("How are you doing today?\n") 4 | f.close() 5 | 6 | # read 7 | f = open("new.txt", "r") 8 | print(f.read()) 9 | f.close() -------------------------------------------------------------------------------- /python/file/copy.py: -------------------------------------------------------------------------------- 1 | # open new.txt and copy.txt in read and write mode respectively 2 | fr = open("new.txt", "r") 3 | fw = open("copy.txt", "w") 4 | 5 | # read from new.txt and write to copy.txt 6 | for line in fr: 7 | fw.write(line) 8 | 9 | # close files 10 | fr.close() 11 | fw.close() 12 | 13 | 14 | # Read copy.txt 15 | f = open("copy.txt", "r") 16 | print(f.read()) 17 | f.close() 18 | 19 | 20 | -------------------------------------------------------------------------------- /python/file/copy.txt: -------------------------------------------------------------------------------- 1 | Hello world 2 | How are you doing today? 3 | How are you doing today? 4 | How are you doing today? 5 | -------------------------------------------------------------------------------- /python/file/delete.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | if os.path.exists("code.txt"): 4 | os.remove("code.txt") 5 | else: 6 | print("File Not Found") 7 | -------------------------------------------------------------------------------- /python/file/file.txt: -------------------------------------------------------------------------------- 1 | Hey there! what are you learning today? 2 | I am learning file handling in Python -------------------------------------------------------------------------------- /python/file/new.txt: -------------------------------------------------------------------------------- 1 | Hello world 2 | How are you doing today? 3 | How are you doing today? 4 | How are you doing today? 5 | -------------------------------------------------------------------------------- /python/file/read.py: -------------------------------------------------------------------------------- 1 | f = open("file.txt", "r") 2 | 3 | # print file object 4 | # print(f.read(10)) 5 | # print(f.readline()) 6 | # print(f.read()) 7 | 8 | # Read lines 9 | # list = f.readlines() 10 | # print(list) 11 | 12 | # print using for loop 13 | for x in f: 14 | print(x, end=" ") 15 | print() 16 | 17 | f.close() -------------------------------------------------------------------------------- /python/file/write.py: -------------------------------------------------------------------------------- 1 | # write 2 | f = open("new.txt", "w") 3 | f.write("Hello world\n") 4 | f.close() 5 | 6 | # read 7 | f = open("new.txt", "r") 8 | print(f.read()) 9 | f.close() -------------------------------------------------------------------------------- /python/function/arbitrage_args.py: -------------------------------------------------------------------------------- 1 | def sum(*input): 2 | ans = 0 3 | for x in input: 4 | ans += x 5 | print(f'Sum : {ans}') 6 | 7 | 8 | sum(4, 7, 9) 9 | sum(20, 7, 9, 10) 10 | sum(20, 7, 9, 50, 100) 11 | 12 | # def display(*data): 13 | # for d in data: 14 | # print(d) 15 | 16 | # list2 = [20, 7, 9, 10] 17 | # display('Beloved', 'b', 6, 9) 18 | # display(list2) -------------------------------------------------------------------------------- /python/function/check.py: -------------------------------------------------------------------------------- 1 | def check(num): 2 | if num == 0: 3 | print("Neither Negative nor Postive") 4 | elif num < 0: 5 | print("Negative") 6 | else: 7 | print("Positive") 8 | 9 | 10 | number = int(input("Enter a number: ")) 11 | check(number) -------------------------------------------------------------------------------- /python/function/display_date.py: -------------------------------------------------------------------------------- 1 | def display_date(day, month, year=2022): 2 | print("Day :" + str(day), end=' ') 3 | print("Month :" + str(month),end=' ') 4 | print("Year :" + str(year)) 5 | 6 | 7 | def init(): 8 | display_date(2, 11, 1996) 9 | display_date(2, 11) 10 | display_date(month = 12, day = 16, year = 1996) 11 | 12 | init() -------------------------------------------------------------------------------- /python/function/factorial.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | if n <= 1: 3 | return n 4 | return n * factorial(n - 1) 5 | 6 | 7 | num = int(input("Enter a number: ")) 8 | 9 | if num >= 0: 10 | ans = factorial(num) 11 | print(ans) -------------------------------------------------------------------------------- /python/function/lambda.py: -------------------------------------------------------------------------------- 1 | # Lambda function 2 | # var = lambda x, y: x * y 3 | # a = var(2, 4) 4 | # print(a) 5 | 6 | def fun(n): 7 | return lambda x : x ** n 8 | 9 | 10 | square = fun(2) 11 | cube = fun(3) 12 | 13 | print(square(6)) 14 | print(cube(2)) -------------------------------------------------------------------------------- /python/if_statmenet/app.py: -------------------------------------------------------------------------------- 1 | a = 10 2 | b = 10 3 | # c = a ^ b 4 | 5 | 6 | 7 | print(f' {id(a)} {id(b)}') -------------------------------------------------------------------------------- /python/if_statmenet/app2.c: -------------------------------------------------------------------------------- 1 | if 1: 2 | print(1) 3 | 4 | if 0: 5 | print(0) 6 | 7 | if -1: 8 | print(-1) -------------------------------------------------------------------------------- /python/if_statmenet/if_else.py: -------------------------------------------------------------------------------- 1 | # N = 3 2 | # if not(N <= 100 and N >= 10): 3 | # print("Yes") 4 | #else: 5 | # print("No") 6 | 7 | 8 | # a = 10 9 | # if (a < 100): 10 | # print(f"{a} is less than 100") 11 | 12 | N = int(input('Enter a number: ')) 13 | 14 | if N > 0: 15 | print('Positive') 16 | elif N < 0: 17 | print('Negative') 18 | else: 19 | print('Neither Positive nor Negative') -------------------------------------------------------------------------------- /python/list/list.py: -------------------------------------------------------------------------------- 1 | a = [30, 40, 50] 2 | b = [10, 20] 3 | 4 | # nums = [1, 3, 5, 7, 9] 5 | # copy = nums[:] 6 | # slicenum = nums[1:] 7 | # print(nums) 8 | 9 | # concatenate 10 | # c = b + a 11 | # append 12 | # b.append(250) 13 | # insert 14 | # b.insert(0, 50) 15 | # remove 16 | # a.remove(40) 17 | print(a) -------------------------------------------------------------------------------- /python/loop/break.py: -------------------------------------------------------------------------------- 1 | # prints the sum of number entered by user before negative number 2 | sum = 0 3 | while True: 4 | num = int(input('Enter a positive number: ')) 5 | 6 | if num < 0: 7 | break 8 | else: 9 | sum += num 10 | 11 | print(sum) 12 | -------------------------------------------------------------------------------- /python/loop/check_vowel.py: -------------------------------------------------------------------------------- 1 | # Write a program to get a string S from the user and 2 | # count the number of vowels present in the string. 3 | # Finally, display the vowel count. 4 | 5 | str = input("Enter a string: ") 6 | vowels = {'a', 'e', 'i', 'o', 'u'} 7 | count = 0 8 | 9 | for ch in str: 10 | for v in vowels: 11 | if ch == v: 12 | count += 1 13 | 14 | 15 | print(f'There are {count} vowels in the string "{str}"') 16 | 17 | -------------------------------------------------------------------------------- /python/loop/continue.py: -------------------------------------------------------------------------------- 1 | # prints odd number 2 | for x in range(10): 3 | if x % 2 == 0: 4 | continue 5 | else: 6 | print(x) -------------------------------------------------------------------------------- /python/loop/else_loop.py: -------------------------------------------------------------------------------- 1 | # checks if all element of num is even 2 | nums = [2, 4, 8, 6] 3 | 4 | for x in nums: 5 | if x % 2 == 1: 6 | break 7 | else: 8 | print('All Even!') -------------------------------------------------------------------------------- /python/loop/loop.py: -------------------------------------------------------------------------------- 1 | str = "Python" 2 | for ch in str: 3 | print(ch) 4 | 5 | print('') 6 | 7 | num = [1, 4, 6, 9, 5] 8 | for n in num: 9 | print(n) -------------------------------------------------------------------------------- /python/loop/nested_loop.py: -------------------------------------------------------------------------------- 1 | i = 1 2 | 3 | while i <= 3: 4 | print(f'Outer loop iteration {i}') 5 | j = 1 6 | while j <= 3: 7 | print(f'Inner loop iteration {j}') 8 | j +=1 9 | print('_________________________') 10 | i +=1 11 | -------------------------------------------------------------------------------- /python/loop/pass.py: -------------------------------------------------------------------------------- 1 | for x in range(10): 2 | if x % 3 == 0: 3 | pass 4 | else: 5 | print(x) 6 | 7 | def void(x): 8 | pass 9 | 10 | 11 | class Name(): 12 | pass -------------------------------------------------------------------------------- /python/loop/pattern.py: -------------------------------------------------------------------------------- 1 | # print * (5 x 5) 2 | i = 1 3 | while i <= 5: 4 | j = 1 5 | while j <= 5: 6 | print('*', end=' ') 7 | j += 1 8 | print() 9 | i += 1 -------------------------------------------------------------------------------- /python/loop/print_digit.py: -------------------------------------------------------------------------------- 1 | # Write a program to get a positive integer number from the user and print its digits one by one. 2 | num = int(input('Enter a positive number: ')) 3 | digit = 0 4 | 5 | while num > 0: 6 | digit = num % 10 7 | print(digit) 8 | num = num // 10 9 | -------------------------------------------------------------------------------- /python/loop/print_sum.py: -------------------------------------------------------------------------------- 1 | # Write a program to get a positive integer from the user and print the sum of all digits. 2 | num = int(input('Enter a positive number: ')) 3 | last_digit = 0 4 | sum = 0 5 | 6 | while num > 0: 7 | last_digit = num % 10 8 | sum += last_digit 9 | num = num // 10 10 | 11 | print(sum) 12 | -------------------------------------------------------------------------------- /python/loop/range.py: -------------------------------------------------------------------------------- 1 | # Write a program to print all the numbers in between x, y (including x, y). 2 | x = 10 3 | y = 20 4 | 5 | # for x in range(x, y+1): 6 | # print(x) 7 | 8 | # Write a program to print all the even numbers from 0 to 100. 9 | for x in range(2, 100, 2): 10 | print(x) 11 | -------------------------------------------------------------------------------- /python/loop/while.py: -------------------------------------------------------------------------------- 1 | num = int(input('Enter a number: ')) 2 | count = 0 3 | 4 | while num > 1: 5 | num = num >> 1 6 | count += 1 7 | 8 | print(count) -------------------------------------------------------------------------------- /python/module/module.py: -------------------------------------------------------------------------------- 1 | import util 2 | import util as u 3 | from util import negative_or_positive 4 | 5 | # import util 6 | util.even_or_odd(10) 7 | util.negative_or_positive(0) 8 | 9 | # import util as u 10 | u.even_or_odd(3) 11 | u.negative_or_positive(-1) 12 | 13 | # from util import negative_or_positive 14 | negative_or_positive(1) -------------------------------------------------------------------------------- /python/module/util.py: -------------------------------------------------------------------------------- 1 | def even_or_odd(num): 2 | if num % 2 == 0: 3 | print("Even") 4 | else: 5 | print("Odd") 6 | 7 | 8 | def negative_or_positive(num): 9 | if num == 0: 10 | print("Neither Positive Nor Negative") 11 | elif num < 0: 12 | print("Negative") 13 | else: 14 | print("Positive") -------------------------------------------------------------------------------- /python/set/diff_symmetric_diff.py: -------------------------------------------------------------------------------- 1 | a = {10, 20, 30} 2 | b = {10, 20, 100} 3 | 4 | res = a.difference(b) 5 | print(f'a difference b: {res}') 6 | res = b.difference(a) 7 | print(f'b difference a: {res}') 8 | 9 | res = a.symmetric_difference(b) 10 | print(f'a asymmetric_difference b: {res}') 11 | 12 | res = a.union(b) 13 | print(f'a union b {res}') -------------------------------------------------------------------------------- /python/set/find_vowel.py: -------------------------------------------------------------------------------- 1 | a = {'a', 'e', 'x', 'y', 'i', 'f', 'k', 'c'} 2 | #Write your code here 3 | vowels = {'a', 'e', 'i', 'o', 'u'} 4 | new_set = set() 5 | # new_set.add('b') 6 | 7 | for i in a: 8 | for v in vowels: 9 | if v == i: 10 | new_set.add(v) 11 | 12 | print(new_set) 13 | -------------------------------------------------------------------------------- /python/set/set.py: -------------------------------------------------------------------------------- 1 | data = {20, 60, 30 ,30} 2 | 3 | 4 | # for x in data: 5 | # print(x) 6 | 7 | # set modification 8 | data.add('e') 9 | # data.update([1, 5, 8, 9]) 10 | # data.remove(9) 11 | # data.discard(8) 12 | # del 13 | 14 | # set methods 15 | 16 | # data = {10, 200, 100, 500, 2000} 17 | # #Write your code here 18 | # copy = data.copy() 19 | 20 | # print(copy) 21 | 22 | print(data) -------------------------------------------------------------------------------- /python/set/set_union.py: -------------------------------------------------------------------------------- 1 | a = {20, 60, 30 ,30} 2 | b = {5, 10, 15, 20} 3 | 4 | # set union and intersection 5 | set_union = b.union(a) 6 | set_intersection = a.intersection(b) 7 | 8 | print(set_union) 9 | print(set_intersection) -------------------------------------------------------------------------------- /python/set/subset_superset_disjoin.py: -------------------------------------------------------------------------------- 1 | # Subset, SuperSet, Disjoint 2 | a = {20, 60, 30 ,30, 60, 40} 3 | b = {20, 30, 40} 4 | 5 | res = a.issubset(b) 6 | print(f'Subset: {res}') 7 | res = b.issubset(a) 8 | print(f'Subset: {res}') 9 | 10 | res = a.issuperset(b) 11 | print(f'Superset: {res}') 12 | res = b.issuperset(a) 13 | print(f'Superset: {res}') 14 | 15 | 16 | a = {20, 60, 40} 17 | b = {5, 14, 23} 18 | 19 | res = a.isdisjoint(b) 20 | print(f'Disjoint: {res}') 21 | 22 | a = {20, 60, 40} 23 | b = {5, 14, 23, 20} 24 | res = b.isdisjoint(a) 25 | print(f'Disjoint: {res}') -------------------------------------------------------------------------------- /python/string/slice_str.py: -------------------------------------------------------------------------------- 1 | greet = "Hello World" 2 | 3 | # start-index and end-index 4 | # end index is exclusive 5 | print(greet[0:3]) 6 | print(greet[-4:-1]) 7 | 8 | # omitting index will be to the length of the string 9 | print(greet[-4:]) 10 | print(greet[:-4]) 11 | -------------------------------------------------------------------------------- /python/string/str.py: -------------------------------------------------------------------------------- 1 | greet = "Hello World" 2 | 3 | print(greet) 4 | print(f'First character in {greet} is : {greet[0]}') 5 | print(f'Last character in {greet} is : {greet[-1]}') -------------------------------------------------------------------------------- /python/string/str_method.py: -------------------------------------------------------------------------------- 1 | greet = "Hello World" 2 | 3 | strlen = len(greet) 4 | print(f'string length: {strlen}') 5 | 6 | str_replace = greet.replace('H', 'Y') 7 | print(f'string replace: {str_replace}') 8 | 9 | str_upper = greet.upper() 10 | str_lower = str_upper.lower() 11 | print(f'string upper: {str_upper}') 12 | print(f'string lower: {str_lower}') 13 | 14 | str = "Python-Scripting-Language" 15 | str_split = str.split('-') 16 | print(f'string split: {str_split}') 17 | 18 | greet = " Hello World " 19 | str_strip = greet.strip() 20 | print(f'Without string strip: {greet}') 21 | print(f'With string strip: {str_strip}') 22 | 23 | -------------------------------------------------------------------------------- /python/string/strlen.py: -------------------------------------------------------------------------------- 1 | greet = "Hello World" 2 | 3 | 4 | -------------------------------------------------------------------------------- /python/tuple/tuple.py: -------------------------------------------------------------------------------- 1 | tuple_list = (1, 3, 6, 5) 2 | # num = int(input("Enter number: ")) 3 | 4 | # for item in tuple_list: 5 | # print(item) 6 | 7 | # print(tuple_list) 8 | 9 | # if num in tuple_list: 10 | # print('Yes') 11 | # else: 12 | # print('No') 13 | 14 | # Tuple Methods 15 | 16 | data = (20, 50, 40, 50, 20, 20) 17 | dataLen = len(data) 18 | dataCount = data.count(20) 19 | 20 | print(dataCount) --------------------------------------------------------------------------------