|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +mkdir -p tasks |
| 4 | + |
| 5 | +function write_task_and_test() { |
| 6 | + num=$1 |
| 7 | + name=$2 |
| 8 | + task_code=$3 |
| 9 | + test_code=$4 |
| 10 | + |
| 11 | + task_file="tasks/task_${num}_${name}.py" |
| 12 | + test_file="test_${num}_${name}.py" |
| 13 | + |
| 14 | + echo "# task_${num}_${name}.py" > "$task_file" |
| 15 | + echo -e "$task_code" >> "$task_file" |
| 16 | + |
| 17 | + echo "# test_${num}_${name}.py" > "$test_file" |
| 18 | + echo -e "$test_code" >> "$test_file" |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +write_task_and_test "26" "is_palindrome_sentence" ' |
| 23 | +def is_palindrome_sentence(s: str) -> bool: |
| 24 | + """ |
| 25 | + Vrátí True, pokud je věta palindrom (ignoruje mezery, interpunkci a velikost písmen). |
| 26 | + """ |
| 27 | +' ' |
| 28 | +import pytest |
| 29 | +from tasks.task_26_is_palindrome_sentence import is_palindrome_sentence |
| 30 | +
|
| 31 | +def test_is_palindrome_sentence(): |
| 32 | + assert is_palindrome_sentence("A man, a plan, a canal: Panama") |
| 33 | + assert is_palindrome_sentence("No lemon, no melon") |
| 34 | + assert is_palindrome_sentence("Eva, can I see bees in a cave?") |
| 35 | + assert is_palindrome_sentence("Was it a car or a cat I saw?") |
| 36 | + assert not is_palindrome_sentence("This is not a palindrome") |
| 37 | + assert not is_palindrome_sentence("And this also not") |
| 38 | +' |
| 39 | + |
| 40 | + |
| 41 | +write_task_and_test "27" "group_anagrams" ' |
| 42 | +def group_anagrams(words: list[str]) -> list[list[str]]: |
| 43 | + """ |
| 44 | + Skupiny anagramů v seznamu slov. Seznamy musí být seřazené. Anagramy v seznamech také seřazené. |
| 45 | + """ |
| 46 | +' ' |
| 47 | +import pytest |
| 48 | +from tasks.task_27_group_anagrams import group_anagrams |
| 49 | +
|
| 50 | +def test_group_anagrams(): |
| 51 | + assert group_anagrams(["bat", "cat", "tab", "act"]) == [["act", "cat"], ["bat", "tab"]] |
| 52 | + assert group_anagrams(["google", "silent", "gogole", "listen", "enlist"]) == [["enlist", "listen", "silent"], ["gogole", "google"]] |
| 53 | + assert group_anagrams([]) == [] |
| 54 | + assert group_anagrams(["abc"]) == [["abc"]] |
| 55 | + assert group_anagrams(["cab", "xyz", "abc", "zyx", "bca"]) == [["abc", "bca", "cab"], ["xyz", "zyx"]] |
| 56 | + assert group_anagrams(["rat", "tar", "art", "star", "tars", "cheese"]) == [["art", "rat", "tar"], ["cheese"], ["star", "tars"]] |
| 57 | +' |
| 58 | + |
| 59 | + |
| 60 | +write_task_and_test "28" "flatten_list" ' |
| 61 | +def flatten_list(nested: list[list[int]]) -> list[int]: |
| 62 | + """ |
| 63 | + Převede dvourozměrný seznam na jednorozměrný. |
| 64 | + """ |
| 65 | +' ' |
| 66 | +import pytest |
| 67 | +from tasks.task_28_flatten_list import flatten_list |
| 68 | +
|
| 69 | +def test_flatten_list(): |
| 70 | + assert flatten_list([[1, 2], [3, 4]]) == [1, 2, 3, 4] |
| 71 | + assert flatten_list([[], [1], [], [2, 3]]) == [1, 2, 3] |
| 72 | + assert flatten_list([]) == [] |
| 73 | + assert flatten_list([[1]]) == [1] |
| 74 | + assert flatten_list([[1, 2, 3], [], [4], [5, 6]]) == [1, 2, 3, 4, 5, 6] |
| 75 | + assert flatten_list([[0], [0, 0]]) == [0, 0, 0] |
| 76 | + assert flatten_list([[10, 20], [30], [], [40, 50, 60]]) == [10, 20, 30, 40, 50, 60] |
| 77 | + assert flatten_list([[1], [], [], [], [2]]) == [1, 2] |
| 78 | +' |
| 79 | + |
| 80 | + |
| 81 | +write_task_and_test "29" "longest_common_prefix" ' |
| 82 | +def longest_common_prefix(strings: list[str]) -> str: |
| 83 | + """ |
| 84 | + Vrátí nejdelší společný prefix v seznamu řetězců. |
| 85 | + """ |
| 86 | +' ' |
| 87 | +import pytest |
| 88 | +from tasks.task_29_longest_common_prefix import longest_common_prefix |
| 89 | +
|
| 90 | +def test_longest_common_prefix(): |
| 91 | + assert longest_common_prefix(["flower", "flow", "flight"]) == "fl" |
| 92 | + assert longest_common_prefix(["dog", "racecar", "car"]) == "" |
| 93 | + assert longest_common_prefix(["interspace", "interstellar", "interstate"]) == "inters" |
| 94 | + assert longest_common_prefix(["throne", "throne"]) == "throne" |
| 95 | + assert longest_common_prefix(["prefix", "pre", "presume", "presentation"]) == "pre" |
| 96 | + assert longest_common_prefix(["a"]) == "a" |
| 97 | + assert longest_common_prefix([]) == "" |
| 98 | + assert longest_common_prefix(["abc", "abcde", "abcdef"]) == "abc" |
| 99 | + assert longest_common_prefix(["", "abc"]) == "" |
| 100 | + assert longest_common_prefix(["aaa", "aa", "aaa", "aaaaa"]) == "aa" |
| 101 | +' |
| 102 | + |
| 103 | + |
| 104 | +write_task_and_test "30" "remove_duplicates" ' |
| 105 | +def remove_duplicates(nums: list[int]) -> list[int]: |
| 106 | + """ |
| 107 | + Odstraní duplikáty ze seznamu a zachová pořadí. |
| 108 | + """ |
| 109 | +' ' |
| 110 | +import pytest |
| 111 | +from tasks.task_30_remove_duplicates import remove_duplicates |
| 112 | +
|
| 113 | +def test_remove_duplicates(): |
| 114 | + assert remove_duplicates([1,2,2,3,1]) == [1,2,3] |
| 115 | + assert remove_duplicates([]) == [] |
| 116 | + assert remove_duplicates([1,1,1,1]) == [1] |
| 117 | + assert remove_duplicates([3,2,1,2,3]) == [3,2,1] |
| 118 | +' |
| 119 | + |
| 120 | + |
| 121 | +write_task_and_test "31" "reverse_words" ' |
| 122 | +def reverse_words(s: str) -> str: |
| 123 | + """ |
| 124 | + Otočí pořadí slov ve větě. |
| 125 | + """ |
| 126 | +' ' |
| 127 | +import pytest |
| 128 | +from tasks.task_31_reverse_words import reverse_words |
| 129 | +
|
| 130 | +def test_reverse_words(): |
| 131 | + assert reverse_words("hello world") == "world hello" |
| 132 | + assert reverse_words("one two three") == "three two one" |
| 133 | + assert reverse_words("single") == "single" |
| 134 | + assert reverse_words("a b c d") == "d c b a" |
| 135 | + assert reverse_words(" spaced words test ") == "test words spaced" |
| 136 | + assert reverse_words("") == "" |
| 137 | +' |
| 138 | + |
| 139 | + |
| 140 | +write_task_and_test "32" "decode_string" ' |
| 141 | +def decode_string(s: str) -> str: |
| 142 | + """ |
| 143 | + Dekóduje zakódovaný řetězec ve formátu k[encoded_string], |
| 144 | + např. "3[a]2[bc]" → "aaabcbc". |
| 145 | + """ |
| 146 | +' ' |
| 147 | +import pytest |
| 148 | +from tasks.task_32_decode_string import decode_string |
| 149 | +
|
| 150 | +def test_decode_string(): |
| 151 | + assert decode_string("3[a]2[bc]") == "aaabcbc" |
| 152 | + assert decode_string("2[abc]3[cd]ef") == "abcabccdcdcdef" |
| 153 | + assert decode_string("3[a2[c]]") == "accaccacc" |
| 154 | + assert decode_string("2[2[b]3[a]]") == "bbaaabbaaa" |
| 155 | + assert decode_string("10[a]") == "aaaaaaaaaa" |
| 156 | + assert decode_string("3[z]2[2[y]pq4[2[jk]e1[f]]]ef") == "zzzyypqjkjkefjkjkefjkjkefjkjkefyypqjkjkefjkjkefjkjkefjkjkefef" |
| 157 | + assert decode_string("abc3[cd]xyz") == "abccdcdcdxyz" |
| 158 | + assert decode_string("2[ab3[c]]") == "abcccabccc" |
| 159 | + assert decode_string("100[code]") == "code" * 100 |
| 160 | + assert decode_string("0[abc]") == "" |
| 161 | +' |
| 162 | + |
| 163 | + |
| 164 | +write_task_and_test "33" "valid_parentheses" ' |
| 165 | +def valid_parentheses(s: str) -> bool: |
| 166 | + """ |
| 167 | + Zjistí, zda má řetězec validní závorky. |
| 168 | + """ |
| 169 | +' ' |
| 170 | +import pytest |
| 171 | +from tasks.task_33_valid_parentheses import valid_parentheses |
| 172 | +
|
| 173 | +def test_valid_parentheses(): |
| 174 | + assert valid_parentheses("()") |
| 175 | + assert valid_parentheses("()[]{}") |
| 176 | + assert not valid_parentheses("(]") |
| 177 | + assert not valid_parentheses("([)]") |
| 178 | + assert valid_parentheses("{[]}") |
| 179 | + assert not valid_parentheses("(") |
| 180 | + assert not valid_parentheses("(()") |
| 181 | + assert not valid_parentheses(")(") |
| 182 | + assert valid_parentheses("((()))") |
| 183 | + assert valid_parentheses("{[()]}[]({})") |
| 184 | + assert not valid_parentheses("{[}]") |
| 185 | + assert valid_parentheses("") |
| 186 | +' |
| 187 | + |
| 188 | + |
| 189 | +write_task_and_test "34" "rotate_list" ' |
| 190 | +def rotate_list(lst: list[int], k: int) -> list[int]: |
| 191 | + """ |
| 192 | + Posune prvky seznamu o k pozic doprava. |
| 193 | + """ |
| 194 | +' ' |
| 195 | +import pytest |
| 196 | +from tasks.task_34_rotate_list import rotate_list |
| 197 | +
|
| 198 | +def test_rotate_list(): |
| 199 | + assert rotate_list([1,2,3,4,5], 2) == [4,5,1,2,3] |
| 200 | + assert rotate_list([1,2,3], 1) == [3,1,2] |
| 201 | + assert rotate_list([1,2,3], 0) == [1,2,3] |
| 202 | + assert rotate_list([1,2,3], 3) == [1,2,3] |
| 203 | + assert rotate_list([], 2) == [] |
| 204 | + assert rotate_list([1], 10) == [1] |
| 205 | +' |
| 206 | + |
| 207 | + |
| 208 | +write_task_and_test "35" "rotate_matrix" ' |
| 209 | +def rotate_matrix(matrix: list[list[int]]) -> list[list[int]]: |
| 210 | + """ |
| 211 | + Otočí čtvercovou matici o 90 stupňů ve směru hodinových ručiček. |
| 212 | + """ |
| 213 | +' ' |
| 214 | +import pytest |
| 215 | +from tasks.task_35_rotate_matrix import rotate_matrix |
| 216 | +
|
| 217 | +def test_rotate_matrix(): |
| 218 | + assert rotate_matrix([[1]]) == [[1]] |
| 219 | +
|
| 220 | + assert rotate_matrix([ |
| 221 | + [1, 2], |
| 222 | + [3, 4] |
| 223 | + ]) == [ |
| 224 | + [3, 1], |
| 225 | + [4, 2] |
| 226 | + ] |
| 227 | +
|
| 228 | + assert rotate_matrix([ |
| 229 | + [1, 2, 3], |
| 230 | + [4, 5, 6], |
| 231 | + [7, 8, 9] |
| 232 | + ]) == [ |
| 233 | + [7, 4, 1], |
| 234 | + [8, 5, 2], |
| 235 | + [9, 6, 3] |
| 236 | + ] |
| 237 | +
|
| 238 | + assert rotate_matrix([ |
| 239 | + [1, 2, 3, 4], |
| 240 | + [5, 6, 7, 8], |
| 241 | + [9, 10,11,12], |
| 242 | + [13,14,15,16] |
| 243 | + ]) == [ |
| 244 | + [13, 9, 5, 1], |
| 245 | + [14,10, 6, 2], |
| 246 | + [15,11, 7, 3], |
| 247 | + [16,12, 8, 4] |
| 248 | + ] |
| 249 | +
|
| 250 | + assert rotate_matrix([ |
| 251 | + [0, 1], |
| 252 | + [2, 3] |
| 253 | + ]) == [ |
| 254 | + [2, 0], |
| 255 | + [3, 1] |
| 256 | + ] |
| 257 | +
|
| 258 | +' |
0 commit comments