This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def odd_numbers(n): | |
return [x for x in range(0,n+1) if x%2 != 0] | |
print(odd_numbers(5)) # Should print [1, 3, 5] | |
print(odd_numbers(10)) # Should print [1, 3, 5, 7, 9] | |
print(odd_numbers(11)) # Should print [1, 3, 5, 7, 9, 11] | |
print(odd_numbers(1)) # Should print [1] | |
print(odd_numbers(-1)) # Should print [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def skip_elements(elements): | |
# code goes here | |
element = [e for i, e in enumerate(elements) if i % 2 == 0] | |
return element | |
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g'] | |
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def skip_elements(elements): | |
# Initialize variables | |
new_list = [] | |
i = 0 | |
# Iterate through the list | |
for i in range(len(elements)): | |
# Does this element belong in the resulting list? | |
if i%2 ==0: | |
# Add this element to the resulting list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_word(sentence, n): | |
# Only proceed if n is positive | |
if n > 0: | |
words = sentence.split() | |
# Only proceed if n is not more than the number of words | |
if n <= len(words): | |
return(words[n-1]) | |
return("") | |
print(get_word("This is a lesson about lists", 4)) # Should print: lesson |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def replace_ending(sentence, old, new): | |
# Check if the old string is at the end of the sentence | |
if sentence.endswith(old): | |
# Using i as the slicing index, combine the part | |
# of the sentence up to the matched string at the | |
# end with the new string | |
i = sentence.rindex(old) | |
new_sentence = sentence[:i]+new | |
return new_sentence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nametag(first_name, last_name): | |
return("{} {[0]}.".format(first_name, last_name)) | |
print(nametag("Jane", "Smith")) | |
# Should display "Jane S." | |
print(nametag("Francesco", "Rinaldi")) | |
# Should display "Francesco R." | |
print(nametag("Jean-Luc", "Grand-Pierre")) | |
# Should display "Jean-Luc G." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convert_distance(miles): | |
km = miles * 1.6 | |
result = "{} miles equals {:.1f} km".format(miles, km) | |
return result | |
print(convert_distance(12)) # Should be: 12 miles equals 19.2 km | |
print(convert_distance(5.5)) # Should be: 5.5 miles equals 8.8 km | |
print(convert_distance(11)) # Should be: 11 miles equals 17.6 km |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_palindrome(input_string): | |
# We'll create two strings, to compare them | |
new_string = "" | |
reverse_string = "" | |
# Traverse through each letter of the input string | |
for string in input_string.lower(): | |
# Add any non-blank letters to the | |
# end of one string, and to the front | |
# of the other string. | |
if string.replace(" ",""): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def student_grade(name, grade): | |
return ("{} received {}% on the exam" .format(name, grade)) | |
print(student_grade("Reed", 80)) | |
print(student_grade("Paige", 92)) | |
print(student_grade("Jesse", 85)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def initials(phrase): | |
words = phrase.split() | |
result = "" | |
for word in words: | |
result += word[0].upper() | |
return result | |
print(initials("Universal Serial Bus")) # Should be: USB | |
print(initials("local area network")) # Should be: LAN | |
print(initials("Operating system")) # Should be: OS |