Posts

Showing posts from June, 2024

29

 name = "YourName" swapped_case_dict = {char: char.swapcase() for char in name} print(swapped_case_dict)

28

 def sort_numbers(*, num1, num2, num3, num4, num5):     numbers = [num1, num2, num3, num4, num5]     return sorted(numbers) print(sort_numbers(num1=5, num2=3, num3=9, num4=1, num5=4))

27

 def create_server_list(*args, **kwargs):     servers = list(args)     for key, value in kwargs.items():         servers.append(f"{key}: {value}")     return servers print(create_server_list("Server1", "Server2", IP1="192.168.1.1", IP2="192.168.1.2", Port1=80, Port2=443))

26

 def matrix_multiply(A, B):     result = [[sum(a * b for a, b in zip(A_row, B_col)) for B_col in zip(*B)] for A_row in A]     return result A = [[1, 2], [3, 4]] B = [[5, 6], [7, 8]] print(matrix_multiply(A, B))

25

 import itertools for perm in itertools.permutations('ABCDE'):     print(''.join(perm))

24

 import inflect p = inflect.engine() def convert_numbers_to_words(file_path):     with open(file_path, 'r') as file:         text = file.read()     words = text.split()     result = []     for word in words:         if word.isdigit():             result.append(p.number_to_words(word))         else:             result.append(word)     return ' '.join(result) file_path = 'example.txt' print(convert_numbers_to_words(file_path))

23

 def tokenize_and_convert(file_path):     token_dict = {}     unique_id = 1     with open(file_path, 'r') as file:         lines = file.readlines()     tokenized_lines = []     for line in lines:         tokens = line.split()         converted_tokens = []         for token in tokens:             if token not in token_dict:                 token_dict[token] = unique_id                 unique_id += 1             converted_tokens.append(token_dict[token])         tokenized_lines.append(converted_tokens)     max_length = max(len(line) for line in tokenized_lines)     padded_lines = [line + [0] * (max_length - len(line)) for line in tokenized_lines]     return padded_lines...

22

 import cProfile def find_pythagorean_triples(limit):     triples = []     for a in range(1, limit):         for b in range(a, limit):             c = (a**2 + b**2) ** 0.5             if c.is_integer():                 triples.append((a, b, int(c)))     return triples cProfile.run('find_pythagorean_triples(100)')

21

 import pytest def power(x, y, expected):     assert x ** y == expected @pytest.mark.parametrize("x, y, expected", [     (2, 2, 4),     (2, 3, 8),     (1, 9, 1),     (0, 9, 0), ]) def test_power(x, y, expected):     power(x, y, expected)

Bst

 #include <stdio.h> #include <stdlib.h> // Structure definition for a BST node typedef struct Node {     int data;     struct Node *left, *right; } Node; // Function prototypes Node* createNode(int data); Node* insert(Node* root, int data); Node* deleteNode(Node* root, int data); Node* minValueNode(Node* node); Node* search(Node* root, int data); void inorderTraversal(Node* root); void menu(); int main() {     Node* root = NULL;     int choice, data;     while (1) {         menu();         printf("Enter your choice: ");         scanf("%d", &choice);         switch (choice) {             case 1:                 printf("Enter the data to insert: ");                 scanf("%d", &data);             ...

Th

 #include <stdio.h> #include <stdlib.h> typedef struct ThreadedNode {     int data;     struct ThreadedNode *left, *right;     int rightThread; // 1 if right pointer is a thread, otherwise 0 } ThreadedNode; // Function prototypes ThreadedNode* createNode(int data); ThreadedNode* insert(ThreadedNode* root, int data); ThreadedNode* leftMost(ThreadedNode* node); void inorderTraversal(ThreadedNode* root); void menu(); int main() {     ThreadedNode* root = NULL;     int choice, data;     while (1) {         menu();         printf("Enter your choice: ");         scanf("%d", &choice);         switch (choice) {             case 1:                 printf("Enter the data to insert: ");                 scanf("%d", &data); ...

Form

 #include <stdio.h> #include <stdlib.h> #define MAX 100 int tree[MAX]; // Function prototypes void insertNode(int data); void preorderTraversal(int index); void inorderTraversal(int index); void postorderTraversal(int index); void menu(); int main() {     int choice, data;     // Initialize the tree array with -1 indicating empty nodes     for (int i = 0; i < MAX; i++) {         tree[i] = -1;     }     while (1) {         menu();         printf("Enter your choice: ");         scanf("%d", &choice);         switch (choice) {             case 1:                 printf("Enter the data to insert: ");                 scanf("%d", &data);                 insertNode(data); ...