Skip to content

Commit 9f43245

Browse files
committed
day1 converted to ipython notebook
1 parent bc4b745 commit 9f43245

File tree

1 file changed

+299
-0
lines changed

1 file changed

+299
-0
lines changed

notebooks/Day_1.ipynb

+299
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Question 1\n",
8+
"\n",
9+
"### **Question:**\n",
10+
"\n",
11+
"> **_Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,\n",
12+
"> between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line._**\n",
13+
"\n",
14+
"---\n",
15+
"\n",
16+
"### Hints:\n",
17+
"\n",
18+
"> **_Consider use range(#begin, #end) method._**\n",
19+
"\n",
20+
"---\n",
21+
"\n",
22+
"**Main author's Solution: Python 2**"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": null,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"l=[]\n",
32+
"for i in range(2000, 3201):\n",
33+
" if (i%7==0) and (i%5!=0):\n",
34+
" l.append(str(i))\n",
35+
"\n",
36+
"print ','.join(l)"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"---\n",
44+
"\n",
45+
"**My Solution: Python 3**\n",
46+
"- **Using for loops**\n",
47+
" > > > > > > > 100-plus-Python-programming-exercises-extended/master"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"for i in range(2000,3201):\n",
57+
" if i%7 == 0 and i%5!=0:\n",
58+
" print(i,end=',')\n",
59+
"print(\"\\b\")"
60+
]
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"metadata": {},
65+
"source": [
66+
"- **Using generators and list comprehension**"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"print(*(i for i in range(2000, 3201) if i%7 == 0 and i%5 != 0), sep=\",\")"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"> > > > > > > 100-plus-Python-programming-exercises-extended/master\n",
83+
"\n",
84+
"# Question 2\n",
85+
"\n",
86+
"### **Question:**\n",
87+
"\n",
88+
"> **_Write a program which can compute the factorial of a given numbers.The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program: 8\n",
89+
"> Then, the output should be:40320_**\n",
90+
"\n",
91+
"---\n",
92+
"\n",
93+
"### Hints:\n",
94+
"\n",
95+
"> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
96+
"\n",
97+
"---\n",
98+
"\n",
99+
"**Main author's Solution: Python 2**"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"def fact(x):\n",
109+
" if x == 0:\n",
110+
" return 1\n",
111+
" return x * fact(x - 1)\n",
112+
"\n",
113+
"x = int(raw_input())\n",
114+
"print fact(x)"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"---\n",
122+
"\n",
123+
"**My Solution: Python 3**\n",
124+
"\n",
125+
"- **Using While Loop**\n"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"metadata": {},
132+
"outputs": [],
133+
"source": [
134+
"n = int(input()) #input() function takes input as string type\n",
135+
" #int() converts it to integer type\n",
136+
"fact = 1\n",
137+
"i = 1\n",
138+
"while i <= n:\n",
139+
" fact = fact * i;\n",
140+
" i = i + 1\n",
141+
"print(fact)"
142+
]
143+
},
144+
{
145+
"cell_type": "markdown",
146+
"metadata": {},
147+
"source": [
148+
"- **Using For Loop**\n"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": [
157+
"n = int(input()) #input() function takes input as string type\n",
158+
" #int() converts it to integer type\n",
159+
"fact = 1\n",
160+
"for i in range(1,n+1):\n",
161+
" fact = fact * i\n",
162+
"print(fact)"
163+
]
164+
},
165+
{
166+
"cell_type": "markdown",
167+
"metadata": {},
168+
"source": [
169+
"- **Using Lambda Function**\n"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {},
176+
"outputs": [],
177+
"source": [
178+
"# Solution by: harshraj22\n",
179+
"\n",
180+
"n = int(input())\n",
181+
"def short_fact(x): return 1 if x <= 1 else x*short_fact(x-1)\n",
182+
"print(short_fact(n))\n"
183+
]
184+
},
185+
{
186+
"cell_type": "markdown",
187+
"metadata": {},
188+
"source": [
189+
"---\n",
190+
"\n",
191+
"# Question 3\n",
192+
"\n",
193+
"### **Question:**\n",
194+
"\n",
195+
"> **_With a given integral number n, write a program to generate a dictionary that contains (i, i x i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.Suppose the following input is supplied to the program: 8_**\n",
196+
"\n",
197+
"> **_Then, the output should be:_**"
198+
]
199+
},
200+
{
201+
"cell_type": "markdown",
202+
"metadata": {},
203+
"source": [
204+
"{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}"
205+
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {},
210+
"source": [
211+
"---\n",
212+
"\n",
213+
"### Hints:\n",
214+
"\n",
215+
"> **_In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()_**\n",
216+
"\n",
217+
"---\n",
218+
"\n",
219+
"**Main author's Solution: Python 2**"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": null,
225+
"metadata": {},
226+
"outputs": [],
227+
"source": [
228+
"n = int(raw_input())\n",
229+
"d = dict()\n",
230+
"for i in range(1,n+1):\n",
231+
" d[i] = i * i\n",
232+
"print d"
233+
]
234+
},
235+
{
236+
"cell_type": "markdown",
237+
"metadata": {},
238+
"source": [
239+
"**My Solution: Python 3:**\n",
240+
"\n",
241+
"- **Using for loop**"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": null,
247+
"metadata": {},
248+
"outputs": [],
249+
"source": [
250+
"n = int(input())\n",
251+
"ans = {}\n",
252+
"for i in range (1,n+1):\n",
253+
" ans[i] = i * i\n",
254+
"print(ans)"
255+
]
256+
},
257+
{
258+
"cell_type": "markdown",
259+
"metadata": {},
260+
"source": [
261+
"- **Using dictionary comprehension**"
262+
]
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": null,
267+
"metadata": {},
268+
"outputs": [],
269+
"source": [
270+
"n = int(input())\n",
271+
"ans={i : i*i for i in range(1,n+1)}\n",
272+
"print(ans)"
273+
]
274+
},
275+
{
276+
"cell_type": "markdown",
277+
"metadata": {},
278+
"source": [
279+
"---\n",
280+
"\n",
281+
"## Conclusion\n",
282+
"\n",
283+
"**_These was the solved problems of day 1. The above problems are very easy for the basic syntex learners.I have shown some easy ways of coding in my solutions. Lets see how to face and attack new problems in the next day._**\n",
284+
"\n",
285+
"[**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md \"Next Day\")\n",
286+
"\n",
287+
"[**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)"
288+
]
289+
}
290+
],
291+
"metadata": {
292+
"kernelspec": {
293+
"name": "52918bd4-40ea-43d9-b560-0e0992d7c326",
294+
"display_name": "'Python Interactive'"
295+
}
296+
},
297+
"nbformat": 4,
298+
"nbformat_minor": 4
299+
}

0 commit comments

Comments
 (0)