Monday 28 December 2020

Python Program to Merge Two Lists and Sort it

 This is a Python Program to merge two lists and sort it.

Problem Description

The program takes two lists, merges them and sorts the merged list.

Problem Solution

1. Take in the number of elements for the first list and store it in a variable.
2. Take in the elements of the list one by one.
3. Similarly, take in the elements for the second list also.
4. Merge both the lists using the ‘+’ operator and then sort the list.
5. Display the elements in the sorted list.
6. Exit.

Program/Source Code

Here is source code of the Python Program to merge two lists and sort it. The program output is also shown below.

a=[]
c=[]
n1=int(input("Enter number of elements:"))
for i in range(1,n1+1):
    b=int(input("Enter element:"))
    a.append(b)
n2=int(input("Enter number of elements:"))
for i in range(1,n2+1):
    d=int(input("Enter element:"))
    c.append(d)
new=a+c
new.sort()
print("Sorted list is:",new)
Program Explanation

1. User must enter the number of elements for the first list and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a list.
3. User must similarly enter the elements of the second list one by one.
4. The ‘+’ operator is then used to merge both the lists.
5. The sort function then sorts the list in ascending order.
6. The sorted list is then printed.

Runtime Test Cases
 
Case 1:
Enter number of elements:5
Enter element:10
Enter element:20
Enter element:35
Enter element:55
Enter element:71
Enter number of elements:2
Enter element:5
Enter element:37
Sorted list is: [5, 10, 20, 35, 37, 55, 71]
 
Case 2:
Enter number of elements:3
Enter element:2
Enter element:4
Enter element:8
Enter number of elements:3
Enter element:2
Enter element:4
Enter element:0
Sorted list is: [0, 2, 2, 4, 4, 8]

Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10

 This is a Python Program to create a list of of all numbers in a range which are perfect squares and the sum of the digits of the number is less than 10.

Problem Description

The program takes a range and creates a list of all numbers in the range which are perfect squares and the sum of the digits is less than 10.

Problem Solution

1. User must enter the upper and lower range for the numbers.
2. A list must be created using list comprehension where the element is a perfect square within the range and the sum of the digits of the number is less than 10.
3. This list must then be printed.
4. Exit.

Program/Source Code

Here is source code of the Python Program to create a list of of all numbers in a range which are perfect squares and the sum of the digits of the number is less than 10. The program output is also shown below.

l=int(input("Enter lower range: "))
u=int(input("Enter upper range: "))
a=[]
a=[x for x in range(l,u+1) if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10]
print(a)
Program Explanation

1. User must enter the upper and lower range for the numbers.
2. List comprehension must be used to create a list of numbers which are perfect squares and sum of the digits is less than 10.
3. The second part of the list comprehension first maps separate digits of the number into a list and finds the sum of the elements in the list.
3. The list which is created is printed.

Runtime Test Cases
 
Case 1:
Enter lower range: 1
Enter upper range: 40
[1, 4, 9, 16, 25, 36]
 
Case 2:
Enter lower range: 50
Enter upper range: 100
[81, 100]

Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N

 This is a Python Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N.

Problem Description

The program takes in the the number of terms and finds the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N.

Problem Solution

1. Take in the number of terms to find the sum of the series for.
2. Initialize the sum variable to 0.
3. Use a for loop ranging from 1 to the number and find the sum of the series.
4. Print the sum of the series after rounding it off to two decimal places.
5. Exit.

Program/Source Code

Here is source code of the Python Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N. The program output is also shown below.

n=int(input("Enter the number of terms: "))
sum1=0
for i in range(1,n+1):
    sum1=sum1+(1/i)
print("The sum of series is",round(sum1,2))
Program Explanation

1. User must enter the number of terms to find the sum of.
2. The sum variable is initialized to 0.
3. The for loop is used to find the sum of the series and the number is incremented for each iteration.
4. The numbers are added to the sum variable and this continues till the value of i reaches the number of terms.
5. Then the sum of the series is printed.

Runtime Test Cases
 
Case 1:
Enter the number of terms: 7
The sum of series is 2.59
 
Case 2:
Enter the number of terms: 15
The sum of series is 3.32

Python Program to Search the Number of Times a Particular Number Occurs in a List

 This is a Python Program to search the number of times a particular number occurs in a list.

Problem Description

The program takes a number and searches the number of times the particular number occurs in a list.

Problem Solution

1. Take in the number of elements for the first list and store it in a variable.
2. Take in the elements of the list one by one.
3. Then take in the number to be searched in the list.
4. Use a for loop to traverse through the elements in the list and increment the count variable.
5. Display the value of the count variable which contains the number of times a particular number occurs in a list.
6. Exit.

Program/Source Code

Here is source code of the Python Program to search the number of times a particular number occurs in a list. The program output is also shown below.

a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
    b=int(input("Enter element:"))
    a.append(b)
k=0
num=int(input("Enter the number to be counted:"))
for j in a:
    if(j==num):
        k=k+1
print("Number of times",num,"appears is",k)
Program Explanation

1. User must enter the number of elements for the first list and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a list.
3. User must also enter the number to be search the list for.
4. A for loop is used to traverse through the elements in the list and an if statement is used to count a particular number.
5. The total count of a particular number in the list is printed.

Runtime Test Cases
 
Case 1:
Enter number of elements:4
Enter element:23
Enter element:45
Enter element:23
Enter element:67
Enter the number to be counted:23
Number of times 23 appears is 2
 
Case 2:
Enter number of elements:7
Enter element:12
Enter element:45
Enter element:67
Enter element:45
Enter element:67
Enter element:67
Enter element:67
Enter the number to be counted:67
Number of times 67 appears is 4