Hints AI - Python - Session 4.ipynb - Colab
Hints AI - Python - Session 4.ipynb - Colab
Write a function count_even_odd(numbers) that takes a list of integers and returns a tuple (even_count, odd_count), where:
a. Find all even numbers, for each even number increase the counter for even by 1.
Iterates over the group of elements and for each element check the condition.
2 3
(2, 3)
Write a function reverse_words(sentence) that takes a string and returns a new string where the order of words is reversed.
Steps:
Write a function find_min_max(values) that takes a tuple of numbers and returns a tuple (min_value, max_value).
1. Sort the elements and then get the last element and the first element.
33 77
Write a function remove_duplicates(lst) that takes a list and returns a new list with duplicates removed (while maintaining order).
x = [55 66 77 88 99]
1. Create an empty list.
2. Iterate over the first list, then check if the current element is in the second one if not append, if use skip and go to the next element.
3. Keep doing this till iterating over all elements.
print(y)
Write a function most_frequent_char(s) that takes a string and returns the character that appears the most times.
dictionary:
aaabbcc
Iterates over the sentence, for each character check if it is already in the dictionary, if yes ==> increase its count by 1 (value). If no initialize its
value by 1.
dict_keys(['a', 'b'])
x = 60
print("Qualified" if x > 50 else "Not Qualified")
Qualified
Write a function common_elements(list1, list2) that takes two lists and returns a new list containing only the elements that appear in both
lists (no duplicates).
x = [1, 2, 3, 4, 5]
y = [2, 3, 6, 7, 8]
iterates over x, if there was an element in x and also found in y append to list called common list.
x = [1, 2, 3, 4, 5]
y = [2, 3, 6, 7, 8]
common = []
for i in x: # i = 2
if i in y:
common.append(i)
common
[2, 3]
keyboard_arrow_down Sets
Functions, exceptions, map, filter, reduce and Lambda Functions.
{True, 2, 3, 4, 'welcome'}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-ca90c6e7048b> in <cell line: 0>()
----> 1 print(set1[0])
print(len(set1))
for i in set1:
print(i)
True
2
3
4
welcome
False
set1.add("orange")
print(set1)
set1 = {1, 2, 3}
set2 = {4, 5, 6}
set1.update(set2)
# set1 = set1.union(set2)
print(set1) # Union
print(set1 | set2) # Union
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6}
set1 = {2, 3, 4, 5}
set2 = {4, 5, 6}
set1&set2 # & Intersection ==> common
{4, 5}
lst1 = [2, 3, 4, 5]
lst2 = [4, 5, 6]
set(lst1) & set(lst2)
{4, 5}
set(lst1) - set(lst2)
{2, 3}
set1.remove(7)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-36-beb694c72006> in <cell line: 0>()
----> 1 set1.remove(7)
KeyError: 7
set1.discard(7)
set1.clear()
set1 = {4, 5, 6}
set2 = {4, 7, 9}
set1 & set2
set1.intersection(set2)
{4}
set1 = {4, 5, 6}
set2 = {4, 7, 9}
set1.union(set2)
{4, 5, 6, 7, 9}