Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1328 of 3366
684 Views
When it is required to reverse a string without using recursion technique, simple negative indexing can be used.Indexing helps the values to access the elements at a specific index.ExampleBelow is a demonstration for the same − Live Demomy_string = str(input("Enter a string that needs to be reversed: ")) print("The string after reversal is: ") print(my_string[::-1])OutputEnter a string that needs to be reversed: Jane The string after reversal is: enaJExplanationThe string input is taken by the user.This value is assigned to a variable.It is displayed on the console.It is negative indexed and displayed on the console.Read More
250 Views
When it is required to flatten a list without using recursion technique, the lambda function, ‘sum’ method, ‘map’ method and the ‘isinstance’ method can be used.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).The ‘isinstance’ method checks to see if a given parameter belong to a specific data type or not.Anonymous function is a function which is defined without a name. In general, functions in Python are defined using ‘def’ keyword, but anonymous function is defined with the help of ‘lambda’ keyword. It takes a single ... Read More
6K+ Views
When it is required to find the factorial of a number without using recursion, the ‘while’ loop can be used.ExampleBelow is a demonstration for the same − Live Demomy_num = int(input("Enter a number :")) my_factorial = 1 while(my_num>0): my_factorial = my_factorial*my_num my_num=my_num-1 print("The factorial of the number is : ") print(my_factorial)OutputEnter a number :7 The factorial of the number is : 5040ExplanationThe input number is takne from the user.A variable is assigned to 1.It is checked to see for being 0.If not, it is multiplied by the previous value in the variable.It is assigned to the same variable.This is ... Read More
5K+ Views
When it is required to find the Fibonacci series without using recursion technique, the input is taken from the user, and a ‘while’ loop is used to get the numbers in the sequence.ExampleBelow is a demonstration for the same − Live Demofirst_num = int(input("Enter the first number of the fibonacci series... ")) second_num = int(input("Enter the second number of the fibonacci series... ")) num_of_terms = int(input("Enter the number of terms... ")) print(first_num, second_num) print("The numbers in fibonacci series are : ") while(num_of_terms-2): third_num = first_num + second_num first_num=second_num second_num=third_num print(third_num) num_of_terms=num_of_terms-1OutputEnter the first number of the ... Read More
2K+ Views
When it is required to find the length of a list with the help of recursion technique, a user defined method is used, and simple indexing technique is used.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same − Live Demodef list_length(my_list): if not my_list: return 0 return 1 + list_length(my_list[1::2]) + list_length(my_list[2::2]) my_list = ... Read More
656 Views
When it is required to find the total sum of a nest list using the recursion technique, a user defined method is used, that takes the list as a parameter.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).ExampleBelow is a demonstration for the same − Live Demodef recursion_sum(my_list): my_total = 0 for elem in my_list: if (type(elem) == type([])): ... Read More
1K+ Views
When it is required to flatten a given nested list using recursion technique, simple indexing, and the ‘isinstance’ method can be used along with recursion.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same − Live Demodef flatten_list(my_list): if my_list == []: return my_list if isinstance(my_list[0], list): return flatten_list(my_list[0]) + flatten_list(my_list[1:]) return my_list[:1] + flatten_list(my_list[1:]) my_list = [[1, 2], [3, 4], [90, 11], [56, 78], [[34, 56]]] print("The list is :") print(my_list) print("The ... Read More
2K+ Views
When it is required to reverse a string using recursion technique, a user defined method is used along with recursion.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same − Live Demodef reverse_string(my_string): if len(my_string) == 0: return my_string else: return reverse_string(my_string[1:]) + my_string[0] my_str = str(input("Enter the string that needs to be reversed : ")) print("The string is :") print(my_str) print("The reversed string is :") print(reverse_string(my_str))OutputEnter the string that needs to be reversed ... Read More
3K+ Views
When it is required to check if a string is a palindrome or not using recursion technique, simple indexing and a user defined function, along with recutsion is used.Palindromes are those strings or values which when read from left to right and right to left have the same characters in their respective indices.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.Below is a demonstration for the same −Example Live Demodef check_palindrome(my_str): if len(my_str) < 1: return True else: if ... Read More
694 Views
When it is required to find the product of two numbers using recursion technique, a simple if condition and recursion is used.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same − Live Demodef compute_product(val_1,val_2): if(val_1