Skip to content

Commit 4ff89a5

Browse files
committed
Created using Colab
1 parent 235b637 commit 4ff89a5

1 file changed

Lines changed: 273 additions & 0 deletions

File tree

maymee/Ex7_Debugging.ipynb

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "-7zf3zw6DHGh"
7+
},
8+
"source": [
9+
"## Simple Debugging\n",
10+
"\n",
11+
"Add print statements to your program.\n",
12+
"Add input() to pause the program"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 1,
18+
"metadata": {
19+
"id": "KDn4qzsODHGt",
20+
"outputId": "cdc7cdc6-7498-434a-f444-1d8dc11978cb",
21+
"colab": {
22+
"base_uri": "https://localhost:8080/"
23+
}
24+
},
25+
"outputs": [
26+
{
27+
"output_type": "stream",
28+
"name": "stdout",
29+
"text": [
30+
"Hello Hello Hello Hello Hello \n",
31+
"Hello Hello Hello Hello Hello \n",
32+
"Hello Hello Hello Hello Hello \n",
33+
"Hello Hello Hello Hello Hello \n",
34+
"Hello Hello Hello Hello Hello \n",
35+
"Hello Hello Hello Hello Hello \n"
36+
]
37+
}
38+
],
39+
"source": [
40+
"from random import randint\n",
41+
"\n",
42+
"#Step 1: generate a random number between 1 and 5\n",
43+
"rand_num = randint(1,5)\n",
44+
"\n",
45+
"# add print statment here to know the value of rand_num\n",
46+
"\n",
47+
"\n",
48+
"# Step 2: What does this code do?\n",
49+
"\n",
50+
"for i in range(6):\n",
51+
" print('Hello '*rand_num)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 2,
57+
"metadata": {
58+
"id": "0IyW01lDDHGy",
59+
"outputId": "eaccf63c-fc74-4d0e-fb4f-591111cec6c5",
60+
"colab": {
61+
"base_uri": "https://localhost:8080/"
62+
}
63+
},
64+
"outputs": [
65+
{
66+
"output_type": "stream",
67+
"name": "stdout",
68+
"text": [
69+
"Hello \n",
70+
"Hello Hello \n",
71+
"Hello Hello Hello Hello Hello \n",
72+
"Hello Hello Hello Hello Hello \n",
73+
"Hello \n",
74+
"Hello Hello Hello Hello Hello \n"
75+
]
76+
}
77+
],
78+
"source": [
79+
"from random import randint\n",
80+
"\n",
81+
"# Step 1: generate a random number between 1 and 5\n",
82+
"\n",
83+
"# Step 2: What does this code do?\n",
84+
"\n",
85+
"for i in range(6):\n",
86+
" rand_num = randint(1,5)\n",
87+
" print('Hello '*rand_num)"
88+
]
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {
93+
"id": "cBJGhKZNDHGz"
94+
},
95+
"source": [
96+
"Write a program that generates 10000 random numbers between 1 and 100 and counts how many of them are multiples of 12."
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 3,
102+
"metadata": {
103+
"id": "dGnF1QJ-DHG0",
104+
"outputId": "129d1967-f32f-4f57-b23a-16ac4f4663e8",
105+
"colab": {
106+
"base_uri": "https://localhost:8080/"
107+
}
108+
},
109+
"outputs": [
110+
{
111+
"output_type": "stream",
112+
"name": "stdout",
113+
"text": [
114+
"Number of multiples of 12: 781\n"
115+
]
116+
}
117+
],
118+
"source": [
119+
"# Step 1:\n",
120+
"from random import randint\n",
121+
"# Step 2:\n",
122+
"count = 0\n",
123+
"# Step 3:\n",
124+
"for i in range(10000):\n",
125+
" #Step 4:\n",
126+
" num = randint(1, 100)\n",
127+
" #Step 5:\n",
128+
" if num%12==0:\n",
129+
" count=count+1\n",
130+
" # Step 6:\n",
131+
"print('Number of multiples of 12:', count)\n"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {
137+
"id": "MuFdecbgDHG1"
138+
},
139+
"source": [
140+
"The following program should counts how many of the squares of the numbers from 1 to 100 end in 1. (Example: 1,81,121). Debug the code."
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 4,
146+
"metadata": {
147+
"id": "grwHNugFDHG2",
148+
"outputId": "3f6779d1-d2b4-480b-e43c-2cb5bd162652",
149+
"colab": {
150+
"base_uri": "https://localhost:8080/"
151+
}
152+
},
153+
"outputs": [
154+
{
155+
"output_type": "execute_result",
156+
"data": {
157+
"text/plain": [
158+
"1"
159+
]
160+
},
161+
"metadata": {},
162+
"execution_count": 4
163+
}
164+
],
165+
"source": [
166+
"import numpy as np\n",
167+
"def fun_square_count():\n",
168+
" num_list = np.arange(1,101)\n",
169+
" squred_num = [value**2 for value in num_list]\n",
170+
" count=0\n",
171+
" for num in squred_num:\n",
172+
" if(num//10) == 1:\n",
173+
" count +=1\n",
174+
"\n",
175+
" return count\n",
176+
"\n",
177+
"fun_square_count()"
178+
]
179+
},
180+
{
181+
"cell_type": "markdown",
182+
"metadata": {
183+
"id": "2YCRTiO7DHG3"
184+
},
185+
"source": [
186+
"Debug the following programs"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": null,
192+
"metadata": {
193+
"id": "dpGkN_r4DHG4"
194+
},
195+
"outputs": [],
196+
"source": [
197+
"def add(a, b):\n",
198+
" return a + b\n",
199+
"\n",
200+
"print(add(5, '2'))\n"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": 5,
206+
"metadata": {
207+
"id": "Dy_0tD0bDHG5"
208+
},
209+
"outputs": [],
210+
"source": [
211+
"def divide(a, b):\n",
212+
" result = a / b\n",
213+
" return result\n",
214+
"\n"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": 6,
220+
"metadata": {
221+
"id": "y32cfRQ8DHG5",
222+
"outputId": "171b150d-efc0-419b-d1b7-2f3d79163d68",
223+
"colab": {
224+
"base_uri": "https://localhost:8080/"
225+
}
226+
},
227+
"outputs": [
228+
{
229+
"output_type": "stream",
230+
"name": "stdout",
231+
"text": [
232+
"True\n"
233+
]
234+
}
235+
],
236+
"source": [
237+
"def is_prime(n):\n",
238+
" if n <= 0:\n",
239+
" return False\n",
240+
" for i in range(2, n):\n",
241+
" if n // i == 0:\n",
242+
" return False\n",
243+
" return True\n",
244+
"\n",
245+
"print(is_prime(15))\n"
246+
]
247+
}
248+
],
249+
"metadata": {
250+
"kernelspec": {
251+
"display_name": "Python 3",
252+
"language": "python",
253+
"name": "python3"
254+
},
255+
"language_info": {
256+
"codemirror_mode": {
257+
"name": "ipython",
258+
"version": 3
259+
},
260+
"file_extension": ".py",
261+
"mimetype": "text/x-python",
262+
"name": "python",
263+
"nbconvert_exporter": "python",
264+
"pygments_lexer": "ipython3",
265+
"version": "3.12.1"
266+
},
267+
"colab": {
268+
"provenance": []
269+
}
270+
},
271+
"nbformat": 4,
272+
"nbformat_minor": 0
273+
}

0 commit comments

Comments
 (0)