Program of Cumulative sum in python
How to convert list to dictionary in Python
How to Remove Duplicates from a list in Python
Program of Cumulative sum in python
strong-number-in-python
Python Example Code: Find all the pairs of numbers which sum upto k
python-example-code-convert-the-string-123-into-123-without-using-the-built-api-int
python-example-code-find-the-pair-of-adjacent-elements-that-has-the-largest-product
python-example-code-program-to-display-the-fibonacci-sequence-up-to-n-th-termpython-example-code
python-example-code-program-for-printing-k-largest-elements-in-an-array
python-example-code-find-out-string-is-palindrome-or-not
What is the cumulative sum?
The cumulative sum means "how much so far". The definition of the cumulative sum is the sum of a given sequence that is increasing or getting bigger with more additions. The real example of a cumulative sum is the increasing amount of water in a swing pool.
Example:
Input: 10, 15, 20, 25, 30
Output: 10, 25, 45, 70, 100
Input: 1, 2, 3, 4, 5 6, 7 ,8, 9, 10
Output: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55
Program: 1
*Cumulative sum *
def Cumulative_sum(lists):
cum_list = []
lenlength = len(lists)
cum_list = [sum(lists[0:x:1]) for x in range(0, length+1)]
return cum_list[1:]
lists = [10, 15, 20, 25, 30]
print (Cumulative_sum(lists))
Output:
10, 25, 45, 70, 100
Program: 2
list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
cum_list=[]
y = 0
for x in range(0,len(list)):
y+=list[x]
cum_list.append(y)
print(cum_list)
Output:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55
Program 3: User define program
i = []
n = int(input("enter the no of elements in list:"))
for x in range(0,n):
element=int(input("enter the element" + str(x+1) + ":"))
i.append(element)
j=[sum(i[0:x+1]) for x in range(0,len(i))]
print("Original list is: ",i)
print("Cumulative sum list is: ",j)
Output:
enter the no of elements in list: 10
enter the element1: 2
enter the element2: 3
enter the element3: 8
enter the element4: 6
enter the element5: 45
enter the element6: 32
enter the element7: 56
enter the element8: 32
enter the element9: 14
enter the element10: 25
Original list is: [2, 3, 8, 6, 45, 32, 56, 32, 14, 25]
Cumulative sum list is: [2, 5, 13, 19, 64, 96, 152, 184, 198, 223]
How to convert list to dictionary in Python
Lists and Dictionaries are two data structure which is used to store the Data. List stores the heterogeneous data type and Dictionary stores data in key-value pair. Here, we are converting the Python list into dictionary. Since list is ordered and dictionary is unordered so output can differ in order. Python list stores the element in the following way.
student_marks = [56, 78, 96, 37, 85]
On the other hand, Dictionary is unordered, and stores the unique data. It stores the data in key value pair where each key is associated with it value. Python Dictionary stores the data in following way.
student_dict = {'Abhinay': 56, 'Sharma': 78, 'Himanshu': 96, 'Peter': 37}
In this tutorial, we will learn the conversion Python list to dictionary.
Sample Input:
Input : ['Name', 'Abhinay', 'age', 25, 'Marks', 90]
Output : {'Name', 'Abhinay', 'age', 25, 'Marks', 90}
Input : ['a', 10, 'b', 42, 'c', 86]
Output : {'a', 10, 'b', 42, 'c', 86}
Let's understand the following methods.
Method - 1 Using Dictionary Comprehension
We can convert the list into dictionary using the dictionary comprehension. Let's understand the following code.
Example -
student = ["James", "Abhinay", "Peter", "Bicky"]
student_dictionary = { stu : "Passed" for stu in student }
print(student_dictionary)
Output:
{'James': 'Passed', 'Abhinay': 'Passed', 'Peter': 'Passed', 'Bicky': 'Passed'}
Explanation -
In the above code, we have created a student list to be converted into the dictionary. Using the dictionary compression, we converted the list in dictionary in a single line. The list elements tuned into key and passed as a value.
Let's understand another example.
Example - 2
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
square_dict = {n: n*n for n in list1}
print(square_dict)
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
Explanation:
In the above code, we have created square_dict with number-square key/value pair.
Method - 2 Using zip() function
The zip() function is used to zip the two values together. First, we need to create an iterator and initialize to any variable and then typecast to the dict() function.
Let's understand the following example.
Example -
def Convert_dict(a):
init = iter(list1)
res_dct = dict(zip(init, init))
return res_dct
# Driver code
list1 = ['x', 1, 'y', 2, 'z', 3]
print(Convert_dict(list1))
Output:
{'x': 1, 'y': 2, 'z': 3}
strong-number-in-python
Strong Number in Python
In this tutorial, we will learn a Python program to find a given number is a Strong number or not.
What is a strong number?
A Strong number is a special number whose sum of the all digit factorial should be equal to the number itself.
To find a whether given number is strong or not. We pick each digit from the given number and find its factorial, and we will do this every digit of the number.
Once we get the factorial of all digit, then we do the sum of factorials. If the sum is equal to the given number then the given number is strong otherwise not.
For example - The given number is 145, we have to pick each digit and find the factorial 1! = 1, 4! = 24, and 5! = 120.
Now, we will do the sum of the factorials, we get 1+24+120 = 145, which is exactly the same as the given number. So we can say that 145 is a strong number.
We got the logic of the strong number. Now implements it using the Python Program.
Problem Approach
Ask the user to enter an integer number.
Find the factorial of each digit in the number using the two while loop.
Now, sum up all the factorial number.
Check if it is equal to the given number.
Print the Output.
Exit
Sample Input: num = 132
Sample Output: Given number is not a strong number
Explanation: 1! + 3! + 2! = 9 which is not equal to the 132
Sample Input: num = 145
Sample Output: Given number is a strong number.
Python Program to Find Strong Number
Below is the code of the Python program to print the given number is a strong or not.
Example -
# Variable to store sum of the numbers
sum=0
# Ask user to enter the number
num=int(input("Enter a number:"))
# temporary variable store copy of the original number
temp=num
# Using while loop
while(num):
# intialize with 1
i=1
# fact variable with 1
fact=1
rem=num%10
while(i<=rem):
fact=fact*i # Find factorial of each number
i=i+1
sum=sum+fact
num=num//10
if(sum==temp):
print("Given number is a strong number")
else:
print("Given number is not a strong number")
Output:
Enter a number: 145
Given number is a strong number.
Explanation:
In the above code
We declared a variable integer value num for enter a number.
Defined the sum variable with zero.
A copy of the num value to the temp variable.
In the first while loop, ensure that the given number is greater than 0.
Inside the while loop, split the number and assigns the variable to find the factorial of each number.
In the second while loop (nested while loop), finds the factorial of the each number.
Suppose user enter the value = 145 and sum = 0
Assigning initial values
i = 0
fact = 0
temp = num
temp = 145
Now understand the loop iteration. First Iteration
rem = temp % 10
rem = 145 % 10 = 5
Now, we entered in the nested while loop. It calculates the factorial of 5 is 120.
sum = sum + 120> 0+120
sum = 120
temp = temp//10 = 14
temp = 14
Second Iteration
temp = 14,
sum = 120
rem = 14 % 10 = 4
Now, it enters into nested While loop. Here, it calculates the factorial of 4 = 24.
sum = 120 + 24
sum = 144
temp = 14//10
temp = 1
Third Iteration
temp = 1
sum = 144
rem = 1 % 10 = 0
The factorial of 1 is 1
sum = 144 + 1
sum = 145
temp = 1 / 10
temp = 0
Here, the temp = 0 so, the while loop condition fails.
If (num == sum) Now, we check the condition whether the user enter number is exactly equal to sum or not. If this condition returns True, then it is strong Number, else it is not Strong Number.
We have complete the program using the while loop. We can also use for loop to find whether a given number is strong or not.
Strong Number Using the for loop
We can also find the strong number using for loop. The logic is the same as above program, the while loop is replaced by for loop.
Example -
Python Program to find Strong Number
num = int(input(" Enter the Number:"))
sum = 0
temp = num
while(temp > 0):
fact = 1
rem = temp % 10
for i in range(1, rem + 1):
fact = fact * i
print("Factorial of %d = %d" %(rem, fact))
sum = sum + fact
temp = temp // 10
print("\n Sum of Factorials of a Given Number %d = %d" %(num, sum))
if (sum == num):
print(" The given number is a Strong Number")
else:
print(" The given number is not a Strong Number")
Output:
Enter the Number:145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
Sum of Factorials of a Given Number 145 = 145
The given number is a Strong Number
Python Program to find strong number using factorial function
Python math module provides the built-in math module. By using this method, we can omit the use of the nested while loop.
Example -
Python Program to find Strong Number
import math
num = int(input(" Enter the Number:"))
sum = 0
temp = num
while(temp > 0):
rem = temp % 10
fact = math.factorial(rem) # Using the buitlt-in factorial() function
print("Factorial of %d = %d" %(rem, fact))
sum = sum + fact
temp = temp // 10
print("\n Sum of Factorials of a Given Number %d = %d" %(num, sum))
if (sum == num):
print(" The given number is a Strong Number")
else:
print(" The given number is not a Strong Number")
Output:
Enter the Number: 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
Sum of Factorials of a Given Number 145 = 145
The given number is a Strong Number
Explanation -
In the above code,
We used the factorial() function and passed a reminder as an argument.
In the first iteration of while loop, it returned the reminder 5 and passed to the factorial of 5.
It will go on until the temp value is greater than zero. We don't need to use another while loop.
Top comments (0)