Repository navigation
1-4. Рамазанова Дарина Наимовна. 614 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8d82445
339b23f
300a25a
ba3bd01
f82f25a
51eaf8e
be4c522
49c76a0
dc33ce7
942fe69
8af4a6e
87704b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,13 +13,16 @@ | |
| :func:`decode_node_id` and :func:`decode_sequence_id`. | ||
| """ | ||
|
|
||
| import time # noqa: F401 | ||
| import time | ||
|
|
||
| from .constants import ( # noqa: F401 | ||
| EPOCH_MS_DEFAULT, | ||
| NODE_ID_BITS, | ||
| NODE_ID_DEFAULT, | ||
| NODE_ID_MAX, | ||
| SEQUENCE_ID_BITS, | ||
| SEQUENCE_ID_MAX, | ||
| TIMESTAMP_BITS, | ||
| TIMESTAMP_MS_MAX, | ||
| ) | ||
|
|
||
|
|
@@ -35,7 +38,7 @@ def read_current_millis(epoch_ms: int) -> int: | |
| negative if ``epoch_ms`` lies in the future. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return int(time.time() * 1000) - epoch_ms | ||
|
|
||
|
|
||
| def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> int: | ||
|
|
@@ -51,7 +54,7 @@ def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> | |
| generated. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return epoch_ms + (snowflake_id >> (NODE_ID_BITS + SEQUENCE_ID_BITS)) | ||
|
|
||
|
|
||
| def decode_node_id(snowflake_id: int) -> int: | ||
|
|
@@ -65,7 +68,7 @@ def decode_node_id(snowflake_id: int) -> int: | |
| ``[0, NODE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return (snowflake_id >> SEQUENCE_ID_BITS) & NODE_ID_MAX | ||
|
|
||
|
|
||
| def decode_sequence_id(snowflake_id: int) -> int: | ||
|
|
@@ -79,7 +82,7 @@ def decode_sequence_id(snowflake_id: int) -> int: | |
| the range ``[0, SEQUENCE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return snowflake_id & SEQUENCE_ID_MAX | ||
|
|
||
|
|
||
| def generate_snowflake_id( | ||
|
|
@@ -111,4 +114,19 @@ def generate_snowflake_id( | |
| cases an explanatory message is printed to stdout first. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| if not (0 <= node_id <= NODE_ID_MAX): | ||
| print(f"node_id must be in [0, {NODE_ID_MAX}], have {node_id}") | ||
| return None | ||
|
|
||
| if not (0 <= sequence_id <= SEQUENCE_ID_MAX): | ||
| print(f"sequence_id must be in [0; {SEQUENCE_ID_MAX}], have {sequence_id}") | ||
| return None | ||
|
|
||
| vremya = read_current_millis(epoch_ms) | ||
| if vremya > TIMESTAMP_MS_MAX: | ||
| print("Timestamps overflows") | ||
| return None | ||
|
|
||
| return (vremya << (NODE_ID_BITS + SEQUENCE_ID_BITS)) | ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вычисление этого сдвига встречается в коде несколько раз. Это нарушает критерий |
||
| node_id << (SEQUENCE_ID_BITS | sequence_id) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вы ошиблись с расстановкой скобок. Правильно так: (
(vremya << (NODE_ID_BITS + SEQUENCE_ID_BITS))
| (node_id << SEQUENCE_ID_BITS )
| sequence_id
)Это вычислительная ошибка, из-за которой часть тестов не пройдена |
||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше не использовать транслитерацию для именования переменных. Используйте валидные английские слова при выборе имен переменных. Например,
timestamp_ms