Skip to content

Commit 3f1145b

Browse files
authored
Question Set 2 (10Q)
1 parent 821baa0 commit 3f1145b

File tree

10 files changed

+242
-0
lines changed

10 files changed

+242
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Write a function named group_even_odd that
3+
takes a list of integers and returns:
4+
{
5+
"even": [...],
6+
"odd": [...]
7+
}
8+
9+
"""
10+
def group_even_odd(nums):
11+
even_odd_dict = {"even": [], "odd": []}
12+
13+
if not nums:
14+
return even_odd_dict
15+
16+
for n in nums:
17+
if n %2 == 0:
18+
even_odd_dict['even'].append(n)
19+
else:
20+
even_odd_dict['odd'].append(n)
21+
22+
return even_odd_dict
23+
24+
print(group_even_odd([1, 2, 3, 4, 5, 6]))
25+
# {'even': [2, 4, 6], 'odd': [1, 3, 5]}
26+
27+
print(group_even_odd([]))
28+
# {'even': [], 'odd': []}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Write a function named group_words_by_vowel_start
3+
that takes a list of words and groups them
4+
into two lists:
5+
6+
{
7+
"vowel": [...],
8+
"consonant": [...]
9+
}
10+
11+
"""
12+
13+
def group_words_by_vowel_start(words):
14+
result = {"vowel": [], "consonant": []}
15+
vowels = {'a', 'e', 'i', 'o', 'u'}
16+
17+
if not words:
18+
return result
19+
20+
for w in words:
21+
first_char = w[0].lower()
22+
if first_char in vowels:
23+
result['vowel'].append(w)
24+
else:
25+
result['consonant'].append(w)
26+
27+
return result
28+
29+
print(group_words_by_vowel_start(["apple", "banana", "orange", "grape", "umbrella"]))
30+
# {'vowel': ['apple', 'orange', 'umbrella'], 'consonant': ['banana', 'grape']}
31+
32+
print(group_words_by_vowel_start([]))
33+
# {'vowel': [], 'consonant': []}
34+
35+
print(group_words_by_vowel_start(None))
36+
# {'vowel': [], 'consonant': []}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Write a function named group_numbers_by_digits
3+
that takes a list of integers and groups them
4+
by number of digits.
5+
"""
6+
7+
def group_numbers_by_digits(nums):
8+
if not nums:
9+
return {}
10+
11+
result = {}
12+
for n in nums:
13+
count = len(str(n))
14+
if count in result:
15+
result[count].append(n)
16+
else:
17+
result[count] = [n]
18+
return result
19+
20+
21+
print(group_numbers_by_digits([1, 22, 333, 4, 55, 6666]))
22+
# {1: [1, 4], 2: [22, 55], 3: [333], 4: [6666]}
23+
24+
print(group_numbers_by_digits([]))
25+
# {}
26+
27+
print(group_numbers_by_digits(None))
28+
# {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Write a function named is_sorted_ascending
3+
that takes a list of numbers and returns
4+
True if the list is sorted in ascending order,
5+
otherwise False.
6+
"""
7+
8+
def is_sorted_ascending(nums):
9+
if not nums:
10+
return True
11+
12+
sort_nums = sorted(nums)
13+
return nums == sort_nums
14+
15+
print(is_sorted_ascending([1, 2, 3, 4])) # True
16+
print(is_sorted_ascending([1, 3, 2])) # False
17+
print(is_sorted_ascending([])) # True
18+
print(is_sorted_ascending([5])) # True
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Write a function named square_even_numbers
3+
that takes a list of numbers and returns a
4+
new list containing the square of only the
5+
even numbers.
6+
"""
7+
8+
def square_even_numbers(nums):
9+
if not nums:
10+
return []
11+
12+
even_list = []
13+
14+
for n in nums:
15+
if n % 2 == 0:
16+
nn = n** 2
17+
even_list.append(nn)
18+
19+
return even_list
20+
21+
22+
print(square_even_numbers([1, 2, 3, 4, 5]))
23+
# [4, 16]
24+
25+
print(square_even_numbers([]))
26+
# []
27+
28+
print(square_even_numbers([1, 3, 5]))
29+
# []
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Write a function named has_duplicates that
3+
returns True if a list contains any duplicate
4+
values, otherwise False.
5+
"""
6+
# M1
7+
def has_duplicates(nums):
8+
if not nums:
9+
return False
10+
unique = []
11+
12+
for w in nums:
13+
if w not in unique:
14+
unique.append(w)
15+
16+
return nums != unique
17+
18+
# M2
19+
def has_duplicates(nums):
20+
seen = set()
21+
for n in nums:
22+
if n in seen:
23+
return True
24+
seen.add(n)
25+
return False
26+
27+
print(has_duplicates([1, 2, 3, 4])) # False
28+
print(has_duplicates([1, 2, 3, 2])) # True
29+
print(has_duplicates([])) # False
30+
print(has_duplicates([5])) # False
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Write a function named reverse_list
3+
that reverses a list without using
4+
.reverse().
5+
"""
6+
7+
def reverse_list(nums):
8+
return nums[::-1]
9+
10+
print(reverse_list([1, 2, 3, 4])) # [4, 3, 2, 1]
11+
print(reverse_list([])) # []
12+
print(reverse_list([5])) # [5]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Write a function named running_sum that
3+
returns a list where each element is the
4+
sum of all previous elements including itself.
5+
"""
6+
7+
def running_sum(nums):
8+
new_list = []
9+
total = 0
10+
for n in nums:
11+
total += n
12+
new_list.append(total)
13+
return new_list
14+
15+
print(running_sum([1, 2, 3, 4])) # [1, 3, 6, 10]
16+
print(running_sum([])) # []
17+
print(running_sum([5])) # [5]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Write a function named second_largest that returns
3+
the second largest number in a list.
4+
5+
- Do not use sorted()
6+
- Assume the list has at least two distinct numbers
7+
"""
8+
9+
def second_largest(nums):
10+
first = float('-inf')
11+
second = float('-inf')
12+
13+
for n in nums:
14+
if n > first:
15+
second = first
16+
first = n
17+
elif n < first and n > second:
18+
second = n
19+
20+
return first, second
21+
22+
print(second_largest([10, 5, 20, 8])) # 10
23+
print(second_largest([1, 2, 3, 4])) # 3
24+
print(second_largest([5, 5, 3, 2, 5])) # 3
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Write a function named max_difference that returns
3+
the maximum difference between two numbers in a list
4+
such that the larger number comes after the smaller one
5+
without using sort.
6+
Ex-
7+
max_difference([2, 3, 10, 6, 4, 8, 1]) # 8 (10 - 2)
8+
"""
9+
10+
def max_difference(nums):
11+
min_so_far = nums[0]
12+
max_diff = nums[1] - nums[0]
13+
14+
for i in range(1, len(nums)):
15+
max_diff = max(max_diff, nums[i] - min_so_far)
16+
min_so_far = min(nums[i], min_so_far)
17+
18+
return max_diff
19+
20+
print(max_difference([2, 3, 10, 6, 4, 8, 1]))

0 commit comments

Comments
 (0)