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
When it is required to print all the disarium numbers between 1 and 100, a simple loop can be run between 1 and 100 and the length of every number can be calculated, and the power of the position can be multipled with the number itself.If they are equal, it is considered as a disarium number.A Disarium number is the one where the sum of its digits to the power of their respective position is equal to the original number itself.Below is a demonstration for the same −Example Live Demodef length_calculation(my_val): len_val = 0 while(my_val != 0): ... Read More
When it is required to check if a given nmber is a disarium number, the sum of digits powered to their respective position is computed. Before this, the number of digits present in the number is determined.A Disarium Number is the one where the sum of its digits to the power of their respective position is equal to the original number itself.Below is a demonstration for the same −Example Live Demodef length_calculation(num_val): length = 0 while(num_val != 0): length = length + 1 num_val = num_val//10 return length my_num = 192 remaining ... Read More
When it is required to find the sum of digits in a number without using the method of recursion, the ‘%’ operator, the ‘+’ operator and the ‘//’ operator can be used.Below is a demonstration for the same −Example Live Demodef sum_of_digits(my_num): sum_val = 0 while (my_num != 0): sum_val = sum_val + (my_num % 10) my_num = my_num//10 return sum_val my_num = 12345671 print("The number is : ") print(my_num) print("The method to calculate sum of digits is being called...") print("The sum of " +str(my_num) + " is : ") print(sum_of_digits(my_num))OutputThe number ... Read More
When it is required to find all the numbers that are odd, and are palindromes and lie between a given range of values, and it has been told that recursion can’t be used, then, list comprehension, and ‘%’ operator can be used to achieve the same.Palindromes are string that are same when they are read in either way- left to right and right to left.Below is a demonstration for the same −Example Live Demomy_list = [] lower_limit = 5 upper_limit = 189 print("The lower limit is : ") print(lower_limit) print("The upper limit is : ") print(upper_limit) my_list = [x for x ... Read More
When it is required to implement a binomial tree in Python, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform calculator operations.Below is a demonstration for the same −Example Live Democlass binomial_tree: def __init__(self, key): self.key = key self.children = [] self.order = 0 def add_at_end(self, t): self.children.append(t) self.order = self.order + 1 my_tree = ... Read More
When it is required to split the list, and then add this first part to the end of the list, a simple iteration through the list and list slicing is required.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a demonstration for the same −Example Live Demodef split_list(my_list, n_val, k_val): for i in range(0, k_val): first_val = my_list[0] for k in range(0, n_val-1): my_list[k] = my_list[k + 1] my_list[n_val-1] = first_val my_list ... Read More
When it is required to create a class to get all the possible subsets of integers from a list, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform calculator operations.Below is a demonstration for the same −Example Live Democlass get_subset: def sort_list(self, my_list): return self. subset_find([], sorted(my_list)) def subset_find(self, curr, my_list): if my_list: return self. subset_find(curr, my_list[1:]) + self. subset_find(curr ... Read More
When it is required to create a class that has a method that accepts a string from the user, and another method that prints the string, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform calculator operations.Below is a demonstration for the same −Example Live Democlass print_it(): def __init__(self): self.string = "" def get_data(self): self.string=input("Enter the string : ") def put_data(self): ... Read More
When it is required to create a class that performs calculator operations, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform calculator operations.Below is a demonstration for the same −Example Live Democlass calculator_implementation(): def __init__(self, in_1, in_2): self.a=in_1 self.b=in_2 def add_vals(self): return self.a+self.b def multiply_vals(self): return self.a*self.b def divide_vals(self): return self.a/self.b def ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP