Created
January 7, 2018 02:58
-
-
Save doughsay/cb50d9e4d344230ebc166255a202f81d to your computer and use it in GitHub Desktop.
Python List Comprehension Practice Problems
This file contains 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 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' |
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").
Thanks a lot for sharing this informative exercise. Got to learn a bit
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
Thanks for the help. It was really helpful
…On Tue, 22 Jun 2021, 11:29 pm PRASHANTH V, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/cb50d9e4d344230ebc166255a202f81d#gistcomment-3789242>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ATZMBONBCFXCE6CAOY2GHTTTUDFQDANCNFSM4OUFDWDQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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