Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@doughsay
Created January 7, 2018 02:58
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save doughsay/cb50d9e4d344230ebc166255a202f81d to your computer and use it in GitHub Desktop.
Save doughsay/cb50d9e4d344230ebc166255a202f81d to your computer and use it in GitHub Desktop.
Python List Comprehension Practice Problems
def identity(nums):
"""Identity:
Given a list of numbers, write a list comprehension that produces a copy of the list.
>>> identity([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]
>>> identity([])
[]
"""
pass
def doubled(nums):
"""Doubled:
Given a list of numbers, write a list comprehension that produces a list of each number doubled.
>>> doubled([1, 2, 3, 4, 5])
[2, 4, 6, 8, 10]
>>> doubled([-2, 2, -10, 10])
[-4, 4, -20, 20]
"""
pass
def squared(nums):
"""Squared:
Given a list of numbers, write a list comprehension that produces a list of the squares of each number.
>>> squared([1, 2, 3, 4, 5])
[1, 4, 9, 16, 25]
>>> squared([-2, 2, -10, 10])
[4, 4, 100, 100]
"""
pass
def evens(nums):
"""Evens:
Given a list of numbers, write a list comprehension that produces a list of only the even numbers in that list.
>>> evens([1, 2, 3, 4, 5])
[2, 4]
>>> evens([1, 3, 5])
[]
>>> evens([-2, -4, -7])
[-2, -4]
"""
pass
def odds(nums):
"""Odds:
Given a list of numbers, write a list comprehension that produces a list of only the odd numbers in that list.
>>> odds([1, 2, 3, 4, 5])
[1, 3, 5]
>>> odds([2, 4, 6])
[]
>>> odds([-2, -4, -7])
[-7]
"""
pass
def positives(nums):
"""Positives:
Given a list of numbers, write a list comprehension that produces a list of only the positive numbers in that list.
>>> positives([-2, -1, 0, 1, 2])
[1, 2]
"""
pass
def selective_stringify_nums(nums):
"""Selectively stringify nums:
Given a list of numbers, write a list comprehension that produces a list of strings of each number that is divisible by 5.
>>> selective_stringify_nums([25, 91, 22, -7, -20])
['25', '-20']
"""
pass
def words_not_the(sentence):
"""Words not 'the'
Given a sentence, produce a list of the lengths of each word in the sentence, but only if the word is not 'the'.
>>> words_not_the('the quick brown fox jumps over the lazy dog')
[5, 5, 3, 5, 4, 4, 3]
"""
pass
def vowels(word):
"""Vowels:
Given a string representing a word, write a list comprehension that produces a list of all the vowels in that word.
>>> vowels('mathematics')
['a', 'e', 'a', 'i']
"""
pass
def vowels_set(word):
"""Vowels set:
Given a string representing a word, write a set comprehension that produces a set of all the vowels in that word.
>>> vowels_set('mathematics')
set(['a', 'i', 'e'])
"""
pass
def disemvowel(sentence):
"""Disemvowel:
Given a sentence, return the sentence with all vowels removed.
>>> disemvowel('the quick brown fox jumps over the lazy dog')
'th qck brwn fx jmps vr th lzy dg'
"""
pass
def wiggle_numbers(nums):
"""Wiggle numbers:
Given a list of number, return the list with all even numbers doubled, and all odd numbers turned negative.
>>> wiggle_numbers([72, 26, 79, 70, 20, 68, 43, -71, 71, -2])
[144, 52, -79, 140, 40, 136, -43, 71, -71, -4]
"""
pass
def encrypt_lol(sentence):
"""Encrypt lol:
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.
>>> encrypt_lol('the quick brown fox jumps over the lazy dog')
'uif rvjdl cspxo gpy kvnqt pwfs uif mbzy eph'
"""
pass
# STOP HERE! You solved everything!
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print '\n*** ALL TESTS PASSED!\n'
@the-rahulpatel
Copy link

Thanks, the exercises were pretty easy.

@JayM1911
Copy link

Thanks for this exercise .... !!!!!

@neoxk
Copy link

neoxk commented Feb 25, 2021

Thanks!!

@BigDru
Copy link

BigDru commented Apr 16, 2021

First, I'd like to say thank you for the exercises. I had no knowledge of the isspace(), ord() and chr() functions before starting this.

I'd also like to point out that the correct output for encrypt_lol should be
'uif rvjdl cspxo gpy kvnqt pwfs uif mbzz eph'
The answer provided in the comments doesn't have the "y" from lazy shifted to "z".

It'd also be cool if you or someone else provided a separate answer sheet. I think I over complicated a few of my answers.
Thanks again for the exercises!

@the-rahulpatel
Copy link

the-rahulpatel commented Apr 17, 2021

@BigDru

I have provided the answers on my Github Gist, which can be accessed by clicking on my profile.

or

Link: https://gist.github.com/the-rahulpatel/c7c010ef7a16dd4f2b09cbe6d582ec06

Copy link

ghost commented May 19, 2021

Thanks a lot for the exercises, they are of gold value!

P.S: I would like to note a problem with the last exercise you proposed. In the statement it says "a-y" but in the provided output there is no change on the "y" element. (last character of the word "lazy").

@suchy1511
Copy link

Thanks a lot for sharing this informative exercise. Got to learn a bit

@Just-Simple-59
Copy link

Thanks for the practice problems.
I have written the programs for all the Questions to best of my beginner knowledge, hope someone get's used my this
Link: https://gist.github.com/Just-Simple-59/8e908575fe138e8fca58982df4f6c397

@suchy1511
Copy link

suchy1511 commented Jun 23, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment