Found 26504 Articles for Server Side Programming

Python Program to Find the Length of a List Using Recursion

AmitDiwan
Updated on 12-Mar-2021 12:30:03

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

Python Program to Find the Total Sum of a Nested List Using Recursion

AmitDiwan
Updated on 12-Mar-2021 12:28:23

646 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

Python Program to Flatten a Nested List using Recursion

AmitDiwan
Updated on 12-Mar-2021 12:26:58

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

Python Program to Reverse a String Using Recursion

AmitDiwan
Updated on 12-Mar-2021 12:24:52

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

Python Program to Check Whether a String is a Palindrome or not Using Recursion

AmitDiwan
Updated on 12-Mar-2021 12:23:35

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

Python Program to Find the Product of two Numbers Using Recursion

AmitDiwan
Updated on 12-Mar-2021 12:22:03

675 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

Python Program to Find if a Number is Prime or Not Prime Using Recursion

AmitDiwan
Updated on 12-Mar-2021 12:20:02

2K+ Views

When it is required to find out if a number is a prime number or not using recursion technique, a method is defined, and the ‘while’ condition 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 −def check_prime(my_num, my_val = None):    if my_val is None:       my_val = my_num – 1    while my_val >= 2:       if my_num % my_val == 0:          print(“The number is not a prime number”) ... Read More

Python Program for Reversal algorithm for array rotation

AmitDiwan
Updated on 12-Mar-2021 12:16:54

347 Views

When it is required to reverse a rotated array, a method is defined, that iterates through the list and reverse the list. Another method is defined, that rotates the list, and another method is defined that displays the list. A simple loop and indexing is used to achieve this.Below is a demonstration for the same −Example Live Demodef reverse_list(my_list, begin, end):    while (begin < end):       temp = my_list[begin]       my_list[begin] = my_list[end]       my_list[end] = temp       begin += 1       end = end-1 def left_rotate(my_list, to_rotate):    n ... Read More

Python program to determine whether the given number is a Harshad Number

AmitDiwan
Updated on 12-Mar-2021 12:15:06

1K+ Views

When it is required to determine if a given number is a Harshad number or not, a simple loop and ‘%’ operator, ‘+’ operator and ‘//’ operators can be used.A Harshad number is also known as a Niven number. It is a number whoses base is an integer that can be divided by the sum of its digits when it is written as that base value.Below is a demonstration for the same −Example Live Demomy_num = 134 remaining = sum_val = 0 print("A copy of the number to be checked is being made...") my_num_copy = my_num; while(my_num > 0):    remaining ... Read More

Python program to print all Happy numbers between 1 and 100

AmitDiwan
Updated on 12-Mar-2021 12:13:33

747 Views

When it is required to print all the ahppy numbers between 1 and 100, a simple loop and operations like ‘%’, ‘+’, and ‘//’ are used.A happy number is the one that ends up as 1, when it is replaced by the sum of square of every digit in the number.To print the happy numbers between a given range, a simple loop can be used.Below is a demonstration for the same −Example Live Demodef check_happy_num(my_num):    remaining = sum_val = 0    while(my_num > 0):       remaining = my_num%10       sum_val = sum_val + (remaining*remaining)     ... Read More

Advertisements