added exercises for list comprehensions
This commit is contained in:
0
list_comprehensions/README.md
Normal file
0
list_comprehensions/README.md
Normal file
36
list_comprehensions/comprehensions.py
Normal file
36
list_comprehensions/comprehensions.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
def divisible_by_7(n: int) -> list[int]:
|
||||||
|
"""
|
||||||
|
Returns a list of all numbers till 'n' that are divisible by '7'
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def contains_3(n: int) -> list[int]:
|
||||||
|
"""
|
||||||
|
Returns a list of all numbers that contain the digit '3' in them
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def count_spaces(string: str) -> int:
|
||||||
|
"""
|
||||||
|
Count the spaces in a string
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def remove_vowels(string: str) -> str:
|
||||||
|
"""
|
||||||
|
Remove all vowels from the string
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def word_lengths(string: str) -> dict[str, int]:
|
||||||
|
"""
|
||||||
|
Create a dictionary of all words with their lengths
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def prime_numbers(n: int) -> list[int]:
|
||||||
|
"""
|
||||||
|
Returns a list of all prime numbers till 'n' (HARD)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
0
list_comprehensions/solution/README.md
Normal file
0
list_comprehensions/solution/README.md
Normal file
41
list_comprehensions/solution/comprehensions.py
Normal file
41
list_comprehensions/solution/comprehensions.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
def divisible_by_7(n: int) -> list[int]:
|
||||||
|
"""
|
||||||
|
Returns a list of all numbers till 'n' that are divisible by '7'
|
||||||
|
"""
|
||||||
|
return [x for x in range(1, n) if x % 7 == 0]
|
||||||
|
|
||||||
|
|
||||||
|
def contains_3(n: int) -> list[int]:
|
||||||
|
"""
|
||||||
|
Returns a list of all numbers that contain the digit '3' in them
|
||||||
|
"""
|
||||||
|
return [x for x in range(1, n) if '3' in str(x)]
|
||||||
|
|
||||||
|
|
||||||
|
def count_spaces(string: str) -> int:
|
||||||
|
"""
|
||||||
|
Count the spaces in a string
|
||||||
|
"""
|
||||||
|
return len([ch for ch in string if ' ' == ch])
|
||||||
|
|
||||||
|
|
||||||
|
def remove_vowels(string: str) -> str:
|
||||||
|
"""
|
||||||
|
Remove all vowels from the string
|
||||||
|
"""
|
||||||
|
return ''.join([ch for ch in string if ch.lower() not in 'aeiou'])
|
||||||
|
|
||||||
|
|
||||||
|
def word_lengths(string: str) -> dict[str, int]:
|
||||||
|
"""
|
||||||
|
Create a dictionary of all words with their lengths
|
||||||
|
"""
|
||||||
|
return {word: len(word) for word in string.split(' ') if len(word) > 0}
|
||||||
|
|
||||||
|
|
||||||
|
def prime_numbers(n: int) -> list[int]:
|
||||||
|
"""
|
||||||
|
Returns a list of all prime numbers till 'n' (HARD)
|
||||||
|
"""
|
||||||
|
return [x for x in range(2, n) if all(x % y != 0 for y in range(2, x))]
|
||||||
|
|
77
list_comprehensions/test.py
Normal file
77
list_comprehensions/test.py
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user