Python exercices notebook 2
Problem 1¶
Write a function to calculate the factorial of a number.
In [ ]:
Copied!
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5)) # Output: 120
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5)) # Output: 120
Problem 2¶
Write a function that takes two lists and returns True if they have at least one common member.
In [ ]:
Copied!
def common_member(a, b):
for i in a:
for j in b:
if i == j:
return True
return False
a = [1, 2, 3, 4, 5]
b = [5, 6, 7, 8, 9]
print(common_member(a, b)) # Output: True
def common_member(a, b):
for i in a:
for j in b:
if i == j:
return True
return False
a = [1, 2, 3, 4, 5]
b = [5, 6, 7, 8, 9]
print(common_member(a, b)) # Output: True
Problem 3¶
Write a function that takes a string and returns the first non-repeated character in the string.
In [ ]:
Copied!
def first_non_repeated_character(string):
char_count = {}
for i in string:
if i in char_count:
char_count[i] += 1
else:
char_count[i] = 1
for i in string:
if char_count[i] == 1:
return i
return None
string = 'abcdefghijklmnopqrstuvwxyzbcdefghijklmnopqrstuvwxyz'
print(first_non_repeated_character(string)) # Output: 'a'
def first_non_repeated_character(string):
char_count = {}
for i in string:
if i in char_count:
char_count[i] += 1
else:
char_count[i] = 1
for i in string:
if char_count[i] == 1:
return i
return None
string = 'abcdefghijklmnopqrstuvwxyzbcdefghijklmnopqrstuvwxyz'
print(first_non_repeated_character(string)) # Output: 'a'
a
Problem 4¶
Write a Python function that takes two strings and returns True if the first string is a substring of the second string, and False otherwise.
In [ ]:
Copied!
def is_substring(s1, s2):
return s1 in s2
print(is_substring("hello", "hello world"))
def is_substring(s1, s2):
return s1 in s2
print(is_substring("hello", "hello world"))
Problem 5¶
Write a Python program to find the sum of the digits of a number.
In [ ]:
Copied!
def sum_of_digits(number):
return sum(int(digit) for digit in str(number))
print(sum_of_digits(123))
def sum_of_digits(number):
return sum(int(digit) for digit in str(number))
print(sum_of_digits(123))
Problem 6¶
Write a Python program to find the longest word in a list of words.
In [ ]:
Copied!
def find_longest_word(words):
return max(words, key=len)
print(find_longest_word(["apple", "banana", "cherry"]))
def find_longest_word(words):
return max(words, key=len)
print(find_longest_word(["apple", "banana", "cherry"]))
Problem 7¶
Write a Python program to sort a list of dictionaries by a given key.
In [ ]:
Copied!
def sort_dict_list(dict_list, key):
return sorted(dict_list, key=lambda x: x[key])
dict_list = [{"name": "John", "age": 25}, {"name": "Jane", "age": 22}, {"name": "Jim", "age": 30}]
print(sort_dict_list(dict_list, "age"))
def sort_dict_list(dict_list, key):
return sorted(dict_list, key=lambda x: x[key])
dict_list = [{"name": "John", "age": 25}, {"name": "Jane", "age": 22}, {"name": "Jim", "age": 30}]
print(sort_dict_list(dict_list, "age"))
[{'name': 'Jane', 'age': 22}, {'name': 'John', 'age': 25}, {'name': 'Jim', 'age': 30}]
Problem 8¶
Write a Python program to find the frequency of each word in a string.
In [ ]:
Copied!
def word_frequency(string):
#splitting my sentence into words
words = string.split()
#print(words)
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
return word_count
string = "the quick brown the fox jumps over the lazy dog"
dic = word_frequency(string)
dic["fox"]
def word_frequency(string):
#splitting my sentence into words
words = string.split()
#print(words)
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
return word_count
string = "the quick brown the fox jumps over the lazy dog"
dic = word_frequency(string)
dic["fox"]
Out[ ]:
1
Problem 9¶
Write a Python program to find the prime numbers in a given range.
In [ ]:
Copied!
def prime_numbers(start, end):
prime_list = []
for number in range(start, end + 1):
for i in range(2, number):
if number % i == 0:
break
else:
prime_list.append(number)
return prime_list
print(prime_numbers(2, 20))
def prime_numbers(start, end):
prime_list = []
for number in range(start, end + 1):
for i in range(2, number):
if number % i == 0:
break
else:
prime_list.append(number)
return prime_list
print(prime_numbers(2, 20))
[2, 3, 5, 7, 11, 13, 17, 19]
In [ ]:
Copied!