Skip to content

Instantly share code, notes, and snippets.

@Just-Simple-59
Last active June 23, 2021 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Just-Simple-59/8e908575fe138e8fca58982df4f6c397 to your computer and use it in GitHub Desktop.
Save Just-Simple-59/8e908575fe138e8fca58982df4f6c397 to your computer and use it in GitHub Desktop.
List comprehension practice programs. (14 Question)
# Question 1: Given a list of numbers, write a list comprehension that produces a copy of the list in reverse.
# [1, 2, 3, 4, 5]
a = [1, 2, 3, 4, 5]
b = a[::-1]
print("Solution for Question 1 \n Que: " + str(a) + "\n Ans: " + str(b))
# Question 2: Given a list of numbers, write a list comprehension that produces a list of each number doubled.
# [2, 4, 6, 8, 10]
a1 = [2, 4, 6, 8, 10]
b1 = []
for element in a1:
b1.append(element * 2)
print("Solution for Question 2 \n Que: " + str(a1) + "\n Ans: " + str(b1))
# Question 3: Given a list of numbers, write a list comprehension that produces a list of the squares of
# each number.
# [1, 3, 5, 7, 9]
a2 = [1, 3, 5, 7, 9]
b2 = []
for element in a2:
b2.append(element * element)
print("Solution for Question 3 \n Que: " + str(a2) + "\n Ans: " + str(b2))
# Question 4: Given a list of numbers, write a list comprehension that produces a list of only
# the even numbers in that list.
# [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a3 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
b3 = []
for element in a3:
if element % 2 == 0:
b3.append(element)
print("Solution for Question 4 \n Que: " + str(a3) + "\n Ans: " + str(b3))
# Question 5: Given a list of numbers, write a list comprehension that produces a list of only
# the odd numbers in that list.
# [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a4 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
b4 = []
for element in a4:
if element % 2 != 0:
b4.append(element)
print("Solution for Question 5 \n Que: " + str(a4) + "\n Ans: " + str(b4))
# Question 6: Given a list of numbers, write a list comprehension that produces a list of only
# the positive numbers in that list.
# [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
a5 = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
b5 = []
for element in a5:
if float(element) >= 0:
b5.append(element)
print("Solution for Question 6 \n Que: " + str(a5) + "\n Ans: " + str(b5))
# Question 7: Given a list of numbers, write a list comprehension that produces a list of only
# the negative numbers in that list.
# [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
a6 = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
b6 = []
for element in a6:
if float(element) < 0:
b6.append(element)
print("Solution for Question 7 \n Que: " + str(a6) + "\n Ans: " + str(b6))
# Question 8: Given a list of numbers, write a list comprehension that produces a list of strings of
# each number that is divisible by 5.
# [25, 91, 22, -7, -20]
a7 = [25, 91, 22, -7, -20]
b7 = []
for element in a7:
if float(element) % 5 == 0:
b7.append(element)
print("Solution for Question 8 \n Que: " + str(a7) + "\n Ans: " + str(b7))
# Question 9: Given a sentence, produce a list of the lengths of each word in the sentence,
# but only if the word is not 'the'.
# ('the quick brown fox jumps over the lazy dog')
a8 = 'the quick brown fox jumps over the lazy dog'
b8 = a8.split()
c8 = []
for element in b8:
if element != "the":
c8.append(len(element))
print("Solution for Question 9 \n Que: " + str(a8) + "\n Ans: " + str(c8))
# Question 10: Given a string representing a word, write a list comprehension that produces a
# list of all the vowels in that word. ('mathematics')
a9 = 'mathematics'
b9 = list(a9)
vowels = 'aeiou'
c9 = []
for element in b9:
if element in vowels:
c9.append(element)
print("Solution for Question 10 \n Que: " + str(a9) + "\n Ans: " + str(c9))
# Question 11: Given a string representing a word, write a set comprehension that produces a
# set of all the vowels in that word. ('mathematics')
a10 = 'mathematics'
vowels = 'aeiou'
c10 = []
for element in list(a10):
if element in vowels:
c10.append(element)
d10 = []
for element in c10:
if element not in d10:
d10.append(element)
print("Solution for Question 11 \n Que: " + str(a10) + "\n Ans: " + str(d10))
# Question 12: Given a sentence, return the sentence with all vowels removed.
# ('the quick brown fox jumps over the lazy dog')
a11 = 'the quick brown fox jumps over the lazy dog'
vowels = ['a', 'e', 'i', 'o', 'u']
b11 = ''
for element in a11:
if element not in vowels:
b11 = b11 + element
print("Solution for Question 12 \n Que: " + str(a11) + "\n Ans: " + str(b11))
# Question 13: Given a list of number, return the list with all even numbers doubled,
# and all odd numbers turned negative.
# [72, 26, 79, 70, 20, 68, 43, -71, 71, -2]
a12 = [72, 26, 79, 70, 20, 68, 43, -71, 71, -2]
b12 = []
for element in a12:
if float(element) % 2 == 0:
b12.append(element * 2)
if float(element) % 2 != 0:
b12.append(-abs(element))
print("Solution for Question 13 \n Que: " + str(a12) + "\n Ans: " + str(b12))
# Question 14: Given a sentence, return the setence will all it's letter transposed by 1 in the
# alphabet, but only if the letter is a-y.
# ('the quick brown fox jumps over the lazy dog')
a13 = 'the quick brown fox jumps over the lazy dog'
b13 = ''
for element in a13:
if ord(element) == ord(' '):
b13 = b13 + chr(ord(element))
elif ord(element) == ord('z'):
b13 = b13 + chr(ord(element))
elif ord(element) == ord('y'):
b13 = b13 + chr(ord(element))
else:
b13 = b13 + chr(ord(element) + 1)
print("Solution for Question 14 \n Que: " + str(a13) + "\n Ans: " + str(b13))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment