0% found this document useful (0 votes)
2 views

python questions[1]

The document contains a series of programming tasks and their corresponding solutions in Python. Each task includes an expected input and output, along with the code to achieve the desired result. The tasks cover various topics such as list manipulation, string processing, searching algorithms, and sorting techniques.

Uploaded by

bannujeetham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python questions[1]

The document contains a series of programming tasks and their corresponding solutions in Python. Each task includes an expected input and output, along with the code to achieve the desired result. The tasks cover various topics such as list manipulation, string processing, searching algorithms, and sorting techniques.

Uploaded by

bannujeetham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Write a program to reverse a list without using built-in functions.

Expected Input and Output:


Input: [1, 2, 3, 4, 5]
Output: Reversed List: [5, 4, 3, 2, 1]

Program:
a= [1, 2, 3, 4, 5]
n=len(a)-1
start, end = 0, n
while start < end:
a[start], a[end] = a[end], a[start]
start += 1
end -= 1
print(a)

2. Write a program to find the largest and smallest numbers in a list.


Expected Input and Output:
Input: [34, 78, 12, 56, 99, 23]
Output:
Largest: 99
Smallest: 12
Program:
a= [34,78,12,56,99,23]
largest=a [0]
smallest=a [0]
for i in a:
if i>largest:
largest=i
if i<smallest:
smallest=i
print (largest, smallest)

3. Write a program to check if a string is a palindrome.


Expected Input and Output:
Input: "radar"
Output: 'radar' is a palindrome.
Input: "hello"
Output: 'hello' is not a palindrome.
Program:
s=input (“enter the input:”)
n=len(s)-1
is_palindrome=True
for i in range (0, n//2):
if s[i]==s[n-i-1]:
is_palindrome =False
break
if is_palindrome:
print (s, "is a palindrome")
else:
print (s, "is not a palindrome")

4. Write a program to count the frequency of each word in a sentence.

Expected Input and Output:


Input: "this is a test this is only a test"
Output: Word Frequencies: {'this': 2, 'is': 2, 'a': 2, 'test': 2, 'only': 1}

Program
from collections import Counter
def count_word(sentence):
words=sentence.lower().split()
wordcount=Counter(words)
return wordcount
sentence=input('enter the sentence')
wordfrequencies=count_word(sentence)
print(wordfrequencies)

5. Write a program to reverse a string using slicing.

Expected Input and Output:


Input: "Python"
Output: Reversed String: nohtyP

Program:
s=input ("enter the string input: ")
rev=s [::-1]
print(rev)

6. Write a program to find the union of two sets.

Input: set_a = {1, 2, 3}, set_b = {3, 4, 5}


Output: Union of Sets: {1, 2, 3, 4, 5}

Program:
Method 1
a= {1,2,3}
b= {3,4,5}
a=a.union(b)
print(a)

Method 2
def union_of_sets(set_a, set_b):
union_set = set_a.copy()
for element in set_b:
if element not in union_set:
union_set.add(element)
return union_set

set_a = {1, 2, 3}
set_b = {3, 4, 5}

print ("Set A:", set_a)


print ("Set B:", set_b)
print ("Union of Sets:", union_of_sets(set_a, set_b))

7. Write a program to filter even numbers from a list using list comprehension.

Expected Input and Output:


Input: [1, 2, 3, 4, 5, 6]
Output: Even Numbers: [2, 4, 6]
Program:
def even_num(a):
even_numbers=[]
for i in a:
if i%2==0:
even_numbers.append(i)
return even_numbers
a=[1,2,3,4,5,6]
even_num(a)
print(even_num(a))

8. Write a program to count the number of vowels in a string.

Expected Input and Output:


Input: "Hello World"
Output: Number of Vowels: 3
Program:

def number_vowels(a):
count=0
vowels='aeiouAEIOU'
for i in a:
if i in vowels:
count=count+1
return count
a="venkata sairam"
print(number_vowels(a))

9. Rotate a list by a given number of positions using slicing.

Expected Input and Output:


Input: numbers = [1, 2, 3, 4, 5], positions = 2
Output: Rotated List: [3, 4, 5, 1, 2]

10.Write a program to merge two sorted lists into a single sorted list

Expected Input:

list1 = [1, 3, 5]

list2 = [2, 4, 6]
Expected Output:
Merged List: [1, 2, 3, 4, 5, 6]

Program:
list1 = [1, 3, 5]
list2 = [2, 4, 6]
for i in range(0,len(list1)):
if list2[i] in list1:
pass
else:
list1.append(list2[i])
list1.sort()
print(list1)

11.Write a program to find the first non-repeating character in a string

Expected Input:
"swiss"
Expected Output:

First Non-Repeating Character: w

12.Sum and Product of Tuple Elements

Expected Input: (1, 2, 3, 4)

Expected Output: Su m and Product: (10, 24)

n=(1,2,3,4)
sum_of_tup=0
product_of_tup=1
for i in n:
sum_of_tup+=i
product_of_tup*=i
tple=(sum_of_tup,product_of_tup)
print(tple)
13. Write a program to create an array and display its elements

Program:
import array as arr
numbers = arr.array('i', [10, 20, 30, 40, 50])
print("The elements in the array are:")
for num in numbers:
print(num)

 Write a program to sort an array using Quick Sort.

Program

def partition(a,lb,ub):
pivot=a[lb]
start=lb
end=ub
while start<end:
while a[start]<=pivot and start<ub:
start=start+1
while a[end]>pivot and end>lb:
end=end-1
if start<end:
a[start],a[end]=a[end],a[start]
a[lb],a[end]=a[end],a[lb]
return end

def quicksort(a,lb,ub):
if lb<ub:
loc=partition(a,lb,ub)
quicksort(a,lb,loc-1)
quicksort(a,loc+1,ub)

a=[50,61,75,3,1,56,4,0]
lb=0
ub=len(a)-1
quicksort(a,lb,ub)
print(a)

14. Write a program to insert an element at a given index in an array.

Program:

a= [1,5,99,4,23,4]
element=int (input ('enter the number'))
index=int (input ('enter the index'))
a.insert(index, element)
print(a)

 Write a program to find the square root of a number using Binary Search.

def square_root(n,precison=0.0000001):
if n<0:
return None
left,right=0, max(1, n)
while right-left>=precison:
mid=(left+right)/2
if mid*mid==n:
return mid
elif mid*mid<n:
left=mid
else:
right=mid
return round((left + right) / 2, 6)
num=20
result=square_root(num)
if result!=None:
print(result)
else:
print("square root cannot compute for negative numbers")

15. Write a program to delete an element from a given index in an array.

def delete_element(arr,index):
if index<0 or index>=len(arr):
print(f"{index}: out of range")
else:
arr.pop(index)
return arr
arr=[5,4,3,2,1,6]
index=int(input('enter the index'))
print(delete_element(arr,index))

 Write a program to sort an array using Bubble Sort.


Program
a=[45,69,78,21,45,67,31,1,0,5,2]

for i in range(0,len(a)-1):
for j in range(0,len(a)-i-1):
if a[j]>a[j+1]:
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
print(a)

16. Write a program to find the largest and smallest element in an array.

def find_largest_smallest(arr): smallest = min(arr)


largest = max(arr)
print("Smallest Element:", smallest)
print("Largest Element:", largest)
array = [23, 56, 12, 78, 34, 90, 11]
print("Original Array:", array)
find_largest_smallest(array)

 Write a program to search for an element in an array using Linear Search.


Program
a= [547,69,4,78,235,48,1656,7,27,245]
n= len(a)
k=1000
for i in range (0, n):
if a[i]==k:
print ('element found')
break
else:
print ("element not found")

17. Write a program to reverse an array without using extra space.

Program
array = [10, 20, 30, 40, 50]
print("Original Array:", array)
array_reversed = sorted(array, reverse=True)
print("Reversed Array:", array_reversed)

 Write a program to sort an array using Insertion Sort.


Program
a=[56,547,6387,46,257,2457,14,1]
n=len(a)
for i in range(0,n):
temp=a[i]
j=i-1
while j>=0 and a[j]>temp:
a[j+1]=a[j]
j=j-1
a[j+1]=temp
print(a)

18. Write a program to search for an element in an array and print its index.

def search_element(arr, target):


if target in arr:
index = arr.index(target)
print(f"Element {target} found at index {index}.")
else:
print(f"Element {target} is not in the array.")

array = [10, 20, 30, 40, 50]


print("Array:", array)
element_to_find = 30
search_element(array, element_to_find)

 Write a program to merge two sorted arrays using Merge Sort technique.

19. Write a program to find the sum and average of elements in an array.

def calculate_sum_and_average(array):
total_sum = sum(array)
average = total_sum / len(array) if array else 0
return total_sum, average
array = [10, 20, 30, 40, 50]
total_sum, average = calculate_sum_and_average(array)
print(f"Sum: {total_sum}")
print(f"Average: {average}")

 Write a program to find the first and last occurrence of a given element using
Linear Search.
Program
a= [547,69,4,245,78,235,48,245,1656,7,27,245]
n= len (a)
k=245
first_occurance = -1
last_occurance = -1
for i in range (0, n):
if a[i]==k:
if first_occurance == -1:
first_occurance = i
last_occurance = i
if first_occurance ==-1:
print ("not found")
else:
print (first_occurance, last_occurance))

20.Write a program to copy elements of one array into another array.

Program:
import array
import copy
array_1 = array.array('i', [1, 2, 3, 4, 5])
array_2 = copy.copy(array_1) # Using the copy function
print(list(array_2))

 Write a program to sort an array using Selection Sort.


Program
a= [82,549,12,79,46,18,37,512,475]
n=len(a)
for i in range (0, n):
min=i
for j in range (i+1, n):
if a[j]<a[min]:
j=min
a[j], a[min]=a[min], a[j]
print(a)

21.Write a program to sort an array in ascending and descending order.


Program
def sort_array(arr):
ascending = sorted(arr)
print("Ascending Order:", ascending)

descending = sorted(arr, reverse=True)


print("Descending Order:", descending)
array = [5, 2, 9, 1, 5, 6]
print("Original Array:", array)
sort_array(array)

 Write a program to search for an element in a sorted array using Binary Search.
Program
a= [85,97,654,3512,10000]
n=len(a)
l=1
r=n-1
k=100000
while l<=r:
mid=(l+r)//2
if a[mid]==k:
print ("element found")
break
if a[mid]>k:
r=mid-1
if a[mid]<k:
l=mid+1
else:
print ("element not founded")

22. Write a program to merge two sorted arrays into one sorted array.

 Write a program to sort an array using Merge Sort.

You might also like