added exercises for list comprehensions
This commit is contained in:
1 parent
f89435117a
commit
f3e49c96cd
5 files changed
+154
No files matched your search
Whitespace-only changes.
@@ -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)
|
||||
"""
|
||||
|
||||
|
||||
Whitespace-only changes.
@@ -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))]
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import pytest
|
||||
|
||||
# ========================| divisible_by_7 |===========================
|
||||
def test_divisible_by_7():
|
||||
from comprehensions import divisible_by_7
|
||||
assert divisible_by_7(-1) == []
|
||||
assert divisible_by_7(0) == []
|
||||
assert divisible_by_7(43) == [7, 14, 21, 28, 35, 42]
|
||||
assert divisible_by_7(325) == [7, 14, 21, 28, 35, 42, \
|
||||
49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, \
|
||||
126, 133, 140, 147, 154, 161, 168, 175, 182, \
|
||||
189, 196, 203, 210, 217, 224, 231, 238, 245, \
|
||||
252, 259, 266, 273, 280, 287, 294, 301, 308, 315, 322]
|
||||
|
||||
|
||||
# ========================| contains_3 |===========================
|
||||
def test_contains_3():
|
||||
from comprehensions import contains_3
|
||||
assert contains_3(-1) == []
|
||||
assert contains_3(0) == []
|
||||
assert contains_3(5) == [3]
|
||||
assert contains_3(24) == [3, 13, 23]
|
||||
assert contains_3(4238) == [3, 13, 23, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 53, 63, 73, 83, 93, 103, 113, 123, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 143, 153, 163, 173, 183, 193, 203, 213, 223, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 243, 253, 263, 273, 283, 293, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 403, 413, 423, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 443, 453, 463, 473, 483, 493, 503, 513, 523, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 543, 553, 563, 573, 583, 593, 603, 613, 623, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 643, 653, 663, 673, 683, 693, 703, 713, 723, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 743, 753, 763, 773, 783, 793, 803, 813, 823, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 843, 853, 863, 873, 883, 893, 903, 913, 923, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 943, 953, 963, 973, 983, 993, 1003, 1013, 1023, 1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1043, 1053, 1063, 1073, 1083, 1093, 1103, 1113, 1123, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 1143, 1153, 1163, 1173, 1183, 1193, 1203, 1213, 1223, 1230, 1231, 1232, 1233, 1234, 1235, 1236, 1237, 1238, 1239, 1243, 1253, 1263, 1273, 1283, 1293, 1300, 1301, 1302, 1303, 1304, 1305, 1306, 1307, 1308, 1309, 1310, 1311, 1312, 1313, 1314, 1315, 1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324, 1325, 1326, 1327, 1328, 1329, 1330, 1331, 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340, 1341, 1342, 1343, 1344, 1345, 1346, 1347, 1348, 1349, 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 1360, 1361, 1362, 1363, 1364, 1365, 1366, 1367, 1368, 1369, 1370, 1371, 1372, 1373, 1374, 1375, 1376, 1377, 1378, 1379, 1380, 1381, 1382, 1383, 1384, 1385, 1386, 1387, 1388, 1389, 1390, 1391, 1392, 1393, 1394, 1395, 1396, 1397, 1398, 1399, 1403, 1413, 1423, 1430, 1431, 1432, 1433, 1434, 1435, 1436, 1437, 1438, 1439, 1443, 1453, 1463, 1473, 1483, 1493, 1503, 1513, 1523, 1530, 1531, 1532, 1533, 1534, 1535, 1536, 1537, 1538, 1539, 1543, 1553, 1563, 1573, 1583, 1593, 1603, 1613, 1623, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639, 1643, 1653, 1663, 1673, 1683, 1693, 1703, 1713, 1723, 1730, 1731, 1732, 1733, 1734, 1735, 1736, 1737, 1738, 1739, 1743, 1753, 1763, 1773, 1783, 1793, 1803, 1813, 1823, 1830, 1831, 1832, 1833, 1834, 1835, 1836, 1837, 1838, 1839, 1843, 1853, 1863, 1873, 1883, 1893, 1903, 1913, 1923, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1943, 1953, 1963, 1973, 1983, 1993, 2003, 2013, 2023, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2043, 2053, 2063, 2073, 2083, 2093, 2103, 2113, 2123, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138, 2139, 2143, 2153, 2163, 2173, 2183, 2193, 2203, 2213, 2223, 2230, 2231, 2232, 2233, 2234, 2235, 2236, 2237, 2238, 2239, 2243, 2253, 2263, 2273, 2283, 2293, 2300, 2301, 2302, 2303, 2304, 2305, 2306, 2307, 2308, 2309, 2310, 2311, 2312, 2313, 2314, 2315, 2316, 2317, 2318, 2319, 2320, 2321, 2322, 2323, 2324, 2325, 2326, 2327, 2328, 2329, 2330, 2331, 2332, 2333, 2334, 2335, 2336, 2337, 2338, 2339, 2340, 2341, 2342, 2343, 2344, 2345, 2346, 2347, 2348, 2349, 2350, 2351, 2352, 2353, 2354, 2355, 2356, 2357, 2358, 2359, 2360, 2361, 2362, 2363, 2364, 2365, 2366, 2367, 2368, 2369, 2370, 2371, 2372, 2373, 2374, 2375, 2376, 2377, 2378, 2379, 2380, 2381, 2382, 2383, 2384, 2385, 2386, 2387, 2388, 2389, 2390, 2391, 2392, 2393, 2394, 2395, 2396, 2397, 2398, 2399, 2403, 2413, 2423, 2430, 2431, 2432, 2433, 2434, 2435, 2436, 2437, 2438, 2439, 2443, 2453, 2463, 2473, 2483, 2493, 2503, 2513, 2523, 2530, 2531, 2532, 2533, 2534, 2535, 2536, 2537, 2538, 2539, 2543, 2553, 2563, 2573, 2583, 2593, 2603, 2613, 2623, 2630, 2631, 2632, 2633, 2634, 2635, 2636, 2637, 2638, 2639, 2643, 2653, 2663, 2673, 2683, 2693, 2703, 2713, 2723, 2730, 2731, 2732, 2733, 2734, 2735, 2736, 2737, 2738, 2739, 2743, 2753, 2763, 2773, 2783, 2793, 2803, 2813, 2823, 2830, 2831, 2832, 2833, 2834, 2835, 2836, 2837, 2838, 2839, 2843, 2853, 2863, 2873, 2883, 2893, 2903, 2913, 2923, 2930, 2931, 2932, 2933, 2934, 2935, 2936, 2937, 2938, 2939, 2943, 2953, 2963, 2973, 2983, 2993, 3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008, 3009, 3010, 3011, 3012, 3013, 3014, 3015, 3016, 3017, 3018, 3019, 3020, 3021, 3022, 3023, 3024, 3025, 3026, 3027, 3028, 3029, 3030, 3031, 3032, 3033, 3034, 3035, 3036, 3037, 3038, 3039, 3040, 3041, 3042, 3043, 3044, 3045, 3046, 3047, 3048, 3049, 3050, 3051, 3052, 3053, 3054, 3055, 3056, 3057, 3058, 3059, 3060, 3061, 3062, 30Line truncated
|
||||
|
||||
|
||||
# ========================| count_spaces |===========================
|
||||
def test_count_spaces():
|
||||
from comprehensions import count_spaces
|
||||
assert count_spaces('') == 0
|
||||
assert count_spaces(' ') == 1
|
||||
assert count_spaces(' ') == 5
|
||||
assert count_spaces('this is a test string') == 4
|
||||
|
||||
# ========================| remove_vowels |===========================
|
||||
def test_remove_vowels():
|
||||
from comprehensions import remove_vowels
|
||||
assert remove_vowels('') == ''
|
||||
assert remove_vowels(' ') == ' '
|
||||
|
||||
string = 'this is a test string'
|
||||
assert remove_vowels(string) == ''.join(filter(lambda ch: ch.lower() not in 'aeiou', string))
|
||||
|
||||
string = 'this is a test string'.upper()
|
||||
assert remove_vowels(string) == ''.join(filter(lambda ch: ch.lower() not in 'aeiou', string))
|
||||
|
||||
# ========================| word_lengths |===========================
|
||||
def test_word_lengths():
|
||||
from comprehensions import word_lengths
|
||||
sol = lambda xs: {word: len(word) for word in xs.split(' ') if len(word) > 0}
|
||||
|
||||
# No words
|
||||
assert word_lengths('') == dict()
|
||||
assert word_lengths(' ') == dict()
|
||||
assert word_lengths(' ') == dict()
|
||||
|
||||
|
||||
string = 'this is a test sentence'
|
||||
assert word_lengths('this is a test sentence') == sol(string)
|
||||
|
||||
string = 'helloworld'
|
||||
assert word_lengths(string) == sol(string)
|
||||
|
||||
|
||||
# ========================| prime_numbers |===========================
|
||||
def test_prime_numbers():
|
||||
from comprehensions import prime_numbers
|
||||
|
||||
assert prime_numbers(-1) == []
|
||||
assert prime_numbers(-918273987) == []
|
||||
assert prime_numbers(0) == []
|
||||
assert prime_numbers(1) == []
|
||||
assert prime_numbers(3) == [2]
|
||||
assert prime_numbers(10) == [2, 3, 5, 7]
|
||||
assert prime_numbers(100) == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
|
||||
assert prime_numbers(2384) == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383]
|
||||
|
||||
|
||||
Reference in new issue
Block a user