Skip to content

Commit 24e44dc

Browse files
committed
added new solution using list comprehension to Day 1
0 parents commit 24e44dc

28 files changed

+4965
-0
lines changed

.gitignore

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
57+
# Flask stuff:
58+
instance/
59+
.webassets-cache
60+
61+
# Scrapy stuff:
62+
.scrapy
63+
64+
# Sphinx documentation
65+
docs/_build/
66+
67+
# PyBuilder
68+
target/
69+
70+
# Jupyter Notebook
71+
.ipynb_checkpoints
72+
73+
# pyenv
74+
.python-version
75+
76+
# celery beat schedule file
77+
celerybeat-schedule
78+
79+
# SageMath parsed files
80+
*.sage.py
81+
82+
# dotenv
83+
.env
84+
85+
# virtualenv
86+
.venv
87+
venv/
88+
ENV/
89+
90+
# Spyder project settings
91+
.spyderproject
92+
.spyproject
93+
94+
# Rope project settings
95+
.ropeproject
96+
97+
# mkdocs documentation
98+
/site
99+
100+
# mypy
101+
.mypy_cache/

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Python-programming-exercises
2+
---------------------
3+
##Introduction
4+
5+
***The exercise text contents of this repository was collected from GitHub account of [zhiwehu](https://github.com/zhiwehu/Python-programming-exercises). I collected it to practice and solve all the listed problems with python. Even after these collected problems are all set up, I will try to add more problems in near future. If you are a very beginner with python then I hope this 100+ exercise will help you a lot to get your hands free with python.***
6+
7+
***One will find the given problems very simple and easy to understand. A beginner can try 3-5 problems a day which will take a little time to solve but definitely will learn a couple of new stuff (no matter how lazy you are :P ). And after regular practice of only a month, one can find himself solved more than 100++ problems which are obviously not a deniable achievement.***
8+
9+
***In this repository, I will be gradually updating the codebase of the given problems with my own solutions. Also, I may try to explain the code and tell my opinion about the problem if needed. Main Authors solutions are in python 2 & my solutions will be in python 3. Every problem is divided into a template format which is discussed below. There is a [discussion](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) section so don't forget to share your opinion, ideas and feel free to discuss anything wrong or mistake***
10+
11+
----------------
12+
13+
# 100+ Python challenging programming exercises
14+
15+
16+
## 1. Problem Template
17+
18+
* ***Question***
19+
* ***Hints***
20+
* ***Solution***
21+
22+
-----------------
23+
24+
## 2. Practice Status
25+
26+
* **[Day 1](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%201.md "Day 1 Status")**- ***Question 1-3***
27+
28+
* **[Day 2](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md "Day 2 Status")**- ***Question 4-9***
29+
30+
* **[Day 3](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%203.md "Day 3 Status")**- ***Question 10-13***
31+
32+
33+
* **[Day 4](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%204.md "Day 4 Status")**- ***Question 14-15***
34+
35+
36+
* **[Day 5](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%205.md "Day 5 Status")**- ***Question 16-17***
37+
38+
39+
* **[Day 6](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%206.md "Day 6 Status")**- ***Question 18-19***
40+
41+
* **[Day 7](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%207.md "Day 7 Status")**- ***Question 20-21***
42+
43+
44+
* **[Day 8](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%208.md "Day 8 Status")**- ***Question 22-25***
45+
46+
47+
* **[Day 9](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%209.md "Day 9 Status")**- ***Question 26-30***
48+
49+
50+
* **[Day 10](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_10.md "Day 10 Status")**- ***Question 31-37***
51+
52+
* **[Day 11](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_11.md "Day 11 Status")**- ***Question 38-43***
53+
54+
* **[Day 12](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_12.md "Day 12 Status")**- ***Question 44-46***
55+
56+
57+
* **[Day 13](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_13.md "Day 13 Status")**- ***Question 47-50***
58+
59+
60+
* **[Day 14](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_14.md "Day 14 Status")**- ***Question 51-53***
61+
62+
* **[Day 15](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_15.md "Day 15 Status")**- ***Question 54-59***
63+
64+
* **[Day 16](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_16.md "Day 16 Status")**- ***Question 60-64***
65+
66+
67+
* **[Day 17](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_17.md "Day 17 Status")**- ***Question 65-69***
68+
69+
70+
* **[Day 18](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_18.md "Day 18 Status")**- ***Question 70-74***
71+
72+
73+
* **[Day 19](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_19.md "Day 19 Status")**- ***Question 75-79***
74+
75+
76+
* **[Day 20](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_20.md "Day 20 Status")**- ***Question 80-84***
77+
78+
* **[Day 21](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_21.md "Day 21 Status")**- ***Question 85-89***
79+
80+
* **[Day 22](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_22.md "Day 22 Status")**- ***Question 90-94***
81+
82+
* **[Day 23](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_23.md "Day 23 Status")**- ***Question 95-99***
83+
84+
* **[Day 24](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_24.md "Day 24 Status")**- ***Question 100-103***
85+
86+
87+
----------------------
88+
### **_Sharing new questions and solutions are warmly welcome. Be a proud contributor of this repository by just making a pull request of your changes._**

Status/Day 1.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
2+
# Question 1
3+
4+
### **Question:**
5+
6+
> ***Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,
7+
between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line.***
8+
9+
--------------------------------------
10+
### Hints:
11+
> ***Consider use range(#begin, #end) method.***
12+
13+
---------------------------------------
14+
15+
**Main author's Solution: Python 2**
16+
```python
17+
l=[]
18+
for i in range(2000, 3201):
19+
if (i%7==0) and (i%5!=0):
20+
l.append(str(i))
21+
22+
print ','.join(l)
23+
```
24+
----------------------------------------
25+
26+
**My Solution: Python 3**
27+
28+
* **Using for loops**
29+
```python
30+
for i in range(2000,3201):
31+
if i%7 == 0 and i%5!=0:
32+
print(i,end=',')
33+
print("\b")
34+
```
35+
-------------------------------
36+
37+
* **Using generators and list comprehension**
38+
```python
39+
print(*(i for i in range(2000, 3201) if i%7 == 0 and i%5 != 0), sep=",")
40+
```
41+
42+
# Question 2
43+
44+
### **Question:**
45+
46+
> ***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
47+
Then, the output should be:40320***
48+
49+
--------------------
50+
### Hints:
51+
>***In case of input data being supplied to the question, it should be assumed to be a console input.***
52+
53+
---------------
54+
**Main author's Solution: Python 2**
55+
```python
56+
def fact(x):
57+
if x == 0:
58+
return 1
59+
return x * fact(x - 1)
60+
61+
x = int(raw_input())
62+
print fact(x)
63+
```
64+
------------
65+
**My Solution: Python 3**
66+
67+
* **Using While Loop**
68+
```python
69+
n = int(input()) #input() function takes input as string type
70+
#int() converts it to integer type
71+
fact = 1
72+
i = 1
73+
while i <= n:
74+
fact = fact * i;
75+
i = i + 1
76+
print(fact)
77+
```
78+
* **Using For Loop**
79+
```python
80+
n = int(input()) #input() function takes input as string type
81+
#int() converts it to integer type
82+
fact = 1
83+
for i in range(1,n+1):
84+
fact = fact * i
85+
print(fact)
86+
```
87+
* **Using Lambda Function**
88+
```python
89+
# Solution by: harshraj22
90+
91+
n = int(input())
92+
def shortFact(x): return 1 if x <= 1 else x*shortFact(x-1)
93+
print(shortFact(n))
94+
95+
```
96+
-------------------
97+
98+
# Question 3
99+
100+
### **Question:**
101+
102+
>***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***
103+
104+
>***Then, the output should be:***
105+
```
106+
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
107+
```
108+
------------------
109+
110+
### Hints:
111+
>***In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()***
112+
113+
-----------------
114+
115+
**Main author's Solution: Python 2**
116+
```python
117+
n = int(raw_input())
118+
d = dict()
119+
for i in range(1,n+1):
120+
d[i] = i * i
121+
print d
122+
```
123+
124+
**My Solution: Python 3:**
125+
```python
126+
n = int(input())
127+
ans = {}
128+
for i in range (1,n+1):
129+
ans[i] = i * i
130+
print(ans)
131+
```
132+
**OR**
133+
```python
134+
# This is done with dictionary comprehension method
135+
n = int(input())
136+
ans={i : i*i for i in range(1,n+1)}
137+
print(ans)
138+
```
139+
----------------------------------
140+
141+
## Conclusion
142+
***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.***
143+
144+
[***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md "Next Day")
145+
146+
147+
148+
[***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)

0 commit comments

Comments
 (0)