RECORD FILE FINALS
YASHNEIL AGGARWAL 11C
1. Function to find the sum of all odd numbers in a list:
def odd_sum(lst):
return sum(x for x in lst if x % 2)
2. Program to find the difference between the maximum and minimum
elements in a list:
lst = [3, 1, 9, 7, 4]
d = max(lst) - min(lst)
print(d)
3. Function to remove all occurrences of a given element from a list:
def rm_all(lst, v):
while v in lst:
[Link](v)
4. Function to check if a list is sorted in ascending order:
def is_sorted(lst):
for i in range(len(lst) - 1):
if lst[i] > lst[i + 1]:
return False
return True
5. Program to count the number of elements greater than a given
value in a list:
lst = [5, 12, 3, 8, 15]
v = 7
c = sum(1 for x in lst if x > v)
print(c)
6. Function to split a list into two halves and return both:
def split_lst(lst):
m = len(lst) // 2
return lst[:m], lst[m:]
7. Program to find the product of all elements in a list:
lst = [2, 3, 4, 5]
p = 1
for x in lst:
p *= x
print(p)
8. Function that takes a list and returns a new list with only the
elements at even indices:
def even_idx(lst):
return lst[::2]
9. Program to rotate a list to the left by one position:
lst = [1, 2, 3, 4, 5]
[Link]([Link](0))
print(lst)
Tuples
10. Program to find the sum of all elements in a tuple:
t = (1, 2, 3, 4, 5)
s = sum(t)
print(s)
11. Function to convert a tuple of strings into a single concatenated
string:
def tup_str(t):
return ''.join(t)
12. Program to find the length of a tuple without using len():
t = (1, 2, 3, 4, 5)
c = 0
for _ in t:
c += 1
print(c)
13. Function to check if a given element exists in a tuple:
def exists(t, v):
return v in t
14. Program to find the element that occurs most frequently in a
tuple:
t = (1, 3, 2, 3, 4, 3, 5)
m = max(set(t), key=[Link])
print(m)
Strings
15. Function to check if a string contains only digits:
def is_digit(s):
return [Link]()
16. Program to capitalize the first letter of each word in a given
string:
s = "hello world"
s = [Link]()
print(s)
17. Function to remove all punctuation from a string:
import string
def rm_punct(s):
return [Link]([Link]('', '',
[Link]))
18. Program to count the occurrences of each character in a string:
s = "banana"
d = {}
for c in s:
d[c] = [Link](c, 0) + 1
print(d)
19. Function to replace multiple spaces in a string with a single space:
def fix_spaces(s):
return ' '.join([Link]())
Dictionaries
20. Program to create a dictionary from two lists (keys and values):
k = ['a', 'b', 'c']
v = [1, 2, 3]
d = dict(zip(k, v))
print(d)
21. Function to reverse the keys and values in a dictionary:
def rev_dict(d):
return {v: k for k, v in [Link]()}
22. Program to check if two dictionaries are equal:
d1 = {'a': 1, 'b': 2}
d2 = {'a': 1, 'b': 2}
print(d1 == d2)
23. Function to get the sum of all values in a dictionary:
def sum_vals(d):
return sum([Link]())
24. Program to find the key with the smallest value in a dictionary:
d = {'a': 3, 'b': 1, 'c': 5}
m = min(d, key=[Link])
print(m)
[Link] a Python program to find the smallest element in a list
lst = [4, 2, 9, 1, 5]
s = min(lst)
print(s)