diff --git a/README.md b/README.md index 57da3bf..085a553 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,10 @@ [![Open Source Love](https://firstcontributions.github.io/open-source-badges/badges/open-source-v1/open-source.svg)](https://github.com/GDSC-CEC) - Hacktoberfest is a month-long celebration of open source software run by DigitalOcean in partnership with GitHub and Twilio. Hacktoberfest is open to everyone in our global community! Four quality pull requests must be submitted to public GitHub repositories. You can sign up anytime between October 1 and October 31. +This repo is aimed at people who are new to open-source, earn and learn from that experience. + ### Goal of this repository There are many repositories on GitHub that contain a collection of algorithms and data structures in different programming languages. The goal of this repository is to provide a collection of algorithms and data structures in Python. Aim of this repository is to become a one-stop repository for all the algorithms and data structures in Python. Code should be well documented and easy to understand. The algorithms and data structures should be implemented according to the style guide mentioned below. @@ -32,7 +33,6 @@ Please read [CONTRIBUTING.md](/CONTRIBUTING.md) and [CODE_OF_CONDUCT.md](/CODE_O ### Rules * There are total 70 problems open for contribution. -* Pull Requests are accepted on **First Come First Serve Basis**. * Once Pull Request is accepted on one problem, no other pull request will be reviewed on the that problem. Please try to contribute on other problem if any pull request is already merged. * Read the [CONTRIBUTING.md](/CONTRIBUTING.md) file. * Respect people. diff --git a/easy/binary_search/README.md b/easy/binary_search/README.md index d75b0a7..9fb9fe8 100644 --- a/easy/binary_search/README.md +++ b/easy/binary_search/README.md @@ -5,7 +5,7 @@ Write a program to implement Binary Search in Python. ### Input Format -Create a function with name binary_search that takes a list and an integer as arguments and returns the index of the integer in the list if it exists, else return -1. If invalid input is entered, return -1. +Create a function with name solution that takes a list and an integer as arguments and returns the index of the integer in the list if it exists, else return -1. If invalid input is entered, return -1. ### Output Format Return an integer denoting the index of the integer in the list if it exists, else return -1. diff --git a/easy/binary_search_recursion/README.md b/easy/binary_search_recursion/README.md index 0ba1e1b..91b0d08 100644 --- a/easy/binary_search_recursion/README.md +++ b/easy/binary_search_recursion/README.md @@ -5,7 +5,7 @@ Write a program to implement Binary Search in Python. ### Input Format -Create a function with name binary_search that takes a list and an integer as arguments and returns the index of the integer in the list if it exists, else return -1. If invalid input is entered, return -1. +Create a function with name solution that takes a list and an integer as arguments and returns the index of the integer in the list if it exists, else return -1. If invalid input is entered, return -1. ### Output Format Return an integer denoting the index of the integer in the list if it exists, else return -1. @@ -48,4 +48,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/binary_to_decimal/README.md b/easy/binary_to_decimal/README.md index 78cbfa4..b496a62 100644 --- a/easy/binary_to_decimal/README.md +++ b/easy/binary_to_decimal/README.md @@ -5,7 +5,7 @@ Write a program to convert a binary number to decimal in Python. ### Input Format -Create a function with name binary_to_decimal that takes a binary number as an argument and returns the decimal equivalent of the binary number. If invalid input is entered, return -1. +Create a function with name solution that takes a binary number as an argument and returns the decimal equivalent of the binary number. If invalid input is entered, return -1. ### Output Format Return an integer denoting the decimal equivalent of the binary number. @@ -47,4 +47,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/binary_to_decimal/tests.py b/easy/binary_to_decimal/tests.py index e8d3628..a9aa78e 100644 --- a/easy/binary_to_decimal/tests.py +++ b/easy/binary_to_decimal/tests.py @@ -46,4 +46,4 @@ def test_binary_to_decimal_test_j(self): if __name__ == "__main__": - unittest.main() \ No newline at end of file + utility.main() diff --git a/easy/bubble_sort/README.md b/easy/bubble_sort/README.md index c66c0e0..d328c1a 100644 --- a/easy/bubble_sort/README.md +++ b/easy/bubble_sort/README.md @@ -5,7 +5,7 @@ Write a program to sort a list of numbers in ascending order using Bubble Sort in Python. ### Input Format -Create a function with name bubble_sort that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. +Create a function with name solution that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. ### Output Format Return a list of numbers in ascending order. @@ -46,4 +46,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/bubble_sort/tests.py b/easy/bubble_sort/tests.py index 243341b..c98c3cb 100644 --- a/easy/bubble_sort/tests.py +++ b/easy/bubble_sort/tests.py @@ -50,4 +50,4 @@ def test_bubble_sort_test_k(self): if __name__ == "__main__": - unittest.main() \ No newline at end of file + utility.main() diff --git a/easy/bubble_sort_recursion/README.md b/easy/bubble_sort_recursion/README.md index c66c0e0..d328c1a 100644 --- a/easy/bubble_sort_recursion/README.md +++ b/easy/bubble_sort_recursion/README.md @@ -5,7 +5,7 @@ Write a program to sort a list of numbers in ascending order using Bubble Sort in Python. ### Input Format -Create a function with name bubble_sort that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. +Create a function with name solution that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. ### Output Format Return a list of numbers in ascending order. @@ -46,4 +46,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/bubble_sort_recursion/solution.py b/easy/bubble_sort_recursion/solution.py index 4d6454c..252207e 100644 --- a/easy/bubble_sort_recursion/solution.py +++ b/easy/bubble_sort_recursion/solution.py @@ -1,2 +1,23 @@ -def solution(): - pass +def solution(list_of_nums = []): + return bubble_sort(list_of_nums) + +def bubble_sort(list_of_nums): + # Check if it is a valid input. + if not list_of_nums or not isinstance(list_of_nums, list): + # If not a valid input return empty list. + return [] + + # Return bubble sort recursive solution + return recursive_sort(list_of_nums, len(list_of_nums)) + +def recursive_sort(list_of_nums, len): + # Basic case. + if len == 1: + return list_of_nums + + # Order sublist. + for i in range(len-1): + if list_of_nums[i] > list_of_nums[i+1]: + list_of_nums[i], list_of_nums[i+1] = list_of_nums[i+1], list_of_nums[i] + + return recursive_sort(list_of_nums, len-1) diff --git a/easy/bubble_sort_recursion/tests.py b/easy/bubble_sort_recursion/tests.py index 243341b..c98c3cb 100644 --- a/easy/bubble_sort_recursion/tests.py +++ b/easy/bubble_sort_recursion/tests.py @@ -50,4 +50,4 @@ def test_bubble_sort_test_k(self): if __name__ == "__main__": - unittest.main() \ No newline at end of file + utility.main() diff --git a/easy/calculator/README.md b/easy/calculator/README.md index f183307..09845f9 100644 --- a/easy/calculator/README.md +++ b/easy/calculator/README.md @@ -5,7 +5,7 @@ Write a program to create a calculator in Python. ### Input Format -Create a function with name calculator that takes two numbers and an operator as arguments and returns the result of the operation. If invalid input is entered, return -1. +Create a function with name solution that takes two numbers and an operator as arguments and returns the result of the operation. If invalid input is entered, return -1. ### Output Format Return the result of the operation. @@ -51,4 +51,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/calculator/solution.py b/easy/calculator/solution.py index 4d6454c..5e1956a 100644 --- a/easy/calculator/solution.py +++ b/easy/calculator/solution.py @@ -1,2 +1,25 @@ -def solution(): - pass +def solution(num1, num2, op =""): + if(op == "+"): + n1 = str(num1) + n2 = str(num2) + if not n1.isdigit() and not n2.isdigit(): + return -1 + else: + calc = num1 + num2 + return calc + elif(op == "-"): + calc = num1 - num2 + return calc + elif(op == "*"): + calc = num1 * num2 + return calc + elif(op == "/"): + if(num2 == 0): + return -1 + else: + calc = num1 / num2 + return calc + else: + return -1 + + diff --git a/easy/calculator/tests.py b/easy/calculator/tests.py index ea525d3..c12b658 100644 --- a/easy/calculator/tests.py +++ b/easy/calculator/tests.py @@ -37,4 +37,4 @@ def test_calculator_test_h(self): self.assertEqual(solution(1, 1), -1, "Test 8 Failed") if __name__ == "__main__": - unittest.main() \ No newline at end of file + utility.main() diff --git a/easy/decimal_to_binary/README.md b/easy/decimal_to_binary/README.md index 866c48e..101872a 100644 --- a/easy/decimal_to_binary/README.md +++ b/easy/decimal_to_binary/README.md @@ -6,7 +6,7 @@ Write a function that takes a decimal number as an argument and returns the binary equivalent of that number. #### Input Format -Create a function with name answer that takes an integer n as an argument and returns the binary equivalent of that number. +Create a function with name solution that takes an integer n as an argument and returns the binary equivalent of that number. #### Output Format Return a integer denoting the binary equivalent of the decimal number. If invalid input is entered, return 0. @@ -47,4 +47,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/diamond_pattern/README.md b/easy/diamond_pattern/README.md index 66cac09..6a98c13 100644 --- a/easy/diamond_pattern/README.md +++ b/easy/diamond_pattern/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and prints a diamond pattern of height n. #### Input Format -Create a function with name answer that takes an integer n as an argument and prints a diamond pattern of height n. n should be an odd number and greater than 1. If n is even, return -1. +Create a function with name solution that takes an integer n as an argument and prints a diamond pattern of height n. n should be an odd number and greater than 1. If n is even, return -1. #### Output Format Print a diamond pattern of height n. If invalid input is entered, return -1. @@ -41,7 +41,7 @@ The diamond pattern of height 9 is: ***** *** * - ``` +``` - The first line contains 8 spaces and 1 star. - The second line contains 7 spaces and 3 stars. - The third line contains 6 spaces and 5 stars. @@ -77,4 +77,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/factorial_iterative/README.md b/easy/factorial_iterative/README.md index d220f05..cc8725b 100644 --- a/easy/factorial_iterative/README.md +++ b/easy/factorial_iterative/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and returns the factorial of that number. #### Input Format -Create a function with name answer that takes an integer n as an argument and returns the factorial of that number. +Create a function with name solution that takes an integer n as an argument and returns the factorial of that number. #### Output Format Return a integer denoting the factorial of the number. If invalid input is entered, return -1. @@ -47,4 +47,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/factorial_recursion/README.md b/easy/factorial_recursion/README.md index d220f05..cc8725b 100644 --- a/easy/factorial_recursion/README.md +++ b/easy/factorial_recursion/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and returns the factorial of that number. #### Input Format -Create a function with name answer that takes an integer n as an argument and returns the factorial of that number. +Create a function with name solution that takes an integer n as an argument and returns the factorial of that number. #### Output Format Return a integer denoting the factorial of the number. If invalid input is entered, return -1. @@ -47,4 +47,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/fibonacci_iterative/README.md b/easy/fibonacci_iterative/README.md index caaa756..6025e24 100644 --- a/easy/fibonacci_iterative/README.md +++ b/easy/fibonacci_iterative/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and returns the nth Fibonacci number. #### Input Format -Create a function with name answer that takes an integer n as an argument and returns the nth Fibonacci number. +Create a function with name solution that takes an integer n as an argument and returns the nth Fibonacci number. #### Output Format Return a integer denoting the nth Fibonacci number. If invalid input is entered, return -1. @@ -47,4 +47,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/fibonacci_recursion/README.md b/easy/fibonacci_recursion/README.md index caaa756..6025e24 100644 --- a/easy/fibonacci_recursion/README.md +++ b/easy/fibonacci_recursion/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and returns the nth Fibonacci number. #### Input Format -Create a function with name answer that takes an integer n as an argument and returns the nth Fibonacci number. +Create a function with name solution that takes an integer n as an argument and returns the nth Fibonacci number. #### Output Format Return a integer denoting the nth Fibonacci number. If invalid input is entered, return -1. @@ -47,4 +47,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/fibonacci_recursion/solution.py b/easy/fibonacci_recursion/solution.py index 4d6454c..d2ae861 100644 --- a/easy/fibonacci_recursion/solution.py +++ b/easy/fibonacci_recursion/solution.py @@ -1,2 +1,7 @@ -def solution(): - pass +def solution(*args): + if len(args)==0: + return -1 + + if args[0]<=1: + return args[0] + return solution(args[0]-1)+solution(args[0]-2) diff --git a/easy/half_diamond_pattern/README.md b/easy/half_diamond_pattern/README.md index 52e93ef..69eca0d 100644 --- a/easy/half_diamond_pattern/README.md +++ b/easy/half_diamond_pattern/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and prints a half diamond star pattern of height n. #### Input Format -Create a function with name answer that takes an integer n as an argument and prints a half diamond star pattern of height n. n should be odd and greater than 1. +Create a function with name solution that takes an integer n as an argument and prints a half diamond star pattern of height n. n should be odd and greater than 1. #### Output Format Print a half diamond star pattern of height n. If invalid input is entered, return -1. @@ -55,4 +55,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/half_pyramid_pattern/README.md b/easy/half_pyramid_pattern/README.md index 5b46d24..bb9f7f2 100644 --- a/easy/half_pyramid_pattern/README.md +++ b/easy/half_pyramid_pattern/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and prints a half pyramid star pattern of height n. #### Input Format -Create a function with name answer that takes an integer n as an argument and prints a half pyramid star pattern of height n. +Create a function with name solution that takes an integer n as an argument and prints a half pyramid star pattern of height n. #### Output Format Print a half pyramid star pattern of height n. If invalid input is entered, return -1. @@ -51,4 +51,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/hourglass_pattern/README.md b/easy/hourglass_pattern/README.md index 49bb039..0e04215 100644 --- a/easy/hourglass_pattern/README.md +++ b/easy/hourglass_pattern/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and prints an hourglass star pattern of height n. #### Input Format -Create a function with name answer that takes an integer n as an argument and prints an hourglass star pattern of height n. n should be odd and greater than 1. +Create a function with name solution that takes an integer n as an argument and prints an hourglass star pattern of height n. n should be odd and greater than 1. #### Output Format Print an hourglass star pattern of height n. If invalid input is entered, return -1. @@ -51,4 +51,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/insertion_sort/README.md b/easy/insertion_sort/README.md index ffe71dc..cdd1012 100644 --- a/easy/insertion_sort/README.md +++ b/easy/insertion_sort/README.md @@ -6,7 +6,7 @@ Write a function that takes a list as an argument and sorts it using the insertion sort algorithm. #### Input Format -Create a function with name answer that takes a list as an argument and sorts it using the insertion sort algorithm. +Create a function with name solution that takes a list as an argument and sorts it using the insertion sort algorithm. #### Output Format Return the sorted list. If invalid input is entered, return []. @@ -48,4 +48,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/insertion_sort_recursion/README.md b/easy/insertion_sort_recursion/README.md index ffe71dc..cdd1012 100644 --- a/easy/insertion_sort_recursion/README.md +++ b/easy/insertion_sort_recursion/README.md @@ -6,7 +6,7 @@ Write a function that takes a list as an argument and sorts it using the insertion sort algorithm. #### Input Format -Create a function with name answer that takes a list as an argument and sorts it using the insertion sort algorithm. +Create a function with name solution that takes a list as an argument and sorts it using the insertion sort algorithm. #### Output Format Return the sorted list. If invalid input is entered, return []. @@ -48,4 +48,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/inverted_half_pyramid_pattern/README.md b/easy/inverted_half_pyramid_pattern/README.md index 8332e65..9ea919f 100644 --- a/easy/inverted_half_pyramid_pattern/README.md +++ b/easy/inverted_half_pyramid_pattern/README.md @@ -5,8 +5,8 @@ #### Problem Statement Write a function that takes a number n as an argument and prints a inverted half pyramid star pattern of height n. -#### Input Format -Create a function with name answer that takes an integer n as an argument and prints a half pyramid star pattern of height n. +#### Input Format +Create a function with name solution that takes an integer n as an argument and prints a half pyramid star pattern of height n. #### Output Format Print a inverted half pyramid star pattern of height n. If invalid input is entered, return -1. diff --git a/easy/inverted_right_angle_triangle_pattern/README.md b/easy/inverted_right_angle_triangle_pattern/README.md index 0d48ded..4ffce2d 100644 --- a/easy/inverted_right_angle_triangle_pattern/README.md +++ b/easy/inverted_right_angle_triangle_pattern/README.md @@ -5,8 +5,8 @@ #### Problem Statement Write a function that takes a number n as an argument and prints a inverted right angled star pattern of height n. -#### Input Format -Create a function with name answer that takes an integer n as an argument and prints a inverted right angled star pattern of height n. +#### Input Forman +Create a function with name solution that takes an integer n as an argument and prints a inverted right angled star pattern of height n. #### Output Format Print a inverted right angled star pattern of height n. If invalid input is entered, return -1. diff --git a/easy/inverted_right_angle_triangle_pattern/solution.py b/easy/inverted_right_angle_triangle_pattern/solution.py index e69de29..f46b01d 100644 --- a/easy/inverted_right_angle_triangle_pattern/solution.py +++ b/easy/inverted_right_angle_triangle_pattern/solution.py @@ -0,0 +1,14 @@ +def solution(n=-1): + if n==-1: + return -1 + if n<1: + return -1 + string= "" + if(n>0): + for row in range(n): + for column in range(n-row): + string+="*" + if row==n-1: + break + string+="\n" + return string \ No newline at end of file diff --git a/easy/inverted_right_angle_triangle_pattern/tests.py b/easy/inverted_right_angle_triangle_pattern/tests.py index 0638c72..e5c27d0 100644 --- a/easy/inverted_right_angle_triangle_pattern/tests.py +++ b/easy/inverted_right_angle_triangle_pattern/tests.py @@ -15,7 +15,7 @@ def test_half_diamond_b(self): def test_half_diamond_c(self): """Normal Test 3""" - self.assertEqual(solution(7), "*******\n******\n*****\n****\n***\n**\*", "Normal Test 3 Failed") + self.assertEqual(solution(7), "*******\n******\n*****\n****\n***\n**\n*", "Normal Test 3 Failed") def test_half_diamond_d(self): """Normal Test 4""" diff --git a/easy/inverted_right_angle_triangle_pattern_with_number/README.md b/easy/inverted_right_angle_triangle_pattern_with_number/README.md index 6ca64b6..4c49880 100644 --- a/easy/inverted_right_angle_triangle_pattern_with_number/README.md +++ b/easy/inverted_right_angle_triangle_pattern_with_number/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and prints a inverted right angle triangle pattern with number of height n. #### Input Format -Create a function with name answer that takes an integer n as an argument and prints a inverted right angle pattern star pattern with number of height n. +Create a function with name solution that takes an integer n as an argument and prints a inverted right angle pattern star pattern with number of height n. #### Output Format Print a inverted right angle triangle star pattern wuth number of height n. If invalid input is entered, return -1. diff --git a/easy/inverted_right_angle_triangle_pattern_with_number/solution.py b/easy/inverted_right_angle_triangle_pattern_with_number/solution.py index e69de29..1e95103 100644 --- a/easy/inverted_right_angle_triangle_pattern_with_number/solution.py +++ b/easy/inverted_right_angle_triangle_pattern_with_number/solution.py @@ -0,0 +1,14 @@ +def solution(n=-1): + if n==-1: + return -1 + if n<1: + return -1 + string= "" + if(n>0): + for row in range(n): + for column in range(n-row): + string+=str(column+1) + if row==n-1: + break + string+="\n" + return string \ No newline at end of file diff --git a/easy/inverted_right_angle_triangle_pattern_with_number/tests.py b/easy/inverted_right_angle_triangle_pattern_with_number/tests.py index 5beb798..dfa0d0a 100644 --- a/easy/inverted_right_angle_triangle_pattern_with_number/tests.py +++ b/easy/inverted_right_angle_triangle_pattern_with_number/tests.py @@ -15,11 +15,11 @@ def test_half_diamond_b(self): def test_half_diamond_c(self): """Normal Test 3""" - self.assertEqual(solution(7), "1234567\n123456\n12345\n1234\n123\n12\1", "Normal Test 3 Failed") + self.assertEqual(solution(7), "1234567\n123456\n12345\n1234\n123\n12\n1", "Normal Test 3 Failed") def test_half_diamond_d(self): """Normal Test 4""" - self.assertEqual(solution(9), "123456789\n12345678\m1234567\n123456\n12345\n1234\n123\n12\n1", "Normal Test 4 Failed") + self.assertEqual(solution(9), "123456789\n12345678\n1234567\n123456\n12345\n1234\n123\n12\n1", "Normal Test 4 Failed") def test_half_diamond_e(self): """Exception Test 1""" diff --git a/easy/linear_search/solution.py b/easy/linear_search/solution.py index e69de29..e9dadd2 100644 --- a/easy/linear_search/solution.py +++ b/easy/linear_search/solution.py @@ -0,0 +1,7 @@ +def solution(array = [], item = -1): + if type(array) != list: + return -1 + for index, value in enumerate(array): + if value == item: + return index + return -1 diff --git a/easy/linear_search_recursion/README.md b/easy/linear_search_recursion/README.md index 7e72d70..ed78a3c 100644 --- a/easy/linear_search_recursion/README.md +++ b/easy/linear_search_recursion/README.md @@ -4,8 +4,8 @@ ### Problem Statement Write a program to implement Linear Search in Python. -### Input Format -Create a function with name linear_search that takes a list and an integer as arguments and returns the index of the integer in the list if it exists, else return -1. If invalid input is entered, return -1. +### Input Format +Create a function with name solution that takes a list and an integer as arguments and returns the index of the integer in the list if it exists, else return -1. If invalid input is entered, return -1. ### Output Format Return an integer denoting the index of the integer in the list if it exists, else return -1. diff --git a/easy/merge_sort/README.md b/easy/merge_sort/README.md index b403b96..db829dc 100644 --- a/easy/merge_sort/README.md +++ b/easy/merge_sort/README.md @@ -5,7 +5,7 @@ Write a program to sort a list of numbers in ascending order using Merge Sort in Python. ### Input Format -Create a function with name merge_sort that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. +Create a function with name solution that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. ### Output Format Return a list of numbers in ascending order. diff --git a/easy/merge_sort/tests.py b/easy/merge_sort/tests.py index 47d839c..0ef5631 100644 --- a/easy/merge_sort/tests.py +++ b/easy/merge_sort/tests.py @@ -50,4 +50,4 @@ def test_bubble_sort_test_k(self): if __name__ == "__main__": - unittest.main() + utility.main() diff --git a/easy/merge_sort_recursion/README.md b/easy/merge_sort_recursion/README.md index b403b96..db829dc 100644 --- a/easy/merge_sort_recursion/README.md +++ b/easy/merge_sort_recursion/README.md @@ -5,7 +5,7 @@ Write a program to sort a list of numbers in ascending order using Merge Sort in Python. ### Input Format -Create a function with name merge_sort that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. +Create a function with name solution that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. ### Output Format Return a list of numbers in ascending order. diff --git a/easy/merge_sort_recursion/tests.py b/easy/merge_sort_recursion/tests.py index e5af977..4468fdb 100644 --- a/easy/merge_sort_recursion/tests.py +++ b/easy/merge_sort_recursion/tests.py @@ -50,4 +50,4 @@ def test_bubble_sort_test_k(self): if __name__ == "__main__": - unittest.main() + utility.main() diff --git a/easy/palindrome_iterative/README.md b/easy/palindrome_iterative/README.md index 12d15c7..f71bddc 100644 --- a/easy/palindrome_iterative/README.md +++ b/easy/palindrome_iterative/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and returns whether the number is plaindrome or not in 0 or 1 (0-Not a plindrome, 1-Plaindrome). #### Input Format -Create a function with name answer that takes an integer n as an argument and returns 0 or 1. +Create a function with name solution that takes an integer n as an argument and returns 0 or 1. #### Output Format Returns an integer i.e, 0 or 1. If invalid input is entered, return -1. diff --git a/easy/palindrome_recursion/README.md b/easy/palindrome_recursion/README.md index 6788777..6aaaf16 100644 --- a/easy/palindrome_recursion/README.md +++ b/easy/palindrome_recursion/README.md @@ -5,8 +5,8 @@ #### Problem Statement Write a function that takes a number n as an argument and returns whther the number is plaindrome or not in 0 or 1 (0-Not a plindrome, 1-Plaindrome). -#### Input Format -Create a function with name answer that takes an integer n as an argument and returns 0 or 1. +#### Input Format +Create a function with name solution that takes an integer n as an argument and returns 0 or 1. #### Output Format Returns an integer i.e, 0 or 1. If invalid input is entered, return -1. @@ -22,7 +22,7 @@ n = 21312 ``` #### Explanation -The 21312 is same from fornt as well as back. +The 21312 is same from front as well as back. #### Testing Run the following command: diff --git a/easy/palindrome_recursion/solution.py b/easy/palindrome_recursion/solution.py index e69de29..880b93a 100644 --- a/easy/palindrome_recursion/solution.py +++ b/easy/palindrome_recursion/solution.py @@ -0,0 +1,21 @@ +from typing import Optional + + +def solution(number: Optional[int] = None): + if number is None or number < 0: + return -1 + current_word = str(number) + if len(current_word) < 2: + return 1 + if len(current_word) < 2: + return 1 + else: + first_letter = current_word[0] + last_letter = current_word[len(current_word) - 1] + if first_letter == last_letter: + middle_word = current_word[1:len(current_word) - 1] + if middle_word == '': + return 1 + return solution(int(middle_word)) + else: + return 0 diff --git a/easy/palindrome_recursion/tests.py b/easy/palindrome_recursion/tests.py index df840ef..125a51c 100644 --- a/easy/palindrome_recursion/tests.py +++ b/easy/palindrome_recursion/tests.py @@ -2,6 +2,7 @@ import utility from solution import solution + class Test(unittest.TestCase): """Palindrome Check""" diff --git a/easy/prime_iterative/README.md b/easy/prime_iterative/README.md index 8b8b4fd..59a67da 100644 --- a/easy/prime_iterative/README.md +++ b/easy/prime_iterative/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and returns whether the number is prime or not in 0 or 1 (0-Not a plindrome, 1-Plaindrome). #### Input Format -Create a function with name answer that takes an integer n as an argument and returns 0 or 1. +Create a function with name solution that takes an integer n as an argument and returns 0 or 1. #### Output Format Returns an integer i.e, 0 or 1. If invalid input is entered, return -1. diff --git a/easy/prime_recursion/README.md b/easy/prime_recursion/README.md index 8b8b4fd..59a67da 100644 --- a/easy/prime_recursion/README.md +++ b/easy/prime_recursion/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and returns whether the number is prime or not in 0 or 1 (0-Not a plindrome, 1-Plaindrome). #### Input Format -Create a function with name answer that takes an integer n as an argument and returns 0 or 1. +Create a function with name solution that takes an integer n as an argument and returns 0 or 1. #### Output Format Returns an integer i.e, 0 or 1. If invalid input is entered, return -1. diff --git a/easy/quick_sort/README.md b/easy/quick_sort/README.md index 660d556..72efd1d 100644 --- a/easy/quick_sort/README.md +++ b/easy/quick_sort/README.md @@ -5,7 +5,7 @@ Write a program to sort a list of numbers in ascending order using Quick Sort in Python. ### Input Format -Create a function with name quick_sort that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. +Create a function with name solution that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. ### Output Format Return a list of numbers in ascending order. diff --git a/easy/quick_sort/tests.py b/easy/quick_sort/tests.py index d274e78..dbc8725 100644 --- a/easy/quick_sort/tests.py +++ b/easy/quick_sort/tests.py @@ -50,4 +50,4 @@ def test_bubble_sort_test_k(self): if __name__ == "__main__": - unittest.main() + utility.main() diff --git a/easy/quick_sort_recursion/README.md b/easy/quick_sort_recursion/README.md index 660d556..72efd1d 100644 --- a/easy/quick_sort_recursion/README.md +++ b/easy/quick_sort_recursion/README.md @@ -5,7 +5,7 @@ Write a program to sort a list of numbers in ascending order using Quick Sort in Python. ### Input Format -Create a function with name quick_sort that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. +Create a function with name solution that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. ### Output Format Return a list of numbers in ascending order. diff --git a/easy/quick_sort_recursion/solution.py b/easy/quick_sort_recursion/solution.py index e69de29..a2f5a75 100644 --- a/easy/quick_sort_recursion/solution.py +++ b/easy/quick_sort_recursion/solution.py @@ -0,0 +1,33 @@ +def partition(array, low, high): + + pivot = array[high] + i = low - 1 + + for j in range(low, high): + if array[j] <= pivot: + + i = i + 1 + + (array[i], array[j]) = (array[j], array[i]) + + (array[i + 1], array[high]) = (array[high], array[i + 1]) + + return i + 1 + + +def quickSort(array, low, high): + if low < high: + + pi = partition(array, low, high) + + quickSort(array, low, pi - 1) + + quickSort(array, pi + 1, high) + + +def solution(arr=[]): + if type(arr) != list: + return[] + quickSort(arr,0,len(arr)-1) + return arr + diff --git a/easy/quick_sort_recursion/tests.py b/easy/quick_sort_recursion/tests.py index d274e78..dbc8725 100644 --- a/easy/quick_sort_recursion/tests.py +++ b/easy/quick_sort_recursion/tests.py @@ -50,4 +50,4 @@ def test_bubble_sort_test_k(self): if __name__ == "__main__": - unittest.main() + utility.main() diff --git a/easy/right_angle_triangle_pattern/README.md b/easy/right_angle_triangle_pattern/README.md index 2149108..5b35fc8 100644 --- a/easy/right_angle_triangle_pattern/README.md +++ b/easy/right_angle_triangle_pattern/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and prints a right angle triangle pattern of height n. #### Input Format -Create a function with name answer that takes an integer n as an argument and prints a right angle triangle star pattern of height n. +Create a function with name solution that takes an integer n as an argument and prints a right angle triangle star pattern of height n. #### Output Format Print a right angle triangle star pattern of height n. If invalid input is entered, return -1. diff --git a/easy/right_angle_triangle_pattern/solution.py b/easy/right_angle_triangle_pattern/solution.py index 4d6454c..54b249a 100644 --- a/easy/right_angle_triangle_pattern/solution.py +++ b/easy/right_angle_triangle_pattern/solution.py @@ -1,2 +1,12 @@ -def solution(): - pass +def solution(n): + if n<1: + return -1; + string= "" + if(n>0): + for row in range(n): + for column in range(row+1): + string+="*" + if row==n-1: + break + string+="\n" + return string diff --git a/easy/right_angle_triangle_pattern/tests.py b/easy/right_angle_triangle_pattern/tests.py index d0893e6..6728585 100644 --- a/easy/right_angle_triangle_pattern/tests.py +++ b/easy/right_angle_triangle_pattern/tests.py @@ -31,7 +31,7 @@ def test_half_diamond_f(self): def test_half_diamond_g(self): """Exception Test 3""" - self.assertEqual(solution(), -1, "Exception Test 3 Failed") + self.assertEqual(solution(-34), -1, "Exception Test 3 Failed") if __name__ == '__main__': - utility.main() \ No newline at end of file + utility.main() diff --git a/easy/right_angle_triangle_pattern_with_number/README.md b/easy/right_angle_triangle_pattern_with_number/README.md index d1b5fce..abb912f 100644 --- a/easy/right_angle_triangle_pattern_with_number/README.md +++ b/easy/right_angle_triangle_pattern_with_number/README.md @@ -6,7 +6,7 @@ Write a function that takes a number n as an argument and prints a right angle triangle with number pattern of height n. #### Input Format -Create a function with name answer that takes an integer n as an argument and prints a right angle triangle with number pattern of height n. +Create a function with name solution that takes an integer n as an argument and prints a right angle triangle with number pattern of height n. #### Output Format Print a right angle triangle with number pattern of height n. If invalid input is entered, return -1. diff --git a/easy/selection_sort/README.md b/easy/selection_sort/README.md index 63755c3..771b210 100644 --- a/easy/selection_sort/README.md +++ b/easy/selection_sort/README.md @@ -5,7 +5,7 @@ Write a program to sort a list of numbers in ascending order using Selection Sort in Python. ### Input Format -Create a function with name merge_sort that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. +Create a function with name solution that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. ### Output Format Return a list of numbers in ascending order. diff --git a/easy/selection_sort/tests.py b/easy/selection_sort/tests.py index d274e78..dbc8725 100644 --- a/easy/selection_sort/tests.py +++ b/easy/selection_sort/tests.py @@ -50,4 +50,4 @@ def test_bubble_sort_test_k(self): if __name__ == "__main__": - unittest.main() + utility.main() diff --git a/easy/selection_sort_recursion/README.md b/easy/selection_sort_recursion/README.md index 63755c3..771b210 100644 --- a/easy/selection_sort_recursion/README.md +++ b/easy/selection_sort_recursion/README.md @@ -5,7 +5,7 @@ Write a program to sort a list of numbers in ascending order using Selection Sort in Python. ### Input Format -Create a function with name merge_sort that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. +Create a function with name solution that takes a list of numbers as an argument and returns the sorted list of numbers. If invalid input is entered, return empty list. ### Output Format Return a list of numbers in ascending order. diff --git a/easy/selection_sort_recursion/tests.py b/easy/selection_sort_recursion/tests.py index d274e78..dbc8725 100644 --- a/easy/selection_sort_recursion/tests.py +++ b/easy/selection_sort_recursion/tests.py @@ -50,4 +50,4 @@ def test_bubble_sort_test_k(self): if __name__ == "__main__": - unittest.main() + utility.main() diff --git a/easy/sum_of_digits/README.md b/easy/sum_of_digits/README.md index f689223..04587e3 100644 --- a/easy/sum_of_digits/README.md +++ b/easy/sum_of_digits/README.md @@ -5,7 +5,7 @@ Write a program to calculate sum of digits in Python. ### Input Format -Create a function with name sum that takes a number and returns the sum of the digits. If invalid input is entered, return -1. +Create a function with name solution that takes a number and returns the sum of the digits. If invalid input is entered, return -1. ### Output Format Return the sum of digits. Else if input is invalid then return -1. diff --git a/easy/sum_of_digits/solution.py b/easy/sum_of_digits/solution.py index e69de29..54fbc03 100644 --- a/easy/sum_of_digits/solution.py +++ b/easy/sum_of_digits/solution.py @@ -0,0 +1,19 @@ + +def solution(n): + if not str(n).isnumeric(): + return -1 + + if int(n) == 0: + return 0 + + if int(n)<0: + return -1 + + sum = 0 + for digit in str(n): + sum += int(digit) + return sum + + + + diff --git a/easy/sum_of_digits_recursion/README.md b/easy/sum_of_digits_recursion/README.md index f689223..e58a4af 100644 --- a/easy/sum_of_digits_recursion/README.md +++ b/easy/sum_of_digits_recursion/README.md @@ -1,11 +1,11 @@ -#Hacktoberfest 2022 +# Hacktoberfest 2022 ## Sum Of Digits in Python ### Problem Statement Write a program to calculate sum of digits in Python. ### Input Format -Create a function with name sum that takes a number and returns the sum of the digits. If invalid input is entered, return -1. +Create a function with name solution that takes a number and returns the sum of the digits. If invalid input is entered, return -1. ### Output Format Return the sum of digits. Else if input is invalid then return -1. diff --git a/easy/sum_of_n_numbers/README.md b/easy/sum_of_n_numbers/README.md index ce92a3f..d9d1802 100644 --- a/easy/sum_of_n_numbers/README.md +++ b/easy/sum_of_n_numbers/README.md @@ -1,11 +1,11 @@ -#Hacktoberfest 2022 +# Hacktoberfest 2022 ## Sum Of N Natural Numbers in Python ### Problem Statement Write a program to calculate sum of n natural numbers in Python. ### Input Format -Create a function with name sum that takes a number upto which the sum is required and returns the sum of the numbers. If invalid input is entered, return -1. +Create a function with name solution that takes a number upto which the sum is required and returns the sum of the numbers. If invalid input is entered, return -1. ### Output Format Returns the sum. Else if input is invalid then return -1. diff --git a/easy/sum_of_n_numbers/solution.py b/easy/sum_of_n_numbers/solution.py index e69de29..cdfbc2c 100644 --- a/easy/sum_of_n_numbers/solution.py +++ b/easy/sum_of_n_numbers/solution.py @@ -0,0 +1,11 @@ +def solution(n): + sum = 0 + if not str(n).isnumeric(): + sum = -1 + return sum + else: + n = int(n) + for i in range(n): + i = i + 1 + sum = sum + i + return sum \ No newline at end of file diff --git a/easy/sum_of_n_numbers/tests.py b/easy/sum_of_n_numbers/tests.py index 659d0a6..aa8cb5a 100644 --- a/easy/sum_of_n_numbers/tests.py +++ b/easy/sum_of_n_numbers/tests.py @@ -37,4 +37,4 @@ def test_calculator_test_h(self): self.assertEqual(solution(-123), -1, "Test 8 Failed") if __name__ == "__main__": - unittest.main() + utility.main() diff --git a/easy/sum_of_n_numbers_recursion/README.md b/easy/sum_of_n_numbers_recursion/README.md index ce92a3f..d9d1802 100644 --- a/easy/sum_of_n_numbers_recursion/README.md +++ b/easy/sum_of_n_numbers_recursion/README.md @@ -1,11 +1,11 @@ -#Hacktoberfest 2022 +# Hacktoberfest 2022 ## Sum Of N Natural Numbers in Python ### Problem Statement Write a program to calculate sum of n natural numbers in Python. ### Input Format -Create a function with name sum that takes a number upto which the sum is required and returns the sum of the numbers. If invalid input is entered, return -1. +Create a function with name solution that takes a number upto which the sum is required and returns the sum of the numbers. If invalid input is entered, return -1. ### Output Format Returns the sum. Else if input is invalid then return -1. diff --git a/easy/sum_of_n_numbers_recursion/tests.py b/easy/sum_of_n_numbers_recursion/tests.py index 659d0a6..aa8cb5a 100644 --- a/easy/sum_of_n_numbers_recursion/tests.py +++ b/easy/sum_of_n_numbers_recursion/tests.py @@ -37,4 +37,4 @@ def test_calculator_test_h(self): self.assertEqual(solution(-123), -1, "Test 8 Failed") if __name__ == "__main__": - unittest.main() + utility.main() diff --git a/easy/tower_of_hanoi/README.md b/easy/tower_of_hanoi/README.md index de5ef75..c0d0c32 100644 --- a/easy/tower_of_hanoi/README.md +++ b/easy/tower_of_hanoi/README.md @@ -6,7 +6,7 @@ The Tower of Hanoi is a mathematical puzzle where we have three rods and n disks 2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3. No disk may be placed on top of a smaller disk. #### Input Format -Create a function with name answer that takes an integer n as an argument and returns the minimum number of moves required to solve the Tower of Hanoi puzzle. +Create a function with name solution that takes an integer n as an argument and returns the minimum number of moves required to solve the Tower of Hanoi puzzle. #### Output Format Return an integer denoting the minimum number of moves required to solve the Tower of Hanoi puzzle. If invalid input is entered, return 0. #### Sample Input @@ -52,4 +52,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/easy/tower_of_hanoi_recursion/README.md b/easy/tower_of_hanoi_recursion/README.md index de5ef75..c0d0c32 100644 --- a/easy/tower_of_hanoi_recursion/README.md +++ b/easy/tower_of_hanoi_recursion/README.md @@ -6,7 +6,7 @@ The Tower of Hanoi is a mathematical puzzle where we have three rods and n disks 2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3. No disk may be placed on top of a smaller disk. #### Input Format -Create a function with name answer that takes an integer n as an argument and returns the minimum number of moves required to solve the Tower of Hanoi puzzle. +Create a function with name solution that takes an integer n as an argument and returns the minimum number of moves required to solve the Tower of Hanoi puzzle. #### Output Format Return an integer denoting the minimum number of moves required to solve the Tower of Hanoi puzzle. If invalid input is entered, return 0. #### Sample Input @@ -52,4 +52,4 @@ If all test cases pass, your solution will be merged with the main branch. If yo ## License **This project is licensed under the GNU GENERAL PUBLIC License - see the [LICENSE](../../LICENSE) file for details** -## Happy Coding! :smile: \ No newline at end of file +## Happy Coding! :smile: diff --git a/hard/A*/README.md b/hard/A-Star/README.md similarity index 100% rename from hard/A*/README.md rename to hard/A-Star/README.md diff --git a/hard/A*/solution.py b/hard/A-Star/solution.py similarity index 100% rename from hard/A*/solution.py rename to hard/A-Star/solution.py diff --git a/hard/linear_regression/solution.py b/hard/linear_regression/solution.py index e69de29..d3f3d4e 100644 --- a/hard/linear_regression/solution.py +++ b/hard/linear_regression/solution.py @@ -0,0 +1,35 @@ +import numpy as np + +class LinearRegression(): + + def __init__(self): + self.w = None + self.b = None + + def fit(self, X, y, lr=0.01, epochs=100): + self.w = np.zeros(X.shape[1]) + self.b = 0 + for _ in range(epochs): + y_pred = self.predict(X) + dw = np.mean((y_pred - y) * X, axis=0) + db = np.mean(y_pred - y) + self.w -= lr * dw + self.b -= lr * db + + def predict(self, X): + return X @ self.w + self.b + + def score(self, X, y): + y_pred = self.predict(X) + return 1 - np.sum((y - y_pred) ** 2) / np.sum((y - np.mean(y)) ** 2) + +def main(): + X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + m, c = 2, 3 + y = m * X[:, 0] + c + model = LinearRegression() + model.fit(X, y) + print(f"Accuracy: {model.score(X, y)}") + +if __name__ == '__main__': + main() \ No newline at end of file