├── .github
└── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── .gitignore
├── README.md
├── Status
├── Day 1.md
├── Day 2.md
├── Day 3.md
├── Day 4.md
├── Day 5.md
├── Day 6.md
├── Day 7.md
├── Day 8.md
├── Day 9.md
├── Day_10.md
├── Day_11.md
├── Day_12.md
├── Day_13.md
├── Day_14.md
├── Day_15.md
├── Day_16.md
├── Day_17.md
├── Day_18.md
├── Day_19.md
├── Day_20.md
├── Day_21.md
├── Day_22.md
├── Day_23.md
└── Day_24.md
├── _config.yml
└── notebooks
├── Day_01.ipynb
├── Day_02.ipynb
├── Day_03.ipynb
├── Day_04.ipynb
├── Day_05.ipynb
├── Day_06.ipynb
├── Day_07.ipynb
├── Day_08.ipynb
├── Day_09.ipynb
├── Day_10.ipynb
├── Day_11.ipynb
├── Day_12.ipynb
├── Day_13.ipynb
├── Day_14.ipynb
├── Day_15.ipynb
├── Day_16.ipynb
├── Day_17.ipynb
├── Day_18.ipynb
├── Day_19.ipynb
├── Day_20.ipynb
├── Day_21.ipynb
├── Day_22.ipynb
├── Day_23.ipynb
└── Day_24.ipynb
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Desktop (please complete the following information):**
27 | - OS: [e.g. iOS]
28 | - Browser [e.g. chrome, safari]
29 | - Version [e.g. 22]
30 |
31 | **Smartphone (please complete the following information):**
32 | - Device: [e.g. iPhone6]
33 | - OS: [e.g. iOS8.1]
34 | - Browser [e.g. stock browser, safari]
35 | - Version [e.g. 22]
36 |
37 | **Additional context**
38 | Add any other context about the problem here.
39 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 | copy/*
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 | .idea
28 |
29 | # PyInstaller
30 | # Usually these files are written by a python script from a template
31 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
32 | *.manifest
33 | *.spec
34 |
35 | # Installer logs
36 | pip-log.txt
37 | pip-delete-this-directory.txt
38 |
39 | # Unit test / coverage reports
40 | htmlcov/
41 | .tox/
42 | .coverage
43 | .coverage.*
44 | .cache
45 | nosetests.xml
46 | coverage.xml
47 | *.cover
48 | .hypothesis/
49 |
50 | # Translations
51 | *.mo
52 | *.pot
53 |
54 | # Django stuff:
55 | *.log
56 | local_settings.py
57 |
58 | # Flask stuff:
59 | instance/
60 | .webassets-cache
61 |
62 | # Scrapy stuff:
63 | .scrapy
64 |
65 | # Sphinx documentation
66 | docs/_build/
67 |
68 | # PyBuilder
69 | target/
70 |
71 | # Jupyter Notebook
72 | .ipynb_checkpoints
73 |
74 | # pyenv
75 | .python-version
76 |
77 | # celery beat schedule file
78 | celerybeat-schedule
79 |
80 | # SageMath parsed files
81 | *.sage.py
82 |
83 | # dotenv
84 | .env
85 |
86 | # virtualenv
87 | .venv
88 | venv/
89 | ENV/
90 |
91 | # Spyder project settings
92 | .spyderproject
93 | .spyproject
94 |
95 | # Rope project settings
96 | .ropeproject
97 |
98 | # mkdocs documentation
99 | /site
100 |
101 | # mypy
102 | .mypy_cache/
103 |
104 | clean_files.py
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Break The Ice With Python
2 |
3 | ### A journey of 100+ simple yet interesting problems which are explained, solved, discussed in different pythonic ways
4 |
5 | [](https://mybinder.org/v2/gh/darkprinx/100-plus-Python-programming-exercises-extended/master?filepath=notebooks%2F)
6 | [](https://deepnote.com/launch?url=https%3A%2F%2Fgithub.com%2Fdarkprinx%2F100-plus-Python-programming-exercises-extended%2Fblob%2Fmaster%2Fnotebooks%2FDay_01.ipynb)
8 |
9 | ---------------------
10 | ## Introduction
11 |
12 | ***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.***
13 |
14 | ***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.***
15 |
16 | ***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***
17 |
18 | ***A Big Thanks to [apurvmishra99](https://github.com/apurvmishra99) for contributing the repository by cleaning up the formatting of all Days_.md files. fixing some random errors, fixing some variable naming with PEP8 conventions, and adding a whole new folder of [jupyter](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/tree/master/notebooks) notebook of all 24 days.***
19 |
20 | ----------------
21 |
22 | # 100+ Python challenging programming exercises
23 |
24 |
25 | ## 1. Problem Template
26 |
27 | * ***Question***
28 | * ***Hints***
29 | * ***Solution***
30 |
31 | -----------------
32 |
33 | ## 2. Practice Status
34 |
35 | * **[Day 1](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%201.md "Day 1 Status")**- ***Question 1-3***
36 |
37 | * **[Day 2](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md "Day 2 Status")**- ***Question 4-9***
38 |
39 | * **[Day 3](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%203.md "Day 3 Status")**- ***Question 10-13***
40 |
41 |
42 | * **[Day 4](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%204.md "Day 4 Status")**- ***Question 14-15***
43 |
44 |
45 | * **[Day 5](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%205.md "Day 5 Status")**- ***Question 16-17***
46 |
47 |
48 | * **[Day 6](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%206.md "Day 6 Status")**- ***Question 18-19***
49 |
50 | * **[Day 7](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%207.md "Day 7 Status")**- ***Question 20-21***
51 |
52 |
53 | * **[Day 8](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%208.md "Day 8 Status")**- ***Question 22-25***
54 |
55 |
56 | * **[Day 9](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%209.md "Day 9 Status")**- ***Question 26-30***
57 |
58 |
59 | * **[Day 10](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_10.md "Day 10 Status")**- ***Question 31-37***
60 |
61 | * **[Day 11](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_11.md "Day 11 Status")**- ***Question 38-43***
62 |
63 | * **[Day 12](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_12.md "Day 12 Status")**- ***Question 44-46***
64 |
65 |
66 | * **[Day 13](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_13.md "Day 13 Status")**- ***Question 47-50***
67 |
68 |
69 | * **[Day 14](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_14.md "Day 14 Status")**- ***Question 51-53***
70 |
71 | * **[Day 15](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_15.md "Day 15 Status")**- ***Question 54-59***
72 |
73 | * **[Day 16](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_16.md "Day 16 Status")**- ***Question 60-64***
74 |
75 |
76 | * **[Day 17](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_17.md "Day 17 Status")**- ***Question 65-69***
77 |
78 |
79 | * **[Day 18](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_18.md "Day 18 Status")**- ***Question 70-74***
80 |
81 |
82 | * **[Day 19](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_19.md "Day 19 Status")**- ***Question 75-79***
83 |
84 |
85 | * **[Day 20](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_20.md "Day 20 Status")**- ***Question 80-84***
86 |
87 | * **[Day 21](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_21.md "Day 21 Status")**- ***Question 85-89***
88 |
89 | * **[Day 22](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_22.md "Day 22 Status")**- ***Question 90-94***
90 |
91 | * **[Day 23](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_23.md "Day 23 Status")**- ***Question 95-99***
92 |
93 | * **[Day 24](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_24.md "Day 24 Status")**- ***Question 100-103***
94 |
95 |
96 |
97 | ---
98 |
99 | ## 🌱 Empower dedication with your generosity
100 | #### Every single coffee boosts towards greater motivation, turning compassion into action. Show your kind support with just a little click! 😃
101 |
102 |
103 |
104 |
--------------------------------------------------------------------------------
/Status/Day 1.md:
--------------------------------------------------------------------------------
1 | # Question 1
2 |
3 | ### **Question:**
4 |
5 | > **_Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,
6 | > between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line._**
7 |
8 | ---
9 |
10 | ### Hints:
11 |
12 | > **_Consider use range(#begin, #end) method._**
13 |
14 | ---
15 |
16 | **Main author's Solution: Python 2**
17 |
18 | ```python
19 | l=[]
20 | for i in range(2000, 3201):
21 | if (i%7==0) and (i%5!=0):
22 | l.append(str(i))
23 |
24 | print ','.join(l)
25 | ```
26 |
27 | ---
28 |
29 | **My Solution: Python 3**
30 | - **Using for loops**
31 |
32 | ```python
33 | for i in range(2000,3201):
34 | if i%7 == 0 and i%5!=0:
35 | print(i,end=',')
36 | print("\b")
37 | ```
38 |
39 | ---
40 | - **Using generators and list comprehension**
41 |
42 | ```python
43 | print(*(i for i in range(2000, 3201) if i%7 == 0 and i%5 != 0), sep=",")
44 | ```
45 | # Question 2
46 |
47 | ### **Question:**
48 |
49 | > **_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
50 | > Then, the output should be:40320_**
51 |
52 | ---
53 |
54 | ### Hints:
55 |
56 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
57 |
58 | ---
59 |
60 | **Main author's Solution: Python 2**
61 |
62 | ```python
63 | def fact(x):
64 | if x == 0:
65 | return 1
66 | return x * fact(x - 1)
67 |
68 | x = int(raw_input())
69 | print fact(x)
70 | ```
71 |
72 | ---
73 |
74 | **My Solution: Python 3**
75 |
76 | - **Using While Loop**
77 | ```python
78 | n = int(input()) #input() function takes input as string type
79 | #int() converts it to integer type
80 | fact = 1
81 | i = 1
82 | while i <= n:
83 | fact = fact * i;
84 | i = i + 1
85 | print(fact)
86 | ```
87 | - **Using For Loop**
88 | ```python
89 | n = int(input()) #input() function takes input as string type
90 | #int() converts it to integer type
91 | fact = 1
92 | for i in range(1,n+1):
93 | fact = fact * i
94 | print(fact)
95 | ```
96 | - **Using Lambda Function**
97 |
98 | ```python
99 | # Solution by: harshraj22
100 |
101 | n = int(input())
102 | def shortFact(x): return 1 if x <= 1 else x*shortFact(x-1)
103 | print(shortFact(n))
104 |
105 | ```
106 | ---
107 | ```python
108 | '''Solution by: minnielahoti
109 | '''
110 |
111 | while True:
112 | try:
113 | num = int(input("Enter a number: "))
114 | break
115 | except ValueError as err:
116 | print(err)
117 |
118 | org = num
119 | fact = 1
120 | while num:
121 | fact = num * fact
122 | num = num - 1
123 | print(f'the factorial of {org} is {fact}')
124 | ```
125 | ---
126 | ```python
127 | '''Soltuion by: KruthikaSR
128 | '''
129 | from functools import reduce
130 |
131 | def fun(acc, item):
132 | return acc*item
133 |
134 | num = int(input())
135 | print(reduce(fun,range(1, num+1), 1))
136 | ```
137 | ---
138 |
139 | # Question 3
140 |
141 | ### **Question:**
142 |
143 | > **_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_**
144 |
145 | > **_Then, the output should be:_**
146 |
147 | ```
148 | {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
149 | ```
150 |
151 | ---
152 |
153 | ### Hints:
154 |
155 | > **_In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()_**
156 |
157 | ---
158 |
159 | **Main author's Solution: Python 2**
160 |
161 | ```python
162 | n = int(raw_input())
163 | d = dict()
164 | for i in range(1,n+1):
165 | d[i] = i * i
166 | print d
167 | ```
168 |
169 | **My Solution: Python 3:**
170 |
171 | - **Using for loop**
172 |
173 | ```python
174 | n = int(input())
175 | ans = {}
176 | for i in range (1,n+1):
177 | ans[i] = i * i
178 | print(ans)
179 | ```
180 |
181 | - **Using dictionary comprehension**
182 |
183 | ```python
184 | n = int(input())
185 | ans={i : i*i for i in range(1,n+1)}
186 | print(ans)
187 | ```
188 | ---
189 | ```python
190 | '''Solution by: minnielahoti
191 | Corrected by: TheNobleKnight
192 | '''
193 |
194 | try:
195 | num = int(input("Enter a number: "))
196 | except ValueError as err:
197 | print(err)
198 |
199 | dictio = dict()
200 | for item in range(num+1):
201 | if item == 0:
202 | continue
203 | else:
204 | dictio[item] = item * item
205 | print(dictio)
206 | ```
207 | ---
208 | ```python
209 | '''Solution by: yurbika
210 | Corrected by: developer-47
211 | '''
212 |
213 | num = int(input("Number: "))
214 | print(dict(enumerate([i*i for i in range(1, num+1)], 1)))
215 | ```
216 | ---
217 | ## Conclusion
218 |
219 | **_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._**
220 |
221 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md "Next Day")
222 |
223 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
224 |
--------------------------------------------------------------------------------
/Status/Day 4.md:
--------------------------------------------------------------------------------
1 | # Question 14
2 |
3 | ### **Question:**
4 |
5 | > **_Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters._**
6 |
7 | > **_Suppose the following input is supplied to the program:_**
8 |
9 | ```
10 | Hello world!
11 | ```
12 |
13 | > **_Then, the output should be:_**
14 |
15 | ```
16 | UPPER CASE 1
17 | LOWER CASE 9
18 | ```
19 |
20 | ---
21 |
22 | ### Hints:
23 |
24 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
25 |
26 | ---
27 |
28 | **Main author's Solution: Python 2**
29 |
30 | ```python
31 | s = raw_input()
32 | d = {"UPPER CASE":0, "LOWER CASE":0}
33 | for c in s:
34 | if c.isupper():
35 | d["UPPER CASE"]+=1
36 | elif c.islower():
37 | d["LOWER CASE"]+=1
38 | else:
39 | pass
40 | print "UPPER CASE", d["UPPER CASE"]
41 | print "LOWER CASE", d["LOWER CASE"]
42 | ```
43 |
44 | ---
45 |
46 | **My Solution: Python 3**
47 |
48 | ```python
49 | word = input()
50 | upper,lower = 0,0
51 |
52 | for i in word:
53 | if 'a'<=i and i<='z' :
54 | lower+=1
55 | if 'A'<=i and i<='Z':
56 | upper+=1
57 |
58 | print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower))
59 | ```
60 |
61 | **OR**
62 |
63 | ```python
64 | word = input()
65 | upper,lower = 0,0
66 |
67 | for i in word:
68 | lower+=i.islower()
69 | upper+=i.isupper()
70 |
71 | print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower))
72 | ```
73 |
74 | **OR**
75 |
76 | ```python
77 | word = input()
78 | upper = sum(1 for i in word if i.isupper()) # sum function cumulatively sum up 1's if the condition is True
79 | lower = sum(1 for i in word if i.islower())
80 |
81 | print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower))
82 | ```
83 |
84 | **OR**
85 |
86 | ```python
87 | # solution by Amitewu
88 |
89 | string = input("Enter the sentense")
90 | upper = 0
91 | lower = 0
92 | for x in string:
93 | if x.isupper() == True:
94 | upper += 1
95 | if x.islower() == True:
96 | lower += 1
97 |
98 | print("UPPER CASE: ", upper)
99 | print("LOWER CASE: ", lower)
100 | ```
101 |
102 | ---
103 |
104 | # Question 15
105 |
106 | ### **Question:**
107 |
108 | > **_Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a._**
109 |
110 | > **_Suppose the following input is supplied to the program:_**
111 |
112 | ```
113 | 9
114 | ```
115 |
116 | > **_Then, the output should be:_**
117 |
118 | ```
119 | 11106
120 | ```
121 |
122 | ---
123 |
124 | ### Hints:
125 |
126 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
127 |
128 | ---
129 |
130 | **Main author's Solution: Python 2**
131 |
132 | ```python
133 | a = raw_input()
134 | n1 = int( "%s" % a )
135 | n2 = int( "%s%s" % (a,a) )
136 | n3 = int( "%s%s%s" % (a,a,a) )
137 | n4 = int( "%s%s%s%s" % (a,a,a,a) )
138 | print n1+n2+n3+n4
139 | ```
140 |
141 | ---
142 |
143 | **My Solution: Python 3**
144 |
145 | ```python
146 | a = input()
147 | total,tmp = 0,str() # initialing an integer and empty string
148 |
149 | for i in range(4):
150 | tmp+=a # concatenating 'a' to 'tmp'
151 | total+=int(tmp) # converting string type to integer type
152 |
153 | print(total)
154 | ```
155 |
156 | **OR**
157 |
158 | ```python
159 | a = input()
160 | total = int(a) + int(2*a) + int(3*a) + int(4*a) # N*a=Na, for example a="23", 2*a="2323",3*a="232323"
161 | print(total)
162 | ```
163 | ---
164 | ```python
165 | '''Solution by: ChichiLovesDonkeys
166 | '''
167 | from functools import reduce
168 | x = input('please enter a digit:')
169 | reduce(lambda x, y: int(x) + int(y), [x*i for i in range(1,5)])
170 | ```
171 | ---
172 | ```python
173 | '''Solution by: lcastrooliveira
174 | '''
175 | def question_15(string_digit):
176 | return sum(int(string_digit * n) for n in range(1, 5))
177 |
178 | inp = input()
179 | print(question_15(inp))
180 | ```
181 | ---
182 | ```python
183 | '''Solution by: apenam7
184 | '''
185 | a = input()
186 | print(sum(int(i*a) for i in range(1,5)))
187 | ```
188 | ---
189 |
190 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%203.md "Day 3")
191 |
192 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%205.md "Day 5")
193 |
194 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
195 |
--------------------------------------------------------------------------------
/Status/Day 5.md:
--------------------------------------------------------------------------------
1 | # Question 16
2 |
3 | ### **Question:**
4 |
5 | > **_Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers._** >**_Suppose the following input is supplied to the program:_**
6 |
7 | ```
8 | 1,2,3,4,5,6,7,8,9
9 | ```
10 |
11 | > **_Then, the output should be:_**
12 |
13 | ```
14 | 1,9,25,49,81
15 | ```
16 |
17 | ---
18 |
19 | ### Hints:
20 |
21 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
22 |
23 | ---
24 |
25 | **Main author's Solution: Python 2**
26 |
27 | ```python
28 | ## The solution by the author is incorrect.Thus it's not included here.
29 | ```
30 |
31 | ---
32 |
33 | **My Solution: Python 3**
34 |
35 | ```python
36 | lst = [str(int(i)**2) for i in input().split(',') if int(i) % 2]
37 | print(",".join(lst))
38 | ```
39 |
40 | ---
41 |
42 | ```python
43 | '''Solution by: shagun'''
44 | square odd no
45 |
46 | lst = input().split(',') # splits in comma position and set up in list
47 |
48 | seq = []
49 | lst = [int(i) for i in lst] # converts string to integer
50 | for i in lst:
51 | if i%2 != 0:
52 | i = i*i
53 | seq.append(i)
54 |
55 |
56 | seq = [str(i) for i in seq] # All the integers are converted to string to be able to apply join operation
57 | print(",".join(seq))
58 | ```
59 |
60 |
61 | ```python
62 | '''Solution by: Jack'''
63 | seq = input().split(',')
64 | lst = [int(i) for i in seq]
65 | def flt(i): #Define a filter function
66 | return i % 2 != 0
67 | result_l = [str(i * i) for i in filter(flt,lst)]
68 | print(",".join(result_l))
69 | ```
70 | ---
71 |
72 | **_There were a mistake in the the test case and the solution's whice were notified and fixed with the help of @dwedigital. My warm thanks to him._**
73 |
74 | # Question 17
75 |
76 | ### **Question:**
77 |
78 | > **_Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:_**
79 |
80 | ```
81 | D 100
82 | W 200
83 | ```
84 |
85 | - D means deposit while W means withdrawal.
86 |
87 | > **_Suppose the following input is supplied to the program:_**
88 |
89 | ```
90 | D 300
91 | D 300
92 | W 200
93 | D 100
94 | ```
95 |
96 | > **_Then, the output should be:_**
97 |
98 | ```
99 | 500
100 | ```
101 |
102 | ---
103 |
104 | ### Hints:
105 |
106 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
107 |
108 | ---
109 |
110 | **Main author's Solution: Python 2**
111 |
112 | ```python
113 | import sys
114 | netAmount = 0
115 | while True:
116 | s = raw_input()
117 | if not s:
118 | break
119 | values = s.split(" ")
120 | operation = values[0]
121 | amount = int(values[1])
122 | if operation=="D":
123 | netAmount+=amount
124 | elif operation=="W":
125 | netAmount-=amount
126 | else:
127 | pass
128 | print netAmount
129 | ```
130 |
131 | ---
132 |
133 | **My Solution: Python 3**
134 |
135 | ```python
136 | total = 0
137 | while True:
138 | s = input().split()
139 | if not s: # break if the string is empty
140 | break
141 | cm,num = map(str,s) # two inputs are distributed in cm and num in string data type
142 |
143 | if cm=='D':
144 | total+=int(num)
145 | if cm=='W':
146 | total-=int(num)
147 |
148 | print(total)
149 | ```
150 |
151 | ---
152 |
153 | ```python
154 | '''Solution by: leonedott'''
155 |
156 | lst = []
157 | while True:
158 | x = input()
159 | if len(x)==0:
160 | break
161 | lst.append(x)
162 |
163 | balance = 0
164 | for item in lst:
165 | if 'D' in item:
166 | balance += int(item.strip('D '))
167 | if 'W' in item:
168 | balance -= int(item.strip('W '))
169 | print(balance)
170 | ```
171 |
172 | ---
173 |
174 | ```python
175 | '''Solution by: AlexanderSro'''
176 |
177 | account = 0
178 | while True:
179 | action = input("Deposit/Whitdrow/Balance/Quit? D/W/B/Q: ").lower()
180 | if action == "d":
181 | deposit = input("How much would you like to deposit? ")
182 | account = account + int(deposit)
183 | elif action == "w":
184 | withdrow = input("How much would you like to withdrow? ")
185 | account = account - int(withdrow)
186 | elif action == "b":
187 | print(account)
188 | else:
189 | quit()
190 | ```
191 |
192 | ---
193 |
194 | ```python
195 | '''Solution by: ShalomPrinz
196 | '''
197 | lines = []
198 | while True:
199 | loopInput = input()
200 | if loopInput == "done":
201 | break
202 | else:
203 | lines.append(loopInput)
204 |
205 | lst = list(int(i[2:]) if i[0] == 'D' else -int(i[2:]) for i in lines)
206 | print(sum(lst))
207 | ```
208 | ---
209 | ```python
210 | '''Solution by: popomaticbubble
211 | '''
212 | transactions = []
213 |
214 | while True:
215 | text = input("> ")
216 | if text:
217 | text = text.strip('D ')
218 | text = text.replace('W ', '-')
219 | transactions.append(text)
220 | else:
221 | break
222 |
223 | transactions = (int(i) for i in transactions)
224 | balance = sum(transactions)
225 | print(f"Balance is {balance}")
226 | ```
227 | ---
228 | ```python
229 | '''Solution by: ChichiLovesDonkeys
230 | '''
231 |
232 | money = 0
233 | while 1:
234 | trans = input().split(' ')
235 | if trans[0] == 'D':
236 | money = money + int(trans[1])
237 | elif trans[0] == 'W':
238 | money = money - int(trans[1])
239 | elif input() == '':
240 | break
241 | print(f'Your current balance is: {money}')
242 | ```
243 | ---
244 |
245 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%204.md "Day 4")
246 |
247 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%206.md "Day 6")
248 |
249 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
250 |
--------------------------------------------------------------------------------
/Status/Day 6.md:
--------------------------------------------------------------------------------
1 | # Question 18
2 |
3 | ### **Question:**
4 |
5 | > **_A website requires the users to input username and password to register. Write a program to check the validity of password input by users._**
6 |
7 | > **_Following are the criteria for checking the password:_**
8 |
9 | - **_At least 1 letter between [a-z]_**
10 | - **_At least 1 number between [0-9]_**
11 | - **_At least 1 letter between [A-Z]_**
12 | - **_At least 1 character from [$#@]_**
13 | - **_Minimum length of transaction password: 6_**
14 | - **_Maximum length of transaction password: 12_**
15 |
16 | > **_Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma._**
17 |
18 | > **_Example_**
19 |
20 | > **_If the following passwords are given as input to the program:_**
21 |
22 | ```
23 | ABd1234@1,a F1#,2w3E*,2We3345
24 | ```
25 |
26 | > **_Then, the output of the program should be:_**
27 |
28 | ```
29 | ABd1234@1
30 | ```
31 |
32 | ---
33 |
34 | ### Hints:
35 |
36 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
37 |
38 | ---
39 |
40 | **Main author's Solution: Python 2**
41 |
42 | ```python
43 | import re
44 | value = []
45 | items = [x for x in raw_input().split(',')]
46 | for p in items:
47 | if len(p) < 6 or len(p) > 12:
48 | continue
49 | else:
50 | pass
51 | if not re.search("[a-z]",p):
52 | continue
53 | elif not re.search("[0-9]",p):
54 | continue
55 | elif not re.search("[A-Z]",p):
56 | continue
57 | elif not re.search("[$#@]",p):
58 | continue
59 | elif re.search("\s",p):
60 | continue
61 | else:
62 | pass
63 | value.append(p)
64 | print ",".join(value)
65 | ```
66 |
67 | ---
68 |
69 | **My Solution: Python 3**
70 |
71 | ```python
72 | def is_low(x): # Returns True if the string has a lowercase
73 | for i in x:
74 | if 'a'<=i and i<='z':
75 | return True
76 | return False
77 |
78 | def is_up(x): # Returns True if the string has a uppercase
79 | for i in x:
80 | if 'A'<= i and i<='Z':
81 | return True
82 | return False
83 |
84 | def is_num(x): # Returns True if the string has a numeric digit
85 | for i in x:
86 | if '0'<=i and i<='9':
87 | return True
88 | return False
89 |
90 | def is_other(x): # Returns True if the string has any "$#@"
91 | for i in x:
92 | if i=='$' or i=='#' or i=='@':
93 | return True
94 | return False
95 |
96 | s = input().split(',')
97 | lst = []
98 |
99 | for i in s:
100 | length = len(i)
101 | if 6 <= length and length <= 12 and is_low(i) and is_up(i) and is_num(i) and is_other(i): #Checks if all the requirments are fulfilled
102 | lst.append(i)
103 |
104 | print(",".join(lst))
105 | ```
106 |
107 | **OR**
108 |
109 | ```python
110 | def check(x):
111 | cnt = (6<=len(x) and len(x)<=12)
112 | for i in x:
113 | if i.isupper():
114 | cnt+=1
115 | break
116 | for i in x:
117 | if i.islower():
118 | cnt+=1
119 | break
120 | for i in x:
121 | if i.isnumeric():
122 | cnt+=1
123 | break
124 | for i in x:
125 | if i=='@' or i=='#'or i=='$':
126 | cnt+=1
127 | break
128 | return cnt == 5 # counting if total 5 all conditions are fulfilled then returns True
129 |
130 | s = input().split(',')
131 | lst = filter(check,s) # Filter function pick the words from s, those returns True by check() function
132 | print(",".join(lst))
133 | ```
134 |
135 | **OR**
136 |
137 | ```python
138 | import re
139 |
140 | s = input().split(',')
141 | lst = []
142 |
143 | for i in s:
144 | cnt = 0
145 | cnt+=(6<=len(i) and len(i)<=12)
146 | cnt+=bool(re.search("[a-z]",i)) # here re module includes a function re.search() which returns the object information
147 | cnt+=bool(re.search("[A-Z]",i)) # of where the pattern string i is matched with any of the [a-z]/[A-z]/[0=9]/[@#$] characters
148 | cnt+=bool(re.search("[0-9]",i)) # if not a single match found then returns NONE which converts to False in boolean
149 | cnt+=bool(re.search("[@#$]",i)) # expression otherwise True if found any.
150 | if cnt == 5:
151 | lst.append(i)
152 |
153 | print(",".join(lst))
154 | ```
155 |
156 | ---
157 |
158 | ```python
159 | '''Solution by: pratikb0501
160 | '''
161 | import re
162 | a = input('Enter passwords: ').split(',')
163 | pass_pattern = re.compile(r"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[$#@]).{6,12}$")
164 | for i in a:
165 | if pass_pattern.fullmatch(i):
166 | print(i)
167 | ```
168 |
169 | **OR**
170 |
171 | ```python
172 | import re
173 | def multiple (patterns, string):
174 | for i in patterns:
175 | if not re.search(i, string):
176 | return False
177 |
178 | if 6 <= len(string) <= 12:
179 | return True
180 | else:
181 | return False
182 | x = str(input("Type password: "))
183 | patterns = [r"[a-z]", r"[A-Z]", r"[0-9]", r"[$|#|@]"]
184 | print(multiple(patterns, x))
185 | ```
186 |
187 | ---
188 |
189 | # Question 19
190 |
191 | ### **Question:**
192 |
193 | > **_You are required to write a program to sort the (name, age, score) tuples by ascending order where name is string, age and score are numbers. The tuples are input by console. The sort criteria is:_**
194 |
195 | - **_1: Sort based on name_**
196 | - **_2: Then sort based on age_**
197 | - **_3: Then sort by score_**
198 |
199 | > **_The priority is that name > age > score._**
200 |
201 | > **_If the following tuples are given as input to the program:_**
202 |
203 | ```
204 | Tom,19,80
205 | John,20,90
206 | Jony,17,91
207 | Jony,17,93
208 | Json,21,85
209 | ```
210 |
211 | > **_Then, the output of the program should be:_**
212 |
213 | ```
214 | [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
215 | ```
216 |
217 | ---
218 |
219 | ### Hints:
220 |
221 | > **_In case of input data being supplied to the question, it should be assumed to be a console input.We use itemgetter to enable multiple sort keys._**
222 |
223 | ---
224 |
225 | **Main author's Solution: Python 2**
226 |
227 | ```python
228 | from operator import itemgetter, attrgetter
229 |
230 | l = []
231 | while True:
232 | s = raw_input()
233 | if not s:
234 | break
235 | l.append(tuple(s.split(",")))
236 |
237 | print sorted(l, key=itemgetter(0,1,2))
238 | ```
239 |
240 | ---
241 |
242 | **My Solution: Python 3**
243 |
244 | ```python
245 | lst = []
246 | while True:
247 | s = input().split(',')
248 | if not s[0]: # breaks for blank input
249 | break
250 | lst.append(tuple(s))
251 |
252 | lst.sort(key= lambda x:(x[0],int(x[1]),int(x[2]))) # here key is defined by lambda and the data is sorted by element priority 0>1>2 in accending order
253 | print(lst)
254 | ```
255 |
256 | ---
257 |
258 | ## Conclusion
259 |
260 | **_Before the above problems, I didn't even know about re(regular expression) module and its use. I didn't even know how to sort by multiple keys. To solve those problems in different ways I had to explore and learn those syntax. There are a lots of interesting stuffs in re module though I faced quite a bit hardship to understand many of them._**
261 |
262 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%205.md "Day 5")
263 |
264 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%207.md "Day 7")
265 |
266 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
267 |
--------------------------------------------------------------------------------
/Status/Day 7.md:
--------------------------------------------------------------------------------
1 | # Question 20
2 |
3 | ### **Question:**
4 |
5 | > **_Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n._**
6 |
7 | > **_Suppose the following input is supplied to the program:_**
8 |
9 | ```
10 | 7
11 | ```
12 |
13 | > **_Then, the output should be:_**
14 |
15 | ```
16 | 0
17 | 7
18 | 14
19 | ```
20 | ---
21 |
22 | ### Hints:
23 |
24 | > **_Consider use class, function and comprehension._**
25 |
26 | ---
27 |
28 | **Main author's Solution: Python 2**
29 |
30 | #### **_The solution code for this problem was not as reltive to as the problem mentioned and there was a typing mistake while calling the function._**
31 |
32 | ---
33 |
34 | **Solution: Python 3**
35 |
36 | ```python
37 | '''Solution by: ShalomPrinz
38 | '''
39 | class MyGen():
40 | def by_seven(self, n):
41 | for i in range(0, int(n/7) + 1):
42 | yield i * 7
43 |
44 | for i in MyGen().by_seven( int(input('Please enter a number... ')) ):
45 | print(i)
46 | ```
47 |
48 | ---
49 |
50 | ```python
51 | '''Solution by: Seawolf159
52 | '''
53 | class Divisible:
54 |
55 | def by_seven(self, n):
56 | for number in range(1,n + 1):
57 | if number % 7 == 0: yield number
58 |
59 |
60 | divisible = Divisible()
61 | generator = divisible.by_seven(int(input("Please insert a number. --> ")))
62 | for number in generator:
63 | print(number)
64 |
65 | ```
66 |
67 | ---
68 |
69 | # Question 21
70 |
71 | ### **Question:**
72 |
73 | > **_A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:_**
74 |
75 | ```
76 | UP 5
77 | DOWN 3
78 | LEFT 3
79 | RIGHT 2
80 | ```
81 |
82 | > **_The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer._**
83 | > **_Example:_**
84 | > **_If the following tuples are given as input to the program:_**
85 |
86 | ```
87 | UP 5
88 | DOWN 3
89 | LEFT 3
90 | RIGHT 2
91 | ```
92 |
93 | > **_Then, the output of the program should be:_**
94 |
95 | ```
96 | 2
97 | ```
98 |
99 | ---
100 |
101 | ### Hints:
102 |
103 | > **_In case of input data being supplied to the question, it should be assumed to be a console input.Here distance indicates to euclidean distance.Import math module to use sqrt function._**
104 |
105 | ---
106 |
107 | **Main author's Solution: Python 2**
108 |
109 | ```python
110 | import math
111 | pos = [0,0]
112 | while True:
113 | s = raw_input()
114 | if not s:
115 | break
116 | movement = s.split(" ")
117 | direction = movement[0]
118 | steps = int(movement[1])
119 | if direction=="UP":
120 | pos[0]+=steps
121 | elif direction=="DOWN":
122 | pos[0]-=steps
123 | elif direction=="LEFT":
124 | pos[1]-=steps
125 | elif direction=="RIGHT":
126 | pos[1]+=steps
127 | else:
128 | pass
129 |
130 | print int(round(math.sqrt(pos[1]**2+pos[0]**2)))
131 | ```
132 |
133 | ---
134 |
135 | **My Solution: Python 3**
136 |
137 | ```python
138 | import math
139 |
140 | x,y = 0,0
141 | while True:
142 | s = input().split()
143 | if not s:
144 | break
145 | if s[0]=='UP': # s[0] indicates command
146 | x-=int(s[1]) # s[1] indicates unit of move
147 | if s[0]=='DOWN':
148 | x+=int(s[1])
149 | if s[0]=='LEFT':
150 | y-=int(s[1])
151 | if s[0]=='RIGHT':
152 | y+=int(s[1])
153 | # N**P means N^P
154 | dist = round(math.sqrt(x**2 + y**2)) # euclidean distance = square root of (x^2+y^2) and rounding it to nearest integer
155 | print(dist)
156 | ```
157 | ---
158 | ```python
159 | '''Solution by: pratikb0501
160 | '''
161 |
162 | from math import sqrt
163 | lst = []
164 | position = [0,0]
165 | while True:
166 | a = input()
167 | if not a:
168 | break
169 | lst.append(a)
170 | for i in lst:
171 | if 'UP' in i:
172 | position[0] -= int(i.strip('UP '))
173 | if 'DOWN' in i:
174 | position[0] += int(i.strip('DOWN '))
175 | if 'LEFT' in i:
176 | position[1] -= int(i.strip('LEFT '))
177 | if 'RIGHT' in i:
178 | position[1] += int(i.strip('RIGHT '))
179 | print(round(sqrt(position[1] ** 2 + position[0] ** 2)))
180 | ```
181 | ---
182 |
183 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%206.md "Day 6")
184 |
185 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%208.md "Day 8")
186 |
187 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
188 |
--------------------------------------------------------------------------------
/Status/Day 8.md:
--------------------------------------------------------------------------------
1 | # Question 22
2 |
3 | ### **Question:**
4 |
5 | > **_Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically._**
6 |
7 | > **_Suppose the following input is supplied to the program:_**
8 |
9 | ```
10 | New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
11 | ```
12 |
13 | > **_Then, the output should be:_**
14 |
15 | ```
16 | 2:2
17 | 3.:1
18 | 3?:1
19 | New:1
20 | Python:5
21 | Read:1
22 | and:1
23 | between:1
24 | choosing:1
25 | or:2
26 | to:1
27 | ```
28 |
29 | ---
30 |
31 | ### Hints
32 |
33 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
34 |
35 | ---
36 |
37 | **Main author's Solution: Python 2**
38 |
39 | ```python
40 | freq = {} # frequency of words in text
41 | line = raw_input()
42 | for word in line.split():
43 | freq[word] = freq.get(word,0)+1
44 |
45 | words = freq.keys()
46 | words.sort()
47 |
48 | for w in words:
49 | print "%s:%d" % (w,freq[w])
50 | ```
51 |
52 | ---
53 |
54 | **My Solution: Python 3**
55 |
56 | ```python
57 | ss = input().split()
58 | word = sorted(set(ss)) # split words are stored and sorted as a set
59 |
60 | for i in word:
61 | print("{0}:{1}".format(i,ss.count(i)))
62 | ```
63 |
64 | **OR**
65 |
66 | ```python
67 | ss = input().split()
68 | dict = {}
69 | for i in ss:
70 | i = dict.setdefault(i,ss.count(i)) # setdefault() function takes key & value to set it as dictionary.
71 |
72 | dict = sorted(dict.items()) # items() function returns both key & value of dictionary as a list
73 | # and then sorted. The sort by default occurs in order of 1st -> 2nd key
74 | for i in dict:
75 | print("%s:%d"%(i[0],i[1]))
76 | ```
77 |
78 | **OR**
79 |
80 | ```python
81 | ss = input().split()
82 | dict = {i:ss.count(i) for i in ss} # sets dictionary as i-> split word & ss.count(i) -> total occurrence of i in ss
83 | dict = sorted(dict.items()) # items() function returns both key & value of dictionary as a list
84 | # and then sorted. The sort by default occurs in order of 1st -> 2nd key
85 | for i in dict:
86 | print("%s:%d"%(i[0],i[1]))
87 | ```
88 |
89 | **OR**
90 |
91 | ```python
92 | from collections import Counter
93 |
94 | ss = input().split()
95 | ss = Counter(ss) # returns key & frequency as a dictionary
96 | ss = sorted(ss.items()) # returns as a tuple list
97 |
98 | for i in ss:
99 | print("%s:%d"%(i[0],i[1]))
100 | ```
101 |
102 | **Solution by: AnjanKumarG**
103 |
104 | ```python
105 | from pprint import pprint
106 | p=input().split()
107 | pprint({i:p.count(i) for i in p})
108 | ```
109 |
110 | ---
111 |
112 | # Question 23
113 |
114 | ### **Question:**
115 |
116 | > **_Write a method which can calculate square value of number_**
117 |
118 | ---
119 |
120 | ### Hints:
121 |
122 | ```
123 | Using the ** operator which can be written as n**p where means n^p
124 | ```
125 |
126 | ---
127 |
128 | **Main author's Solution: Python 2**
129 |
130 | ```python
131 | def square(num):
132 | return num ** 2
133 |
134 | print square(2)
135 | print square(3)
136 | ```
137 |
138 | ---
139 |
140 | **My Solution: Python 3**
141 |
142 | ```python
143 | n=int(input())
144 | print(n**2)
145 | ```
146 |
147 | ---
148 |
149 | # Question 24
150 |
151 | ### **Question:**
152 |
153 | > **_Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions._**
154 |
155 | > **_Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input()_**
156 |
157 | > **_And add document for your own function_**
158 |
159 | ### Hints:
160 |
161 | ```
162 | The built-in document method is __doc__
163 | ```
164 |
165 | ---
166 |
167 | **Main author's Solution: Python 2**
168 |
169 | ```python
170 | print abs.__doc__
171 | print int.__doc__
172 | print raw_input.__doc__
173 |
174 | def square(num):
175 | '''Return the square value of the input number.
176 |
177 | The input number must be integer.
178 | '''
179 | return num ** 2
180 |
181 | print square(2)
182 | print square.__doc__
183 | ```
184 |
185 | ---
186 |
187 | **My Solution: Python 3**
188 |
189 | ```python
190 | print(str.__doc__)
191 | print(sorted.__doc__)
192 |
193 | def pow(n,p):
194 | '''
195 | param n: This is any integer number
196 | param p: This is power over n
197 | return: n to the power p = n^p
198 | '''
199 |
200 | return n**p
201 |
202 | print(pow(3,4))
203 | print(pow.__doc__)
204 | ```
205 |
206 | ---
207 |
208 | # Question 25
209 |
210 | ### **Question:**
211 |
212 | > **_Define a class, which have a class parameter and have a same instance parameter._**
213 |
214 | ---
215 |
216 | ### Hints:
217 |
218 | ```
219 | Define an instance parameter, need add it in __init__ method.You can init an object with construct parameter or set the value later
220 | ```
221 |
222 | ---
223 |
224 | **Main author's Solution: Python 2**
225 |
226 | ```python
227 | class Person:
228 | # Define the class parameter "name"
229 | name = "Person"
230 |
231 | def __init__(self, name = None):
232 | # self.name is the instance parameter
233 | self.name = name
234 |
235 | jeffrey = Person("Jeffrey")
236 | print "%s name is %s" % (Person.name, jeffrey.name)
237 |
238 | nico = Person()
239 | nico.name = "Nico"
240 | print "%s name is %s" % (Person.name, nico.name)
241 | ```
242 |
243 | ---
244 |
245 | **My Solution: Python 3**
246 |
247 | ```python
248 | class Car:
249 | name = "Car"
250 |
251 | def __init__(self,name = None):
252 | self.name = name
253 |
254 | honda=Car("Honda")
255 | print("%s name is %s"%(Car.name,honda.name))
256 |
257 | toyota=Car()
258 | toyota.name="Toyota"
259 | print("%s name is %s"%(Car.name,toyota.name))
260 | ```
261 |
262 | ---
263 |
264 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%207.md "Day 7")
265 |
266 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%209.md "Day 9")
267 |
268 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
269 |
--------------------------------------------------------------------------------
/Status/Day 9.md:
--------------------------------------------------------------------------------
1 | # Question 26
2 |
3 | ### **Question:**
4 |
5 | > **_Define a function which can compute the sum of two numbers._**
6 |
7 | ---
8 |
9 | ### Hints:
10 |
11 | > **_Define a function with two numbers as arguments. You can compute the sum in the function and return the value._**
12 |
13 | ---
14 |
15 | **Main author's Solution: Python 2**
16 |
17 | ```python
18 | def SumFunction(number1, number2):
19 | return number1 + number2
20 |
21 | print SumFunction(1,2)
22 | ```
23 |
24 | ---
25 |
26 | **My Solution: Python 3**
27 |
28 | ```python
29 | sum = lambda n1,n2 : n1 + n2 # here lambda is use to define little function as sum
30 | print(sum(1,2))
31 | ```
32 |
33 | ---
34 |
35 | # Question 27
36 |
37 | ### **Question:**
38 |
39 | > **_Define a function that can convert a integer into a string and print it in console._**
40 |
41 | ---
42 |
43 | ### Hints:
44 |
45 | > **_Use str() to convert a number to string._**
46 |
47 | ---
48 |
49 | **Main author's Solution: Python 2**
50 |
51 | ```python
52 | def printValue(n):
53 | print str(n)
54 |
55 | printValue(3)
56 | ```
57 |
58 | ---
59 |
60 | **My Solution: Python 3**
61 |
62 | ```python
63 | conv = lambda x : str(x)
64 | n = conv(10)
65 | print(n)
66 | print(type(n)) # checks the type of the variable
67 | ```
68 |
69 | ---
70 |
71 | # Question 28
72 |
73 | ### **Question:**
74 |
75 | > **_Define a function that can receive two integer numbers in string form and compute their sum and then print it in console._**
76 |
77 | ---
78 |
79 | ### Hints:
80 |
81 | > **_Use int() to convert a string to integer._**
82 |
83 | ---
84 |
85 | **Main author's Solution: Python 2**
86 |
87 | ```python
88 | def printValue(s1,s2):
89 | print int(s1) + int(s2)
90 | printValue("3","4") #7
91 | ```
92 |
93 | ---
94 |
95 | **My Solution: Python 3**
96 |
97 | ```python
98 | sum = lambda s1,s2 : int(s1) + int(s2)
99 | print(sum("10","45")) # 55
100 | ```
101 |
102 | ---
103 |
104 | # Question 29
105 |
106 | ### **Question:**
107 |
108 | > **_Define a function that can accept two strings as input and concatenate them and then print it in console._**
109 |
110 | ---
111 |
112 | ### Hints:
113 |
114 | > **_Use + sign to concatenate the strings._**
115 |
116 | ---
117 |
118 | **Main author's Solution: Python 2**
119 |
120 | ```python
121 | def printValue(s1,s2):
122 | print s1 + s2
123 |
124 | printValue("3","4") #34
125 | ```
126 |
127 | ---
128 |
129 | **My Solution: Python 3**
130 |
131 | ```python
132 | sum = lambda s1,s2 : s1 + s2
133 | print(sum("10","45")) # 1045
134 | ```
135 |
136 | ---
137 |
138 | # Question 30
139 |
140 | ### **Question:**
141 |
142 | > **_Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print all strings line by line._**
143 |
144 | ---
145 |
146 | ### Hints:
147 |
148 | > **_Use len() function to get the length of a string._**
149 |
150 | ---
151 |
152 | **Main author's Solution: Python 2**
153 |
154 | ```python
155 | def printValue(s1,s2):
156 | len1 = len(s1)
157 | len2 = len(s2)
158 | if len1 > len2:
159 | print s1
160 | elif len2 > len1:
161 | print s2
162 | else:
163 | print s1
164 | print s2
165 |
166 | printValue("one","three")
167 |
168 | ```
169 |
170 | ---
171 |
172 | **My Solution: Python 3**
173 |
174 | ```python
175 | def printVal(s1,s2):
176 | len1 = len(s1)
177 | len2 = len(s2)
178 | if len1 > len2:
179 | print(s1)
180 | elif len1 < len2:
181 | print(s2)
182 | else:
183 | print(s1)
184 | print(s2)
185 |
186 | s1,s2=input().split()
187 | printVal(s1,s2)
188 | ```
189 |
190 | ---
191 |
192 | ```python
193 | '''Solution by: yuan1z'''
194 | func = lambda a,b: print(max((a,b),key=len)) if len(a)!=len(b) else print(a+'\n'+b)
195 | ```
196 |
197 | ---
198 |
199 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%208.md "Day 9")
200 |
201 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_10.md "Day 10")
202 |
203 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
204 |
--------------------------------------------------------------------------------
/Status/Day_10.md:
--------------------------------------------------------------------------------
1 | # Question 31
2 |
3 | ### **Question:**
4 |
5 | > **_Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys._**
6 |
7 | ---
8 |
9 | ### Hints:
10 |
11 | ```
12 | Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.
13 | ```
14 |
15 | ---
16 |
17 | **Main Author's Solution: Python 2**
18 |
19 | ```python
20 | def printDict():
21 | d=dict()
22 | for i in range(1,21):
23 | d[i]=i**2
24 | print d
25 |
26 | printDict()
27 | ```
28 |
29 | ---
30 |
31 | **My Solution: Python 3**
32 |
33 | ```python
34 | def printDict():
35 | dict={i:i**2 for i in range(1,21)} # Using comprehension method and
36 | print(dict)
37 |
38 | printDict()
39 | ```
40 |
41 | ---
42 |
43 | # Question 32
44 |
45 | ### **Question:**
46 |
47 | > **_Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only._**
48 |
49 | ---
50 |
51 | ### Hints:
52 |
53 | ```
54 | Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
55 | ```
56 |
57 | ---
58 |
59 | **Main Author's Solution: Python 2**
60 |
61 | ```python
62 | def printDict():
63 | d=dict()
64 | for i in range(1,21):
65 | d[i]=i**2
66 | for k in d.keys():
67 | print k
68 | printDict()
69 | ```
70 |
71 | ---
72 |
73 | **My Solution: Python 3**
74 |
75 | ```python
76 | def printDict():
77 | dict = {i: i**2 for i in range(1, 21)}
78 | print(dict.keys()) # print keys of a dictionary
79 |
80 | printDict()
81 | ```
82 |
83 | ---
84 |
85 | # Question 33
86 |
87 | ### **Question:**
88 |
89 | > **_Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included)._**
90 |
91 | ---
92 |
93 | ### Hints:
94 |
95 | ```
96 | Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.
97 | ```
98 |
99 | ---
100 |
101 | **Main Author's Solution: Python 2**
102 |
103 | ```python
104 | def printList():
105 | li=list()
106 | for i in range(1,21):
107 | li.append(i**2)
108 | print li
109 |
110 | printList()
111 | ```
112 |
113 | ---
114 |
115 | **My Solution: Python 3**
116 |
117 | ```python
118 | def printList():
119 | lst = [i ** 2 for i in range(1, 21)]
120 | print(lst)
121 |
122 | printList()
123 | ```
124 |
125 | ---
126 |
127 | # Question 34
128 |
129 | ### **Question:**
130 |
131 | > **_Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list._**
132 |
133 | ---
134 |
135 | ### Hints:
136 |
137 | ```
138 | Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
139 | ```
140 |
141 | ---
142 |
143 | **Main Author's Solution: Python 2**
144 |
145 | ```python
146 | def printList():
147 | li=list()
148 | for i in range(1,21):
149 | li.append(i**2)
150 | print li[:5]
151 |
152 | printList()
153 | ```
154 |
155 | ---
156 |
157 | **My Solution: Python 3**
158 |
159 | ```python
160 | def printList():
161 | lst = [i ** 2 for i in range(1, 21)]
162 |
163 | for i in range(5):
164 | print(lst[i])
165 |
166 | printList()
167 | ```
168 | ---
169 | ```python
170 | '''Solution by: popomaticbubble
171 | '''
172 | def squares(n):
173 | squares_list = [i**2 for i in range(1,n+1)]
174 | print(squares_list[0:5])
175 | squares(20)
176 | ```
177 | ---
178 |
179 | ```python
180 | '''Solution by: yuan1z'''
181 | func = lambda :print([i**2 for i in range(1,21)][:5])
182 | ```
183 |
184 | ---
185 |
186 | # Question 35
187 |
188 | ### **Question:**
189 |
190 | > **_Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list._**
191 |
192 | ---
193 |
194 | ### Hints:
195 |
196 | ```
197 | Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
198 | ```
199 |
200 | ---
201 |
202 | **Main Author's Solution: Python 2**
203 |
204 | ```python
205 | def printList():
206 | li=list()
207 | for i in range(1,21):
208 | li.append(i**2)
209 | print li[-5:]
210 |
211 | printList()
212 | ```
213 |
214 | ---
215 |
216 | **My Solution: Python 3**
217 |
218 | ```python
219 | def printList():
220 | lst = [i ** 2 for i in range(1, 21)]
221 | for i in range(19,14,-1):
222 | print(lst[i])
223 |
224 | printList()
225 | ```
226 | ---
227 | ```python
228 | '''Solution by: popomaticbubble
229 | '''
230 | def squares(n):
231 | squares_list = [i**2 for i in range(1,n+1)]
232 | print(squares_list[-5:])
233 | squares(20)
234 | ```
235 |
236 | ---
237 |
238 | # Question 36
239 |
240 | ### **Question:**
241 |
242 | > **_Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list._**
243 |
244 | ---
245 |
246 | ```
247 | Hints: Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list
248 | ```
249 |
250 | ---
251 |
252 | **Main Author's Solution: Python 2**
253 |
254 | ```python
255 | def printList():
256 | li=list()
257 | for i in range(1,21):
258 | li.append(i**2)
259 | print li[5:]
260 |
261 | printList()
262 | ```
263 |
264 | ---
265 |
266 | **My Solution: Python 3**
267 |
268 | ```python
269 | def printList():
270 | lst = [i ** 2 for i in range(1, 21)]
271 | for i in range(5,20):
272 | print(lst[i])
273 |
274 | printList()
275 | ```
276 |
277 | ---
278 |
279 | # Question 37
280 |
281 | ### **Question:**
282 |
283 | > **_Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included)._**
284 |
285 | ---
286 |
287 | ### Hints:
288 |
289 | ```
290 | Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use tuple() to get a tuple from a list.
291 | ```
292 |
293 | ---
294 |
295 | **Main Author's Solution: Python 2**
296 |
297 | ```python
298 | def printTuple():
299 | li=list()
300 | for i in range(1,21):
301 | li.append(i**2)
302 | print tuple(li)
303 |
304 | printTuple()
305 | ```
306 |
307 | ---
308 |
309 | **My Solution: Python 3**
310 |
311 | ```python
312 | def printTupple():
313 | lst = [i ** 2 for i in range(1, 21)]
314 | print(tuple(lst))
315 |
316 | printTupple()
317 | ```
318 |
319 | ---
320 |
321 | ```python
322 | '''
323 | Solution by: Seawolf159
324 | '''
325 | def square_of_numbers():
326 | return tuple(i ** 2 for i in range(1, 21))
327 |
328 | print(square_of_numbers())
329 | ```
330 |
331 | ---
332 |
333 | ### Comment
334 |
335 | **_Problems of this section is very much easy and all of those are of a modification of same type problem which mainly focused on using some commonly used function works with list,dictionary, tupple.In my entire solutions, I havn't tried to solve problems in efficient way.Rather I tried to solve in a different way that I can.This will help a beginner to know how simplest problems can be solved in different ways._**
336 |
337 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%209.md "Day 9")
338 |
339 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_11.md "Day 11")
340 |
341 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
342 |
--------------------------------------------------------------------------------
/Status/Day_11.md:
--------------------------------------------------------------------------------
1 | # Question 38
2 |
3 | ### **Question:**
4 |
5 | > **_With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line._**
6 |
7 | ---
8 |
9 | ### Hints:
10 |
11 | > **_Use [n1:n2] notation to get a slice from a tuple._**
12 |
13 | ---
14 |
15 | **Main Author's Solution: Python 2**
16 |
17 | ```python
18 | tp = (1,2,3,4,5,6,7,8,9,10)
19 | tp1 = tp[:5]
20 | tp2 = tp[5:]
21 | print tp1
22 | print tp2
23 | ```
24 |
25 | ---
26 |
27 | **My Solution: Python 3**
28 |
29 | ```python
30 | tpl = (1,2,3,4,5,6,7,8,9,10)
31 |
32 | for i in range(0,5):
33 | print(tpl[i],end = ' ')
34 | print()
35 | for i in range(5,10):
36 | print(tpl[i],end = ' ')
37 | ```
38 |
39 | **OR**
40 |
41 | ```python
42 | tpl = (1,2,3,4,5,6,7,8,9,10)
43 | lst1,lst2 = [],[]
44 |
45 | for i in range(0,5):
46 | lst1.append(tpl[i])
47 |
48 | for i in range(5,10):
49 | lst2.append(tpl[i])
50 |
51 | print(lst1)
52 | print(lst2)
53 | ```
54 | ----
55 |
56 | ```python
57 |
58 | '''
59 | Solution by: CoffeeBrakeInc
60 | '''
61 |
62 | tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
63 | lt = int(len(tup)/2)
64 | print(tup[:lt], tup[lt:])
65 | ```
66 |
67 | ---
68 |
69 | ```python
70 |
71 | '''
72 | Solution by: AasaiAlangaram
73 | '''
74 |
75 | tp = (1,2,3,4,5,6,7,8,9,10)
76 |
77 | print('The Original Tuple:',tp)
78 |
79 | [print('Splitted List :{List}'.format(List = tp[x:x+5])) for x in range(0,len(tp),5)]
80 |
81 | ```
82 |
83 | ---
84 | ```python
85 |
86 | '''
87 | Solution by: saxenaharsh24
88 | '''
89 |
90 | tup = [i for i in range(1, 11)]
91 | print(f'{tuple(tup[:5])} \n{tuple(tup[5:])}')
92 | ```
93 |
94 | # Question 39
95 |
96 | ### **Question:**
97 |
98 | > **_Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10)._**
99 |
100 | ---
101 |
102 | ### Hints:
103 |
104 | > **_Use "for" to iterate the tuple. Use tuple() to generate a tuple from a list._**
105 |
106 | ---
107 |
108 | **Main Author's Solution: Python 2**
109 |
110 | ```python
111 | tp = (1,2,3,4,5,6,7,8,9,10)
112 | li = list()
113 | for i in tp:
114 | if tp[i]%2 == 0:
115 | li.append(tp[i])
116 |
117 | tp2 = tuple(li)
118 | print tp2
119 | ```
120 |
121 | ---
122 |
123 | **My Solution: Python 3**
124 |
125 | ```python
126 | tpl = (1,2,3,4,5,6,7,8,9,10)
127 | tpl1 = tuple(i for i in tpl if i%2 == 0)
128 | print(tpl1)
129 | ```
130 |
131 | **OR**
132 |
133 | ```python
134 | tpl = (1,2,3,4,5,6,7,8,9,10)
135 | tpl1 = tuple(filter(lambda x : x%2==0,tpl)) # Lambda function returns True if found even element.
136 | # Filter removes data for which function returns False
137 | print(tpl1)
138 | ```
139 |
140 | ---
141 |
142 | # Question 40
143 |
144 | ### **Question:**
145 |
146 | > **_Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No"._**
147 |
148 | ---
149 |
150 | ### Hints:
151 |
152 | > **_Use if statement to judge condition._**
153 |
154 | ---
155 |
156 | **Main Author's Solution: Python 2**
157 |
158 | ```python
159 | s= raw_input()
160 | if s=="yes" or s=="YES" or s=="Yes":
161 | print "Yes"
162 | else:
163 | print "No"
164 | ```
165 |
166 | ---
167 |
168 | **Solution: Python 3**
169 |
170 | ```python
171 | '''
172 | Solution by: Seawolf159
173 | '''
174 | text = input("Please type something. --> ")
175 | if text == "yes" or text == "YES" or text == "Yes":
176 | print("Yes")
177 | else:
178 | print("No")
179 | ```
180 |
181 | ---
182 |
183 | ```python
184 | '''
185 | Solution by: AasaiAlangaram
186 | '''
187 | input = input('Enter string:')
188 | output = ''.join(['Yes' if input == 'yes' or input =='YES' or input =='Yes' else 'No' ])
189 | print(str(output))
190 | ```
191 | ----------------
192 | ```
193 | Solution by: Prashanth
194 | '''
195 | x = str(input().lower())
196 | if x == 'yes':
197 | print('Yes')
198 | else:
199 | print('No')
200 | ```
201 | --------
202 |
203 | # Question 41
204 |
205 | ### **Question:**
206 |
207 | > **_Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10]._**
208 |
209 | ---
210 |
211 | ### Hints:
212 |
213 | > **_Use map() to generate a list.Use lambda to define anonymous functions._**
214 |
215 | ---
216 |
217 | **Main Author's Solution: Python 2**
218 |
219 | ```python
220 | li = [1,2,3,4,5,6,7,8,9,10]
221 | squaredNumbers = map(lambda x: x**2, li)
222 | print squaredNumbers
223 | ```
224 |
225 | ---
226 |
227 | **My Solution: Python 3**
228 |
229 | ```python
230 | # No different way of code is written as the requirment is specificly mentioned in problem description
231 |
232 | li = [1,2,3,4,5,6,7,8,9,10]
233 | squaredNumbers = map(lambda x: x**2, li) # returns map type object data
234 | print(list(squaredNumbers)) # converting the object into list
235 | ```
236 | ---
237 | ```python
238 | '''
239 | Solution by: saxenaharsh24
240 | '''
241 | def sqrs(item):
242 | return item ** 2
243 |
244 |
245 | lst = [i for i in range(1, 11)]
246 | print(list(map(sqrs, lst)))
247 | ```
248 | ---
249 |
250 | # Question 42
251 |
252 | ### **Question:**
253 |
254 | > **_Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10]._**
255 |
256 | ---
257 |
258 | ### Hints:
259 |
260 | > **_Use map() to generate a list.Use filter() to filter elements of a list.Use lambda to define anonymous functions._**
261 |
262 | ---
263 |
264 | **Main Author's Solution: Python 2**
265 |
266 | ```python
267 | li = [1,2,3,4,5,6,7,8,9,10]
268 | evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li))
269 | print evenNumbers
270 | ```
271 |
272 | ---
273 |
274 | **My Solution: Python 3**
275 |
276 | ```python
277 | def even(x):
278 | return x%2==0
279 |
280 | def squer(x):
281 | return x*x
282 |
283 | li = [1,2,3,4,5,6,7,8,9,10]
284 | li = map(squer,filter(even,li)) # first filters number by even number and the apply map() on the resultant elements
285 | print(list(li))
286 | ```
287 | ---
288 | ```python
289 | """
290 | Solution by: saxenaharsh24
291 | """
292 | def even(item):
293 | if item % 2 == 0:
294 | return item**2
295 |
296 |
297 | lst = [i for i in range(1, 11)]
298 | print(list(filter(lambda j: j is not None, list(map(even, lst)))))
299 | ```
300 | ---
301 |
302 | # Question 43
303 |
304 | ### **Question:**
305 |
306 | > **_Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included)._**
307 |
308 | ---
309 |
310 | ### Hints:
311 |
312 | > **_Use filter() to filter elements of a list.Use lambda to define anonymous functions._**
313 |
314 | ---
315 |
316 | **Main Author's Solution: Python 2**
317 |
318 | ```python
319 | evenNumbers = filter(lambda x: x%2==0, range(1,21))
320 | print evenNumbers
321 | ```
322 |
323 | ---
324 |
325 | **My Solution: Python 3**
326 |
327 | ```python
328 | def even(x):
329 | return x%2==0
330 |
331 | evenNumbers = filter(even, range(1,21))
332 | print(list(evenNumbers))
333 | ```
334 |
335 | ---
336 |
337 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_10.md "Day 10")
338 |
339 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_12.md "Day 12")
340 |
341 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
342 |
--------------------------------------------------------------------------------
/Status/Day_12.md:
--------------------------------------------------------------------------------
1 | # Question 44
2 |
3 | ### **Question:**
4 |
5 | > **_Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included)._**
6 |
7 | ---
8 |
9 | ### Hints:
10 |
11 | > **_Use map() to generate a list. Use lambda to define anonymous functions._**
12 |
13 | ---
14 |
15 | **Main Author's Solution: Python 2**
16 |
17 | ```python
18 | squaredNumbers = map(lambda x: x**2, range(1,21))
19 | print squaredNumbers
20 | ```
21 |
22 | ---
23 |
24 | **My Solution: Python 3**
25 |
26 | ```python
27 | def sqr(x):
28 | return x*x
29 |
30 | squaredNumbers = list(map(sqr, range(1,21)))
31 | print (squaredNumbers)
32 | ```
33 |
34 | ---
35 |
36 | # Question 45
37 |
38 | ### **Question:**
39 |
40 | > **_Define a class named American which has a static method called printNationality._**
41 |
42 | ---
43 |
44 | ### Hints:
45 |
46 | > **_Use @staticmethod decorator to define class static method.There are also two more methods.To know more, go to this [link](https://realpython.com/blog/python/instance-class-and-static-methods-demystified/)._**
47 |
48 | ---
49 |
50 | **Main Author's Solution: Python 2**
51 |
52 | ```python
53 | class American(object):
54 | @staticmethod
55 | def printNationality():
56 | print "America"
57 |
58 | anAmerican = American()
59 | anAmerican.printNationality()
60 | American.printNationality()
61 | ```
62 |
63 | ---
64 |
65 | **My Solution: Python 3**
66 |
67 | ```python
68 | class American():
69 | @staticmethod
70 | def printNationality():
71 | print("I am American")
72 |
73 | american = American()
74 | american.printNationality() # this will not run if @staticmethod does not decorates the function.
75 | # Because the class has no instance.
76 |
77 | American.printNationality() # this will run even though the @staticmethod
78 | # does not decorate printNationality()
79 | ```
80 |
81 | ---
82 |
83 | # Question 46
84 |
85 | ### **Question:**
86 |
87 | > **_Define a class named American and its subclass NewYorker._**
88 |
89 | ---
90 |
91 | ### Hints:
92 |
93 | > **Use class Subclass(ParentClass) to define a subclass.\***
94 |
95 | ---
96 |
97 | **Main Author's Solution: Python 2**
98 |
99 | ```python
100 | class American(object):
101 | pass
102 |
103 | class NewYorker(American):
104 | pass
105 |
106 | anAmerican = American()
107 | aNewYorker = NewYorker()
108 | print anAmerican
109 | print aNewYorker
110 | ```
111 |
112 | ---
113 |
114 | **My Solution: Python 3**
115 |
116 | ```python
117 | class American():
118 | pass
119 |
120 | class NewYorker(American):
121 | pass
122 |
123 | american = American()
124 | newyorker = NewYorker()
125 |
126 | print(american)
127 | print(newyorker)
128 | ```
129 |
130 | ---
131 |
132 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_11.md "Day 11")
133 |
134 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_13.md "Day 13")
135 |
136 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
137 |
--------------------------------------------------------------------------------
/Status/Day_13.md:
--------------------------------------------------------------------------------
1 | # Question 47
2 |
3 | ### **Question**
4 |
5 | > **_Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area._**
6 |
7 | ---
8 |
9 | ### Hints
10 |
11 | > **_Use def methodName(self) to define a method._**
12 |
13 | ---
14 |
15 | **Main author's Solution: Python 2**
16 |
17 | ```python
18 | class Circle(object):
19 | def __init__(self, r):
20 | self.radius = r
21 |
22 | def area(self):
23 | return self.radius**2*3.14
24 |
25 | aCircle = Circle(2)
26 | print aCircle.area()
27 | ```
28 |
29 | ---
30 |
31 | **My Solution: Python 3**
32 |
33 | ```python
34 | class Circle():
35 | def __init__(self,r):
36 | self.radius = r
37 |
38 | def area(self):
39 | return 3.1416*(self.radius**2)
40 |
41 |
42 | circle = Circle(5)
43 | print(circle.area())
44 | ```
45 |
46 | ---
47 |
48 | # Question 48
49 |
50 | ### **Question**
51 |
52 | > **_Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area._**
53 |
54 | ---
55 |
56 | ### Hints
57 |
58 | > **_Use def methodName(self) to define a method._**
59 |
60 | ---
61 |
62 | **Main author's Solution: Python 2**
63 |
64 | ```python
65 | class Rectangle(object):
66 | def __init__(self, l, w):
67 | self.length = l
68 | self.width = w
69 |
70 | def area(self):
71 | return self.length*self.width
72 |
73 | aRectangle = Rectangle(2,10)
74 | print aRectangle.area()
75 |
76 | ```
77 |
78 | ---
79 |
80 | **My Solution: Python 3**
81 |
82 | ```python
83 | class Rectangle():
84 | def __init__(self,l,w):
85 | self.length = l
86 | self.width = w
87 |
88 | def area(self):
89 | return self.length*self.width
90 |
91 |
92 | rect = Rectangle(2,4)
93 | print(rect.area())
94 |
95 | ```
96 |
97 | ---
98 |
99 | # Question 49
100 |
101 | ### **Question**
102 |
103 | > **_Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default._**
104 |
105 | ---
106 |
107 | ### Hints
108 |
109 | > **_To override a method in super class, we can define a method with the same name in the super class._**
110 |
111 | ---
112 |
113 | **Main author's Solution: Python 2**
114 |
115 | ```python
116 | class Shape(object):
117 | def __init__(self):
118 | pass
119 |
120 | def area(self):
121 | return 0
122 |
123 | class Square(Shape):
124 | def __init__(self, l):
125 | Shape.__init__(self)
126 | self.length = l
127 |
128 | def area(self):
129 | return self.length*self.length
130 |
131 | aSquare= Square(3)
132 | print aSquare.area()
133 | ```
134 |
135 | ---
136 |
137 | **My Solution: Python 3**
138 |
139 | ```python
140 | class Shape():
141 | def __init__(self):
142 | pass
143 |
144 | def area(self):
145 | return 0
146 |
147 | class Square(Shape):
148 | def __init__(self,length = 0):
149 | Shape.__init__(self)
150 | self.length = length
151 |
152 | def area(self):
153 | return self.length*self.length
154 |
155 | Asqr = Square(5)
156 | print(Asqr.area()) # prints 25 as given argument
157 |
158 | print(Square().area()) # prints zero as default area
159 | ```
160 |
161 | ---
162 |
163 | # Question 50
164 |
165 | ### **Question**
166 |
167 | > **_Please raise a RuntimeError exception._**
168 |
169 | ---
170 |
171 | ### Hints
172 |
173 | > **_UUse raise() to raise an exception._**
174 |
175 | ---
176 |
177 | **Solution:**
178 |
179 | ```python
180 | raise RuntimeError('something wrong')
181 | ```
182 |
183 | ---
184 |
185 | ## Conclusion
186 |
187 | **_Well It seems that the above problems are very much focused on basic concpets and implimantation of object oriented programming.As the concepts are not about to solve any functional problem rather design the structure , so both codes are very much similar in there implimantation part._**
188 |
189 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_12.md "Day 12")
190 |
191 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_14.md "Day 14")
192 |
193 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
194 |
--------------------------------------------------------------------------------
/Status/Day_14.md:
--------------------------------------------------------------------------------
1 |
2 | # Question 51
3 |
4 | ### **Question**
5 |
6 | > ***Write a function to compute 5/0 and use try/except to catch the exceptions.***
7 |
8 | ----------------------
9 | ### Hints
10 | > ***Use try/except to catch exceptions.***
11 |
12 | ----------------------
13 |
14 | **Main author's Solution: Python 2**
15 | ```python
16 | def throws():
17 | return 5/0
18 |
19 | try:
20 | throws()
21 | except ZeroDivisionError:
22 | print "division by zero!"
23 | except Exception, err:
24 | print 'Caught an exception'
25 | finally:
26 | print 'In finally block for cleanup'
27 | ```
28 | ----------------
29 | **My Solution: Python 3**
30 | ```python
31 | def divide():
32 | return 5/0
33 |
34 | try:
35 | divide()
36 | except ZeroDivisionError as ze:
37 | print("Why on earth you are dividing a number by ZERO!!")
38 | except:
39 | print("Any other exception")
40 |
41 | ```
42 | ---------------------
43 |
44 |
45 | # Question 52
46 |
47 | ### **Question**
48 |
49 | > ***Define a custom exception class which takes a string message as attribute.***
50 |
51 | ----------------------
52 | ### Hints
53 | > ***To define a custom exception, we need to define a class inherited from Exception.***
54 |
55 | ----------------------
56 |
57 | **Main author's Solution: Python 2**
58 | ```python
59 | class MyError(Exception):
60 | """My own exception class
61 |
62 | Attributes:
63 | msg -- explanation of the error
64 | """
65 |
66 | def __init__(self, msg):
67 | self.msg = msg
68 |
69 | error = MyError("something wrong")
70 |
71 | ```
72 | ----------------
73 | **My Solution: Python 3**
74 | ```python
75 |
76 | class CustomException(Exception):
77 | """Exception raised for custom purpose
78 |
79 | Attributes:
80 | message -- explanation of the error
81 | """
82 |
83 | def __init__(self, message):
84 | self.message = message
85 |
86 |
87 | num = int(input())
88 |
89 | try:
90 | if num < 10:
91 | raise CustomException("Input is less than 10")
92 | elif num > 10:
93 | raise CustomException("Input is grater than 10")
94 | except CustomException as ce:
95 | print("The error raised: " + ce.message)
96 |
97 | ```
98 | ---------------------
99 |
100 |
101 | # Question 53
102 |
103 | ### **Question**
104 |
105 | > ***Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only.***
106 |
107 | > ***Example:
108 | If the following email address is given as input to the
109 | program:***
110 | ```
111 | john@google.com
112 | ```
113 | > ***Then, the output of the program should be:***
114 | ```
115 | john
116 | ```
117 | > ***In case of input data being supplied to the question, it should be assumed to be a console input.***
118 |
119 | ----------------------
120 | ### Hints
121 | > ***Use \w to match letters.***
122 |
123 | ----------------------
124 |
125 | **Main author's Solution: Python 2**
126 | ```python
127 | import re
128 | emailAddress = raw_input()
129 | pat2 = "(\w+)@((\w+\.)+(com))"
130 | r2 = re.match(pat2,emailAddress)
131 | print r2.group(1)
132 | ```
133 | ----------------
134 | **My Solution: Python 3**
135 | ```python
136 | email = "john@google.com"
137 | email = email.split('@')
138 | print(email[0])
139 | ```
140 | ---------------------
141 | **OR**
142 | ```python
143 | import re
144 |
145 | email = "john@google.com elise@python.com"
146 | pattern = "(\w+)@\w+.com"
147 | ans = re.findall(pattern,email)
148 | print(ans)
149 | ```
150 |
151 |
152 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_13.md "Day 13")
153 |
154 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_15.md "Day 15")
155 |
156 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
--------------------------------------------------------------------------------
/Status/Day_15.md:
--------------------------------------------------------------------------------
1 | # Question 54
2 |
3 | ### **Question**
4 |
5 | > **_Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only._**
6 |
7 | > **_Example:
8 | > If the following email address is given as input to the program:_**
9 |
10 | ```
11 | john@google.com
12 | ```
13 |
14 | > **_Then, the output of the program should be:_**
15 |
16 | ```
17 | google
18 | ```
19 |
20 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
21 |
22 | ---
23 |
24 | ### Hints
25 |
26 | > **_Use \w to match letters._**
27 |
28 | ---
29 |
30 | **Main author's Solution: Python 2**
31 |
32 | ```python
33 | import re
34 | emailAddress = raw_input()
35 | pat2 = "(\w+)@(\w+)\.(com)"
36 | r2 = re.match(pat2,emailAddress)
37 | print r2.group(2)
38 | ```
39 |
40 | ---
41 |
42 | **My Solution: Python 3**
43 |
44 | ```python
45 | import re
46 |
47 | email = "john@google.com elise@python.com"
48 | pattern = "\w+@(\w+).com"
49 | ans = re.findall(pattern,email)
50 | print(ans)
51 | ```
52 |
53 | ---
54 |
55 | # Question 55
56 |
57 | ### **Question**
58 |
59 | > **_Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only._**
60 |
61 | > **_Example:
62 | > If the following words is given as input to the program:_**
63 |
64 | ```
65 | 2 cats and 3 dogs.
66 | ```
67 |
68 | > **_Then, the output of the program should be:_**
69 |
70 | ```
71 | ['2', '3']
72 | ```
73 |
74 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
75 |
76 | ---
77 |
78 | ### Hints
79 |
80 | > **_Use re.findall() to find all substring using regex._**
81 |
82 | ---
83 |
84 | **Main author's Solution: Python 2**
85 |
86 | ```python
87 | import re
88 | s = raw_input()
89 | print re.findall("\d+",s)
90 | ```
91 |
92 | ---
93 |
94 | **My Solution: Python 3**
95 |
96 | ```python
97 | import re
98 |
99 | email = input()
100 | pattern = "\d+"
101 | ans = re.findall(pattern,email)
102 | print(ans)
103 | ```
104 |
105 | **OR**
106 |
107 | ```python
108 | email = input().split()
109 | ans = []
110 | for word in email:
111 | if word.isdigit(): # can also use isnumeric() / isdecimal() function instead
112 | ans.append(word)
113 | print(ans)
114 | ```
115 |
116 | **OR**
117 |
118 | ```python
119 | email = input().split()
120 | ans = [word for word in email if word.isdigit()] # using list comprehension method
121 | print(ans)
122 | ```
123 |
124 | ---
125 |
126 | # Question 56
127 |
128 | ### **Question**
129 |
130 | > **_Print a unicode string "hello world"._**
131 |
132 | ---
133 |
134 | ### Hints
135 |
136 | > **_Use u'strings' format to define unicode string._**
137 |
138 | ---
139 |
140 | **Main author's Solution: Python 2**
141 |
142 | ```python
143 | unicodeString = u"hello world!"
144 | print unicodeString
145 | ```
146 |
147 | ---
148 |
149 | # Question 57
150 |
151 | ### **Question**
152 |
153 | > **_Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8._**
154 |
155 | ---
156 |
157 | ### Hints
158 |
159 | > **_Use unicode()/encode() function to convert._**
160 |
161 | ---
162 |
163 | **Main author's Solution: Python 2**
164 |
165 | ```python
166 | s = raw_input()
167 | u = unicode( s ,"utf-8")
168 | print u
169 | ```
170 |
171 | ---
172 |
173 | **My Solution: Python 3**
174 |
175 | ```python
176 | s = input()
177 | u = s.encode('utf-8')
178 | print(u)
179 | ```
180 |
181 | ---
182 |
183 | # Question 58
184 |
185 | ### **Question**
186 |
187 | > **_Write a special comment to indicate a Python source code file is in unicode._**
188 |
189 | ---
190 |
191 | ### Hints
192 |
193 | > **_Use unicode() function to convert._**
194 |
195 | ---
196 |
197 | **Solution:**
198 |
199 | ```python
200 | # -*- coding: utf-8 -*-
201 | ```
202 |
203 | ---
204 |
205 | # Question 59
206 |
207 | ### **Question**
208 |
209 | > **_Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0)._**
210 |
211 | > **_Example:
212 | > If the following n is given as input to the program:_**
213 |
214 | ```
215 | 5
216 | ```
217 |
218 | > **_Then, the output of the program should be:_**
219 |
220 | ```
221 | 3.55
222 | ```
223 |
224 | > **_In case of input data being supplied to the question, it should be assumed to be a console input._**
225 |
226 | ---
227 |
228 | ### Hints
229 |
230 | > **_Use float() to convert an integer to a float.Even if not converted it wont cause a problem because python by default understands the data type of a value_**
231 |
232 | ---
233 |
234 | **Main author's Solution: Python 2**
235 |
236 | ```python
237 | n=int(raw_input())
238 | sum=0.0
239 | for i in range(1,n+1):
240 | sum += float(float(i)/(i+1))
241 | print sum
242 | ```
243 |
244 | ---
245 |
246 | **My Solution: Python 3**
247 |
248 | ```python
249 | n = int(input())
250 | sum = 0
251 | for i in range(1, n+1):
252 | sum+= i/(i+1)
253 | print(round(sum, 2)) # rounded to 2 decimal point
254 | ```
255 | ---
256 | ```python
257 | '''Solution by: lcastrooliveira
258 | '''
259 | def question_59(n):
260 | print(round(sum(map(lambda x: x/(x+1), range(1, n+1))), 2))
261 |
262 | question_59(5)
263 | ```
264 | ---
265 |
266 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_14.md "Day 14")
267 |
268 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_16.md "Day 16")
269 |
270 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
271 |
--------------------------------------------------------------------------------
/Status/Day_17.md:
--------------------------------------------------------------------------------
1 | # Question 65
2 |
3 | ### **Question**
4 |
5 | > **_Please write assert statements to verify that every number in the list [2,4,6,8] is even._**
6 |
7 | ---
8 |
9 | ### Hints
10 |
11 | > **_Use "assert expression" to make assertion._**
12 |
13 | ---
14 |
15 | **Main author's Solution: Python 2**
16 |
17 | ```python
18 | li = [2,4,6,8]
19 | for i in li:
20 | assert i%2==0
21 | ```
22 |
23 | ---
24 |
25 | **My Solution: Python 3**
26 |
27 | ```python
28 | data = [2,4,5,6]
29 | for i in data:
30 | assert i%2 == 0, "{} is not an even number".format(i)
31 | ```
32 |
33 | ---
34 |
35 | # Question 66
36 |
37 | ### **Question**
38 |
39 | > **_Please write a program which accepts basic mathematic expression from console and print the evaluation result._**
40 |
41 | > **_Example:
42 | > If the following n is given as input to the program:_**
43 |
44 | ```
45 | 35 + 3
46 | ```
47 |
48 | > **_Then, the output of the program should be:_**
49 |
50 | ```
51 | 38
52 | ```
53 |
54 | ---
55 |
56 | ### Hints
57 |
58 | > **_Use eval() to evaluate an expression._**
59 |
60 | ---
61 |
62 | **Main author's Solution: Python 2**
63 |
64 | ```python
65 | expression = raw_input()
66 | print eval(expression)
67 | ```
68 |
69 | ---
70 |
71 | **My Solution: Python 3**
72 |
73 | ```python
74 | expression = input()
75 | ans = eval(expression)
76 | print(ans)
77 | ```
78 |
79 | ---
80 |
81 | # Question 67
82 |
83 | ### **Question**
84 |
85 | > **_Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list._**
86 |
87 | ---
88 |
89 | ### Hints
90 |
91 | > **_Use if/elif to deal with conditions._**
92 |
93 | ---
94 |
95 | **Main author's Solution: Python 2**
96 |
97 | ```python
98 | import math
99 | def bin_search(li, element):
100 | bottom = 0
101 | top = len(li)-1
102 | index = -1
103 | while top>=bottom and index==-1:
104 | mid = int(math.floor((top+bottom)/2.0))
105 | if li[mid]==element:
106 | index = mid
107 | elif li[mid]>element:
108 | top = mid-1
109 | else:
110 | bottom = mid+1
111 |
112 | return index
113 |
114 | li=[2,5,7,9,11,17,222]
115 | print bin_search(li,11)
116 | print bin_search(li,12)
117 |
118 | ```
119 |
120 | ---
121 |
122 | **My Solution: Python 3**
123 |
124 | ```python
125 | #to be written
126 |
127 | ```
128 | **Solution by ulmasovjafarbek: Python 3**
129 | ```python
130 | def binary_search(lst, item):
131 | low = 0
132 | high = len(lst) - 1
133 |
134 | while low <= high:
135 | mid = round((low + high) / 2)
136 |
137 | if lst[mid] == item:
138 | return mid
139 | elif lst[mid] > item:
140 | high = mid - 1
141 | else:
142 | low = mid + 1
143 | return None
144 |
145 | lst = [1,3,5,7,]
146 | print(binary_search(lst, 9))
147 | ```
148 | ---
149 |
150 | **Solution by AasaiAlangaram: Python 3**
151 |
152 | ```python
153 | def binary_search_Ascending(array, target):
154 | lower = 0
155 | upper = len(array)
156 | print('Array Length:',upper)
157 | while lower < upper:
158 | x = (lower + upper) // 2
159 | print('Middle Value:',x)
160 | value = array[x]
161 | if target == value:
162 | return x
163 | elif target > value:
164 | lower = x
165 | elif target < value:
166 | upper = x
167 |
168 | Array = [1,5,8,10,12,13,55,66,73,78,82,85,88,99]
169 | print('The Value Found at Index:',binary_search_Ascending(Array, 82))
170 |
171 | ```
172 |
173 | ---
174 |
175 | **Solution by yuan1z: Python 3**
176 |
177 | ```python
178 | idx = 0
179 | def bs(num,num_list):
180 | global idx
181 | if (len(num_list) == 1):
182 | if num_list[0] == num:
183 | return idx
184 | else:
185 | return "No exit in the list"
186 | elif num in num_list[:len(num_list)//2]:
187 | return bs(num,num_list[:len(num_list)//2])
188 | else:
189 | idx += len(num_list)//2
190 | return bs(num,num_list[len(num_list)//2:])
191 |
192 | print(bs(66,[1,5,8,10,12,13,55,66,73,78,82,85,88,99,100]))
193 |
194 | ```
195 |
196 | ---
197 |
198 | # Question 68
199 |
200 | ### **Question**
201 |
202 | > **_Please generate a random float where the value is between 10 and 100 using Python module._**
203 |
204 | ---
205 |
206 | ### Hints
207 |
208 | > **_Use random.random() to generate a random float in [0,1]._**
209 |
210 | ---
211 |
212 | **Main author's Solution: Python 2**
213 |
214 | ```python
215 | import random
216 | print random.random()*100
217 | ```
218 |
219 | ---
220 |
221 | **My Solution: Python 3**
222 |
223 | ```python
224 | import random
225 | rand_num = random.uniform(10,100)
226 | print(rand_num)
227 | ```
228 |
229 | ---
230 |
231 | # Question 69
232 |
233 | ### **Question**
234 |
235 | > **_Please generate a random float where the value is between 5 and 95 using Python module._**
236 |
237 | ---
238 |
239 | ### Hints
240 |
241 | > **_Use random.random() to generate a random float in [0,1]._**
242 |
243 | ---
244 |
245 | **Main author's Solution: Python 2**
246 |
247 | ```python
248 | import random
249 | print random.random()*100-5
250 | ```
251 |
252 | ---
253 |
254 | **My Solution: Python 3**
255 |
256 | ```python
257 | import random
258 | rand_num = random.uniform(5,95)
259 | print(rand_num)
260 | ```
261 |
262 | ---
263 |
264 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_16.md "Day 16")
265 |
266 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_18.md "Day 18")
267 |
268 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
269 |
--------------------------------------------------------------------------------
/Status/Day_18.md:
--------------------------------------------------------------------------------
1 | # Question 70
2 |
3 | ### **Question**
4 |
5 | > **_Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension._**
6 |
7 | ---
8 |
9 | ### Hints
10 |
11 | > **_Use random.choice() to a random element from a list._**
12 |
13 | ---
14 |
15 | **Main author's Solution: Python 2**
16 |
17 | ```python
18 | li = [2,4,6,8]
19 | import random
20 | print random.choice([i for i in range(11) if i%2==0])
21 | ```
22 |
23 | ---
24 |
25 | **My Solution: Python 3**
26 |
27 | ```python
28 | import random
29 | resp = [i for i in range(0,11,2)]
30 | print(random.choice(resp))
31 | ```
32 |
33 | ---
34 |
35 | # Question 71
36 |
37 | ### **Question**
38 |
39 | > **_Please write a program to output a random number, which is divisible by 5 and 7, between 10 and 150 inclusive using random module and list comprehension._**
40 |
41 | ---
42 |
43 | ### Hints
44 |
45 | > **_Use random.choice() to a random element from a list._**
46 |
47 | ---
48 |
49 | **Main author's Solution: Python 2**
50 |
51 | ```python
52 | import random
53 | print random.choice([i for i in range(10,151) if i%5==0 and i%7==0])
54 | ```
55 |
56 | ---
57 |
58 | **My Solution: Python 3**
59 |
60 | ```python
61 | import random
62 | resp = [i for i in range(10,151) if i % 35 == 0 ]
63 | print(random.choice(resp))
64 | ```
65 |
66 | ---
67 |
68 | # Question 72
69 |
70 | ### **Question**
71 |
72 | > **_Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive._**
73 |
74 | ---
75 |
76 | ### Hints
77 |
78 | > **_Use random.sample() to generate a list of random values._**
79 |
80 | ---
81 |
82 | **Main author's Solution: Python 2**
83 |
84 | ```python
85 |
86 | import random
87 | print random.sample(range(100,201), 5)
88 | ```
89 |
90 | ---
91 |
92 | **My Solution: Python 3**
93 |
94 | ```python
95 | import random
96 | resp = random.sample(range(100,201),5)
97 | print(resp)
98 | ```
99 |
100 | ---
101 |
102 | # Question 73
103 |
104 | ### **Question**
105 |
106 | > **_Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive._**
107 |
108 | ---
109 |
110 | ### Hints
111 |
112 | > **_Use random.sample() to generate a list of random values._**
113 |
114 | ---
115 |
116 | **Main author's Solution: Python 2**
117 |
118 | ```python
119 |
120 | import random
121 | print random.sample([i for i in range(100,201) if i%2==0], 5)
122 |
123 | ```
124 |
125 | ---
126 |
127 | **My Solution: Python 3**
128 |
129 | ```python
130 | import random
131 | resp = random.sample(range(100,201,2),5)
132 | print(resp)
133 | ```
134 |
135 | ---
136 |
137 | # Question 74
138 |
139 | ### **Question**
140 |
141 | > **_Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive._**
142 |
143 | ---
144 |
145 | ### Hints
146 |
147 | > **_Use random.sample() to generate a list of random values._**
148 |
149 | ---
150 |
151 | **Main author's Solution: Python 2**
152 |
153 | ```python
154 |
155 | import random
156 | print random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5)
157 | ```
158 |
159 | ---
160 |
161 | **My Solution: Python 3**
162 |
163 | ```python
164 | import random
165 | lst = [i for i in range(1,1001) if i%35 == 0]
166 | resp = random.sample(lst,5)
167 | print(resp)
168 | ```
169 |
170 | ---
171 |
172 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_17.md "Day 17")
173 |
174 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_19.md "Day 19")
175 |
176 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
177 |
--------------------------------------------------------------------------------
/Status/Day_19.md:
--------------------------------------------------------------------------------
1 | # Question 75
2 |
3 | ### **Question**
4 |
5 | > **_Please write a program to randomly print a integer number between 7 and 15 inclusive._**
6 |
7 | ---
8 |
9 | ### Hints
10 |
11 | > **_Use random.randrange() to a random integer in a given range._**
12 |
13 | ---
14 |
15 | **Solution:**
16 |
17 | ```python
18 | import random
19 | print random.randrange(7,16)
20 | ```
21 |
22 | ---
23 |
24 | # Question 76
25 |
26 | ### **Question**
27 |
28 | > **_Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!"._**
29 |
30 | ---
31 |
32 | ### Hints
33 |
34 | > **_Use zlib.compress() and zlib.decompress() to compress and decompress a string._**
35 |
36 | ---
37 |
38 | **Solution:**
39 |
40 | ```python
41 | import zlib
42 | s = 'hello world!hello world!hello world!hello world!'
43 | t = zlib.compress(s)
44 | print t
45 | print zlib.decompress(t)
46 | ```
47 | ---
48 | ```python
49 | '''Solution by: anas1434
50 | '''
51 | s = 'hello world!hello world!hello world!hello world!'
52 | # In Python 3 zlib.compress() accepts only DataType
53 | y = bytes(s, 'utf-8')
54 | x = zlib.compress(y)
55 | print(x)
56 | print(zlib.decompress(x))
57 | ```
58 | ---
59 |
60 | # Question 77
61 |
62 | ### **Question**
63 |
64 | > **_Please write a program to print the running time of execution of "1+1" for 100 times._**
65 |
66 | ---
67 |
68 | ### Hints
69 |
70 | > **_Use timeit() function to measure the running time._**
71 |
72 | ---
73 |
74 | **Main author's Solution: Python 2**
75 |
76 | ```python
77 |
78 | from timeit import Timer
79 | t = Timer("for i in range(100):1+1")
80 | print t.timeit()
81 | ```
82 |
83 | ---
84 |
85 | **My Solution: Python 3**
86 |
87 | ```python
88 | import datetime
89 |
90 | before = datetime.datetime.now()
91 | for i in range(100):
92 | x = 1 + 1
93 | after = datetime.datetime.now()
94 | execution_time = after - before
95 | print(execution_time.microseconds)
96 | ```
97 |
98 | **OR**
99 |
100 | ```python
101 | import time
102 |
103 | before = time.time()
104 | for i in range(100):
105 | x = 1 + 1
106 | after = time.time()
107 | execution_time = after - before
108 | print(execution_time)
109 | ```
110 |
111 | ---
112 |
113 | # Question 78
114 |
115 | ### **Question**
116 |
117 | > **_Please write a program to shuffle and print the list [3,6,7,8]._**
118 |
119 | ---
120 |
121 | ### Hints
122 |
123 | > **_Use shuffle() function to shuffle a list._**
124 |
125 | ---
126 |
127 | **Main author's Solution: Python 2**
128 |
129 | ```python
130 |
131 | from random import shuffle
132 | li = [3,6,7,8]
133 | shuffle(li)
134 | print li
135 |
136 | ```
137 |
138 | ---
139 |
140 | **My Solution: Python 3**
141 |
142 | ```python
143 | import random
144 |
145 | lst = [3,6,7,8]
146 | random.shuffle(lst)
147 | print(lst)
148 | ```
149 |
150 | **OR**
151 |
152 | ```python
153 | import random
154 |
155 | # shuffle with a chosen seed
156 | lst = [3,6,7,8]
157 | seed = 7
158 | random.Random(seed).shuffle(lst)
159 | print(lst)
160 | ```
161 |
162 | ---
163 |
164 | # Question 79
165 |
166 | ### **Question**
167 |
168 | > **_Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"]._**
169 |
170 | ---
171 |
172 | ### Hints
173 |
174 | > **_Use list[index] notation to get a element from a list._**
175 |
176 | ---
177 |
178 | **Main author's Solution: Python 2**
179 |
180 | ```python
181 |
182 | subjects=["I", "You"]
183 | verbs=["Play", "Love"]
184 | objects=["Hockey","Football"]
185 | for i in range(len(subjects)):
186 | for j in range(len(verbs)):
187 | for k in range(len(objects)):
188 | sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k])
189 | print sentence
190 | ```
191 |
192 | ---
193 |
194 | **My Solution: Python 3**
195 |
196 | ```python
197 | subjects=["I", "You"]
198 | verbs=["Play", "Love"]
199 | objects=["Hockey","Football"]
200 |
201 | for sub in subjects:
202 | for verb in verbs:
203 | for obj in objects:
204 | print("{} {} {}".format(sub,verb,obj))
205 | ```
206 | ---
207 | ```python
208 |
209 | '''Solution by: popomaticbubble
210 | '''
211 | import itertools
212 | subject = ["I", "You"]
213 | verb = ["Play", "Love"]
214 | objects = ["Hockey","Football"]
215 |
216 | sentence = [subject, verb, objects]
217 | n = list(itertools.product(*sentence))
218 | for i in n:
219 | print(i)
220 | ```
221 | ---
222 | ```python
223 |
224 | '''Solution by: lcastrooliveira
225 | '''
226 | from itertools import product
227 |
228 | def question_79():
229 | subject = ["I", "You"]
230 | verb = ["Play", "Love"]
231 | object = ["Hockey", "Football"]
232 | prod = [p for p in product(range(2), repeat=3)]
233 | for combination in prod:
234 | print(f'{subject[combination[0]]} {verb[combination[1]]} {object[combination[2]]}')
235 |
236 | question_79()
237 | ```
238 |
239 | ---
240 |
241 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_18.md "Day 18")
242 |
243 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_20.md "Day 20")
244 |
245 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
246 |
--------------------------------------------------------------------------------
/Status/Day_20.md:
--------------------------------------------------------------------------------
1 | # Question 80
2 |
3 | ### **Question**
4 |
5 | > **_Please write a program to print the list after removing even numbers in [5,6,77,45,22,12,24]._**
6 |
7 | ---
8 |
9 | ### Hints
10 |
11 | > **_Use list comprehension to delete a bunch of element from a list._**
12 |
13 | ---
14 |
15 | **Main author's Solution: Python 2**
16 |
17 | ```python
18 | li = [5,6,77,45,22,12,24]
19 | li = [x for x in li if x%2!=0]
20 | print li
21 | ```
22 |
23 | ---
24 |
25 | **My Solution: Python 3**
26 |
27 | ```python
28 | def isEven(n):
29 | return n%2!=0
30 |
31 | li = [5,6,77,45,22,12,24]
32 | lst = list(filter(isEven,li))
33 | print(lst)
34 | ```
35 |
36 | **OR**
37 |
38 | ```python
39 | li = [5,6,77,45,22,12,24]
40 | lst = list(filter(lambda n:n%2!=0,li))
41 | print(lst)
42 | ```
43 |
44 | ---
45 |
46 | # Question 81
47 |
48 | ### **Question**
49 |
50 | > **_By using list comprehension, please write a program to print the list after removing numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155]._**
51 |
52 | ---
53 |
54 | ### Hints
55 |
56 | > **_Use list comprehension to delete a bunch of element from a list._**
57 |
58 | ---
59 |
60 | **Main author's Solution: Python 2**
61 |
62 | ```python
63 | li = [12,24,35,70,88,120,155]
64 | li = [x for x in li if x%5!=0 and x%7!=0]
65 | print li
66 | ```
67 |
68 | ---
69 |
70 | **My Solution: Python 3**
71 |
72 | ```python
73 | li = [12,24,35,70,88,120,155]
74 | li = [x for x in li if x % 35!=0]
75 | print(li)
76 | ```
77 |
78 | ---
79 |
80 | # Question 82
81 |
82 | ### **Question**
83 |
84 | > **_By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155]._**
85 |
86 | ---
87 |
88 | ### Hints
89 |
90 | > **_Use list comprehension to delete a bunch of element from a list.
91 | > Use enumerate() to get (index, value) tuple._**
92 |
93 | ---
94 |
95 | **Main author's Solution: Python 2**
96 |
97 | ```python
98 |
99 | li = [12,24,35,70,88,120,155]
100 | li = [x for (i,x) in enumerate(li) if i%2 != 0 and i <= 6]
101 | print li
102 | ```
103 |
104 | ---
105 |
106 | **My Solution: Python 3**
107 |
108 | ```python
109 | li = [12,24,35,70,88,120,155]
110 | li = [li[i] for i in range(len(li)) if i%2 != 0 and i <= 6]
111 | print(li)
112 | ```
113 | ---
114 | ```python
115 | '''Solution by: popomaticbubble
116 | '''
117 | orig_lst = [12,24,35,70,88,120,155]
118 | indices = [0, 2, 4, 6]
119 |
120 | new_list = [i for (j, i) in enumerate(orig_lst) if j not in indices]
121 | print(new_list)
122 | ```
123 | ---
124 |
125 | # Question 83
126 |
127 | ### **Question**
128 |
129 | > **_By using list comprehension, please write a program to print the list after removing the 2nd - 4th numbers in [12,24,35,70,88,120,155]._**
130 |
131 | ---
132 |
133 | ### Hints
134 |
135 | > **_Use list comprehension to delete a bunch of element from a list.
136 | > Use enumerate() to get (index, value) tuple._**
137 |
138 | ---
139 |
140 | **Main author's Solution: Python 2**
141 |
142 | ```python
143 |
144 | li = [12,24,35,70,88,120,155]
145 | li = [x for (i,x) in enumerate(li) if i<3 or 4 4]
156 | print(li)
157 | ```
158 | ---
159 | ```python
160 | """Solution by: popomaticbubble
161 | """
162 | orig_list = [12,24,35,70,88,120,155]
163 | new_list = [i for (j, i) in enumerate(orig_list) if j not in range(1,4)]
164 | print(new_list)
165 | ```
166 | ---
167 | ```python
168 | """Solution by: saxenaharsh24
169 | """
170 | lst = [12,24,35,70,88,120,155]
171 | print([i for i in lst if lst.index(i) not in range(2,5)])
172 | ```
173 | ---
174 | # Question 84
175 |
176 | ### **Question**
177 |
178 | > **_By using list comprehension, please write a program generate a 3\*5\*8 3D array whose each element is 0._**
179 |
180 | ---
181 |
182 | ### Hints
183 |
184 | > **_Use list comprehension to make an array._**
185 |
186 | ---
187 |
188 | **Solution:**
189 |
190 | ```python
191 | array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)]
192 | print array
193 | ```
194 |
195 | ---
196 |
197 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_19.md "Day 19")
198 |
199 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_21.md "Day 21")
200 |
201 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
202 |
--------------------------------------------------------------------------------
/Status/Day_21.md:
--------------------------------------------------------------------------------
1 | # Question 85
2 |
3 | ### **Question**
4 |
5 | > **_By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155]._**
6 |
7 | ---
8 |
9 | ### Hints
10 |
11 | > **_Use list comprehension to delete a bunch of element from a list.Use enumerate() to get (index, value) tuple._**
12 |
13 | ---
14 |
15 | **Main author's Solution: Python 2**
16 |
17 | ```python
18 | li = [12,24,35,70,88,120,155]
19 | li = [x for (i,x) in enumerate(li) if i not in (0,4,5)]
20 | print li
21 | ```
22 | ---
23 |
24 | **My Solution: Python 3**
25 |
26 | ```python
27 | li = [12,24,35,70,88,120,155]
28 | li = [li[i] for i in range(len(li)) if i not in (0,4,5)]
29 | print(li)
30 | ```
31 | ---
32 | ```python
33 | '''Solution by: pratikb0501
34 | '''
35 | li = [12, 24, 35, 70, 88, 120, 155]
36 | print(list(j for i, j in enumerate(li) if i != 0 and i != 4 and i != 5))
37 |
38 | ```
39 |
40 | ---
41 |
42 | # Question 86
43 |
44 | ### **Question**
45 |
46 | > **_By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155]._**
47 |
48 | ---
49 |
50 | ### Hints
51 |
52 | > **_Use list's remove method to delete a value._**
53 |
54 | ---
55 |
56 | **Main author's Solution: Python 2**
57 |
58 | ```python
59 | li = [12,24,35,24,88,120,155]
60 | li = [x for x in li if x!=24]
61 | print li
62 | ```
63 |
64 | ---
65 |
66 | **My Solution: Python 3**
67 |
68 | ```python
69 | li = [12,24,35,24,88,120,155]
70 | li.remove(24) # this will remove only the first occurrence of 24
71 | print(li)
72 | ```
73 |
74 | ---
75 |
76 | # Question 87
77 |
78 | ### **Question**
79 |
80 | > **_With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists._**
81 |
82 | ---
83 |
84 | ### Hints
85 |
86 | > **_Use set() and "&=" to do set intersection operation._**
87 |
88 | ---
89 |
90 | **Main author's Solution: Python 2**
91 |
92 | ```python
93 |
94 | set1=set([1,3,6,78,35,55])
95 | set2=set([12,24,35,24,88,120,155])
96 | set1 &= set2
97 | li=list(set1)
98 | print li
99 | ```
100 |
101 | ---
102 |
103 | **My Solution: Python 3**
104 |
105 | ```python
106 | list1 = [1,3,6,78,35,55]
107 | list2 = [12,24,35,24,88,120,155]
108 | set1= set(list1)
109 | set2= set(list2)
110 | intersection = set1 & set2
111 | print(intersection)
112 | ```
113 |
114 | **OR**
115 |
116 | ```python
117 | list1 = [1,3,6,78,35,55]
118 | list2 = [12,24,35,24,88,120,155]
119 | set1= set(list1)
120 | set2= set(list2)
121 | intersection = set.intersection(set1,set2)
122 | print(intersection)
123 | ```
124 |
125 | ---
126 |
127 | # Question 88
128 |
129 | ### **Question**
130 |
131 | > **_With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved._**
132 |
133 | ---
134 |
135 | ### Hints
136 |
137 | > **_Use set() to store a number of values without duplicate._**
138 |
139 | ---
140 |
141 | **Main author's Solution: Python 2**
142 |
143 | ```python
144 | def removeDuplicate( li ):
145 | newli=[]
146 | seen = set()
147 | for item in li:
148 | if item not in seen:
149 | seen.add( item )
150 | newli.append(item)
151 |
152 | return newli
153 |
154 | li=[12,24,35,24,88,120,155,88,120,155]
155 | print removeDuplicate(li)
156 |
157 | ```
158 |
159 | ---
160 |
161 | **My Solution: Python 3**
162 |
163 | ```python
164 | li = [12,24,35,24,88,120,155,88,120,155]
165 | for i in li:
166 | if li.count(i) > 1:
167 | li.remove(i)
168 | print(li)
169 | ```
170 |
171 | **OR**
172 |
173 | ```python
174 | def removeDuplicate( li ):
175 | seen = {} # dictionary
176 | for item in li:
177 | if item not in seen:
178 | seen[item] = True
179 | yield item
180 |
181 | li = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155]
182 | ans = list(removeDuplicate(li))
183 | print(ans)
184 | ```
185 |
186 | ---
187 |
188 | # Question 89
189 |
190 | ### **Question**
191 |
192 | > **_Define a class Person and its two child classes: Male and Female. All classes have a method "getGender" which can print "Male" for Male class and "Female" for Female class._**
193 |
194 | ---
195 |
196 | ### Hints
197 |
198 | > **_Use Subclass(Parentclass) to define a child class._**
199 |
200 | ---
201 |
202 | **Solution:**
203 |
204 | ```python
205 | class Person(object):
206 | def getGender( self ):
207 | return "Unknown"
208 |
209 | class Male( Person ):
210 | def getGender( self ):
211 | return "Male"
212 |
213 | class Female( Person ):
214 | def getGender( self ):
215 | return "Female"
216 |
217 | aMale = Male()
218 | aFemale= Female()
219 | print aMale.getGender()
220 | print aFemale.getGender()
221 | ```
222 | ---
223 | ```python
224 | '''Solution by: popomaticbubble
225 | '''
226 | class Person(object):
227 | def __init__(self):
228 | self.gender = "unknown"
229 |
230 | def getGender(self):
231 | print(self.gender)
232 |
233 | class Male(Person):
234 | def __init__(self):
235 | self.gender = "Male"
236 |
237 | class Female(Person):
238 | def __init__(self):
239 | self.gender = "Female"
240 |
241 | sharon = Female()
242 | doug = Male()
243 | sharon.getGender()
244 | doug.getGender()
245 |
246 | ```
247 | ---
248 |
249 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_20.md "Day 20")
250 |
251 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_22.md "Day 22")
252 |
253 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
254 |
--------------------------------------------------------------------------------
/Status/Day_22.md:
--------------------------------------------------------------------------------
1 | # Question 90
2 |
3 | ### **Question**
4 |
5 | > **_Please write a program which count and print the numbers of each character in a string input by console._**
6 |
7 | > **_Example:
8 | > If the following string is given as input to the program:_**
9 |
10 | ```
11 | abcdefgabc
12 | ```
13 |
14 | > **_Then, the output of the program should be:_**
15 |
16 | ```
17 | a,2
18 | c,2
19 | b,2
20 | e,1
21 | d,1
22 | g,1
23 | f,1
24 | ```
25 |
26 | ### Hints
27 |
28 | > **_Use dict to store key/value pairs.
29 | > Use dict.get() method to lookup a key with default value._**
30 |
31 | ---
32 |
33 | **Main author's Solution: Python 2**
34 |
35 | ```python
36 | dic = {}
37 | s=raw_input()
38 | for s in s:
39 | dic[s] = dic.get(s,0)+1
40 | print '\n'.join(['%s,%s' % (k, v) for k, v in dic.items()])
41 | ```
42 |
43 | ---
44 |
45 | **My Solution: Python 3**
46 |
47 | ```python
48 | import string
49 |
50 | s = input()
51 | for letter in string.ascii_lowercase:
52 | cnt = s.count(letter)
53 | if cnt > 0:
54 | print("{},{}".format(letter,cnt))
55 | ```
56 |
57 | **OR**
58 |
59 | ```python
60 | s = input()
61 | for letter in range(ord('a'),ord('z')+1): # ord() gets the ascii value of a char
62 | letter = chr(letter) # chr() gets the char of an ascii value
63 | cnt = s.count(letter)
64 | if cnt > 0:
65 | print("{},{}".format(letter,cnt))
66 | ```
67 | ---
68 | ```python
69 | '''Solution by: Utkarsh4697
70 | '''
71 | s = 'abcdefgabc'
72 | for i in sorted(set(s)):
73 | print(f'{i}, {s.count(i)}')
74 | ```
75 | ---
76 | ```python
77 | '''Solution by: popomaticbubble
78 | '''
79 | def character_counter(text):
80 | characters_list = list(text)
81 | char_count = {}
82 | for x in characters_list:
83 | if x in char_count.keys():
84 | char_count[x] += 1
85 | else:
86 | char_count[x] = 1
87 | return char_count
88 |
89 |
90 | def dict_viewer(dictionary):
91 | for x, y in dictionary.items():
92 | print(f"{x},{y}")
93 |
94 |
95 | text = input("> ")
96 | dict_viewer(character_counter(text))
97 | ```
98 | ---
99 |
100 | # Question 91
101 |
102 | ### **Question**
103 |
104 | > **_Please write a program which accepts a string from console and print it in reverse order._**
105 |
106 | > **Example:
107 | > If the following string is given as input to the program:\***
108 |
109 | ```
110 | rise to vote sir
111 | ```
112 |
113 | > **_Then, the output of the program should be:_**
114 |
115 | ```
116 | ris etov ot esir
117 | ```
118 |
119 | ### Hints
120 |
121 | > **_Use list[::-1] to iterate a list in a reverse order._**
122 |
123 | ---
124 |
125 | **Main author's Solution: Python 2**
126 |
127 | ```python
128 | s=raw_input()
129 | s = s[::-1]
130 | print s
131 | ```
132 |
133 | ---
134 |
135 | **My Solution: Python 3**
136 |
137 | ```python
138 | s = input()
139 | s = ''.join(reversed(s))
140 | print(s)
141 | ```
142 |
143 | ---
144 |
145 | # Question 92
146 |
147 | ### **Question**
148 |
149 | > **_Please write a program which accepts a string from console and print the characters that have even indexes._**
150 |
151 | > **_Example:
152 | > If the following string is given as input to the program:_**
153 |
154 | ```
155 | H1e2l3l4o5w6o7r8l9d
156 | ```
157 |
158 | > **_Then, the output of the program should be:_**
159 |
160 | ```
161 | Helloworld
162 | ```
163 |
164 | ### Hints
165 |
166 | > **_Use list[::2] to iterate a list by step 2._**
167 |
168 | ---
169 |
170 | **Main author's Solution: Python 2**
171 |
172 | ```python
173 | s=raw_input()
174 | s = s[::2]
175 | print s
176 | ```
177 |
178 | ---
179 |
180 | **My Solution: Python 3**
181 |
182 | ```python
183 | s = "H1e2l3l4o5w6o7r8l9d"
184 | s = [ s[i] for i in range(len(s)) if i%2 ==0 ]
185 | print(''.join(s))
186 | ```
187 |
188 | **OR**
189 |
190 | ```python
191 | s = "H1e2l3l4o5w6o7r8l9d"
192 | ns =''
193 | for i in range(len(s)):
194 | if i % 2 == 0:
195 | ns+=s[i]
196 | print(ns)
197 | ```
198 |
199 | ---
200 |
201 | # Question 93
202 |
203 | ### **Question**
204 |
205 | > **_Please write a program which prints all permutations of [1,2,3]_**
206 |
207 | ---
208 |
209 | ### Hints
210 |
211 | > **_Use itertools.permutations() to get permutations of list._**
212 |
213 | ---
214 |
215 | **Solution:**
216 |
217 | ```python
218 |
219 | import itertools
220 | print list(itertools.permutations([1,2,3]))
221 | ```
222 | ---
223 | ```python
224 | """Solution by: popomaticbubble
225 | """
226 | from itertools import permutations
227 |
228 | def permuation_generator(iterable):
229 | p = permutations(iterable)
230 | for i in p:
231 | print(i)
232 |
233 |
234 | x = [1,2,3]
235 | permuation_generator(x)
236 | ```
237 | ---
238 |
239 | # Question 94
240 |
241 | ### **Question**
242 |
243 | > **_Write a program to solve a classic ancient Chinese puzzle:
244 | > We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?_**
245 |
246 | ---
247 |
248 | ### Hints
249 |
250 | > **_Use for loop to iterate all possible solutions._**
251 |
252 | ---
253 |
254 | **Solution:**
255 |
256 | ```python
257 | def solve(numheads,numlegs):
258 | ns='No solutions!'
259 | for i in range(numheads+1):
260 | j=numheads-i
261 | if 2*i+4*j==numlegs:
262 | return i,j
263 | return ns,ns
264 |
265 | numheads = 35
266 | numlegs = 94
267 | solutions=solve(numheads,numlegs)
268 | print solutions
269 | ```
270 | ---
271 | ```python
272 | """Solution by: popomaticbubble
273 | """
274 | import itertools
275 |
276 | def animal_counter(lst):
277 | chickens = 0
278 | rabbits = 0
279 | for i in lst:
280 | if i == 2:
281 | chickens += 1
282 | elif i == 4:
283 | rabbits += 1
284 | print(f"Number of chickens is {chickens}\nNumber of rabbits is {rabbits}")
285 |
286 |
287 | def animal_calculator(total_legs, total_heads, legs_of_each_species):
288 | combinations = itertools.combinations_with_replacement(legs_of_each_species, total_heads)
289 | correct_combos = []
290 | for i in list(combinations):
291 | if sum(i) == total_legs:
292 | correct_combos.append(i)
293 | print(correct_combos)
294 | for i in correct_combos:
295 | animal_counter(i)
296 |
297 | animal_calculator(94, 35, legs_of_each_species=[2,4])
298 | ```
299 | ---
300 |
301 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_21.md "Day 21")
302 |
303 | [**_go to next day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_23.md "Day 23")
304 |
305 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
306 |
--------------------------------------------------------------------------------
/Status/Day_24.md:
--------------------------------------------------------------------------------
1 | # Question 100
2 |
3 | ### **Question**
4 |
5 | > **_You are given words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification._**
6 |
7 | > **_If the following string is given as input to the program:_**
8 | >
9 | > ```
10 | > 4
11 | > bcdef
12 | > abcdefg
13 | > bcde
14 | > bcdef
15 | > ```
16 | >
17 | > **_Then, the output of the program should be:_**
18 | >
19 | > ```
20 | > 3
21 | > 2 1 1
22 | > ```
23 |
24 | ### Hints
25 |
26 | > **_Make a list to get the input order and a dictionary to count the word frequency_**
27 |
28 | ---
29 |
30 | **My Solution: Python 3**
31 |
32 | ```python
33 | n = int(input())
34 |
35 | word_list = []
36 | word_dict = {}
37 |
38 | for i in range(n):
39 | word = input()
40 | if word not in word_dict:
41 | word_list.append(word)
42 | word_dict[word] = word_dict.get(word, 0) + 1
43 |
44 | print(len(word_list))
45 | for word in word_list:
46 | print(word_dict[word], end=' ')
47 | ```
48 |
49 | ---
50 |
51 | # Question 101
52 |
53 | ### **Question**
54 |
55 | > **_You are given a string.Your task is to count the frequency of letters of the string and print the letters in descending order of frequency._**
56 |
57 | > **_If the following string is given as input to the program:_**
58 | >
59 | > ```
60 | > aabbbccde
61 | > ```
62 | >
63 | > **_Then, the output of the program should be:_**
64 | >
65 | > ```
66 | > b 3
67 | > a 2
68 | > c 2
69 | > d 1
70 | > e 1
71 | > ```
72 |
73 | ### Hints
74 |
75 | > **_Count frequency with dictionary and sort by Value from dictionary Items_**
76 |
77 | ---
78 |
79 | **My Solution: Python 3**
80 |
81 | ```python
82 | word = input()
83 | dct = {}
84 | for i in word:
85 | dct[i] = dct.get(i,0) + 1
86 |
87 | dct = sorted(dct.items(),key=lambda x: (-x[1],x[0]))
88 | for i in dct:
89 | print(i[0],i[1])
90 | ```
91 |
92 | ---
93 |
94 | ```python
95 | '''Solution by: yuan1z'''
96 |
97 | X = input()
98 | my_set = set(X)
99 | arr = []
100 | for item in my_set:
101 | arr.append([item,X.count(item)])
102 | tmp = sorted(arr,key = lambda x: (-x[1],x[0]))
103 |
104 | for i in tmp:
105 | print(i[0]+' '+str(i[1]))
106 | ```
107 |
108 | ---
109 |
110 | ```python
111 | '''Solution by: StartZer0'''
112 |
113 | s = list(input())
114 |
115 | dict_count_ = {k:s.count(k) for k in s}
116 | list_of_tuples = [(k,v) for k,v in dict_count_.items()]
117 | list_of_tuples.sort(key = lambda x: x[1], reverse = True)
118 |
119 | for item in list_of_tuples:
120 | print(item[0], item[1])
121 | ```
122 |
123 | ---
124 |
125 | # Question 102
126 |
127 | ### **Question**
128 |
129 | > **_Write a Python program that accepts a string and calculate the number of digits and letters._**
130 |
131 | **Input**
132 |
133 | > ```
134 | > Hello321Bye360
135 | > ```
136 |
137 | **Output**
138 |
139 | > ```
140 | > Digit - 6
141 | > Letter - 8
142 | > ```
143 |
144 | ---
145 |
146 | ### Hints
147 |
148 | > **_Use isdigit() and isalpha() function_**
149 |
150 | ---
151 |
152 | **Solution:**
153 |
154 | ```python
155 | word = input()
156 | digit,letter = 0,0
157 | for char in word:
158 | digit+=char.isdigit()
159 | letter+=char.isalpha()
160 |
161 | print('Digit -',digit)
162 | print('Letter -',letter)
163 | ```
164 |
165 | ---
166 |
167 | # Question 103
168 |
169 | ### **Question**
170 |
171 | > **_Given a number N.Find Sum of 1 to N Using Recursion_**
172 |
173 | **Input**
174 |
175 | > ```
176 | > 5
177 | > ```
178 |
179 | **Output**
180 |
181 | > ```
182 | > 15
183 | > ```
184 |
185 | ---
186 |
187 | ### Hints
188 |
189 | > **_Make a recursive function to get the sum_**
190 |
191 | ---
192 |
193 | **Solution:**
194 |
195 | ```python
196 | def rec(n):
197 | if n == 0:
198 | return n
199 | return rec(n-1) + n
200 |
201 |
202 | n = int(input())
203 | sum = rec(n)
204 | print(sum)
205 | ```
206 | ---
207 | ```python
208 | """Solution by: popomaticbubble
209 | """
210 | def summer(counter, n, current):
211 | if n == 0:
212 | return 0
213 | if counter == n:
214 | return current+n
215 | else:
216 | current = current + counter
217 | counter += 1
218 | return summer(counter, n, current)
219 |
220 |
221 | N = int(input("> "))
222 | print(summer(1, N, 0))
223 | ```
224 | ---
225 |
226 | [**_go to previous day_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_22.md "Day 23")
227 |
228 | [**_Discussion_**](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3)
229 |
230 | # To Be Continue...
231 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-slate
--------------------------------------------------------------------------------
/notebooks/Day_01.ipynb:
--------------------------------------------------------------------------------
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 | "\n",
23 | "\n",
24 | "**Solutions:**\n",
25 | "\n",
26 | "- **Using for loops**"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": [
35 | "for i in range(2000, 3201):\n",
36 | " if i % 7 == 0 and i % 5 != 0:\n",
37 | " print(i, end=\",\")\n",
38 | "print(\"\\b\")"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {},
44 | "source": [
45 | "---\n",
46 | "\n",
47 | "- **Using generators and list comprehension**"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": [
56 | "print(*(i for i in range(2000, 3201) if i % 7 == 0 and i % 5 != 0), sep=\",\")"
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | "# Question 2\n",
64 | "\n",
65 | "### **Question:**\n",
66 | "\n",
67 | "> **_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",
68 | "> Then, the output should be:40320_**\n",
69 | "\n",
70 | "---\n",
71 | "\n",
72 | "### Hints:\n",
73 | "\n",
74 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
75 | "\n",
76 | "---\n",
77 | "\n",
78 | "\n",
79 | "\n",
80 | "**Solutions:**\n"
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {},
86 | "source": [
87 | "- **Using While Loop**"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {},
94 | "outputs": [],
95 | "source": [
96 | "n = int(input()) # input() function takes input as string type\n",
97 | "# int() converts it to integer type\n",
98 | "fact = 1\n",
99 | "i = 1\n",
100 | "while i <= n:\n",
101 | " fact = fact * i\n",
102 | " i = i + 1\n",
103 | "print(fact)"
104 | ]
105 | },
106 | {
107 | "cell_type": "markdown",
108 | "metadata": {},
109 | "source": [
110 | "- **Using For Loop**"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": null,
116 | "metadata": {},
117 | "outputs": [],
118 | "source": [
119 | "n = int(input()) # input() function takes input as string type\n",
120 | "# int() converts it to integer type\n",
121 | "fact = 1\n",
122 | "for i in range(1, n + 1):\n",
123 | " fact = fact * i\n",
124 | "print(fact)"
125 | ]
126 | },
127 | {
128 | "cell_type": "markdown",
129 | "metadata": {},
130 | "source": [
131 | "- **Using Lambda Function**"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": null,
137 | "metadata": {},
138 | "outputs": [],
139 | "source": [
140 | "# Solution by: harshraj22\n",
141 | "n = int(input())\n",
142 | "\n",
143 | "\n",
144 | "def shortFact(x):\n",
145 | " return 1 if x <= 1 else x * shortFact(x - 1)\n",
146 | "\n",
147 | "\n",
148 | "print(shortFact(n))"
149 | ]
150 | },
151 | {
152 | "cell_type": "markdown",
153 | "metadata": {},
154 | "source": [
155 | "# Question 3\n",
156 | "\n",
157 | "### **Question:**\n",
158 | "\n",
159 | "> **_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",
160 | "\n",
161 | "> **_Then, the output should be:_**\n",
162 | "\n",
163 | "\n",
164 | "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n",
165 | "\n",
166 | "\n",
167 | "---\n",
168 | "\n",
169 | "### Hints:\n",
170 | "\n",
171 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()_**\n",
172 | "\n"
173 | ]
174 | },
175 | {
176 | "cell_type": "markdown",
177 | "metadata": {},
178 | "source": [
179 | "**Solutions:**"
180 | ]
181 | },
182 | {
183 | "cell_type": "markdown",
184 | "metadata": {},
185 | "source": [
186 | "- **Using For loop**"
187 | ]
188 | },
189 | {
190 | "cell_type": "code",
191 | "execution_count": null,
192 | "metadata": {},
193 | "outputs": [],
194 | "source": [
195 | "n = int(input())\n",
196 | "ans = {}\n",
197 | "for i in range(1, n + 1):\n",
198 | " ans[i] = i * i\n",
199 | "print(ans)"
200 | ]
201 | },
202 | {
203 | "cell_type": "markdown",
204 | "metadata": {},
205 | "source": [
206 | "- **Using dictionary comprehension**"
207 | ]
208 | },
209 | {
210 | "cell_type": "code",
211 | "execution_count": null,
212 | "metadata": {},
213 | "outputs": [],
214 | "source": [
215 | "n = int(input())\n",
216 | "ans = {i: i * i for i in range(1, n + 1)}\n",
217 | "print(ans)"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": null,
223 | "metadata": {},
224 | "outputs": [],
225 | "source": []
226 | }
227 | ],
228 | "metadata": {
229 | "kernelspec": {
230 | "display_name": "Python 3",
231 | "language": "python",
232 | "name": "python3"
233 | },
234 | "language_info": {
235 | "codemirror_mode": {
236 | "name": "ipython",
237 | "version": 3
238 | },
239 | "file_extension": ".py",
240 | "mimetype": "text/x-python",
241 | "name": "python",
242 | "nbconvert_exporter": "python",
243 | "pygments_lexer": "ipython3",
244 | "version": "3.7.6"
245 | }
246 | },
247 | "nbformat": 4,
248 | "nbformat_minor": 4
249 | }
250 |
--------------------------------------------------------------------------------
/notebooks/Day_04.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 14\n",
8 | "\n",
9 | "### **Question:**\n",
10 | "\n",
11 | "> **_Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters._**\n",
12 | "\n",
13 | "> **_Suppose the following input is supplied to the program:_**\n",
14 | "\n",
15 | "\n",
16 | "Hello world!\n",
17 | "\n",
18 | "\n",
19 | "> **_Then, the output should be:_**\n",
20 | "\n",
21 | "\n",
22 | "UPPER CASE 1\n",
23 | "\n",
24 | "LOWER CASE 9\n",
25 | "\n",
26 | "\n",
27 | "---\n",
28 | "\n",
29 | "### Hints:\n",
30 | "\n",
31 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
32 | "\n",
33 | "---\n",
34 | "\n",
35 | "\n",
36 | "\n",
37 | "**Solutions:**"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {},
44 | "outputs": [],
45 | "source": [
46 | "word = input()\n",
47 | "upper, lower = 0, 0\n",
48 | "\n",
49 | "for i in word:\n",
50 | " if \"a\" <= i and i <= \"z\":\n",
51 | " lower += 1\n",
52 | " if \"A\" <= i and i <= \"Z\":\n",
53 | " upper += 1\n",
54 | "\n",
55 | "print(\"UPPER CASE {0}\\nLOWER CASE {1}\".format(upper, lower))"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {},
61 | "source": [
62 | "**OR**"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "word = input()\n",
72 | "upper, lower = 0, 0\n",
73 | "\n",
74 | "for i in word:\n",
75 | " lower += i.islower()\n",
76 | " upper += i.isupper()\n",
77 | "\n",
78 | "print(\"UPPER CASE {0}\\nLOWER CASE {1}\".format(upper, lower))"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "**OR**"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {},
92 | "outputs": [],
93 | "source": [
94 | "word = input()\n",
95 | "upper = sum(\n",
96 | " 1 for i in word if i.isupper()\n",
97 | ") # sum function cumulatively sum up 1's if the condition is True\n",
98 | "lower = sum(1 for i in word if i.islower())\n",
99 | "\n",
100 | "print(\"UPPER CASE {0}\\nLOWER CASE {1}\".format(upper, lower))"
101 | ]
102 | },
103 | {
104 | "cell_type": "markdown",
105 | "metadata": {},
106 | "source": [
107 | "**OR**"
108 | ]
109 | },
110 | {
111 | "cell_type": "code",
112 | "execution_count": null,
113 | "metadata": {},
114 | "outputs": [],
115 | "source": [
116 | "# solution by Amitewu\n",
117 | "\n",
118 | "string = input(\"Enter the sentense\")\n",
119 | "upper = 0\n",
120 | "lower = 0\n",
121 | "for x in string:\n",
122 | " if x.isupper() == True:\n",
123 | " upper += 1\n",
124 | " if x.islower() == True:\n",
125 | " lower += 1\n",
126 | "\n",
127 | "print(\"UPPER CASE: \", upper)\n",
128 | "print(\"LOWER CASE: \", lower)"
129 | ]
130 | },
131 | {
132 | "cell_type": "markdown",
133 | "metadata": {},
134 | "source": [
135 | "---\n",
136 | "\n",
137 | "# Question 15\n",
138 | "\n",
139 | "### **Question:**\n",
140 | "\n",
141 | "> **_Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a._**\n",
142 | "\n",
143 | "> **_Suppose the following input is supplied to the program:_**\n",
144 | "\n",
145 | "\n",
146 | "9\n",
147 | "\n",
148 | "\n",
149 | "> **_Then, the output should be:_**\n",
150 | "\n",
151 | "\n",
152 | "11106\n",
153 | "\n",
154 | "\n",
155 | "---\n",
156 | "\n",
157 | "### Hints:\n",
158 | "\n",
159 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
160 | "\n",
161 | "---\n",
162 | "\n",
163 | "\n",
164 | "\n",
165 | "**Solutions:**"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": null,
171 | "metadata": {},
172 | "outputs": [],
173 | "source": [
174 | "a = input()\n",
175 | "total, tmp = 0, str() # initialing an integer and empty string\n",
176 | "\n",
177 | "for i in range(4):\n",
178 | " tmp += a # concatenating 'a' to 'tmp'\n",
179 | " total += int(tmp) # converting string type to integer type\n",
180 | "\n",
181 | "print(total)"
182 | ]
183 | },
184 | {
185 | "cell_type": "markdown",
186 | "metadata": {},
187 | "source": [
188 | "**OR**\n",
189 | "\n",
190 | "```python\n",
191 | "a = input()\n",
192 | "total = int(a) + int(2*a) + int(3*a) + int(4*a) # N*a=Na, for example a=\"23\", 2*a=\"2323\",3*a=\"232323\"\n",
193 | "print(total)\n",
194 | "```"
195 | ]
196 | }
197 | ],
198 | "metadata": {
199 | "kernelspec": {
200 | "display_name": "Python 3",
201 | "language": "python",
202 | "name": "python3"
203 | },
204 | "language_info": {
205 | "codemirror_mode": {
206 | "name": "ipython",
207 | "version": 3
208 | },
209 | "file_extension": ".py",
210 | "mimetype": "text/x-python",
211 | "name": "python",
212 | "nbconvert_exporter": "python",
213 | "pygments_lexer": "ipython3",
214 | "version": "3.7.6"
215 | }
216 | },
217 | "nbformat": 4,
218 | "nbformat_minor": 4
219 | }
220 |
--------------------------------------------------------------------------------
/notebooks/Day_05.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 16\n",
8 | "\n",
9 | "### **Question:**\n",
10 | "\n",
11 | "> **_Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers._** >**_Suppose the following input is supplied to the program:_**\n",
12 | "\n",
13 | "\n",
14 | "1,2,3,4,5,6,7,8,9\n",
15 | "\n",
16 | "\n",
17 | "> **_Then, the output should be:_**\n",
18 | "\n",
19 | "\n",
20 | "1,9,25,49,81\n",
21 | "\n",
22 | "\n",
23 | "---\n",
24 | "\n",
25 | "### Hints:\n",
26 | "\n",
27 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
28 | "\n",
29 | "---\n",
30 | "\n",
31 | "\n",
32 | "\n",
33 | "**Solutions:**"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": null,
39 | "metadata": {},
40 | "outputs": [],
41 | "source": [
42 | "lst = [str(int(i) ** 2) for i in input().split(\",\") if int(i) % 2]\n",
43 | "print(\",\".join(lst))"
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "---"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": [
59 | "\"\"\"Solution by: shagun\"\"\"\n",
60 | "\n",
61 | "lst = input().split(\",\") # splits in comma position and set up in list\n",
62 | "\n",
63 | "seq = []\n",
64 | "lst = [int(i) for i in lst] # converts string to integer\n",
65 | "for i in lst:\n",
66 | " if i % 2 != 0:\n",
67 | " i = i * i\n",
68 | " seq.append(i)\n",
69 | "\n",
70 | "\n",
71 | "seq = [\n",
72 | " str(i) for i in seq\n",
73 | "] # All the integers are converted to string to be able to apply join operation\n",
74 | "print(\",\".join(seq))"
75 | ]
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {},
80 | "source": [
81 | "---\n",
82 | "\n",
83 | "**_There were a mistake in the the test case and the solution's whice were notified and fixed with the help of @dwedigital. My warm thanks to him._**\n",
84 | "\n",
85 | "# Question 17\n",
86 | "\n",
87 | "### **Question:**\n",
88 | "\n",
89 | "> **_Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:_**\n",
90 | "\n",
91 | "```\n",
92 | "D 100\n",
93 | "W 200\n",
94 | "```\n",
95 | "\n",
96 | "- D means deposit while W means withdrawal.\n",
97 | "\n",
98 | "> **_Suppose the following input is supplied to the program:_**\n",
99 | "\n",
100 | "```\n",
101 | "D 300\n",
102 | "D 300\n",
103 | "W 200\n",
104 | "D 100\n",
105 | "```\n",
106 | "\n",
107 | "> **_Then, the output should be:_**\n",
108 | "\n",
109 | "```\n",
110 | "500\n",
111 | "```\n",
112 | "\n",
113 | "---\n",
114 | "\n",
115 | "### Hints:\n",
116 | "\n",
117 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
118 | "\n",
119 | "---\n",
120 | "\n",
121 | "\n",
122 | "\n",
123 | "**Solutions:**"
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": null,
129 | "metadata": {},
130 | "outputs": [],
131 | "source": [
132 | "total = 0\n",
133 | "while True:\n",
134 | " s = input().split()\n",
135 | " if not s: # break if the string is empty\n",
136 | " break\n",
137 | " cm, num = map(\n",
138 | " str, s\n",
139 | " ) # two inputs are distributed in cm and num in string data type\n",
140 | "\n",
141 | " if cm == \"D\":\n",
142 | " total += int(num)\n",
143 | " if cm == \"W\":\n",
144 | " total -= int(num)\n",
145 | "\n",
146 | "print(total)"
147 | ]
148 | },
149 | {
150 | "cell_type": "markdown",
151 | "metadata": {},
152 | "source": [
153 | "---"
154 | ]
155 | },
156 | {
157 | "cell_type": "code",
158 | "execution_count": null,
159 | "metadata": {},
160 | "outputs": [],
161 | "source": [
162 | "\"\"\"Solution by: leonedott\"\"\"\n",
163 | "\n",
164 | "lst = []\n",
165 | "while True:\n",
166 | " x = input()\n",
167 | " if len(x) == 0:\n",
168 | " break\n",
169 | " lst.append(x)\n",
170 | "\n",
171 | "balance = 0\n",
172 | "for item in lst:\n",
173 | " if \"D\" in item:\n",
174 | " balance += int(item.strip(\"D \"))\n",
175 | " if \"W\" in item:\n",
176 | " balance -= int(item.strip(\"W \"))\n",
177 | "print(balance)"
178 | ]
179 | },
180 | {
181 | "cell_type": "markdown",
182 | "metadata": {},
183 | "source": [
184 | "---"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": null,
190 | "metadata": {},
191 | "outputs": [],
192 | "source": [
193 | "\"\"\"Solution by: AlexanderSro\"\"\"\n",
194 | "\n",
195 | "account = 0\n",
196 | "while True:\n",
197 | " action = input(\"Deposit/Whitdrow/Balance/Quit? D/W/B/Q: \").lower()\n",
198 | " if action == \"d\":\n",
199 | " deposit = input(\"How much would you like to deposit? \")\n",
200 | " account = account + int(deposit)\n",
201 | " elif action == \"w\":\n",
202 | " withdrow = input(\"How much would you like to withdrow? \")\n",
203 | " account = account - int(withdrow)\n",
204 | " elif action == \"b\":\n",
205 | " print(account)\n",
206 | " else:\n",
207 | " quit()"
208 | ]
209 | },
210 | {
211 | "cell_type": "markdown",
212 | "metadata": {},
213 | "source": [
214 | "---"
215 | ]
216 | },
217 | {
218 | "cell_type": "code",
219 | "execution_count": null,
220 | "metadata": {},
221 | "outputs": [],
222 | "source": [
223 | "\"\"\"Solution by: ShalomPrinz\n",
224 | "\"\"\"\n",
225 | "lines = []\n",
226 | "while True:\n",
227 | " loopInput = input()\n",
228 | " if loopInput == \"done\":\n",
229 | " break\n",
230 | " else:\n",
231 | " lines.append(loopInput)\n",
232 | "\n",
233 | "lst = list(int(i[2:]) if i[0] == \"D\" else -int(i[2:]) for i in lines)\n",
234 | "print(sum(lst))"
235 | ]
236 | },
237 | {
238 | "cell_type": "markdown",
239 | "metadata": {},
240 | "source": [
241 | "---"
242 | ]
243 | }
244 | ],
245 | "metadata": {
246 | "kernelspec": {
247 | "display_name": "Python 3",
248 | "language": "python",
249 | "name": "python3"
250 | },
251 | "language_info": {
252 | "codemirror_mode": {
253 | "name": "ipython",
254 | "version": 3
255 | },
256 | "file_extension": ".py",
257 | "mimetype": "text/x-python",
258 | "name": "python",
259 | "nbconvert_exporter": "python",
260 | "pygments_lexer": "ipython3",
261 | "version": "3.7.6"
262 | }
263 | },
264 | "nbformat": 4,
265 | "nbformat_minor": 4
266 | }
267 |
--------------------------------------------------------------------------------
/notebooks/Day_07.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 20\n",
8 | "\n",
9 | "### **Question:**\n",
10 | "\n",
11 | "> **_Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints:\n",
16 | "\n",
17 | "> **_Consider use class, function and comprehension._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "**Solution: Python 3**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "\"\"\"Solution by: ShalomPrinz\n",
33 | "\"\"\"\n",
34 | "\n",
35 | "\n",
36 | "class MyGen:\n",
37 | " def by_seven(self, n):\n",
38 | " for i in range(0, int(n / 7) + 1):\n",
39 | " yield i * 7\n",
40 | "\n",
41 | "\n",
42 | "for i in MyGen().by_seven(int(input(\"Please enter a number... \"))):\n",
43 | " print(i)"
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "---"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": [
59 | "\"\"\"Solution by: Seawolf159\n",
60 | "\"\"\"\n",
61 | "\n",
62 | "\n",
63 | "class Divisible:\n",
64 | " def by_seven(self, n):\n",
65 | " for number in range(n + 1):\n",
66 | " if number % 7 == 0:\n",
67 | " yield number\n",
68 | "\n",
69 | "\n",
70 | "divisible = Divisible()\n",
71 | "generator = divisible.by_seven(int(input(\"Please insert a number. --> \")))\n",
72 | "for number in generator:\n",
73 | " print(number)"
74 | ]
75 | },
76 | {
77 | "cell_type": "markdown",
78 | "metadata": {},
79 | "source": [
80 | "---\n",
81 | "\n",
82 | "# Question 21\n",
83 | "\n",
84 | "### **Question:**\n",
85 | "\n",
86 | "> **_A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:_**\n",
87 | "\n",
88 | "```\n",
89 | "UP 5\n",
90 | "DOWN 3\n",
91 | "LEFT 3\n",
92 | "RIGHT 2\n",
93 | "```\n",
94 | "> **_The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer._**\n",
95 | "> **_Example:_**\n",
96 | "> **_If the following tuples are given as input to the program:_**\n",
97 | "\n",
98 | "```\n",
99 | "UP 5\n",
100 | "DOWN 3\n",
101 | "LEFT 3\n",
102 | "RIGHT 2\n",
103 | "```\n",
104 | "\n",
105 | "> **_Then, the output of the program should be:_**\n",
106 | "\n",
107 | "```\n",
108 | "2\n",
109 | "```\n",
110 | "---\n",
111 | "\n",
112 | "### Hints:\n",
113 | "\n",
114 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input.Here distance indicates to euclidean distance.Import math module to use sqrt function._**\n",
115 | "\n",
116 | "---\n",
117 | "\n",
118 | "\n",
119 | "\n",
120 | "**Solutions:**"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {},
127 | "outputs": [],
128 | "source": [
129 | "import math\n",
130 | "\n",
131 | "x, y = 0, 0\n",
132 | "while True:\n",
133 | " s = input().split()\n",
134 | " if not s:\n",
135 | " break\n",
136 | " if s[0] == \"UP\": # s[0] indicates command\n",
137 | " x -= int(s[1]) # s[1] indicates unit of move\n",
138 | " if s[0] == \"DOWN\":\n",
139 | " x += int(s[1])\n",
140 | " if s[0] == \"LEFT\":\n",
141 | " y -= int(s[1])\n",
142 | " if s[0] == \"RIGHT\":\n",
143 | " y += int(s[1])\n",
144 | " # N**P means N^P\n",
145 | "dist = round(\n",
146 | " math.sqrt(x ** 2 + y ** 2)\n",
147 | ") # euclidean distance = square root of (x^2+y^2) and rounding it to nearest integer\n",
148 | "print(dist)"
149 | ]
150 | },
151 | {
152 | "cell_type": "markdown",
153 | "metadata": {},
154 | "source": [
155 | "---"
156 | ]
157 | }
158 | ],
159 | "metadata": {
160 | "kernelspec": {
161 | "display_name": "Python 3",
162 | "language": "python",
163 | "name": "python3"
164 | },
165 | "language_info": {
166 | "codemirror_mode": {
167 | "name": "ipython",
168 | "version": 3
169 | },
170 | "file_extension": ".py",
171 | "mimetype": "text/x-python",
172 | "name": "python",
173 | "nbconvert_exporter": "python",
174 | "pygments_lexer": "ipython3",
175 | "version": "3.7.6"
176 | }
177 | },
178 | "nbformat": 4,
179 | "nbformat_minor": 4
180 | }
181 |
--------------------------------------------------------------------------------
/notebooks/Day_09.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 26\n",
8 | "\n",
9 | "### **Question:**\n",
10 | "\n",
11 | "> **_Define a function which can compute the sum of two numbers._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints:\n",
16 | "\n",
17 | "> **_Define a function with two numbers as arguments. You can compute the sum in the function and return the value._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "**Solutions:**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "sum = lambda n1, n2: n1 + n2 # here lambda is use to define little function as sum\n",
33 | "print(sum(1, 2))"
34 | ]
35 | },
36 | {
37 | "cell_type": "markdown",
38 | "metadata": {},
39 | "source": [
40 | "---\n",
41 | "\n",
42 | "# Question 27\n",
43 | "\n",
44 | "### **Question:**\n",
45 | "\n",
46 | "> **_Define a function that can convert a integer into a string and print it in console._**\n",
47 | "\n",
48 | "---\n",
49 | "\n",
50 | "### Hints:\n",
51 | "\n",
52 | "> **_Use str() to convert a number to string._**\n",
53 | "\n",
54 | "---\n",
55 | "\n",
56 | "\n",
57 | "\n",
58 | "**Solutions:**"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {},
65 | "outputs": [],
66 | "source": [
67 | "conv = lambda x: str(x)\n",
68 | "n = conv(10)\n",
69 | "print(n)\n",
70 | "print(type(n)) # checks the type of the variable"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "---\n",
78 | "\n",
79 | "# Question 28\n",
80 | "\n",
81 | "### **Question:**\n",
82 | "\n",
83 | "> **_Define a function that can receive two integer numbers in string form and compute their sum and then print it in console._**\n",
84 | "\n",
85 | "---\n",
86 | "\n",
87 | "### Hints:\n",
88 | "\n",
89 | "> **_Use int() to convert a string to integer._**\n",
90 | "\n",
91 | "---\n",
92 | "\n",
93 | "\n",
94 | "\n",
95 | "**Solutions:**"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": [
104 | "sum = lambda s1, s2: int(s1) + int(s2)\n",
105 | "print(sum(\"10\", \"45\")) # 55"
106 | ]
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {},
111 | "source": [
112 | "---\n",
113 | "\n",
114 | "# Question 29\n",
115 | "\n",
116 | "### **Question:**\n",
117 | "\n",
118 | "> **_Define a function that can accept two strings as input and concatenate them and then print it in console._**\n",
119 | "\n",
120 | "---\n",
121 | "\n",
122 | "### Hints:\n",
123 | "\n",
124 | "> **_Use + sign to concatenate the strings._**\n",
125 | "\n",
126 | "---\n",
127 | "\n",
128 | "\n",
129 | "\n",
130 | "**Solutions:**"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": null,
136 | "metadata": {},
137 | "outputs": [],
138 | "source": [
139 | "sum = lambda s1, s2: s1 + s2\n",
140 | "print(sum(\"10\", \"45\")) # 1045"
141 | ]
142 | },
143 | {
144 | "cell_type": "markdown",
145 | "metadata": {},
146 | "source": [
147 | "---\n",
148 | "\n",
149 | "# Question 30\n",
150 | "\n",
151 | "### **Question:**\n",
152 | "\n",
153 | "> **_Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print all strings line by line._**\n",
154 | "\n",
155 | "---\n",
156 | "\n",
157 | "### Hints:\n",
158 | "\n",
159 | "> **_Use len() function to get the length of a string._**\n",
160 | "\n",
161 | "---\n",
162 | "\n",
163 | "\n",
164 | "\n",
165 | "**Solutions:**"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": null,
171 | "metadata": {},
172 | "outputs": [],
173 | "source": [
174 | "def printVal(s1, s2):\n",
175 | " len1 = len(s1)\n",
176 | " len2 = len(s2)\n",
177 | " if len1 > len2:\n",
178 | " print(s1)\n",
179 | " elif len1 < len2:\n",
180 | " print(s2)\n",
181 | " else:\n",
182 | " print(s1)\n",
183 | " print(s2)\n",
184 | "\n",
185 | "\n",
186 | "s1, s2 = input().split()\n",
187 | "printVal(s1, s2)"
188 | ]
189 | },
190 | {
191 | "cell_type": "markdown",
192 | "metadata": {},
193 | "source": [
194 | "---"
195 | ]
196 | },
197 | {
198 | "cell_type": "code",
199 | "execution_count": null,
200 | "metadata": {},
201 | "outputs": [],
202 | "source": [
203 | "\"\"\"Solution by: yuan1z\"\"\"\n",
204 | "func = (\n",
205 | " lambda a, b: print(max((a, b), key=len))\n",
206 | " if len(a) != len(b)\n",
207 | " else print(a + \"\\n\" + b)\n",
208 | ")"
209 | ]
210 | },
211 | {
212 | "cell_type": "markdown",
213 | "metadata": {},
214 | "source": [
215 | "---"
216 | ]
217 | }
218 | ],
219 | "metadata": {
220 | "kernelspec": {
221 | "display_name": "Python 3",
222 | "language": "python",
223 | "name": "python3"
224 | },
225 | "language_info": {
226 | "codemirror_mode": {
227 | "name": "ipython",
228 | "version": 3
229 | },
230 | "file_extension": ".py",
231 | "mimetype": "text/x-python",
232 | "name": "python",
233 | "nbconvert_exporter": "python",
234 | "pygments_lexer": "ipython3",
235 | "version": "3.7.6"
236 | }
237 | },
238 | "nbformat": 4,
239 | "nbformat_minor": 4
240 | }
241 |
--------------------------------------------------------------------------------
/notebooks/Day_12.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 44\n",
8 | "\n",
9 | "### **Question:**\n",
10 | "\n",
11 | "> **_Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included)._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints:\n",
16 | "\n",
17 | "> **_Use map() to generate a list. Use lambda to define anonymous functions._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "**Solutions:**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {
30 | "code_folding": []
31 | },
32 | "outputs": [],
33 | "source": [
34 | "def sqr(x):\n",
35 | " return x * x\n",
36 | "\n",
37 | "\n",
38 | "squaredNumbers = list(map(sqr, range(1, 21)))\n",
39 | "print(squaredNumbers)"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "---\n",
47 | "\n",
48 | "# Question 45\n",
49 | "\n",
50 | "### **Question:**\n",
51 | "\n",
52 | "> **_Define a class named American which has a static method called printNationality._**\n",
53 | "\n",
54 | "---\n",
55 | "\n",
56 | "### Hints:\n",
57 | "\n",
58 | "> **_Use @staticmethod decorator to define class static method.There are also two more methods.To know more, go to this [link](https://realpython.com/blog/python/instance-class-and-static-methods-demystified/)._**\n",
59 | "\n",
60 | "---\n",
61 | "\n",
62 | "\n",
63 | "\n",
64 | "**Solutions:**"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": null,
70 | "metadata": {},
71 | "outputs": [],
72 | "source": [
73 | "class American:\n",
74 | " @staticmethod\n",
75 | " def printNationality():\n",
76 | " print(\"I am American\")\n",
77 | "\n",
78 | "\n",
79 | "american = American()\n",
80 | "american.printNationality() # this will not run if @staticmethod does not decorates the function.\n",
81 | "# Because the class has no instance.\n",
82 | "\n",
83 | "American.printNationality() # this will run even though the @staticmethod\n",
84 | "# does not decorate printNationality()"
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {},
90 | "source": [
91 | "---\n",
92 | "\n",
93 | "# Question 46\n",
94 | "\n",
95 | "### **Question:**\n",
96 | "\n",
97 | "> **_Define a class named American and its subclass NewYorker._**\n",
98 | "\n",
99 | "---\n",
100 | "\n",
101 | "### Hints:\n",
102 | "\n",
103 | "> **Use class Subclass(ParentClass) to define a subclass.\\***\n",
104 | "\n",
105 | "---\n",
106 | "\n",
107 | "\n",
108 | "\n",
109 | "**Solutions:**"
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": null,
115 | "metadata": {},
116 | "outputs": [],
117 | "source": [
118 | "class American:\n",
119 | " pass\n",
120 | "\n",
121 | "\n",
122 | "class NewYorker(American):\n",
123 | " pass\n",
124 | "\n",
125 | "\n",
126 | "american = American()\n",
127 | "newyorker = NewYorker()\n",
128 | "\n",
129 | "print(american)\n",
130 | "print(newyorker)"
131 | ]
132 | },
133 | {
134 | "cell_type": "markdown",
135 | "metadata": {},
136 | "source": [
137 | "---"
138 | ]
139 | }
140 | ],
141 | "metadata": {
142 | "kernelspec": {
143 | "display_name": "Python 3",
144 | "language": "python",
145 | "name": "python3"
146 | },
147 | "language_info": {
148 | "codemirror_mode": {
149 | "name": "ipython",
150 | "version": 3
151 | },
152 | "file_extension": ".py",
153 | "mimetype": "text/x-python",
154 | "name": "python",
155 | "nbconvert_exporter": "python",
156 | "pygments_lexer": "ipython3",
157 | "version": "3.7.6"
158 | }
159 | },
160 | "nbformat": 4,
161 | "nbformat_minor": 4
162 | }
163 |
--------------------------------------------------------------------------------
/notebooks/Day_13.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 47\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints\n",
16 | "\n",
17 | "> **_Use def methodName(self) to define a method._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "**Solutions:**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "class Circle:\n",
33 | " def __init__(self, r):\n",
34 | " self.radius = r\n",
35 | "\n",
36 | " def area(self):\n",
37 | " return 3.1416 * (self.radius ** 2)\n",
38 | "\n",
39 | "\n",
40 | "circle = Circle(5)\n",
41 | "print(circle.area())"
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "metadata": {},
47 | "source": [
48 | "---\n",
49 | "\n",
50 | "# Question 48\n",
51 | "\n",
52 | "### **Question**\n",
53 | "\n",
54 | "> **_Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area._**\n",
55 | "\n",
56 | "---\n",
57 | "\n",
58 | "### Hints\n",
59 | "\n",
60 | "> **_Use def methodName(self) to define a method._**\n",
61 | "\n",
62 | "---\n",
63 | "\n",
64 | "\n",
65 | "\n",
66 | "**Solutions:**"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "metadata": {},
73 | "outputs": [],
74 | "source": [
75 | "class Rectangle:\n",
76 | " def __init__(self, l, w):\n",
77 | " self.length = l\n",
78 | " self.width = w\n",
79 | "\n",
80 | " def area(self):\n",
81 | " return self.length * self.width\n",
82 | "\n",
83 | "\n",
84 | "rect = Rectangle(2, 4)\n",
85 | "print(rect.area())"
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "---\n",
93 | "\n",
94 | "# Question 49\n",
95 | "\n",
96 | "### **Question**\n",
97 | "\n",
98 | "> **_Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default._**\n",
99 | "\n",
100 | "---\n",
101 | "\n",
102 | "### Hints\n",
103 | "\n",
104 | "> **_To override a method in super class, we can define a method with the same name in the super class._**\n",
105 | "\n",
106 | "---\n",
107 | "\n",
108 | "\n",
109 | "\n",
110 | "**Solutions:**"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": null,
116 | "metadata": {},
117 | "outputs": [],
118 | "source": [
119 | "class Shape:\n",
120 | " def __init__(self):\n",
121 | " pass\n",
122 | "\n",
123 | " def area(self):\n",
124 | " return 0\n",
125 | "\n",
126 | "\n",
127 | "class Square(Shape):\n",
128 | " def __init__(self, length=0):\n",
129 | " Shape.__init__(self)\n",
130 | " self.length = length\n",
131 | "\n",
132 | " def area(self):\n",
133 | " return self.length * self.length\n",
134 | "\n",
135 | "\n",
136 | "Asqr = Square(5)\n",
137 | "print(Asqr.area()) # prints 25 as given argument\n",
138 | "\n",
139 | "print(Square().area()) # prints zero as default area"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "---\n",
147 | "\n",
148 | "# Question 50\n",
149 | "\n",
150 | "### **Question**\n",
151 | "\n",
152 | "> **_Please raise a RuntimeError exception._**\n",
153 | "\n",
154 | "---\n",
155 | "\n",
156 | "### Hints\n",
157 | "\n",
158 | "> **_Use raise to raise an exception._**\n",
159 | "\n",
160 | "---\n",
161 | "\n",
162 | "**Solution:**"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": 1,
168 | "metadata": {},
169 | "outputs": [
170 | {
171 | "ename": "RuntimeError",
172 | "evalue": "something wrong",
173 | "output_type": "error",
174 | "traceback": [
175 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
176 | "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
177 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"something wrong\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
178 | "\u001b[0;31mRuntimeError\u001b[0m: something wrong"
179 | ]
180 | }
181 | ],
182 | "source": [
183 | "raise RuntimeError(\"something wrong\")"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "metadata": {},
189 | "source": [
190 | "---\n",
191 | "\n",
192 | "## Conclusion\n",
193 | "\n",
194 | "**_Well It seems that the above problems are very much focused on basic concpets and implimantation of object oriented programming.As the concepts are not about to solve any functional problem rather design the structure , so both codes are very much similar in there implimantation part._**"
195 | ]
196 | }
197 | ],
198 | "metadata": {
199 | "kernelspec": {
200 | "display_name": "Python 3",
201 | "language": "python",
202 | "name": "python3"
203 | },
204 | "language_info": {
205 | "codemirror_mode": {
206 | "name": "ipython",
207 | "version": 3
208 | },
209 | "file_extension": ".py",
210 | "mimetype": "text/x-python",
211 | "name": "python",
212 | "nbconvert_exporter": "python",
213 | "pygments_lexer": "ipython3",
214 | "version": "3.7.6"
215 | }
216 | },
217 | "nbformat": 4,
218 | "nbformat_minor": 4
219 | }
220 |
--------------------------------------------------------------------------------
/notebooks/Day_14.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 51\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_Write a function to compute 5/0 and use try/except to catch the exceptions._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints\n",
16 | "\n",
17 | "> **_Use try/except to catch exceptions._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "**Solutions:**"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": null,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "def divide():\n",
31 | " return 5 / 0\n",
32 | "\n",
33 | "\n",
34 | "try:\n",
35 | " divide()\n",
36 | "except ZeroDivisionError as ze:\n",
37 | " print(\"Why on earth you are dividing a number by ZERO!!\")\n",
38 | "except:\n",
39 | " print(\"Any other exception\")"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "---\n",
47 | "\n",
48 | "# Question 52\n",
49 | "\n",
50 | "### **Question**\n",
51 | "\n",
52 | "> **_Define a custom exception class which takes a string message as attribute._**\n",
53 | "\n",
54 | "---\n",
55 | "\n",
56 | "### Hints\n",
57 | "\n",
58 | "> **_To define a custom exception, we need to define a class inherited from Exception._**\n",
59 | "\n",
60 | "---\n",
61 | "\n",
62 | "**Solutions:**"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "class CustomException(Exception):\n",
72 | " \"\"\"Exception raised for custom purpose\n",
73 | "\n",
74 | " Attributes:\n",
75 | " message -- explanation of the error\n",
76 | " \"\"\"\n",
77 | "\n",
78 | " def __init__(self, message):\n",
79 | " self.message = message\n",
80 | "\n",
81 | "\n",
82 | "num = int(input())\n",
83 | "\n",
84 | "try:\n",
85 | " if num < 10:\n",
86 | " raise CustomException(\"Input is less than 10\")\n",
87 | " elif num > 10:\n",
88 | " raise CustomException(\"Input is grater than 10\")\n",
89 | "except CustomException as ce:\n",
90 | " print(\"The error raised: \" + ce.message)"
91 | ]
92 | },
93 | {
94 | "cell_type": "markdown",
95 | "metadata": {},
96 | "source": [
97 | "---\n",
98 | "\n",
99 | "# Question 53\n",
100 | "\n",
101 | "### **Question**\n",
102 | "\n",
103 | "> **_Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only._**\n",
104 | "\n",
105 | "> **_Example:\n",
106 | "> If the following email address is given as input to the\n",
107 | "> program:_**\n",
108 | "\n",
109 | "> john@google.com\n",
110 | "\n",
111 | "> **_Then, the output of the program should be:_**\n",
112 | "\n",
113 | "> john\n",
114 | "\n",
115 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
116 | "\n",
117 | "---\n",
118 | "\n",
119 | "### Hints\n",
120 | "\n",
121 | "> **_Use \\w to match letters._**\n",
122 | "\n",
123 | "---\n",
124 | "\n",
125 | "**Solutions:**"
126 | ]
127 | },
128 | {
129 | "cell_type": "code",
130 | "execution_count": null,
131 | "metadata": {},
132 | "outputs": [],
133 | "source": [
134 | "email = \"john@google.com\"\n",
135 | "email = email.split(\"@\")\n",
136 | "print(email[0])"
137 | ]
138 | },
139 | {
140 | "cell_type": "markdown",
141 | "metadata": {},
142 | "source": [
143 | "---\n",
144 | "\n",
145 | "**OR**"
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": null,
151 | "metadata": {},
152 | "outputs": [],
153 | "source": [
154 | "import re\n",
155 | "\n",
156 | "email = \"john@google.com elise@python.com\"\n",
157 | "pattern = \"(\\w+)@\\w+.com\"\n",
158 | "ans = re.findall(pattern, email)\n",
159 | "print(ans)"
160 | ]
161 | },
162 | {
163 | "cell_type": "markdown",
164 | "metadata": {},
165 | "source": [
166 | "---"
167 | ]
168 | }
169 | ],
170 | "metadata": {
171 | "kernelspec": {
172 | "display_name": "Python 3",
173 | "language": "python",
174 | "name": "python3"
175 | },
176 | "language_info": {
177 | "codemirror_mode": {
178 | "name": "ipython",
179 | "version": 3
180 | },
181 | "file_extension": ".py",
182 | "mimetype": "text/x-python",
183 | "name": "python",
184 | "nbconvert_exporter": "python",
185 | "pygments_lexer": "ipython3",
186 | "version": "3.7.6"
187 | }
188 | },
189 | "nbformat": 4,
190 | "nbformat_minor": 4
191 | }
192 |
--------------------------------------------------------------------------------
/notebooks/Day_15.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 54\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only._**\n",
12 | "\n",
13 | "> **_Example:\n",
14 | "> If the following email address is given as input to the program:_**\n",
15 | "\n",
16 | "\n",
17 | "> john@google.com\n",
18 | "\n",
19 | "\n",
20 | "> **_Then, the output of the program should be:_**\n",
21 | "\n",
22 | "\n",
23 | "> google\n",
24 | "\n",
25 | "\n",
26 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
27 | "\n",
28 | "---\n",
29 | "\n",
30 | "### Hints\n",
31 | "\n",
32 | "> **_Use \\w to match letters._**\n",
33 | "\n",
34 | "---\n",
35 | "\n",
36 | "\n",
37 | "\n",
38 | "**Solutions:**"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "import re\n",
48 | "\n",
49 | "email = \"john@google.com elise@python.com\"\n",
50 | "pattern = \"\\w+@(\\w+).com\"\n",
51 | "ans = re.findall(pattern, email)\n",
52 | "print(ans)"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "---\n",
60 | "\n",
61 | "# Question 55\n",
62 | "\n",
63 | "### **Question**\n",
64 | "\n",
65 | "> **_Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only._**\n",
66 | "\n",
67 | "> **_Example:\n",
68 | "> If the following words is given as input to the program:_**\n",
69 | "\n",
70 | "\n",
71 | "> 2 cats and 3 dogs.\n",
72 | "\n",
73 | "\n",
74 | "> **_Then, the output of the program should be:_**\n",
75 | "\n",
76 | "\n",
77 | "> ['2', '3']\n",
78 | "\n",
79 | "\n",
80 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
81 | "\n",
82 | "---\n",
83 | "\n",
84 | "### Hints\n",
85 | "\n",
86 | "> **_Use re.findall() to find all substring using regex._**\n",
87 | "\n",
88 | "---\n",
89 | "\n",
90 | "\n",
91 | "\n",
92 | "**Solutions:**"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {},
99 | "outputs": [],
100 | "source": [
101 | "import re\n",
102 | "\n",
103 | "email = input()\n",
104 | "pattern = \"\\d+\"\n",
105 | "ans = re.findall(pattern, email)\n",
106 | "print(ans)"
107 | ]
108 | },
109 | {
110 | "cell_type": "markdown",
111 | "metadata": {},
112 | "source": [
113 | "**OR**"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": null,
119 | "metadata": {},
120 | "outputs": [],
121 | "source": [
122 | "email = input().split()\n",
123 | "ans = []\n",
124 | "for word in email:\n",
125 | " if word.isdigit(): # can also use isnumeric() / isdecimal() function instead\n",
126 | " ans.append(word)\n",
127 | "print(ans)"
128 | ]
129 | },
130 | {
131 | "cell_type": "markdown",
132 | "metadata": {},
133 | "source": [
134 | "**OR**"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": null,
140 | "metadata": {},
141 | "outputs": [],
142 | "source": [
143 | "email = input().split()\n",
144 | "ans = [word for word in email if word.isdigit()] # using list comprehension method\n",
145 | "print(ans)"
146 | ]
147 | },
148 | {
149 | "cell_type": "markdown",
150 | "metadata": {},
151 | "source": [
152 | "---\n",
153 | "\n",
154 | "# Question 56\n",
155 | "\n",
156 | "### **Question**\n",
157 | "\n",
158 | "> **_Print a unicode string \"hello world\"._**\n",
159 | "\n",
160 | "---\n",
161 | "\n",
162 | "### Hints\n",
163 | "\n",
164 | "> **_Use u'strings' format to define unicode string._**\n",
165 | "\n",
166 | "---\n",
167 | "\n",
168 | "\n",
169 | "\n",
170 | "# Question 57\n",
171 | "\n",
172 | "### **Question**\n",
173 | "\n",
174 | "> **_Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8._**\n",
175 | "\n",
176 | "---\n",
177 | "\n",
178 | "### Hints\n",
179 | "\n",
180 | "> **_Use unicode()/encode() function to convert._**\n",
181 | "\n",
182 | "---\n",
183 | "\n",
184 | "\n",
185 | "\n",
186 | "**Solutions:**"
187 | ]
188 | },
189 | {
190 | "cell_type": "code",
191 | "execution_count": null,
192 | "metadata": {},
193 | "outputs": [],
194 | "source": [
195 | "s = input()\n",
196 | "u = s.encode(\"utf-8\")\n",
197 | "print(u)"
198 | ]
199 | },
200 | {
201 | "cell_type": "markdown",
202 | "metadata": {},
203 | "source": [
204 | "---\n",
205 | "\n",
206 | "# Question 58\n",
207 | "\n",
208 | "### **Question**\n",
209 | "\n",
210 | "> **_Write a special comment to indicate a Python source code file is in unicode._**\n",
211 | "\n",
212 | "---\n",
213 | "\n",
214 | "### Hints\n",
215 | "\n",
216 | "> **_Use unicode() function to convert._**\n",
217 | "\n",
218 | "---\n",
219 | "\n",
220 | "**Solution:**"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": null,
226 | "metadata": {},
227 | "outputs": [],
228 | "source": [
229 | "# -*- coding: utf-8 -*-"
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {},
235 | "source": [
236 | "---\n",
237 | "\n",
238 | "# Question 59\n",
239 | "\n",
240 | "### **Question**\n",
241 | "\n",
242 | "> **_Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0)._**\n",
243 | "\n",
244 | "> **_Example:\n",
245 | "> If the following n is given as input to the program:_**\n",
246 | "\n",
247 | "\n",
248 | "> 5\n",
249 | "\n",
250 | "\n",
251 | "> **_Then, the output of the program should be:_**\n",
252 | "\n",
253 | "\n",
254 | "> 3.55\n",
255 | "\n",
256 | "\n",
257 | "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n",
258 | "\n",
259 | "---\n",
260 | "\n",
261 | "### Hints\n",
262 | "\n",
263 | "> **_Use float() to convert an integer to a float.Even if not converted it wont cause a problem because python by default understands the data type of a value_**\n",
264 | "\n",
265 | "---\n",
266 | "\n",
267 | "\n",
268 | "\n",
269 | "**Solutions:**"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": null,
275 | "metadata": {},
276 | "outputs": [],
277 | "source": [
278 | "n = int(input())\n",
279 | "sum = 0\n",
280 | "for i in range(1, n + 1):\n",
281 | " sum += i / (i + 1)\n",
282 | "print(round(sum, 2)) # rounded to 2 decimal point"
283 | ]
284 | },
285 | {
286 | "cell_type": "markdown",
287 | "metadata": {},
288 | "source": [
289 | "---"
290 | ]
291 | }
292 | ],
293 | "metadata": {
294 | "kernelspec": {
295 | "display_name": "Python 3",
296 | "language": "python",
297 | "name": "python3"
298 | },
299 | "language_info": {
300 | "codemirror_mode": {
301 | "name": "ipython",
302 | "version": 3
303 | },
304 | "file_extension": ".py",
305 | "mimetype": "text/x-python",
306 | "name": "python",
307 | "nbconvert_exporter": "python",
308 | "pygments_lexer": "ipython3",
309 | "version": "3.7.6"
310 | }
311 | },
312 | "nbformat": 4,
313 | "nbformat_minor": 4
314 | }
315 |
--------------------------------------------------------------------------------
/notebooks/Day_17.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 65\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_Please write assert statements to verify that every number in the list [2,4,6,8] is even._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints\n",
16 | "\n",
17 | "> **_Use \"assert expression\" to make assertion._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "**Solutions:**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "data = [2, 4, 5, 6]\n",
33 | "for i in data:\n",
34 | " assert i % 2 == 0, \"{} is not an even number\".format(i)"
35 | ]
36 | },
37 | {
38 | "cell_type": "markdown",
39 | "metadata": {},
40 | "source": [
41 | "---\n",
42 | "\n",
43 | "# Question 66\n",
44 | "\n",
45 | "### **Question**\n",
46 | "\n",
47 | "> **_Please write a program which accepts basic mathematic expression from console and print the evaluation result._**\n",
48 | "\n",
49 | "> **_Example:\n",
50 | "> If the following n is given as input to the program:_**\n",
51 | "\n",
52 | "\n",
53 | "> 35 + 3\n",
54 | "\n",
55 | "\n",
56 | "> **_Then, the output of the program should be:_**\n",
57 | "\n",
58 | "\n",
59 | "> 38\n",
60 | "\n",
61 | "\n",
62 | "---\n",
63 | "\n",
64 | "### Hints\n",
65 | "\n",
66 | "> **_Use eval() to evaluate an expression._**\n",
67 | "\n",
68 | "---\n",
69 | "\n",
70 | "\n",
71 | "\n",
72 | "**Solutions:**"
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": null,
78 | "metadata": {},
79 | "outputs": [],
80 | "source": [
81 | "expression = input()\n",
82 | "ans = eval(expression)\n",
83 | "print(ans)"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "metadata": {},
89 | "source": [
90 | "---\n",
91 | "\n",
92 | "# Question 67\n",
93 | "\n",
94 | "### **Question**\n",
95 | "\n",
96 | "> **_Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list._**\n",
97 | "\n",
98 | "---\n",
99 | "\n",
100 | "### Hints\n",
101 | "\n",
102 | "> **_Use if/elif to deal with conditions._**\n",
103 | "\n",
104 | "---\n",
105 | "\n",
106 | "**Solutions:**\n",
107 | "\n",
108 | "### Solution by AasaiAlangaram"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": [
117 | "def binary_search_Ascending(array, target):\n",
118 | " lower = 0\n",
119 | " upper = len(array)\n",
120 | " print(\"Array Length:\", upper)\n",
121 | " while lower < upper:\n",
122 | " x = (lower + upper) // 2\n",
123 | " print(\"Middle Value:\", x)\n",
124 | " value = array[x]\n",
125 | " if target == value:\n",
126 | " return x\n",
127 | " elif target > value:\n",
128 | " lower = x\n",
129 | " elif target < value:\n",
130 | " upper = x\n",
131 | "\n",
132 | "\n",
133 | "Array = [1, 5, 8, 10, 12, 13, 55, 66, 73, 78, 82, 85, 88, 99]\n",
134 | "print(\"The Value Found at Index:\", binary_search_Ascending(Array, 82))"
135 | ]
136 | },
137 | {
138 | "cell_type": "markdown",
139 | "metadata": {},
140 | "source": [
141 | "---\n",
142 | "**OR**\n",
143 | "\n",
144 | "### Solution by yuan1z: Python"
145 | ]
146 | },
147 | {
148 | "cell_type": "code",
149 | "execution_count": null,
150 | "metadata": {},
151 | "outputs": [],
152 | "source": [
153 | "idx = 0\n",
154 | "\n",
155 | "\n",
156 | "def bs(num, num_list):\n",
157 | " global idx\n",
158 | " if len(num_list) == 1:\n",
159 | " if num_list[0] == num:\n",
160 | " return idx\n",
161 | " else:\n",
162 | " return \"No exit in the list\"\n",
163 | " elif num in num_list[: len(num_list) // 2]:\n",
164 | " return bs(num, num_list[: len(num_list) // 2])\n",
165 | " else:\n",
166 | " idx += len(num_list) // 2\n",
167 | " return bs(num, num_list[len(num_list) // 2 :])\n",
168 | "\n",
169 | "\n",
170 | "print(bs(66, [1, 5, 8, 10, 12, 13, 55, 66, 73, 78, 82, 85, 88, 99, 100]))"
171 | ]
172 | },
173 | {
174 | "cell_type": "markdown",
175 | "metadata": {},
176 | "source": [
177 | "---\n",
178 | "\n",
179 | "# Question 68\n",
180 | "\n",
181 | "### **Question**\n",
182 | "\n",
183 | "> **_Please generate a random float where the value is between 10 and 100 using Python module._**\n",
184 | "\n",
185 | "---\n",
186 | "\n",
187 | "### Hints\n",
188 | "\n",
189 | "> **_Use random.random() to generate a random float in [0,1]._**\n",
190 | "\n",
191 | "---\n",
192 | "\n",
193 | "\n",
194 | "\n",
195 | "**Solutions:**"
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "execution_count": null,
201 | "metadata": {},
202 | "outputs": [],
203 | "source": [
204 | "import random\n",
205 | "\n",
206 | "rand_num = random.uniform(10, 100)\n",
207 | "print(rand_num)"
208 | ]
209 | },
210 | {
211 | "cell_type": "markdown",
212 | "metadata": {},
213 | "source": [
214 | "---\n",
215 | "\n",
216 | "# Question 69\n",
217 | "\n",
218 | "### **Question**\n",
219 | "\n",
220 | "> **_Please generate a random float where the value is between 5 and 95 using Python module._**\n",
221 | "\n",
222 | "---\n",
223 | "\n",
224 | "### Hints\n",
225 | "\n",
226 | "> **_Use random.random() to generate a random float in [0,1]._**\n",
227 | "\n",
228 | "---\n",
229 | "\n",
230 | "\n",
231 | "\n",
232 | "**Solutions:**"
233 | ]
234 | },
235 | {
236 | "cell_type": "code",
237 | "execution_count": null,
238 | "metadata": {},
239 | "outputs": [],
240 | "source": [
241 | "import random\n",
242 | "\n",
243 | "rand_num = random.uniform(5, 95)\n",
244 | "print(rand_num)"
245 | ]
246 | },
247 | {
248 | "cell_type": "markdown",
249 | "metadata": {},
250 | "source": [
251 | "---"
252 | ]
253 | }
254 | ],
255 | "metadata": {
256 | "kernelspec": {
257 | "display_name": "Python 3",
258 | "language": "python",
259 | "name": "python3"
260 | },
261 | "language_info": {
262 | "codemirror_mode": {
263 | "name": "ipython",
264 | "version": 3
265 | },
266 | "file_extension": ".py",
267 | "mimetype": "text/x-python",
268 | "name": "python",
269 | "nbconvert_exporter": "python",
270 | "pygments_lexer": "ipython3",
271 | "version": "3.7.6"
272 | }
273 | },
274 | "nbformat": 4,
275 | "nbformat_minor": 4
276 | }
277 |
--------------------------------------------------------------------------------
/notebooks/Day_18.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 70\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints\n",
16 | "\n",
17 | "> **_Use random.choice() to a random element from a list._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "**Solutions:**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "import random\n",
33 | "\n",
34 | "resp = [i for i in range(0, 11, 2)]\n",
35 | "print(random.choice(resp))"
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | "---\n",
43 | "\n",
44 | "# Question 71\n",
45 | "\n",
46 | "### **Question**\n",
47 | "\n",
48 | "> **_Please write a program to output a random number, which is divisible by 5 and 7, between 10 and 150 inclusive using random module and list comprehension._**\n",
49 | "\n",
50 | "---\n",
51 | "\n",
52 | "### Hints\n",
53 | "\n",
54 | "> **_Use random.choice() to a random element from a list._**\n",
55 | "\n",
56 | "---\n",
57 | "\n",
58 | "\n",
59 | "\n",
60 | "**Solutions:**"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "metadata": {},
67 | "outputs": [],
68 | "source": [
69 | "import random\n",
70 | "\n",
71 | "resp = [i for i in range(10, 151) if i % 35 == 0]\n",
72 | "print(random.choice(resp))"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "metadata": {},
78 | "source": [
79 | "---\n",
80 | "\n",
81 | "# Question 72\n",
82 | "\n",
83 | "### **Question**\n",
84 | "\n",
85 | "> **_Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive._**\n",
86 | "\n",
87 | "---\n",
88 | "\n",
89 | "### Hints\n",
90 | "\n",
91 | "> **_Use random.sample() to generate a list of random values._**\n",
92 | "\n",
93 | "---\n",
94 | "\n",
95 | "\n",
96 | "\n",
97 | "**Solutions:**"
98 | ]
99 | },
100 | {
101 | "cell_type": "code",
102 | "execution_count": null,
103 | "metadata": {},
104 | "outputs": [],
105 | "source": [
106 | "import random\n",
107 | "\n",
108 | "resp = random.sample(range(100, 201), 5)\n",
109 | "print(resp)"
110 | ]
111 | },
112 | {
113 | "cell_type": "markdown",
114 | "metadata": {},
115 | "source": [
116 | "---\n",
117 | "\n",
118 | "# Question 73\n",
119 | "\n",
120 | "### **Question**\n",
121 | "\n",
122 | "> **_Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive._**\n",
123 | "\n",
124 | "---\n",
125 | "\n",
126 | "### Hints\n",
127 | "\n",
128 | "> **_Use random.sample() to generate a list of random values._**\n",
129 | "\n",
130 | "---\n",
131 | "\n",
132 | "\n",
133 | "\n",
134 | "**Solutions:**"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": null,
140 | "metadata": {},
141 | "outputs": [],
142 | "source": [
143 | "import random\n",
144 | "\n",
145 | "resp = random.sample(range(100, 201, 2), 5)\n",
146 | "print(resp)"
147 | ]
148 | },
149 | {
150 | "cell_type": "markdown",
151 | "metadata": {},
152 | "source": [
153 | "---\n",
154 | "\n",
155 | "# Question 74\n",
156 | "\n",
157 | "### **Question**\n",
158 | "\n",
159 | "> **_Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive._**\n",
160 | "\n",
161 | "---\n",
162 | "\n",
163 | "### Hints\n",
164 | "\n",
165 | "> **_Use random.sample() to generate a list of random values._**\n",
166 | "\n",
167 | "---\n",
168 | "\n",
169 | "\n",
170 | "\n",
171 | "**Solutions:**"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": null,
177 | "metadata": {},
178 | "outputs": [],
179 | "source": [
180 | "import random\n",
181 | "\n",
182 | "lst = [i for i in range(1, 1001) if i % 35 == 0]\n",
183 | "resp = random.sample(lst, 5)\n",
184 | "print(resp)"
185 | ]
186 | },
187 | {
188 | "cell_type": "markdown",
189 | "metadata": {},
190 | "source": [
191 | "---"
192 | ]
193 | }
194 | ],
195 | "metadata": {
196 | "kernelspec": {
197 | "display_name": "Python 3",
198 | "language": "python",
199 | "name": "python3"
200 | },
201 | "language_info": {
202 | "codemirror_mode": {
203 | "name": "ipython",
204 | "version": 3
205 | },
206 | "file_extension": ".py",
207 | "mimetype": "text/x-python",
208 | "name": "python",
209 | "nbconvert_exporter": "python",
210 | "pygments_lexer": "ipython3",
211 | "version": "3.7.6"
212 | }
213 | },
214 | "nbformat": 4,
215 | "nbformat_minor": 4
216 | }
217 |
--------------------------------------------------------------------------------
/notebooks/Day_19.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 75\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_Please write a program to randomly print a integer number between 7 and 15 inclusive._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints\n",
16 | "\n",
17 | "> **_Use random.randrange() to a random integer in a given range._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "**Solution:**"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 2,
27 | "metadata": {},
28 | "outputs": [
29 | {
30 | "name": "stdout",
31 | "output_type": "stream",
32 | "text": [
33 | "8\n"
34 | ]
35 | }
36 | ],
37 | "source": [
38 | "import random\n",
39 | "\n",
40 | "print(random.randrange(7, 16))"
41 | ]
42 | },
43 | {
44 | "cell_type": "markdown",
45 | "metadata": {},
46 | "source": [
47 | "---\n",
48 | "\n",
49 | "# Question 76\n",
50 | "\n",
51 | "### **Question**\n",
52 | "\n",
53 | "> **_Please write a program to compress and decompress the string \"hello world!hello world!hello world!hello world!\"._**\n",
54 | "\n",
55 | "---\n",
56 | "\n",
57 | "### Hints\n",
58 | "\n",
59 | "> **_Use zlib.compress() and zlib.decompress() to compress and decompress a string._**\n",
60 | "\n",
61 | "---\n",
62 | "\n",
63 | "**Solution:**"
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": 7,
69 | "metadata": {},
70 | "outputs": [
71 | {
72 | "name": "stdout",
73 | "output_type": "stream",
74 | "text": [
75 | "b'x\\x9c\\xcbH\\xcd\\xc9\\xc9W(\\xcf/\\xcaIQ\\xcc \\x82\\r\\x00\\xbd[\\x11\\xf5'\n",
76 | "b'hello world!hello world!hello world!hello world!'\n"
77 | ]
78 | }
79 | ],
80 | "source": [
81 | "import zlib\n",
82 | "\n",
83 | "t = zlib.compress(b'hello world!hello world!hello world!hello world!')\n",
84 | "print(t)\n",
85 | "print(zlib.decompress(t))"
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "---\n",
93 | "\n",
94 | "# Question 77\n",
95 | "\n",
96 | "### **Question**\n",
97 | "\n",
98 | "> **_Please write a program to print the running time of execution of \"1+1\" for 100 times._**\n",
99 | "\n",
100 | "---\n",
101 | "\n",
102 | "### Hints\n",
103 | "\n",
104 | "> **_Use timeit() function to measure the running time._**\n",
105 | "\n",
106 | "---\n",
107 | "\n",
108 | "\n",
109 | "\n",
110 | "**Solutions:**"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": null,
116 | "metadata": {},
117 | "outputs": [],
118 | "source": [
119 | "import datetime\n",
120 | "\n",
121 | "before = datetime.datetime.now()\n",
122 | "for i in range(100):\n",
123 | " x = 1 + 1\n",
124 | "after = datetime.datetime.now()\n",
125 | "execution_time = after - before\n",
126 | "print(execution_time.microseconds)"
127 | ]
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | "**OR**"
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": null,
139 | "metadata": {},
140 | "outputs": [],
141 | "source": [
142 | "import time\n",
143 | "\n",
144 | "before = time.time()\n",
145 | "for i in range(100):\n",
146 | " x = 1 + 1\n",
147 | "after = time.time()\n",
148 | "execution_time = after - before\n",
149 | "print(execution_time)"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "---\n",
157 | "\n",
158 | "# Question 78\n",
159 | "\n",
160 | "### **Question**\n",
161 | "\n",
162 | "> **_Please write a program to shuffle and print the list [3,6,7,8]._**\n",
163 | "\n",
164 | "---\n",
165 | "\n",
166 | "### Hints\n",
167 | "\n",
168 | "> **_Use shuffle() function to shuffle a list._**\n",
169 | "\n",
170 | "---\n",
171 | "\n",
172 | "\n",
173 | "\n",
174 | "**Solutions:**"
175 | ]
176 | },
177 | {
178 | "cell_type": "code",
179 | "execution_count": null,
180 | "metadata": {},
181 | "outputs": [],
182 | "source": [
183 | "import random\n",
184 | "\n",
185 | "lst = [3, 6, 7, 8]\n",
186 | "random.shuffle(lst)\n",
187 | "print(lst)"
188 | ]
189 | },
190 | {
191 | "cell_type": "markdown",
192 | "metadata": {},
193 | "source": [
194 | "**OR**"
195 | ]
196 | },
197 | {
198 | "cell_type": "code",
199 | "execution_count": null,
200 | "metadata": {},
201 | "outputs": [],
202 | "source": [
203 | "import random\n",
204 | "\n",
205 | "# shuffle with a chosen seed\n",
206 | "lst = [3, 6, 7, 8]\n",
207 | "seed = 7\n",
208 | "random.Random(seed).shuffle(lst)\n",
209 | "print(lst)"
210 | ]
211 | },
212 | {
213 | "cell_type": "markdown",
214 | "metadata": {},
215 | "source": [
216 | "---\n",
217 | "\n",
218 | "# Question 79\n",
219 | "\n",
220 | "### **Question**\n",
221 | "\n",
222 | "> **_Please write a program to generate all sentences where subject is in [\"I\", \"You\"] and verb is in [\"Play\", \"Love\"] and the object is in [\"Hockey\",\"Football\"]._**\n",
223 | "\n",
224 | "---\n",
225 | "\n",
226 | "### Hints\n",
227 | "\n",
228 | "> **_Use list[index] notation to get a element from a list._**\n",
229 | "\n",
230 | "---\n",
231 | "\n",
232 | "\n",
233 | "\n",
234 | "**Solutions:**"
235 | ]
236 | },
237 | {
238 | "cell_type": "code",
239 | "execution_count": null,
240 | "metadata": {},
241 | "outputs": [],
242 | "source": [
243 | "subjects = [\"I\", \"You\"]\n",
244 | "verbs = [\"Play\", \"Love\"]\n",
245 | "objects = [\"Hockey\", \"Football\"]\n",
246 | "\n",
247 | "for sub in subjects:\n",
248 | " for verb in verbs:\n",
249 | " for obj in objects:\n",
250 | " print(\"{} {} {}\".format(sub, verb, obj))"
251 | ]
252 | },
253 | {
254 | "cell_type": "markdown",
255 | "metadata": {},
256 | "source": [
257 | "---"
258 | ]
259 | }
260 | ],
261 | "metadata": {
262 | "kernelspec": {
263 | "display_name": "Python 3",
264 | "language": "python",
265 | "name": "python3"
266 | },
267 | "language_info": {
268 | "codemirror_mode": {
269 | "name": "ipython",
270 | "version": 3
271 | },
272 | "file_extension": ".py",
273 | "mimetype": "text/x-python",
274 | "name": "python",
275 | "nbconvert_exporter": "python",
276 | "pygments_lexer": "ipython3",
277 | "version": "3.7.6"
278 | }
279 | },
280 | "nbformat": 4,
281 | "nbformat_minor": 4
282 | }
283 |
--------------------------------------------------------------------------------
/notebooks/Day_20.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 80\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_Please write a program to print the list after removing even numbers in [5,6,77,45,22,12,24]._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints\n",
16 | "\n",
17 | "> **_Use list comprehension to delete a bunch of element from a list._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "**Solutions:**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "def isEven(n):\n",
33 | " return n % 2 != 0\n",
34 | "\n",
35 | "\n",
36 | "li = [5, 6, 77, 45, 22, 12, 24]\n",
37 | "lst = list(filter(isEven, li))\n",
38 | "print(lst)"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {},
44 | "source": [
45 | "**OR**"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "metadata": {},
52 | "outputs": [],
53 | "source": [
54 | "li = [5, 6, 77, 45, 22, 12, 24]\n",
55 | "lst = list(filter(lambda n: n % 2 != 0, li))\n",
56 | "print(lst)"
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | "---\n",
64 | "\n",
65 | "# Question 81\n",
66 | "\n",
67 | "### **Question**\n",
68 | "\n",
69 | "> **_By using list comprehension, please write a program to print the list after removing numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155]._**\n",
70 | "\n",
71 | "---\n",
72 | "\n",
73 | "### Hints\n",
74 | "\n",
75 | "> **_Use list comprehension to delete a bunch of element from a list._**\n",
76 | "\n",
77 | "---\n",
78 | "\n",
79 | "\n",
80 | "\n",
81 | "**Solutions:**"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": null,
87 | "metadata": {},
88 | "outputs": [],
89 | "source": [
90 | "li = [12, 24, 35, 70, 88, 120, 155]\n",
91 | "li = [x for x in li if x % 35 != 0]\n",
92 | "print(li)"
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "---\n",
100 | "\n",
101 | "# Question 82\n",
102 | "\n",
103 | "### **Question**\n",
104 | "\n",
105 | "> **_By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155]._**\n",
106 | "\n",
107 | "---\n",
108 | "\n",
109 | "### Hints\n",
110 | "\n",
111 | "> **_Use list comprehension to delete a bunch of element from a list.\n",
112 | "> Use enumerate() to get (index, value) tuple._**\n",
113 | "\n",
114 | "---\n",
115 | "\n",
116 | "\n",
117 | "\n",
118 | "**Solutions:**"
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "execution_count": null,
124 | "metadata": {},
125 | "outputs": [],
126 | "source": [
127 | "li = [12, 24, 35, 70, 88, 120, 155]\n",
128 | "li = [li[i] for i in range(len(li)) if i % 2 != 0]\n",
129 | "print(li)"
130 | ]
131 | },
132 | {
133 | "cell_type": "markdown",
134 | "metadata": {},
135 | "source": [
136 | "---\n",
137 | "\n",
138 | "# Question 83\n",
139 | "\n",
140 | "### **Question**\n",
141 | "\n",
142 | "> **_By using list comprehension, please write a program to print the list after removing the 2nd - 4th numbers in [12,24,35,70,88,120,155]._**\n",
143 | "\n",
144 | "---\n",
145 | "\n",
146 | "### Hints\n",
147 | "\n",
148 | "> **_Use list comprehension to delete a bunch of element from a list.\n",
149 | "> Use enumerate() to get (index, value) tuple._**\n",
150 | "\n",
151 | "---\n",
152 | "\n",
153 | "\n",
154 | "\n",
155 | "**Solutions:**"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": null,
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "# to be written\n",
165 | "li = [12, 24, 35, 70, 88, 120, 155]\n",
166 | "li = [li[i] for i in range(len(li)) if i < 3 or 4 < i]\n",
167 | "print(li)"
168 | ]
169 | },
170 | {
171 | "cell_type": "markdown",
172 | "metadata": {},
173 | "source": [
174 | "---\n",
175 | "\n",
176 | "# Question 84\n",
177 | "\n",
178 | "### **Question**\n",
179 | "\n",
180 | "> **_By using list comprehension, please write a program generate a 3\\*5\\*8 3D array whose each element is 0._**\n",
181 | "\n",
182 | "---\n",
183 | "\n",
184 | "### Hints\n",
185 | "\n",
186 | "> **_Use list comprehension to make an array._**\n",
187 | "\n",
188 | "---\n",
189 | "\n",
190 | "**Solution:**"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": null,
196 | "metadata": {},
197 | "outputs": [],
198 | "source": [
199 | "array = [[[0 for col in range(8)] for col in range(5)] for row in range(3)]\n",
200 | "print array"
201 | ]
202 | }
203 | ],
204 | "metadata": {
205 | "kernelspec": {
206 | "display_name": "Python 3",
207 | "language": "python",
208 | "name": "python3"
209 | },
210 | "language_info": {
211 | "codemirror_mode": {
212 | "name": "ipython",
213 | "version": 3
214 | },
215 | "file_extension": ".py",
216 | "mimetype": "text/x-python",
217 | "name": "python",
218 | "nbconvert_exporter": "python",
219 | "pygments_lexer": "ipython3",
220 | "version": "3.7.6"
221 | }
222 | },
223 | "nbformat": 4,
224 | "nbformat_minor": 4
225 | }
226 |
--------------------------------------------------------------------------------
/notebooks/Day_21.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 85\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155]._**\n",
12 | "\n",
13 | "---\n",
14 | "\n",
15 | "### Hints\n",
16 | "\n",
17 | "> **_Use list comprehension to delete a bunch of element from a list.Use enumerate() to get (index, value) tuple._**\n",
18 | "\n",
19 | "---\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "**Solutions:**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": null,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "li = [12, 24, 35, 70, 88, 120, 155]\n",
33 | "li = [li[i] for i in range(len(li)) if i not in (0, 4, 5)]\n",
34 | "print(li)"
35 | ]
36 | },
37 | {
38 | "cell_type": "markdown",
39 | "metadata": {},
40 | "source": [
41 | "---\n",
42 | "\n",
43 | "# Question 86\n",
44 | "\n",
45 | "### **Question**\n",
46 | "\n",
47 | "> **_By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155]._**\n",
48 | "\n",
49 | "---\n",
50 | "\n",
51 | "### Hints\n",
52 | "\n",
53 | "> **_Use list's remove method to delete a value._**\n",
54 | "\n",
55 | "---\n",
56 | "\n",
57 | "\n",
58 | "\n",
59 | "**Solutions:**"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": null,
65 | "metadata": {},
66 | "outputs": [],
67 | "source": [
68 | "li = [12, 24, 35, 24, 88, 120, 155]\n",
69 | "li.remove(24) # this will remove only the first occurrence of 24\n",
70 | "print(li)"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "---\n",
78 | "\n",
79 | "# Question 87\n",
80 | "\n",
81 | "### **Question**\n",
82 | "\n",
83 | "> **_With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists._**\n",
84 | "\n",
85 | "---\n",
86 | "\n",
87 | "### Hints\n",
88 | "\n",
89 | "> **_Use set() and \"&=\" to do set intersection operation._**\n",
90 | "\n",
91 | "---\n",
92 | "\n",
93 | "\n",
94 | "\n",
95 | "**Solutions:**"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": [
104 | "list1 = [1, 3, 6, 78, 35, 55]\n",
105 | "list2 = [12, 24, 35, 24, 88, 120, 155]\n",
106 | "set1 = set(list1)\n",
107 | "set2 = set(list2)\n",
108 | "intersection = set1 & set2\n",
109 | "print(intersection)"
110 | ]
111 | },
112 | {
113 | "cell_type": "markdown",
114 | "metadata": {},
115 | "source": [
116 | "**OR**"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": null,
122 | "metadata": {},
123 | "outputs": [],
124 | "source": [
125 | "list1 = [1, 3, 6, 78, 35, 55]\n",
126 | "list2 = [12, 24, 35, 24, 88, 120, 155]\n",
127 | "set1 = set(list1)\n",
128 | "set2 = set(list2)\n",
129 | "intersection = set.intersection(set1, set2)\n",
130 | "print(intersection)"
131 | ]
132 | },
133 | {
134 | "cell_type": "markdown",
135 | "metadata": {},
136 | "source": [
137 | "---\n",
138 | "\n",
139 | "# Question 88\n",
140 | "\n",
141 | "### **Question**\n",
142 | "\n",
143 | "> **_With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved._**\n",
144 | "\n",
145 | "---\n",
146 | "\n",
147 | "### Hints\n",
148 | "\n",
149 | "> **_Use set() to store a number of values without duplicate._**\n",
150 | "\n",
151 | "---\n",
152 | "\n",
153 | "\n",
154 | "\n",
155 | "**Solutions:**"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": null,
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "li = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155]\n",
165 | "for i in li:\n",
166 | " if li.count(i) > 1:\n",
167 | " li.remove(i)\n",
168 | "print(li)"
169 | ]
170 | },
171 | {
172 | "cell_type": "markdown",
173 | "metadata": {},
174 | "source": [
175 | "**OR**"
176 | ]
177 | },
178 | {
179 | "cell_type": "code",
180 | "execution_count": null,
181 | "metadata": {},
182 | "outputs": [],
183 | "source": [
184 | "def removeDuplicate(li):\n",
185 | " seen = {} # dictionary\n",
186 | " for item in li:\n",
187 | " if item not in seen:\n",
188 | " seen[item] = True\n",
189 | " yield item\n",
190 | "\n",
191 | "\n",
192 | "li = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155]\n",
193 | "ans = list(removeDuplicate(li))\n",
194 | "print(ans)"
195 | ]
196 | },
197 | {
198 | "cell_type": "markdown",
199 | "metadata": {},
200 | "source": [
201 | "---\n",
202 | "\n",
203 | "# Question 89\n",
204 | "\n",
205 | "### **Question**\n",
206 | "\n",
207 | "> **_Define a class Person and its two child classes: Male and Female. All classes have a method \"getGender\" which can print \"Male\" for Male class and \"Female\" for Female class._**\n",
208 | "\n",
209 | "---\n",
210 | "\n",
211 | "### Hints\n",
212 | "\n",
213 | "> **_Use Subclass(Parentclass) to define a child class._**\n",
214 | "\n",
215 | "---\n",
216 | "\n",
217 | "**Solution:**"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": null,
223 | "metadata": {},
224 | "outputs": [],
225 | "source": [
226 | "class Person(object):\n",
227 | " def getGender(self):\n",
228 | " return \"Unknown\"\n",
229 | "\n",
230 | "\n",
231 | "class Male(Person):\n",
232 | " def getGender(self):\n",
233 | " return \"Male\"\n",
234 | "\n",
235 | "\n",
236 | "class Female(Person):\n",
237 | " def getGender(self):\n",
238 | " return \"Female\"\n",
239 | "\n",
240 | "\n",
241 | "aMale = Male()\n",
242 | "aFemale = Female()\n",
243 | "print(aMale.getGender())\n",
244 | "print(aFemale.getGender())"
245 | ]
246 | },
247 | {
248 | "cell_type": "markdown",
249 | "metadata": {},
250 | "source": [
251 | "---"
252 | ]
253 | }
254 | ],
255 | "metadata": {
256 | "kernelspec": {
257 | "display_name": "Python 3",
258 | "language": "python",
259 | "name": "python3"
260 | },
261 | "language_info": {
262 | "codemirror_mode": {
263 | "name": "ipython",
264 | "version": 3
265 | },
266 | "file_extension": ".py",
267 | "mimetype": "text/x-python",
268 | "name": "python",
269 | "nbconvert_exporter": "python",
270 | "pygments_lexer": "ipython3",
271 | "version": "3.7.6"
272 | }
273 | },
274 | "nbformat": 4,
275 | "nbformat_minor": 4
276 | }
277 |
--------------------------------------------------------------------------------
/notebooks/Day_22.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 90\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_Please write a program which count and print the numbers of each character in a string input by console._**\n",
12 | "\n",
13 | "> **_Example:\n",
14 | "> If the following string is given as input to the program:_**\n",
15 | "\n",
16 | "```\n",
17 | "abcdefgabc\n",
18 | "```\n",
19 | "\n",
20 | "> **_Then, the output of the program should be:_**\n",
21 | "\n",
22 | "```\n",
23 | "a,2\n",
24 | "c,2\n",
25 | "b,2\n",
26 | "e,1\n",
27 | "d,1\n",
28 | "g,1\n",
29 | "f,1\n",
30 | "```\n",
31 | "\n",
32 | "### Hints\n",
33 | "\n",
34 | "> **_Use dict to store key/value pairs.\n",
35 | "> Use dict.get() method to lookup a key with default value._**\n",
36 | "\n",
37 | "---\n",
38 | "\n",
39 | "**Solutions:**"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": null,
45 | "metadata": {},
46 | "outputs": [],
47 | "source": [
48 | "import string\n",
49 | "\n",
50 | "s = input()\n",
51 | "for letter in string.ascii_lowercase:\n",
52 | " cnt = s.count(letter)\n",
53 | " if cnt > 0:\n",
54 | " print(\"{},{}\".format(letter, cnt))"
55 | ]
56 | },
57 | {
58 | "cell_type": "markdown",
59 | "metadata": {},
60 | "source": [
61 | "**OR**"
62 | ]
63 | },
64 | {
65 | "cell_type": "code",
66 | "execution_count": null,
67 | "metadata": {},
68 | "outputs": [],
69 | "source": [
70 | "s = input()\n",
71 | "for letter in range(ord(\"a\"), ord(\"z\") + 1): # ord() gets the ascii value of a char\n",
72 | " letter = chr(letter) # chr() gets the char of an ascii value\n",
73 | " cnt = s.count(letter)\n",
74 | " if cnt > 0:\n",
75 | " print(\"{},{}\".format(letter, cnt))"
76 | ]
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {},
81 | "source": [
82 | "---\n",
83 | "\n",
84 | "# Question 91\n",
85 | "\n",
86 | "### **Question**\n",
87 | "\n",
88 | "> **_Please write a program which accepts a string from console and print it in reverse order._**\n",
89 | "\n",
90 | "> **Example:\n",
91 | "> If the following string is given as input to the program:\\***\n",
92 | "\n",
93 | "\n",
94 | "rise to vote sir\n",
95 | "\n",
96 | "\n",
97 | "> **_Then, the output of the program should be:_**\n",
98 | "\n",
99 | "ris etov ot esir\n",
100 | "\n",
101 | "### Hints\n",
102 | "\n",
103 | "> **_Use list[::-1] to iterate a list in a reverse order._**\n",
104 | "\n",
105 | "---\n",
106 | "\n",
107 | "**Solutions:**"
108 | ]
109 | },
110 | {
111 | "cell_type": "code",
112 | "execution_count": null,
113 | "metadata": {},
114 | "outputs": [],
115 | "source": [
116 | "s = input()\n",
117 | "s = \"\".join(reversed(s))\n",
118 | "print(s)"
119 | ]
120 | },
121 | {
122 | "cell_type": "markdown",
123 | "metadata": {},
124 | "source": [
125 | "---\n",
126 | "\n",
127 | "# Question 92\n",
128 | "\n",
129 | "### **Question**\n",
130 | "\n",
131 | "> **_Please write a program which accepts a string from console and print the characters that have even indexes._**\n",
132 | "\n",
133 | "> **_Example:\n",
134 | "> If the following string is given as input to the program:_**\n",
135 | "```\n",
136 | "H1e2l3l4o5w6o7r8l9d\n",
137 | "```\n",
138 | "> **_Then, the output of the program should be:_**\n",
139 | "```\n",
140 | "Helloworld\n",
141 | "```\n",
142 | "### Hints\n",
143 | "\n",
144 | "> **_Use list[::2] to iterate a list by step 2._**\n",
145 | "\n",
146 | "---\n",
147 | "\n",
148 | "\n",
149 | "\n",
150 | "**Solutions:**"
151 | ]
152 | },
153 | {
154 | "cell_type": "code",
155 | "execution_count": null,
156 | "metadata": {},
157 | "outputs": [],
158 | "source": [
159 | "s = \"H1e2l3l4o5w6o7r8l9d\"\n",
160 | "s = [s[i] for i in range(len(s)) if i % 2 == 0]\n",
161 | "print(\"\".join(s))"
162 | ]
163 | },
164 | {
165 | "cell_type": "markdown",
166 | "metadata": {},
167 | "source": [
168 | "**OR**"
169 | ]
170 | },
171 | {
172 | "cell_type": "code",
173 | "execution_count": null,
174 | "metadata": {},
175 | "outputs": [],
176 | "source": [
177 | "s = \"H1e2l3l4o5w6o7r8l9d\"\n",
178 | "ns = \"\"\n",
179 | "for i in range(len(s)):\n",
180 | " if i % 2 == 0:\n",
181 | " ns += s[i]\n",
182 | "print(ns)"
183 | ]
184 | },
185 | {
186 | "cell_type": "markdown",
187 | "metadata": {},
188 | "source": [
189 | "---\n",
190 | "\n",
191 | "# Question 93\n",
192 | "\n",
193 | "### **Question**\n",
194 | "\n",
195 | "> **_Please write a program which prints all permutations of [1,2,3]_**\n",
196 | "\n",
197 | "---\n",
198 | "\n",
199 | "### Hints\n",
200 | "\n",
201 | "> **_Use itertools.permutations() to get permutations of list._**\n",
202 | "\n",
203 | "---\n",
204 | "\n",
205 | "**Solution:**"
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "execution_count": 1,
211 | "metadata": {},
212 | "outputs": [
213 | {
214 | "name": "stdout",
215 | "output_type": "stream",
216 | "text": [
217 | "[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]\n"
218 | ]
219 | }
220 | ],
221 | "source": [
222 | "import itertools\n",
223 | "\n",
224 | "print(list(itertools.permutations([1, 2, 3])))"
225 | ]
226 | },
227 | {
228 | "cell_type": "markdown",
229 | "metadata": {},
230 | "source": [
231 | "---\n",
232 | "\n",
233 | "# Question 94\n",
234 | "\n",
235 | "### **Question**\n",
236 | "\n",
237 | "> **_Write a program to solve a classic ancient Chinese puzzle:\n",
238 | "> We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?_**\n",
239 | "\n",
240 | "---\n",
241 | "\n",
242 | "### Hints\n",
243 | "\n",
244 | "> **_Use for loop to iterate all possible solutions._**\n",
245 | "\n",
246 | "---\n",
247 | "\n",
248 | "**Solution:**"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": 3,
254 | "metadata": {
255 | "code_folding": []
256 | },
257 | "outputs": [
258 | {
259 | "name": "stdout",
260 | "output_type": "stream",
261 | "text": [
262 | "(23, 12)\n"
263 | ]
264 | }
265 | ],
266 | "source": [
267 | "def solve(numheads, numlegs):\n",
268 | " ns = \"No solutions!\"\n",
269 | " for i in range(numheads + 1):\n",
270 | " j = numheads - i\n",
271 | " if 2 * i + 4 * j == numlegs:\n",
272 | " return i, j\n",
273 | " return ns, ns\n",
274 | "\n",
275 | "\n",
276 | "numheads = 35\n",
277 | "numlegs = 94\n",
278 | "solutions = solve(numheads, numlegs)\n",
279 | "print(solutions)"
280 | ]
281 | },
282 | {
283 | "cell_type": "markdown",
284 | "metadata": {},
285 | "source": [
286 | "---"
287 | ]
288 | }
289 | ],
290 | "metadata": {
291 | "kernelspec": {
292 | "display_name": "Python 3",
293 | "language": "python",
294 | "name": "python3"
295 | },
296 | "language_info": {
297 | "codemirror_mode": {
298 | "name": "ipython",
299 | "version": 3
300 | },
301 | "file_extension": ".py",
302 | "mimetype": "text/x-python",
303 | "name": "python",
304 | "nbconvert_exporter": "python",
305 | "pygments_lexer": "ipython3",
306 | "version": "3.7.6"
307 | }
308 | },
309 | "nbformat": 4,
310 | "nbformat_minor": 4
311 | }
312 |
--------------------------------------------------------------------------------
/notebooks/Day_24.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Question 100\n",
8 | "\n",
9 | "### **Question**\n",
10 | "\n",
11 | "> **_You are given words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification._**\n",
12 | "\n",
13 | "> **_If the following string is given as input to the program:_**\n",
14 | ">\n",
15 | "> 4\n",
16 | "> bcdef\n",
17 | "> abcdefg\n",
18 | "> bcde\n",
19 | "> bcdef\n",
20 | ">\n",
21 | "> **_Then, the output of the program should be:_**\n",
22 | ">\n",
23 | "> 3\n",
24 | "> 2 1 1\n",
25 | "\n",
26 | "### Hints\n",
27 | "\n",
28 | "> **_Make a list to get the input order and a dictionary to count the word frequency_**\n",
29 | "\n",
30 | "---\n",
31 | "\n",
32 | "**Solutions:**"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "n = int(input())\n",
42 | "\n",
43 | "word_list = []\n",
44 | "word_dict = {}\n",
45 | "\n",
46 | "for i in range(n):\n",
47 | " word = input()\n",
48 | " if word not in word_dict:\n",
49 | " word_list.append(word)\n",
50 | " word_dict[word] = word_dict.get(word, 0) + 1\n",
51 | "\n",
52 | "print(len(word_list))\n",
53 | "for word in word_list:\n",
54 | " print(word_dict[word], end=\" \")"
55 | ]
56 | },
57 | {
58 | "cell_type": "markdown",
59 | "metadata": {},
60 | "source": [
61 | "---\n",
62 | "\n",
63 | "# Question 101\n",
64 | "\n",
65 | "### **Question**\n",
66 | "\n",
67 | "> **_You are given a string.Your task is to count the frequency of letters of the string and print the letters in descending order of frequency._**\n",
68 | "\n",
69 | "> **_If the following string is given as input to the program:_**\n",
70 | ">\n",
71 | "> aabbbccde\n",
72 | ">\n",
73 | "> **_Then, the output of the program should be:_**\n",
74 | ">\n",
75 | "> b 3\n",
76 | "> a 2\n",
77 | "> c 2\n",
78 | "> d 1\n",
79 | "> e 1\n",
80 | "\n",
81 | "### Hints\n",
82 | "\n",
83 | "> **_Count frequency with dictionary and sort by Value from dictionary Items_**\n",
84 | "\n",
85 | "---\n",
86 | "\n",
87 | "**Solutions:**"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {},
94 | "outputs": [],
95 | "source": [
96 | "word = input()\n",
97 | "dct = {}\n",
98 | "for i in word:\n",
99 | " dct[i] = dct.get(i, 0) + 1\n",
100 | "\n",
101 | "dct = sorted(dct.items(), key=lambda x: (-x[1], x[0]))\n",
102 | "for i in dct:\n",
103 | " print(i[0], i[1])"
104 | ]
105 | },
106 | {
107 | "cell_type": "markdown",
108 | "metadata": {},
109 | "source": [
110 | "---"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": null,
116 | "metadata": {},
117 | "outputs": [],
118 | "source": [
119 | "\"\"\"Solution by: yuan1z\"\"\"\n",
120 | "\n",
121 | "X = input()\n",
122 | "my_set = set(X)\n",
123 | "arr = []\n",
124 | "for item in my_set:\n",
125 | " arr.append([item, X.count(item)])\n",
126 | "tmp = sorted(arr, key=lambda x: (-x[1], x[0]))\n",
127 | "\n",
128 | "for i in tmp:\n",
129 | " print(i[0] + \" \" + str(i[1]))"
130 | ]
131 | },
132 | {
133 | "cell_type": "markdown",
134 | "metadata": {},
135 | "source": [
136 | "---"
137 | ]
138 | },
139 | {
140 | "cell_type": "code",
141 | "execution_count": null,
142 | "metadata": {},
143 | "outputs": [],
144 | "source": [
145 | "\"\"\"Solution by: StartZer0\"\"\"\n",
146 | "\n",
147 | "s = list(input())\n",
148 | "\n",
149 | "dict_count_ = {k: s.count(k) for k in s}\n",
150 | "list_of_tuples = [(k, v) for k, v in dict_count_.items()]\n",
151 | "list_of_tuples.sort(key=lambda x: x[1], reverse=True)\n",
152 | "\n",
153 | "for item in list_of_tuples:\n",
154 | " print(item[0], item[1])"
155 | ]
156 | },
157 | {
158 | "cell_type": "markdown",
159 | "metadata": {},
160 | "source": [
161 | "---\n",
162 | "\n",
163 | "# Question 102\n",
164 | "\n",
165 | "### **Question**\n",
166 | "\n",
167 | "> **_Write a Python program that accepts a string and calculate the number of digits and letters._**\n",
168 | "\n",
169 | "**Input**\n",
170 | "\n",
171 | "> Hello321Bye360\n",
172 | "\n",
173 | "**Output**\n",
174 | "\n",
175 | "> Digit - 6\n",
176 | "\n",
177 | "> Letter - 8\n",
178 | "\n",
179 | "---\n",
180 | "\n",
181 | "### Hints\n",
182 | "\n",
183 | "> **_Use isdigit() and isalpha() function_**\n",
184 | "\n",
185 | "---\n",
186 | "\n",
187 | "**Solution:**"
188 | ]
189 | },
190 | {
191 | "cell_type": "code",
192 | "execution_count": null,
193 | "metadata": {},
194 | "outputs": [],
195 | "source": [
196 | "word = input()\n",
197 | "digit, letter = 0, 0\n",
198 | "for char in word:\n",
199 | " digit += char.isdigit()\n",
200 | " letter += char.isalpha()\n",
201 | "\n",
202 | "print(\"Digit -\", digit)\n",
203 | "print(\"Letter -\", letter)"
204 | ]
205 | },
206 | {
207 | "cell_type": "markdown",
208 | "metadata": {},
209 | "source": [
210 | "---\n",
211 | "\n",
212 | "# Question 103\n",
213 | "\n",
214 | "### **Question**\n",
215 | "\n",
216 | "> **_Given a number N.Find Sum of 1 to N Using Recursion_**\n",
217 | "\n",
218 | "**Input**\n",
219 | "\n",
220 | "> 5\n",
221 | "\n",
222 | "**Output**\n",
223 | "\n",
224 | "> 15\n",
225 | "\n",
226 | "---\n",
227 | "\n",
228 | "### Hints\n",
229 | "\n",
230 | "> **_Make a recursive function to get the sum_**\n",
231 | "\n",
232 | "---\n",
233 | "\n",
234 | "**Solution:**"
235 | ]
236 | },
237 | {
238 | "cell_type": "code",
239 | "execution_count": null,
240 | "metadata": {},
241 | "outputs": [],
242 | "source": [
243 | "def rec(n):\n",
244 | " if n == 0:\n",
245 | " return n\n",
246 | " return rec(n - 1) + n\n",
247 | "\n",
248 | "\n",
249 | "n = int(input())\n",
250 | "sum = rec(n)\n",
251 | "print(sum)"
252 | ]
253 | },
254 | {
255 | "cell_type": "markdown",
256 | "metadata": {},
257 | "source": [
258 | "---\n",
259 | "\n",
260 | "# To Be Continued..."
261 | ]
262 | }
263 | ],
264 | "metadata": {
265 | "kernelspec": {
266 | "display_name": "Python 3",
267 | "language": "python",
268 | "name": "python3"
269 | },
270 | "language_info": {
271 | "codemirror_mode": {
272 | "name": "ipython",
273 | "version": 3
274 | },
275 | "file_extension": ".py",
276 | "mimetype": "text/x-python",
277 | "name": "python",
278 | "nbconvert_exporter": "python",
279 | "pygments_lexer": "ipython3",
280 | "version": "3.7.6"
281 | }
282 | },
283 | "nbformat": 4,
284 | "nbformat_minor": 4
285 | }
286 |
--------------------------------------------------------------------------------