update on git

swadhinbiswas committed 1 years ago
4beca37
1 parent a8fb03b
commit 4beca37
7 files changed
+483 -0
▼ALGROTHMS/ASSCII.py
+23-0
1
+def possible_grid_dimensions(c1, c2):
2
+ diff = abs(ord(c1) - ord(c2))
3
+ factors = []
4
+ for i in range(1, int(diff**0.5) + 1):
5
+ if diff % i == 0:
6
+ factors.append((i, diff // i))
7
+ factors.append((diff // i, i))
8
+
9
+ valid_dimensions = []
10
+ for rows, cols in factors:
11
+ # Ensure both characters can fit in the same row
12
+ if cols >= max(ord(c1) % cols, ord(c2) % cols):
13
+ valid_dimensions.append((rows, cols))
14
+
15
+ valid_dimensions.sort()
16
+ for rows, cols in valid_dimensions:
17
+ print(rows, cols)
18
+
19
+T = int(input())
20
+for _ in range(T):
21
+ c1, c2 = input().split()
22
+ possible_grid_dimensions(c1, c2)
23
+ print()
0
24
▼ALGROTHMS/CAPSLOCKON.py
+19-0
1
+def function(file:str):
2
+ lite=list(file)
3
+ listx=[]
4
+ for i in lite:
5
+ if i.isalpha():
6
+
7
+ x=ord(i)+32
8
+ listx.append(chr(x))
9
+ print(listx)
10
+
11
+
12
+
13
+text="cOdEfOrCeS"
14
+function(text)
15
+
16
+
17
+def function(str)->str:
18
+ arrx=[]
19
+ for
0
20
▼ALGROTHMS/Pess Chuzzle.py
+96-0
1
+from typing import List, Tuple
2
+
3
+class RookMover:
4
+ def __init__(self, n: int, perm: List[int]):
5
+ self.n = n
6
+ self.target = [p-1 for p in perm] # Convert to 0-based indexing
7
+ self.moves = []
8
+ self.board = [[False] * 8 for _ in range(n)]
9
+ # Place initial rooks on bottom row
10
+ for i in range(n):
11
+ self.board[i][0] = True
12
+
13
+ def is_path_clear(self, start_col: int, start_row: int, end_col: int, end_row: int) -> bool:
14
+ """Check if path is clear for horizontal then vertical movement"""
15
+ # Check horizontal path
16
+ if start_row == end_row:
17
+ min_col = min(start_col, end_col)
18
+ max_col = max(start_col, end_col)
19
+ return not any(self.board[col][start_row] for col in range(min_col+1, max_col))
20
+
21
+ # Check vertical path
22
+ min_row = min(start_row, end_row)
23
+ max_row = max(start_row, end_row)
24
+ return not any(self.board[start_col][row] for row in range(min_row+1, max_row))
25
+
26
+ def move_rook(self, start_col: int, start_row: int, end_col: int, end_row: int):
27
+ """Move rook and record the move"""
28
+ # Update board
29
+ self.board[start_col][start_row] = False
30
+ self.board[end_col][end_row] = True
31
+ # Record move (convert to 1-based indexing for output)
32
+ self.moves.append((start_col+1, start_row+1, end_col+1, end_row+1))
33
+
34
+ def solve(self) -> List[Tuple[int, int, int, int]]:
35
+ """Find optimal sequence of moves to arrange rooks according to permutation"""
36
+ rook_positions = list(range(self.n)) # Current column positions of rooks
37
+
38
+ def move_to_target(rook_idx: int, intermediate_row: int = 7):
39
+ """Move rook to its target position using an intermediate row if needed"""
40
+ curr_col = rook_positions[rook_idx]
41
+ target_col = self.target[rook_idx]
42
+
43
+ if curr_col == target_col:
44
+ return
45
+
46
+ # If direct path is clear, move directly
47
+ if self.is_path_clear(curr_col, 0, target_col, 0):
48
+ self.move_rook(curr_col, 0, target_col, 0)
49
+ rook_positions[rook_idx] = target_col
50
+ return
51
+
52
+ # Move up to intermediate row if path is clear
53
+ if self.is_path_clear(curr_col, 0, curr_col, intermediate_row):
54
+ self.move_rook(curr_col, 0, curr_col, intermediate_row)
55
+ # Move horizontally
56
+ self.move_rook(curr_col, intermediate_row, target_col, intermediate_row)
57
+ # Move down
58
+ self.move_rook(target_col, intermediate_row, target_col, 0)
59
+ rook_positions[rook_idx] = target_col
60
+ return
61
+
62
+ # Try different intermediate rows if needed
63
+ for row in range(6, 0, -1):
64
+ if (self.is_path_clear(curr_col, 0, curr_col, row) and
65
+ self.is_path_clear(curr_col, row, target_col, row) and
66
+ self.is_path_clear(target_col, row, target_col, 0)):
67
+ self.move_rook(curr_col, 0, curr_col, row)
68
+ self.move_rook(curr_col, row, target_col, row)
69
+ self.move_rook(target_col, row, target_col, 0)
70
+ rook_positions[rook_idx] = target_col
71
+ return
72
+
73
+ # Try to move each rook to its target position
74
+ for i in range(self.n):
75
+ move_to_target(i)
76
+
77
+ return self.moves
78
+
79
+def solve_test_case():
80
+ n = int(input())
81
+ perm = list(map(int, input().split()))
82
+
83
+ solver = RookMover(n, perm)
84
+ moves = solver.solve()
85
+
86
+ print(len(moves))
87
+ for move in moves:
88
+ print(*move)
89
+
90
+def main():
91
+ T = int(input())
92
+ for _ in range(T):
93
+ solve_test_case()
94
+
95
+if __name__ == "__main__":
96
+ main()
0
97
▼ALGROTHMS/binarysearch.py
+13-0
1
+def findesoluction(num1:int,num2:int):
2
+ while(num1 < num2):
3
+ mid=(num1+num2)//2
4
+ count=count+1
5
+ if (number2>mid):
6
+ number1=mid+1
7
+ else: number2=mid
8
+
9
+ return count
10
+
11
+
12
+num1,num2=input().split()
13
+
0
14
▼ALGROTHMS/pyramid.py
+104-0
1
+import math
2
+from typing import Tuple
3
+
4
+def get_volume_and_surface_area(base_side: float, height: float) -> Tuple[float, float]:
5
+ """
6
+ Calculate volume and surface area of a pyramid given base side and height.
7
+
8
+ Args:
9
+ base_side: Length of one side of the square base
10
+ height: Height of the pyramid
11
+
12
+ Returns:
13
+ Tuple of (volume, surface_area)
14
+ """
15
+ # Volume = (1/3) * base_area * height
16
+ volume = (1/3) * base_side * base_side * height
17
+
18
+ # Surface area = base_area + 4 * triangle_area
19
+ slant_height = math.sqrt(height * height + (base_side/2) * (base_side/2))
20
+ surface_area = base_side * base_side + 2 * base_side * slant_height
21
+
22
+ return volume, surface_area
23
+
24
+def find_optimal_dimensions(target_surface_area: float) -> Tuple[float, float, float]:
25
+ """
26
+ Find the base side length and height that maximize volume for given surface area.
27
+ Uses numerical optimization with binary search.
28
+
29
+ Args:
30
+ target_surface_area: Desired total surface area
31
+
32
+ Returns:
33
+ Tuple of (max_volume, optimal_base_side, optimal_height)
34
+ """
35
+ if target_surface_area <= 0:
36
+ return 0.0, 0.0, 0.0
37
+
38
+ # Start with reasonable bounds for base side
39
+ # Surface area must be at least base_side^2, so base_side ≤ √surface_area
40
+ max_base = math.sqrt(target_surface_area)
41
+ min_base = max_base / 1000 # Arbitrary small value
42
+
43
+ max_volume = 0.0
44
+ optimal_base = 0.0
45
+ optimal_height = 0.0
46
+
47
+ # Binary search for optimal base side
48
+ for _ in range(100): # Sufficient iterations for precision
49
+ third = (max_base - min_base) / 3
50
+ base1 = min_base + third
51
+ base2 = max_base - third
52
+
53
+ # For each base side, find height that gives target surface area
54
+ # Use quadratic formula to solve for height
55
+ def find_height(base: float) -> float:
56
+ a = 2 * base # coefficient of slant_height in surface area equation
57
+ b = 0
58
+ c = base * base - target_surface_area
59
+
60
+ # height = sqrt(slant_height^2 - (base/2)^2)
61
+ # Substitute and solve
62
+ quarter_base_sq = (base/2) * (base/2)
63
+
64
+ if a == 0:
65
+ return 0
66
+
67
+ slant_height = -c/a # simplified from quadratic formula as b=0
68
+ if slant_height * slant_height <= quarter_base_sq:
69
+ return 0
70
+
71
+ return math.sqrt(slant_height * slant_height - quarter_base_sq)
72
+
73
+ height1 = find_height(base1)
74
+ height2 = find_height(base2)
75
+
76
+ vol1 = (1/3) * base1 * base1 * height1
77
+ vol2 = (1/3) * base2 * base2 * height2
78
+
79
+ if vol1 > vol2:
80
+ max_base = base2
81
+ if vol1 > max_volume:
82
+ max_volume = vol1
83
+ optimal_base = base1
84
+ optimal_height = height1
85
+ else:
86
+ min_base = base1
87
+ if vol2 > max_volume:
88
+ max_volume = vol2
89
+ optimal_base = base2
90
+ optimal_height = height2
91
+
92
+ return max_volume, optimal_base, optimal_height
93
+
94
+def main():
95
+ while True:
96
+ surface_area = float(input())
97
+ if surface_area < 0:
98
+ break
99
+
100
+ max_volume, _, _ = find_optimal_dimensions(surface_area)
101
+ print(f"{max_volume:.4f}")
102
+
103
+if __name__ == "__main__":
104
+ main()
0
105
▼Sortnad&.py
+53-0
1
+import sys
2
+from typing import List, Tuple
3
+
4
+def get_optimal_swaps(n: int, arr: List[int]) -> Tuple[int, List[Tuple[int, int]]]:
5
+ # Create position array to quickly find where each number is
6
+ pos = [0] * (n + 1)
7
+ for i in range(n):
8
+ pos[arr[i]] = i + 1 # 1-based indexing
9
+
10
+ operations = []
11
+ total_cost = 0
12
+
13
+ # For each position, put the correct number there
14
+ for i in range(1, n + 1):
15
+ if pos[i] != i:
16
+ # Find where the current number i is
17
+ curr_pos = pos[i]
18
+
19
+ # Calculate cost for this swap
20
+ cost = i & curr_pos
21
+ total_cost += cost
22
+
23
+ # Update positions after swap
24
+ num_at_i = arr[i - 1]
25
+ arr[curr_pos - 1], arr[i - 1] = arr[i - 1], arr[curr_pos - 1]
26
+ pos[i] = i
27
+ pos[num_at_i] = curr_pos
28
+
29
+ # Record the swap operation
30
+ operations.append((i, curr_pos))
31
+
32
+ return total_cost, operations
33
+
34
+def main():
35
+ # For faster input
36
+ input = sys.stdin.readline
37
+
38
+ t = int(input())
39
+ for _ in range(t):
40
+ n = int(input())
41
+ arr = list(map(int, input().split()))
42
+
43
+ # Get minimum cost and operations
44
+ total_cost, operations = get_optimal_swaps(n, arr)
45
+
46
+ # Output results
47
+ print(total_cost)
48
+ print(len(operations))
49
+ for i, j in operations:
50
+ print(i, j)
51
+
52
+if __name__ == "__main__":
53
+ main()
0
54
▼test.c
+175-0
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+
4
+// Function to create a new node
5
+int* createNode(int key) {
6
+ int* newNode = (int*)malloc(3 * sizeof(int));
7
+ newNode[0] = key;
8
+ newNode[1] = 0; // left child
9
+ newNode[2] = 0; // right child
10
+ return newNode;
11
+}
12
+
13
+// Insertion
14
+void insert(int** root, int key) {
15
+ int* newNode = createNode(key);
16
+ if (*root == NULL) {
17
+ *root = newNode;
18
+ return;
19
+ }
20
+
21
+ int* queue = (int*)malloc(100 * 3 * sizeof(int));
22
+ int front = 0, rear = 0;
23
+ queue[rear++] = (*root)[0];
24
+ queue[rear++] = (*root)[1];
25
+ queue[rear++] = (*root)[2];
26
+
27
+ while (front < rear) {
28
+ int temp = queue[front++];
29
+ int left = queue[front++];
30
+ int right = queue[front++];
31
+
32
+ if (!left) {
33
+ queue[rear++] = temp;
34
+ queue[rear++] = (int)newNode;
35
+ queue[rear++] = 0;
36
+ break;
37
+ } else {
38
+ queue[rear++] = left;
39
+ queue[rear++] = right;
40
+ }
41
+
42
+ if (!right) {
43
+ queue[rear++] = temp;
44
+ queue[rear++] = 0;
45
+ queue[rear++] = (int)newNode;
46
+ break;
47
+ } else {
48
+ queue[rear++] = left;
49
+ queue[rear++] = right;
50
+ }
51
+ }
52
+
53
+ free(queue);
54
+}
55
+
56
+// Deletion
57
+int* deleteDeepest(int** root, int* delNode) {
58
+ int* queue = (int*)malloc(100 * 3 * sizeof(int));
59
+ int front = 0, rear = 0;
60
+ queue[rear++] = (*root)[0];
61
+ queue[rear++] = (*root)[1];
62
+ queue[rear++] = (*root)[2];
63
+
64
+ while (front < rear) {
65
+ int temp = queue[front++];
66
+ int left = queue[front++];
67
+ int right = queue[front++];
68
+
69
+ if (left == (int)delNode) {
70
+ left = 0;
71
+ free(delNode);
72
+ return *root;
73
+ } else if (right == (int)delNode) {
74
+ right = 0;
75
+ free(delNode);
76
+ return *root;
77
+ } else {
78
+ queue[rear++] = left;
79
+ queue[rear++] = right;
80
+ }
81
+ }
82
+
83
+ free(queue);
84
+ return *root;
85
+}
86
+
87
+int* delete(int** root, int key) {
88
+ if (*root == NULL)
89
+ return NULL;
90
+
91
+ if ((*root)[1] == 0 && (*root)[2] == 0) {
92
+ if ((*root)[0] == key) {
93
+ free(*root);
94
+ *root = NULL;
95
+ return NULL;
96
+ } else {
97
+ return *root;
98
+ }
99
+ }
100
+
101
+ int* keyNode = NULL;
102
+ int* queue = (int*)malloc(100 * 3 * sizeof(int));
103
+ int front = 0, rear = 0;
104
+ queue[rear++] = (*root)[0];
105
+ queue[rear++] = (*root)[1];
106
+ queue[rear++] = (*root)[2];
107
+ int* temp;
108
+ while (front < rear) {
109
+ int node = queue[front++];
110
+ int left = queue[front++];
111
+ int right = queue[front++];
112
+
113
+ if (node == key)
114
+ keyNode = (int*)node;
115
+
116
+ if (left)
117
+ queue[rear++] = left;
118
+ if (right)
119
+ queue[rear++] = right;
120
+ }
121
+
122
+ if (keyNode != NULL) {
123
+ int x = temp[0];
124
+ *root = deleteDeepest(root, (int*)temp);
125
+ *keyNode = x;
126
+ }
127
+
128
+ free(queue);
129
+ return *root;
130
+}
131
+
132
+void printLevelOrder(int* root) {
133
+ if (root == NULL)
134
+ return;
135
+
136
+ int* queue = (int*)malloc(100 * 3 * sizeof(int));
137
+ int front = 0, rear = 0;
138
+ queue[rear++] = root[0];
139
+ queue[rear++] = root[1];
140
+ queue[rear++] = root[2];
141
+
142
+ while (front < rear) {
143
+ int temp = queue[front++];
144
+ int left = queue[front++];
145
+ int right = queue[front++];
146
+ printf("%d ", temp);
147
+
148
+ if (left)
149
+ queue[rear++] = left;
150
+ if (right)
151
+ queue[rear++] = right;
152
+ }
153
+
154
+ free(queue);
155
+}
156
+
157
+int main() {
158
+ int* root = NULL;
159
+ insert(&root, 1);
160
+ insert(&root, 2);
161
+ insert(&root, 3);
162
+ insert(&root, 4);
163
+ insert(&root, 5);
164
+
165
+ printf("Level order traversal before deletion: ");
166
+ printLevelOrder(root);
167
+
168
+ // Delete a node
169
+ root = delete(&root, 3);
170
+
171
+ printf("\nLevel order traversal after deletion: ");
172
+ printLevelOrder(root);
173
+
174
+ return 0;
175
+}
0
176