-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonQ2.html
More file actions
173 lines (147 loc) · 6.33 KB
/
PythonQ2.html
File metadata and controls
173 lines (147 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Question 2 - Python</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<a href="python.html" class="back-link">← Back to Python List</a>
<h1>Question 2: Menu-driven Stack</h1>
<p>Write a menu-driven program to handle stack operations such as <b>Push</b>, <b>Pop</b>, and <b>Display</b>.
</p>
<h2>Solution</h2>
<div class="solution-actions">
<a href="https://www.online-python.com/Qft634yY8W" target="_blank" class="run-online-link">[Run Online]</a>
<button class="action-btn copy-btn" onclick="copySolution()">📋 Copy Solution</button>
<button class="action-btn download-btn" onclick="downloadSolution()">💾 Download as TXT</button>
<button class="action-btn toggle-btn" onclick="toggleComments()">💬 Hide Comments</button>
</div>
<pre><code id="solution-code">
# Question 2:
# Write a menu-driven program to perform STACK operations:
# PUSH, POP, and DISPLAY. The program should allow user to
# add elements to stack (PUSH), remove elements (POP), and
# display all current stack elements (DISPLAY).
# Program: Stack operations using Menu
STACK = [] # Creating an empty stack
# Function to add (PUSH) an element to the stack
def PUSH_element():
item = input("Enter element to PUSH: ") # Take input from user
STACK.append(item) # Add element to the end of list (top of stack)
print(item, "Pushed into Stack") # Confirm the operation
# Function to remove (POP) an element from the stack
def POP_element():
if len(STACK) == 0: # Check if stack is empty
print("Stack Underflow! Stack is empty") # Cannot pop from empty stack
else:
removed = STACK.pop() # Remove and return last element (top of stack)
print(removed, "Popped from Stack") # Confirm the operation
# Function to display all elements in the stack
def DISPLAY_stack():
if len(STACK) == 0: # Check if stack is empty
print("Stack is empty") # Nothing to display
else:
print("Stack elements are:", STACK) # Show all elements
# --- Main Menu ---
# Infinite loop to keep showing menu until user exits
while True:
print("\n--- Stack Menu ---") # Display menu header
print("1. PUSH | 2. POP | 3. DISPLAY | 4. Exit") # Show options
choice = int(input("Enter your choice (1-4): ")) # Get user choice
# Call appropriate function based on user's choice
if choice == 1:
PUSH_element() # Add element to stack
elif choice == 2:
POP_element() # Remove element from stack
elif choice == 3:
DISPLAY_stack() # Show all elements
elif choice == 4:
print("Exiting Program...") # Exit message
break # Exit the loop and end program
else:
print("Invalid choice! Please try again") # Handle invalid input
</code></pre>
</main>
<footer>
<p>CS10 Laboratory</p>
</footer>
<script>
// Copy solution to clipboard
function copySolution() {
const code = document.getElementById('solution-code').textContent;
navigator.clipboard.writeText(code).then(() => {
const btn = document.querySelector('.copy-btn');
const originalText = btn.textContent;
btn.textContent = '✅ Copied!';
setTimeout(() => {
btn.textContent = originalText;
}, 2000);
}).catch(err => {
alert('Failed to copy: ' + err);
});
}
// Download question and solution as TXT
function downloadSolution() {
// Get the question text
const questionTitle = document.querySelector('h1').textContent;
const questionDesc = document.querySelector('main > p').textContent;
// Format question as comments
const questionComment = `# Question: ${questionTitle}\n# ${questionDesc}\n\n`;
// Get the solution code
const solutionCode = document.getElementById('solution-code').textContent;
// Combine question and solution
const fullContent = questionComment + solutionCode;
// Create and trigger download
const blob = new Blob([fullContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'PythonQ2_Solution.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Toggle comments visibility
let commentsVisible = true;
let originalCode = null;
function toggleComments() {
const codeElement = document.getElementById('solution-code');
const btn = document.querySelector('.toggle-btn');
if (commentsVisible) {
// Save original code
if (originalCode === null) {
originalCode = codeElement.innerHTML;
}
// Hide comments - process line by line
const lines = codeElement.textContent.split('\n');
const processedLines = lines.map(line => {
const trimmed = line.trim();
// Check if line is a comment (starts with #)
if (trimmed.startsWith('#')) {
return '<span class="comment-line comment-hidden">' + escapeHtml(line) + '</span>';
}
return escapeHtml(line);
});
codeElement.innerHTML = processedLines.join('\n');
btn.textContent = '💬 Show Comments';
commentsVisible = false;
} else {
// Restore original code
codeElement.innerHTML = originalCode;
btn.textContent = '💬 Hide Comments';
commentsVisible = true;
}
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
</script>
<script src="script.js"></script>
</body>
</html>