system.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import socket
  2. def find_available_tcp_port() -> int:
  3. """Find an available TCP port, return -1 if none available."""
  4. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. try:
  6. sock.bind(('localhost', 0))
  7. port = sock.getsockname()[1]
  8. return port
  9. except Exception:
  10. return -1
  11. finally:
  12. sock.close()
  13. def display_number_matrix(number: int) -> str | None:
  14. if not 0 <= number <= 999:
  15. return None
  16. # Define the matrix representation for each digit
  17. digits = {
  18. '0': ['###', '# #', '# #', '# #', '###'],
  19. '1': [' #', ' #', ' #', ' #', ' #'],
  20. '2': ['###', ' #', '###', '# ', '###'],
  21. '3': ['###', ' #', '###', ' #', '###'],
  22. '4': ['# #', '# #', '###', ' #', ' #'],
  23. '5': ['###', '# ', '###', ' #', '###'],
  24. '6': ['###', '# ', '###', '# #', '###'],
  25. '7': ['###', ' #', ' #', ' #', ' #'],
  26. '8': ['###', '# #', '###', '# #', '###'],
  27. '9': ['###', '# #', '###', ' #', '###'],
  28. }
  29. # alternatively, with leading zeros: num_str = f"{number:03d}"
  30. num_str = str(number) # Convert to string without padding
  31. result = []
  32. for row in range(5):
  33. line = ' '.join(digits[digit][row] for digit in num_str)
  34. result.append(line)
  35. matrix_display = '\n'.join(result)
  36. return f'\n{matrix_display}\n'