Skip to content

Commit 821baa0

Browse files
authored
Question Set 1 (10Q)
1 parent b0d417b commit 821baa0

File tree

10 files changed

+265
-0
lines changed

10 files changed

+265
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Write a Python function that takes a list
3+
of numbers and returns the largest number
4+
in the list without using max().
5+
"""
6+
7+
def largest_number_in_list(nums):
8+
largest_nums = nums[0]
9+
for i in range(len(nums)):
10+
11+
if largest_nums > nums[i]:
12+
largest_nums = nums[i]
13+
return nums[i]
14+
15+
print(largest_number_in_list([11, 3, 4, 5, 8]))
16+
print(largest_number_in_list([1, 2, 3, ]))
17+
print(largest_number_in_list([10, -5, 5]))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Write a Python function that takes a list
3+
of numbers and returns the largest number
4+
in the list without using max().
5+
"""
6+
7+
def largest_number_in_list(nums):
8+
largest_nums = nums[0]
9+
for n in nums:
10+
if n > largest_nums:
11+
largest_nums = n
12+
return largest_nums
13+
14+
15+
print(largest_number_in_list([11, 3, 4, 5, 8]))
16+
print(largest_number_in_list([1, 2, 3, ]))
17+
print(largest_number_in_list([10, -5, 5]))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Write a Python function named count_even_numbers
3+
that takes a list of integers as input and returns
4+
the count of even numbers in the list.
5+
Do not use count() or any built-in shortcuts.
6+
"""
7+
8+
def count_even_numbers(nums):
9+
total = 0
10+
for n in nums:
11+
if n % 2 == 0:
12+
total += 1
13+
return total
14+
15+
print(count_even_numbers([1, 2, 3, 4, 5, 6]))# 3
16+
print(count_even_numbers([10, 20, 30])) # 3
17+
print(count_even_numbers([1, 3, 5])) # 0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Write a Python function named count_vowels
3+
that takes a string and returns the number
4+
of vowels (a, e, i, o, u) in it.
5+
Uppercase and lowercase both count.
6+
"""
7+
8+
# M1
9+
def count_vowels(text):
10+
count = 0
11+
for t in text.lower():
12+
if t in ['a', 'e', 'i', 'o', 'u']:
13+
count+=1
14+
15+
return count
16+
17+
# M2
18+
def count_vowels(text):
19+
vowels = {'a', 'e', 'i', 'o', 'u'}
20+
count = 0
21+
for ch in text.lower():
22+
if ch in vowels:
23+
count += 1
24+
return count
25+
26+
print(count_vowels("hello")) # 2
27+
print(count_vowels("PYTHON")) # 1
28+
print(count_vowels("bcdfg")) # 0
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Write a Python function named remove_duplicates
3+
that takes a list and returns a new list with
4+
duplicates removed while keeping the original order.
5+
"""
6+
7+
# M1
8+
def remove_duplicates(nums):
9+
unique_list = []
10+
for n in nums:
11+
if n not in unique_list:
12+
unique_list.append(n)
13+
return unique_list
14+
15+
# M2
16+
def remove_duplicates(nums):
17+
seen = set()
18+
results = []
19+
for n in nums:
20+
if n not in seen:
21+
seen.add(n)
22+
results.append(n)
23+
return results
24+
25+
26+
print(remove_duplicates([1, 2, 2, 3, 1, 4]))
27+
# Expected output: [1, 2, 3, 4]
28+
29+
print(remove_duplicates([5, 5, 5]))
30+
# Expected output: [5]
31+
32+
print(remove_duplicates([]))
33+
# Expected output: []
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Write a Python function named word_count
3+
that takes a string and returns a dictionary
4+
where the keys are words and the values are
5+
how many times each word appears.
6+
"""
7+
8+
def word_count(text):
9+
if not text:
10+
return {}
11+
12+
words = text.lower().split(" ")
13+
counts = {}
14+
15+
for w in words:
16+
if w in counts:
17+
counts[w] += 1
18+
else:
19+
counts[w] = 1
20+
return counts
21+
22+
23+
print(word_count("apple banana apple"))
24+
# Expected output: {'apple': 2, 'banana': 1}
25+
26+
print(word_count("Python python PYTHON"))
27+
# Expected output: {'python': 3}
28+
29+
print(word_count(""))
30+
# Expected output: {}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Write a Python function named char_count
3+
that takes a string and returns a dictionary
4+
showing how many times each character appears
5+
(ignore spaces).
6+
"""
7+
8+
def char_count(text):
9+
10+
if text is None:
11+
return {}
12+
13+
counts = {}
14+
for w in text:
15+
if w == " ":
16+
continue
17+
if w in counts:
18+
counts[w] += 1
19+
else:
20+
counts[w] = 1
21+
return counts
22+
23+
print(char_count("hello"))
24+
# Expected output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
25+
26+
print(char_count("a a b"))
27+
# Expected output: {'a': 2, 'b': 1}
28+
29+
print(char_count(""))
30+
# Expected output: {}
31+
32+
33+
# https://cscircles.cemc.uwaterloo.ca/visualize#mode=edit
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Write a function named count_positive_negative
3+
that takes a list of numbers and returns a
4+
dictionary with counts of positive, negative,
5+
and zero numbers.
6+
"""
7+
8+
def count_positive_negative(nums):
9+
count = {"positive": 0, "negative": 0, "zero": 0}
10+
11+
if nums is None:
12+
return count
13+
14+
for n in nums:
15+
if n > 0:
16+
count["positive"] += 1
17+
elif n < 0:
18+
count["negative"] += 1
19+
else:
20+
count["zero"] +=1
21+
22+
return count
23+
24+
25+
print(count_positive_negative([1, -2, 0, 3, -1, 0]))
26+
# Expected output: {'positive': 2, 'negative': 2, 'zero': 2}
27+
28+
print(count_positive_negative([]))
29+
# Expected output: {'positive': 0, 'negative': 0, 'zero': 0}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Write a function named group_by_length
3+
that takes a list of words and returns
4+
a dictionary where:
5+
6+
- the key is the word length
7+
- the value is a list of words of that length
8+
"""
9+
10+
def group_by_length(words):
11+
if not words:
12+
return {}
13+
14+
count = {}
15+
16+
for w in words:
17+
l = len(w)
18+
if l in count:
19+
count[l].append(w)
20+
else:
21+
count[l] = [w]
22+
23+
return count
24+
25+
print(group_by_length(["hi", "hy", "hello", "hey", "a"]))
26+
# Expected output: {2: ["hi", "hy"], 5: ['hello'], 3: ['hey'], 1: ['a']}
27+
28+
print(group_by_length([]))
29+
# Expected output: {}
30+
31+
print(group_by_length(None))
32+
# Expected output: {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Write a function named group_by_first_letter
3+
that takes a list of words and groups them by
4+
their first character.
5+
"""
6+
7+
def group_by_first_letter(words:list) -> dict:
8+
if not words:
9+
return {}
10+
11+
count_dict = {}
12+
13+
for w in words:
14+
key = w[0]
15+
if key in count_dict:
16+
count_dict[key].append(w)
17+
else:
18+
count_dict[key] = [w]
19+
20+
return count_dict
21+
22+
print(group_by_first_letter(["apple", "ant", "banana", "bat"]))
23+
# Expected output: {'a': ['apple', 'ant'], 'b': ['banana', 'bat']}
24+
25+
print(group_by_first_letter([]))
26+
# Expected output: {}
27+
28+
print(group_by_first_letter(None))
29+
# Expected output: {}

0 commit comments

Comments
 (0)