-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonQ1.html
More file actions
176 lines (147 loc) · 6.12 KB
/
PythonQ1.html
File metadata and controls
176 lines (147 loc) · 6.12 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
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Question 1 - 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 1: Stack from List</h1>
<p>Create a list of 10 integers. Write a program with separate user-defined functions to perform the following
operations:</p>
<ul>
<li><b>i) Push:</b> Push the items of the list into a stack which are divisible by 5.</li>
<li><b>ii) Pop & Display:</b> Pop and display the content of the stack.</li>
</ul>
<h2>Solution</h2>
<div class="solution-actions">
<a href="https://www.online-python.com/Cz87WTpisl" 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">
# Q1. Write a program to accept a list of 10 integers and implement the following:
# i) PUSH_Div5(L, STACK) - Push elements divisible by 5 from list L into STACK
# ii) DISPLAY(STACK) - Display the current content of the stack
# iii) POP(STACK) - Pop elements from the stack
# i) Push-item: Function to PUSH elements divisible by 5 into Stack
def PUSH_Div5(L, STACK):
for num in L:
if num % 5 == 0:
STACK.append(num)
if len(STACK) == 0:
print("No element divisible by 5 found in the list")
# ii) Display: Function to show the current content of the stack
def DISPLAY(STACK):
if len(STACK) == 0:
print("Stack is empty")
else:
print(STACK)
# iii) Pop: Function to POP elements from the stack
def POP(STACK):
if len(STACK) == 0:
print("Stack Underflow / No element to pop")
else:
print("Popping element:", STACK.pop())
# ---------- Main Program ----------
# Creating a list of 10 elements
L = []
print("Enter 10 integers:")
for i in range(10):
n = int(input("Enter a number: "))
L.append(n)
print("Original List:", L)
# Creating empty stack
STACK = []
# Calling functions
PUSH_Div5(L, STACK)
DISPLAY(STACK) # Showing the stack after pushing
POP(STACK) # Popping one element
DISPLAY(STACK) # Showing stack after one pop</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;
const questionItems = Array.from(document.querySelectorAll('main > ul > li'))
.map(li => li.textContent).join('\n# ');
// Format question as comments
const questionComment = `# Question: ${questionTitle}\n# ${questionDesc}\n# ${questionItems}\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 = 'PythonQ1_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>