:param s: The string that will be used at bwt algorithm :return: the string composed of the last char of each row of the ordered rotations and the index of the original string at ordered rotations list :raises TypeError: If the s parameter type is not str :raises ValueError: If
(s: str)
| 52 | |
| 53 | |
| 54 | def bwt_transform(s: str) -> BWTTransformDict: |
| 55 | """ |
| 56 | :param s: The string that will be used at bwt algorithm |
| 57 | :return: the string composed of the last char of each row of the ordered |
| 58 | rotations and the index of the original string at ordered rotations list |
| 59 | :raises TypeError: If the s parameter type is not str |
| 60 | :raises ValueError: If the s parameter is empty |
| 61 | Examples: |
| 62 | |
| 63 | >>> bwt_transform("^BANANA") |
| 64 | {'bwt_string': 'BNN^AAA', 'idx_original_string': 6} |
| 65 | >>> bwt_transform("a_asa_da_casa") |
| 66 | {'bwt_string': 'aaaadss_c__aa', 'idx_original_string': 3} |
| 67 | >>> bwt_transform("panamabanana") |
| 68 | {'bwt_string': 'mnpbnnaaaaaa', 'idx_original_string': 11} |
| 69 | >>> bwt_transform(4) |
| 70 | Traceback (most recent call last): |
| 71 | ... |
| 72 | TypeError: The parameter s type must be str. |
| 73 | >>> bwt_transform('') |
| 74 | Traceback (most recent call last): |
| 75 | ... |
| 76 | ValueError: The parameter s must not be empty. |
| 77 | """ |
| 78 | if not isinstance(s, str): |
| 79 | raise TypeError("The parameter s type must be str.") |
| 80 | if not s: |
| 81 | raise ValueError("The parameter s must not be empty.") |
| 82 | |
| 83 | rotations = all_rotations(s) |
| 84 | rotations.sort() # sort the list of rotations in alphabetically order |
| 85 | # make a string composed of the last char of each rotation |
| 86 | response: BWTTransformDict = { |
| 87 | "bwt_string": "".join([word[-1] for word in rotations]), |
| 88 | "idx_original_string": rotations.index(s), |
| 89 | } |
| 90 | return response |
| 91 | |
| 92 | |
| 93 | def reverse_bwt(bwt_string: str, idx_original_string: int) -> str: |
no test coverage detected