|
| 1 | +level = "easy" |
| 2 | +name = "bit_cycle_shift" |
| 3 | +tags = ["string", "bits_operation"] |
| 4 | +time_to_solve_sec = 250 |
| 5 | + |
| 6 | +description_en = """ |
| 7 | +You are given a binary string `bitstring` and an integer `shift`. |
| 8 | +
|
| 9 | +Cyclically shift `bitstring` to the **left** by $|shift|$ positions. If $|shift| \\geq \\text{len}(\\text{bitstring})$, use the shift value modulo the length of the string. The sign of `shift` is ignored, so `shift = 3` and `shift = -3` produce the same result. |
| 10 | +
|
| 11 | +Return the resulting binary string after the cyclic shift. |
| 12 | +""" |
| 13 | + |
| 14 | +description_ru = """ |
| 15 | +Дана двоичная строка `bitstring` и целое число `shift`. |
| 16 | +
|
| 17 | +Необходимо циклически сдвинуть `bitstring` влево на $|shift|$ позиций. Если $|shift| \\geq \\text{len}(\\text{bitstring})$, используйте значение сдвига по модулю длины строки. Знак `shift` игнорируется, поэтому `shift = 3` и `shift = -3` дают один и тот же результат. |
| 18 | +
|
| 19 | +Верните двоичную строку после циклического сдвига. |
| 20 | +""" |
| 21 | + |
| 22 | +limits = """ |
| 23 | +- $1 \\leq \\text{len}(\\text{bitstring}) \\leq 50$ |
| 24 | +- $\\text{bitstring} \\in \\{0, 1\\}^*$ |
| 25 | +- $-1000 \\leq \\text{shift} \\leq 1000$ |
| 26 | +""" |
| 27 | + |
| 28 | +solution = """ |
| 29 | +def solution(bitstring: str, shift: int) -> str: |
| 30 | + n = len(bitstring) |
| 31 | + if n == 0 or shift == 0: |
| 32 | + return bitstring |
| 33 | + k = abs(shift) % n |
| 34 | + return bitstring[k:] + bitstring[:k] |
| 35 | +""" |
| 36 | + |
| 37 | +examples = """ |
| 38 | +solution("1011", -2) == "1110" |
| 39 | +solution("0101", -3) == "1010" |
| 40 | +solution("10101", 5) == "10101" |
| 41 | +solution("10010", -3) == "10100" |
| 42 | +solution("111101", 3) == "101111" |
| 43 | +solution("10000000", -9) == "00000001" |
| 44 | +""" |
| 45 | + |
| 46 | +[[input_signature]] |
| 47 | +argument_name = "bitstring" |
| 48 | +[input_signature.type] |
| 49 | +name = "string" |
| 50 | + |
| 51 | +[[input_signature]] |
| 52 | +argument_name = "shift" |
| 53 | +[input_signature.type] |
| 54 | +name = "integer" |
| 55 | + |
| 56 | +[output_signature.type] |
| 57 | +name = "string" |
| 58 | + |
| 59 | +[[asserts]] |
| 60 | +arguments = ["111101", 3] |
| 61 | +comment = "Basic left cyclic shift by 3" |
| 62 | +expected = "101111" |
| 63 | + |
| 64 | +[[asserts]] |
| 65 | +arguments = ["1011", -2] |
| 66 | +comment = "Negative shift, same as left shift by 2" |
| 67 | +expected = "1110" |
| 68 | + |
| 69 | +[[asserts]] |
| 70 | +arguments = ["10010", -3] |
| 71 | +comment = "Matches original task example with negative shift" |
| 72 | +expected = "10100" |
| 73 | + |
| 74 | +[[asserts]] |
| 75 | +arguments = ["10101", 5] |
| 76 | +comment = "Shift equal to length leaves string unchanged" |
| 77 | +expected = "10101" |
| 78 | + |
| 79 | +[[asserts]] |
| 80 | +arguments = ["0", 1] |
| 81 | +comment = "Single-character string, no visible change" |
| 82 | +expected = "0" |
| 83 | + |
| 84 | +[[asserts]] |
| 85 | +arguments = ["1", -3] |
| 86 | +comment = "Single-character string with large negative shift" |
| 87 | +expected = "1" |
| 88 | + |
| 89 | +[[asserts]] |
| 90 | +arguments = ["01", 1] |
| 91 | +comment = "Two characters, left shift by 1" |
| 92 | +expected = "10" |
| 93 | + |
| 94 | +[[asserts]] |
| 95 | +arguments = ["01", -1] |
| 96 | +comment = "Sign is ignored, same result as +1" |
| 97 | +expected = "10" |
| 98 | + |
| 99 | +[[asserts]] |
| 100 | +arguments = ["0101", 2] |
| 101 | +comment = "Period-2 pattern, shift by period returns same string" |
| 102 | +expected = "0101" |
| 103 | + |
| 104 | +[[asserts]] |
| 105 | +arguments = ["0101", -3] |
| 106 | +comment = "Negative shift magnitude greater than half length" |
| 107 | +expected = "1010" |
| 108 | + |
| 109 | +[[asserts]] |
| 110 | +arguments = ["000111", 1] |
| 111 | +comment = "Simple mixed zeros and ones, shift by 1" |
| 112 | +expected = "001110" |
| 113 | + |
| 114 | +[[asserts]] |
| 115 | +arguments = ["000111", -4] |
| 116 | +comment = "Negative shift with magnitude larger than half length" |
| 117 | +expected = "110001" |
| 118 | + |
| 119 | +[[asserts]] |
| 120 | +arguments = ["101000", 2] |
| 121 | +comment = "Shift by 2, trailing zeros move to middle" |
| 122 | +expected = "100010" |
| 123 | + |
| 124 | +[[asserts]] |
| 125 | +arguments = ["101000", -2] |
| 126 | +comment = "Same magnitude, negative sign ignored" |
| 127 | +expected = "100010" |
| 128 | + |
| 129 | +[[asserts]] |
| 130 | +arguments = ["11001100", 3] |
| 131 | +comment = "Length 8, shift 3" |
| 132 | +expected = "01100110" |
| 133 | + |
| 134 | +[[asserts]] |
| 135 | +arguments = ["11001100", -5] |
| 136 | +comment = "Negative shift, effectively left shift by 5 mod 8" |
| 137 | +expected = "10011001" |
| 138 | + |
| 139 | +[[asserts]] |
| 140 | +arguments = ["1010101", 7] |
| 141 | +comment = "Shift equal to length for odd-length alternating pattern" |
| 142 | +expected = "1010101" |
| 143 | + |
| 144 | +[[asserts]] |
| 145 | +arguments = ["1010101", -14] |
| 146 | +comment = "Shift magnitude multiple of length" |
| 147 | +expected = "1010101" |
| 148 | + |
| 149 | +[[asserts]] |
| 150 | +arguments = ["111000111", 4] |
| 151 | +comment = "Asymmetric pattern, non-trivial rotation" |
| 152 | +expected = "001111110" |
| 153 | + |
| 154 | +[[asserts]] |
| 155 | +arguments = ["111000111", -7] |
| 156 | +comment = "Negative shift leading to different rotation of same pattern" |
| 157 | +expected = "111110001" |
| 158 | + |
| 159 | +[[asserts]] |
| 160 | +arguments = ["10000000", 1] |
| 161 | +comment = "Single 1 at start moves to end" |
| 162 | +expected = "00000001" |
| 163 | + |
| 164 | +[[asserts]] |
| 165 | +arguments = ["10000000", -9] |
| 166 | +comment = "Large negative shift, reduced modulo length" |
| 167 | +expected = "00000001" |
| 168 | + |
| 169 | +[[asserts]] |
| 170 | +arguments = ["0101010101", 3] |
| 171 | +comment = "Long alternating pattern, shift by 3" |
| 172 | +expected = "1010101010" |
| 173 | + |
| 174 | +[[asserts]] |
| 175 | +arguments = ["0101010101", -8] |
| 176 | +comment = "Shift magnitude multiple of pattern period" |
| 177 | +expected = "0101010101" |
| 178 | + |
| 179 | +[[asserts]] |
| 180 | +arguments = ["11111111", 5] |
| 181 | +comment = "All ones, any shift yields same string" |
| 182 | +expected = "11111111" |
| 183 | + |
| 184 | +[[asserts]] |
| 185 | +arguments = ["00000000", -123] |
| 186 | +comment = "All zeros, very large negative shift" |
| 187 | +expected = "00000000" |
0 commit comments