Skip to content

Commit 9cd41da

Browse files
committed
update readme
1 parent b3892f7 commit 9cd41da

38 files changed

+1004
-1010
lines changed

problems/adding-and-removing-dots.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
"""
2-
Adding and removing dots
3-
4-
Write a function named add_dots that takes a string and adds "." in between each letter. For example, calling add_dots("test") should return the string "t.e.s.t".
5-
6-
Then, below the add_dots function, write another function named remove_dots that removes all dots from a string. For example, calling remove_dots("t.e.s.t") should return "test".
7-
8-
If both functions are correct, calling remove_dots(add_dots(string)) should return back the original string for any string.
9-
10-
(You may assume that the input to add_dots does not itself contain any dots.)
11-
"""
12-
13-
14-
def add_dots(string):
15-
return ".".join(list(string))
16-
17-
18-
def remove_dots(string):
19-
return string.replace(".", "")
20-
21-
1+
"""
2+
Adding and removing dots
3+
4+
Write a function named add_dots that takes a string and adds "." in between each letter. For example, calling add_dots("test") should return the string "t.e.s.t".
5+
6+
Then, below the add_dots function, write another function named remove_dots that removes all dots from a string. For example, calling remove_dots("t.e.s.t") should return "test".
7+
8+
If both functions are correct, calling remove_dots(add_dots(string)) should return back the original string for any string.
9+
10+
(You may assume that the input to add_dots does not itself contain any dots.)
11+
"""
12+
13+
14+
def add_dots(string):
15+
return ".".join(list(string))
16+
17+
18+
def remove_dots(string):
19+
return string.replace(".", "")
20+
21+
2222
print(remove_dots(add_dots("hello")))

problems/all-equal.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
"""
2-
All equal
3-
4-
Define a function named all_equal that takes a list and checks whether all elements in the list are the same.
5-
6-
For example, calling all_equal([1, 1, 1]) should return True.
7-
"""
8-
9-
10-
def all_equal(l):
11-
result = True
12-
for i in range(len(l) - 1):
13-
if l[i] != l[i + 1]:
14-
result = False
15-
16-
return result
17-
18-
19-
print(all_equal([1, 1, 1, 1]))
20-
print(all_equal([1, 2, 1, 1]))
21-
22-
# naive solution
23-
""" def all_equal(items):
24-
for item1 in items:
25-
for item2 in items:
26-
if item1 != item2:
27-
return False
1+
"""
2+
All equal
3+
4+
Define a function named all_equal that takes a list and checks whether all elements in the list are the same.
5+
6+
For example, calling all_equal([1, 1, 1]) should return True.
7+
"""
8+
9+
10+
def all_equal(l):
11+
result = True
12+
for i in range(len(l) - 1):
13+
if l[i] != l[i + 1]:
14+
result = False
15+
16+
return result
17+
18+
19+
print(all_equal([1, 1, 1, 1]))
20+
print(all_equal([1, 2, 1, 1]))
21+
22+
# naive solution
23+
""" def all_equal(items):
24+
for item1 in items:
25+
for item2 in items:
26+
if item1 != item2:
27+
return False
2828
return True """

problems/anagrams.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
"""
2-
Anagrams
3-
4-
Two strings are anagrams if you can make one from the other by rearranging the letters.
5-
6-
Write a function named is_anagram that takes two strings as its parameters. Your function should return True if the strings are anagrams, and False otherwise.
7-
8-
For example, the call is_anagram("typhoon", "opython") should return True while the call is_anagram("Alice", "Bob") should return False.
9-
"""
10-
11-
12-
def is_anagram(str1, str2):
13-
str1 = "".join(sorted(str1))
14-
str2 = "".join(sorted(str2))
15-
return str1 == str2
16-
17-
18-
print(is_anagram("python", "nohtyp"))
1+
"""
2+
Anagrams
3+
4+
Two strings are anagrams if you can make one from the other by rearranging the letters.
5+
6+
Write a function named is_anagram that takes two strings as its parameters. Your function should return True if the strings are anagrams, and False otherwise.
7+
8+
For example, the call is_anagram("typhoon", "opython") should return True while the call is_anagram("Alice", "Bob") should return False.
9+
"""
10+
11+
12+
def is_anagram(str1, str2):
13+
str1 = "".join(sorted(str1))
14+
str2 = "".join(sorted(str2))
15+
return str1 == str2
16+
17+
18+
print(is_anagram("python", "nohtyp"))
1919
print(is_anagram("python", "JavaScript"))

problems/capital-indexes.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
"""
2-
Capital indexes
3-
4-
Write a function named capital_indexes. The function takes a single parameter, which is a string. Your function should return a list of all the indexes in the string that have capital letters.
5-
6-
For example, calling capital_indexes("HeLlO") should return the list [0, 2, 4].
7-
"""
8-
9-
10-
def capital_indexes(string):
11-
return [i for i, char in enumerate(string) if char.isupper()]
12-
13-
14-
print(capital_indexes("HeLlO"))
1+
"""
2+
Capital indexes
3+
4+
Write a function named capital_indexes. The function takes a single parameter, which is a string. Your function should return a list of all the indexes in the string that have capital letters.
5+
6+
For example, calling capital_indexes("HeLlO") should return the list [0, 2, 4].
7+
"""
8+
9+
10+
def capital_indexes(string):
11+
return [i for i, char in enumerate(string) if char.isupper()]
12+
13+
14+
print(capital_indexes("HeLlO"))

problems/consecutive-zeros.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
"""
2-
Consecutive zeros
3-
4-
The goal of this challenge is to analyze a binary string consisting of only zeros and ones. Your code should find the biggest number of consecutive zeros in the string. For example, given the string:
5-
6-
"1001101000110"
7-
The biggest number of consecutive zeros is 3.
8-
9-
Define a function named consecutive_zeros that takes a single parameter, which is the string of zeros and ones. Your function should return the number described above.
10-
"""
11-
12-
13-
def consecutive_zeros(string):
14-
string_list = string.split("1")
15-
for i in string_list:
16-
if i == "":
17-
string_list.remove(i)
18-
result = 0
19-
for i in string_list:
20-
if len(i) > result:
21-
result = len(i)
22-
return result
23-
24-
25-
print(consecutive_zeros("1000000"))
1+
"""
2+
Consecutive zeros
3+
4+
The goal of this challenge is to analyze a binary string consisting of only zeros and ones. Your code should find the biggest number of consecutive zeros in the string. For example, given the string:
5+
6+
"1001101000110"
7+
The biggest number of consecutive zeros is 3.
8+
9+
Define a function named consecutive_zeros that takes a single parameter, which is the string of zeros and ones. Your function should return the number described above.
10+
"""
11+
12+
13+
def consecutive_zeros(string):
14+
string_list = string.split("1")
15+
for i in string_list:
16+
if i == "":
17+
string_list.remove(i)
18+
result = 0
19+
for i in string_list:
20+
if len(i) > result:
21+
result = len(i)
22+
return result
23+
24+
25+
print(consecutive_zeros("1000000"))
2626
print(consecutive_zeros("1001101000110"))

problems/counting-sylables.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
"""
2-
Counting syllables
3-
Define a function named count that takes a single parameter. The parameter is a string. The string will contain a single word divided into syllables by hyphens, such as these:
4-
5-
"ho-tel"
6-
"cat"
7-
"met-a-phor"
8-
"ter-min-a-tor"
9-
Your function should count the number of syllables and return it.
10-
11-
For example, the call count("ho-tel") should return 2.
12-
"""
13-
14-
15-
def count(string):
16-
return len(string.split("-"))
17-
18-
1+
"""
2+
Counting syllables
3+
Define a function named count that takes a single parameter. The parameter is a string. The string will contain a single word divided into syllables by hyphens, such as these:
4+
5+
"ho-tel"
6+
"cat"
7+
"met-a-phor"
8+
"ter-min-a-tor"
9+
Your function should count the number of syllables and return it.
10+
11+
For example, the call count("ho-tel") should return 2.
12+
"""
13+
14+
15+
def count(string):
16+
return len(string.split("-"))
17+
18+
1919
print(count("ho-tel-code"))

problems/divisible-by-3.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
"""
2-
Divisible by 3
3-
Define a function named div_3 that returns True if its single integer parameter is divisible by 3 and False otherwise.
4-
5-
For example, div_3(6) is True because 6/3 does not leave any remainder. However div_3(5) is False because 5/3 leaves 2 as a remainder.
6-
"""
7-
8-
9-
def div_3(num):
10-
return num % 3 == 0
11-
12-
13-
print(div_3(3))
14-
print(div_3(100))
1+
"""
2+
Divisible by 3
3+
Define a function named div_3 that returns True if its single integer parameter is divisible by 3 and False otherwise.
4+
5+
For example, div_3(6) is True because 6/3 does not leave any remainder. However div_3(5) is False because 5/3 leaves 2 as a remainder.
6+
"""
7+
8+
9+
def div_3(num):
10+
return num % 3 == 0
11+
12+
13+
print(div_3(3))
14+
print(div_3(100))

problems/double-letters.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
"""
2-
Double letters
3-
4-
The goal of this challenge is to analyze a string to check if it contains two of the same letter in a row. For example, the string "hello" has l twice in a row, while the string "nono" does not have two identical letters in a row.
5-
6-
Define a function named double_letters that takes a single parameter. The parameter is a string. Your function must return True if there are two identical letters in a row in the string, and False otherwise
7-
"""
8-
9-
10-
def double_letters(string):
11-
result = False
12-
for i in range(len(string) - 1):
13-
if string[i] == string[i + 1]:
14-
result = True
15-
return result
16-
17-
18-
print(double_letters("boom"))
19-
print(double_letters("damn!"))
20-
21-
# shorter solution
22-
# using a list comprehension, zip, and any
23-
""" def double_letters(string):
1+
"""
2+
Double letters
3+
4+
The goal of this challenge is to analyze a string to check if it contains two of the same letter in a row. For example, the string "hello" has l twice in a row, while the string "nono" does not have two identical letters in a row.
5+
6+
Define a function named double_letters that takes a single parameter. The parameter is a string. Your function must return True if there are two identical letters in a row in the string, and False otherwise
7+
"""
8+
9+
10+
def double_letters(string):
11+
result = False
12+
for i in range(len(string) - 1):
13+
if string[i] == string[i + 1]:
14+
result = True
15+
return result
16+
17+
18+
print(double_letters("boom"))
19+
print(double_letters("damn!"))
20+
21+
# shorter solution
22+
# using a list comprehension, zip, and any
23+
""" def double_letters(string):
2424
return any([a == b for a, b in zip(string, string[1:])]) """

problems/flatten-list.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
"""
2-
Flatten a list
3-
Write a function that takes a list of lists and flattens it into a one-dimensional list.
4-
5-
Name your function flatten. It should take a single parameter and return a list.
6-
7-
For example, calling:
8-
9-
flatten([[1, 2], [3, 4]])
10-
Should return the list:
11-
12-
[1, 2, 3, 4]
13-
"""
14-
15-
16-
def flatten(l):
17-
result = []
18-
for i in l:
19-
if type(i) == list:
20-
for j in i:
21-
result.append(j)
22-
else:
23-
result.append(i)
24-
return result
25-
26-
27-
print(flatten([[1, 2], [3, 4], 5, 6, 7]))
28-
print(flatten([[1, 2], [3, 4]]))
1+
"""
2+
Flatten a list
3+
Write a function that takes a list of lists and flattens it into a one-dimensional list.
4+
5+
Name your function flatten. It should take a single parameter and return a list.
6+
7+
For example, calling:
8+
9+
flatten([[1, 2], [3, 4]])
10+
Should return the list:
11+
12+
[1, 2, 3, 4]
13+
"""
14+
15+
16+
def flatten(l):
17+
result = []
18+
for i in l:
19+
if type(i) == list:
20+
for j in i:
21+
result.append(j)
22+
else:
23+
result.append(i)
24+
return result
25+
26+
27+
print(flatten([[1, 2], [3, 4], 5, 6, 7]))
28+
print(flatten([[1, 2], [3, 4]]))

0 commit comments

Comments
 (0)