21. Write a function to implement binary search on a sorted list
Required Input:
[1, 3, 5, 7, 9], 5
Expected Output:
2
Code In Python
def binary_search():
lst = [1, 3, 5, 7, 9]
target = 5
# Your code here
return
Output
Click Run Button to view compiled output
22. Write a function to find the longest word in a sentence and return its length
Required Input:
Python is a powerful programming language
Expected Output:
11
Code In Python
def longest_word_length():
sentence = "Python is a powerful programming language"
# Your code here
return
Output
Click Run Button to view compiled output
23. Write a function to calculate the edit distance between two strings (Levenshtein distance)
Required Input:
kitten, "sitting"
Expected Output:
3
Code In Python
def edit_distance():
s1 = "kitten"
s2 = "sitting"
# Your code here
return
Output
Click Run Button to view compiled output
24. Write a function to find all triplets in a list that sum to zero
Required Input:
[-1, 0, 1, 2, -1, -4]
Expected Output:
[[-1, -1, 2], [-1, 0, 1]]
Code In Python
def find_zero_sum_triplets():
lst = [-1, 0, 1, 2, -1, -4]
# Your code here
return
Output
Click Run Button to view compiled output
25. Write a function to determine if a number is a happy number (A happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit)
Required Input:
19
Expected Output:
True
Code In Python
def is_happy_number():
n = 19
# Your code here
return
Output
Click Run Button to view compiled output
26. Write a function to group anagrams from a list of strings
Required Input:
['eat', 'tea', 'tan', 'ate', 'nat', 'bat']
Expected Output:
[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]
Code In Python
def group_anagrams():
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
# Your code here
return
Output
Click Run Button to view compiled output
27. Write a function to check if a string is a valid parenthesis string
Required Input:
()[]{}
Expected Output:
True
Code In Python
def is_valid_parenthesis():
s = "()[]{}"
# Your code here
return
Output
Click Run Button to view compiled output
28. Write a function to determine if two strings are isomorphic (if characters in one string can be replaced to get the other string)
Required Input:
egg, "add"
Expected Output:
True
Code In Python
def is_isomorphic():
s1 = "egg"
s2 = "add"
# Your code here
return
Output
Click Run Button to view compiled output
29. Write a function to find the minimum number of coins needed to make a certain amount using a given set of coin denominations
Required Input:
[1, 2, 5], 11
Expected Output:
3
Code In Python
def coin_change():
coins = [1, 2, 5]
amount = 11
# Your code here
return
Output
Click Run Button to view compiled output
30. Write a function to determine if a list of numbers is monotonic (either entirely non-increasing or non-decreasing)
Required Input:
[1, 2, 2, 3]
Expected Output:
True
Code In Python
def is_monotonic():
lst = [1, 2, 2, 3]
# Your code here
return
Output
Click Run Button to view compiled output