python questions[1]
python questions[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)
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)
Program:
s=input ("enter the string input: ")
rev=s [::-1]
print(rev)
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}
7. Write a program to filter even numbers from a list using list comprehension.
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))
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)
Expected Input:
"swiss"
Expected Output:
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)
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)
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")
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))
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.
Program
array = [10, 20, 30, 40, 50]
print("Original Array:", array)
array_reversed = sorted(array, reverse=True)
print("Reversed Array:", array_reversed)
18. Write a program to search for an element in an array and print its index.
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))
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 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.