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

DS_assignment_3

The document contains a series of Python programming assignments focused on arrays and strings. It includes tasks such as calculating the sum, maximum, and minimum of array elements, sorting arrays, reversing arrays, and string manipulations like finding lengths, checking for palindromes, and counting vowels and consonants. Each assignment is accompanied by code snippets demonstrating the required functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DS_assignment_3

The document contains a series of Python programming assignments focused on arrays and strings. It includes tasks such as calculating the sum, maximum, and minimum of array elements, sorting arrays, reversing arrays, and string manipulations like finding lengths, checking for palindromes, and counting vowels and consonants. Each assignment is accompanied by code snippets demonstrating the required functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment 3 → Array and String (list for python)

1. Write a program to find the sum of all the elements in an array.


2. arr = [1, 2, 3, 4, 5];
3. sum = 0;
4.
5. for i in range(0, len(arr)):
6. sum = sum + arr[i];
7.
8. print("Sum of all the elements of an array: " + str(sum));

2.Create a program to find the maximum element in an array.

arr = [10, 324, 45, 90, 9808]


def largest(arr, n):

max = arr[0]

for i in range(1, n):


if arr[i] > max:
max = arr[i]
return max

# arr = [10, 324, 45, 90, 9808]


n = len(arr)
Ans = largest(arr, n)
print("Largest in given array ", Ans)

3. Write a program to find the minimum element in an array.

arr = [25, 11, 7, 75, 56];

min = arr[0];

for i in range(0, len(arr)):

if(arr[i] < min):


min = arr[i];

print("Smallest element present in given array: " + str(min));


4.Create a program to find the average of all the elements in an array.

def calc_average(lst):
return sum(lst) / len(lst)
lst = [24, 19, 35, 46, 75, 29, 30, 18]
average = calc_average(lst)

print("The average of the list is ", round(average, 3))

5. Write a program to sort an array in ascending order.

arr = [5, 2, 8, 7, 1];


temp = 0;

print("Elements of original array: ");


for i in range(0, len(arr)):
print(arr[i], end=" ");

for i in range(0, len(arr)):


for j in range(i+1, len(arr)):
if(arr[i] > arr[j]):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

print();
print("Elements of array sorted in ascending order: ");
for i in range(0, len(arr)):
print(arr[i], end=" ");

6. Create a program to sort an array in descending order.

arr = [5, 9, 8, 7, 1, 2,4,3];


temp = 0;

print("Elements of original array: ");


for i in range(0, len(arr)):
print(arr[i]),

for i in range(0, len(arr)):


for j in range(i+1, len(arr)):
if(arr[i] < arr[j]):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
print();

print("Elements of array sorted in descending order: ");


for i in range(0, len(arr)):
print(arr[i]),

7.Write a program to reverse an array.

arr = [1, 2, 3, 4, 5];


print("Original array: ");
for i in range(0, len(arr)):
print(arr[i]),
print("Array in reverse order: ");

for i in range(len(arr)-1, -1, -1):


print(arr[i]),

8.Create a program to find the second largest element in an array.

list= [20, 30, 40, 25, 10,70,65]

list.sort()

print("The second largest element of the list is:", list[-2])

9. Write a program to find the length of a string.


10. def findLen(str):
11. counter = 0
12. for i in str:
13. counter += 1
14. return counter
15.
16. str = "vandanadewangan"
17. print(findLen(str))

10.Create a program to find the first occurrence of a character in a string.

test_str = "vandanadewangan"

print ("The original string is : " + str(test_str))

res = test_str.rfind('e')
print ("The index of last element occurrence: " + str(res))

11. Write a program to concatenate two strings.

str1 = "Hello"
str2 = "world,"
str3 = "i am from India"

print("% s % s %s" % (str1, str2, str3))

12.Create a program to check if a given string is a palindrome or not.

s = input("enter a string")
def isPalindrome(s):
return s == s[::-1]

ans = isPalindrome(s)

if ans:
print("Yes")
else:
print("No")

13. Write a program to convert all the characters in a string to uppercase.

string = 'vandanadewangan'
print(string.islower())
string = 'Vandanadewangan'
print(string.islower())

14.Create a program to convert all the characters in a string to lowercase.

string = 'Vandanadewangan'
print(string.islower())

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

string = "vandanadewangan!"
vowels = "aeiouAEIOU"
count = sum(string.count(vowel) for vowel in vowels)
print(count)

16.Create a program to count the number of consonants in a string.

def isConsonant(ch):

ch = ch.upper()

return not (ch == 'A' or ch == 'E' or


ch == 'I' or ch == 'O' or
ch == 'U') and ord(ch) >= 65 and ord(ch) <= 90

def totalConsonants(string):

count = 0

for i in range(len(string)):

if (isConsonant(string[i])):
count += 1

return count

string = "abc de"


print(totalConsonants(string))

18. Write a program to remove all the spaces from a string.


19. def remove(string):
20. return string.replace(" ", "")
21.
22. string = 'v an d a na d e wa n gan'
23. print(remove(string))
24.

18.Create a program to replace a specific character in a string with another character.

string = "Good Morning freinds "

new_string = string.replace("Good", "Great")

print(new_string)
19.Create programme to check whether the array is palindrome or not.

def palindrome(arr, n):

flag = 0;

i = 0;
while (i <= n // 2 and n != 0):

if (arr[i] != arr[n - i - 1]):


flag = 1;
break;
i += 1;

if (flag == 1):
print("Not Palindrome");
else:
print("Palindrome");

arr = [ 1, 2, 3, 2, 1 ];
n = len(arr);

palindrome(arr, n);

20.Create a programme to search specific element in an array.

def search (arr, l, h, key):


if l > h:
return -1

mid = (l + h) // 2
if arr[mid] == key:
return mid

if arr[l] <= arr[mid]:


if key >= arr[l] and key <= arr[mid]:
return search(arr, l, mid-1, key)
return search(arr, mid + 1, h, key)

if key >= arr[mid] and key <= arr[h]:


return search( mid + 1, h, key)
return search(arr, l, mid-1, key)

arr = [4, 5, 6, 7, 8, 9, 1, 2, 3]
key = 6
i = search(arr, 0, len(arr)-1, key)
if i != -1:
print ("Index: % d"% i)
else:
print ("Key not found")

You might also like