# your code goes here """ base58 encoding / decoding functions """ import unittest alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' base_count = len(alphabet) def encode(num): """ Returns num in a base58-encoded string """ encode = '' if (num < 0): return '' while (num >= base_count): mod = num % base_count encode = alphabet[mod] + encode num = num / base_count if (num): encode = alphabet[num] + encode return encode def decode(s): """ Decodes the base58-encoded string s into an integer """ decoded = 0 multi = 1 s = s[::-1] for char in s: decoded += multi * alphabet.index(char) multi = multi * base_count return decoded class Base58Tests(unittest.TestCase): def test_alphabet_length(self): self.assertEqual(58, len(alphabet)) def test_encode_10002343_returns_Tgmc(self): result = encode(10002343) self.assertEqual('Tgmc', result) def test_decode_Tgmc_returns_10002343(self): decoded = decode('Tgmc') self.assertEqual(10002343, decoded) def test_encode_1000_returns_if(self): result = encode(1000) self.assertEqual('if', result) def test_decode_if_returns_1000(self): decoded = decode('if') self.assertEqual(1000, decoded) def test_encode_zero_returns_empty_string(self): self.assertEqual('', encode(0)) def test_encode_negative_number_returns_empty_string(self): self.assertEqual('', encode(-100)) if __name__ == '__main__': #print encode(int("00B94BA6C51B3D8372D82FDE5DC78773D960B5A82FCDAC8181",16)) print hex(decode("Wh4bh"))
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 8080 #define BACKLOG 10 #define MAX_BUFFER 1024 void handle_request(int client_socket) { char buffer[MAX_BUFFER]; int bytes_read; // Read HTTP request from client bytes_read = read(client_socket, buffer, sizeof(buffer) - 1); if (bytes_read > 0) { buffer[bytes_read] = '\0'; // Null-terminate the string // Print request (optional for debugging) printf("Received request:\n%s\n", buffer); // Send HTTP response to client char response[] = "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "\r\n" "<html><body><h1>Hello, World!</h1></body></html>"; write(client_socket, response, sizeof(response) - 1); } close(client_socket); } int main() { int server_socket, client_socket; struct sockaddr_in server_addr, client_addr; socklen_t client_addr_len = sizeof(client_addr); // Create a socket server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket == -1) { perror("Socket creation failed"); return 1; } // Setup server address server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(PORT); // Bind socket to address if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("Binding failed"); return 1; } // Listen for incoming connections if (listen(server_socket, BACKLOG) == -1) { perror("Listen failed"); return 1; } printf("Server running on port %d...\n", PORT); // Accept and handle incoming client connections while ((client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len)) != -1) { handle_request(client_socket); } if (client_socket == -1) { perror("Accept failed"); return 1; } close(server_socket); return 0; }