├── .gitignore ├── .idea └── vcs.xml ├── 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 /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-programming-exercises 2 | --------------------- 3 | ## Introduction 4 | 5 | ***The exercise text contents of this repository was collected from GitHub account of [zhiwehu](https://github.com/zhiwehu/Python-programming-exercises). I collected it to practice and solve all the listed problems with python. Even after these collected problems are all set up, I will try to add more problems in near future. If you are a very beginner with python then I hope this 100+ exercise will help you a lot to get your hands free with python.*** 6 | 7 | ***One will find the given problems very simple and easy to understand. A beginner can try 3-5 problems a day which will take a little time to solve but definitely will learn a couple of new stuff (no matter how lazy you are :P ). And after regular practice of only a month, one can find himself solved more than 100++ problems which are obviously not a deniable achievement.*** 8 | 9 | ***In this repository, I will be gradually updating the codebase of the given problems with my own solutions. Also, I may try to explain the code and tell my opinion about the problem if needed. Main Authors solutions are in python 2 & my solutions will be in python 3. Every problem is divided into a template format which is discussed below. There is a [discussion](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) section so don't forget to share your opinion, ideas and feel free to discuss anything wrong or mistake*** 10 | 11 | ---------------- 12 | 13 | # 100+ Python challenging programming exercises 14 | 15 | 16 | ## 1. Problem Template 17 | 18 | * ***Question*** 19 | * ***Hints*** 20 | * ***Solution*** 21 | 22 | ----------------- 23 | 24 | ## 2. Practice Status 25 | 26 | * **[Day 1](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%201.md "Day 1 Status")**- ***Question 1-3*** 27 | 28 | * **[Day 2](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md "Day 2 Status")**- ***Question 4-9*** 29 | 30 | * **[Day 3](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%203.md "Day 3 Status")**- ***Question 10-13*** 31 | 32 | 33 | * **[Day 4](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%204.md "Day 4 Status")**- ***Question 14-15*** 34 | 35 | 36 | * **[Day 5](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%205.md "Day 5 Status")**- ***Question 16-17*** 37 | 38 | 39 | * **[Day 6](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%206.md "Day 6 Status")**- ***Question 18-19*** 40 | 41 | * **[Day 7](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%207.md "Day 7 Status")**- ***Question 20-21*** 42 | 43 | 44 | * **[Day 8](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%208.md "Day 8 Status")**- ***Question 22-25*** 45 | 46 | 47 | * **[Day 9](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%209.md "Day 9 Status")**- ***Question 26-30*** 48 | 49 | 50 | * **[Day 10](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_10.md "Day 10 Status")**- ***Question 31-37*** 51 | 52 | * **[Day 11](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_11.md "Day 11 Status")**- ***Question 38-43*** 53 | 54 | * **[Day 12](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_12.md "Day 12 Status")**- ***Question 44-46*** 55 | 56 | 57 | * **[Day 13](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_13.md "Day 13 Status")**- ***Question 47-50*** 58 | 59 | 60 | * **[Day 14](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_14.md "Day 14 Status")**- ***Question 51-53*** 61 | 62 | * **[Day 15](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_15.md "Day 15 Status")**- ***Question 54-59*** 63 | 64 | * **[Day 16](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_16.md "Day 16 Status")**- ***Question 60-64*** 65 | 66 | 67 | * **[Day 17](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_17.md "Day 17 Status")**- ***Question 65-69*** 68 | 69 | 70 | * **[Day 18](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_18.md "Day 18 Status")**- ***Question 70-74*** 71 | 72 | 73 | * **[Day 19](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_19.md "Day 19 Status")**- ***Question 75-79*** 74 | 75 | 76 | * **[Day 20](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_20.md "Day 20 Status")**- ***Question 80-84*** 77 | 78 | * **[Day 21](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_21.md "Day 21 Status")**- ***Question 85-89*** 79 | 80 | * **[Day 22](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_22.md "Day 22 Status")**- ***Question 90-94*** 81 | 82 | * **[Day 23](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_23.md "Day 23 Status")**- ***Question 95-99*** 83 | 84 | * **[Day 24](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_24.md "Day 24 Status")**- ***Question 100-103*** -------------------------------------------------------------------------------- /Status/Day 1.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 1 3 | 4 | ### **Question:** 5 | 6 | > ***Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, 7 | between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line.*** 8 | 9 | -------------------------------------- 10 | ### Hints: 11 | > ***Consider use range(#begin, #end) method.*** 12 | 13 | --------------------------------------- 14 | 15 | **Main author's Solution: Python 2** 16 | ```python 17 | l=[] 18 | for i in range(2000, 3201): 19 | if (i%7==0) and (i%5!=0): 20 | l.append(str(i)) 21 | 22 | print ','.join(l) 23 | ``` 24 | ---------------------------------------- 25 | 26 | **My Solution: Python 3** 27 | ```python 28 | for i in range(2000,3201): 29 | if i%7 == 0 and i%5!=0: 30 | print(i,end=',') 31 | print("\b") 32 | ``` 33 | ------------------------------- 34 | 35 | # Question 2 36 | 37 | ### **Question:** 38 | 39 | > ***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 40 | Then, the output should be:40320*** 41 | 42 | -------------------- 43 | ### Hints: 44 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 45 | 46 | --------------- 47 | **Main author's Solution: Python 2** 48 | ```python 49 | def fact(x): 50 | if x == 0: 51 | return 1 52 | return x * fact(x - 1) 53 | 54 | x = int(raw_input()) 55 | print fact(x) 56 | ``` 57 | ------------ 58 | **My Solution: Python 3** 59 | 60 | * **Using While Loop** 61 | ```python 62 | n = int(raw_input()) #input() function takes input as string type 63 | #int() converts it to integer type 64 | fact = 1 65 | i = 1 66 | while i <= n: 67 | fact = fact * i; 68 | i = i + 1 69 | print(fact) 70 | ``` 71 | * **Using For Loop** 72 | ```python 73 | n = int(input()) #input() function takes input as string type 74 | #int() converts it to integer type 75 | fact = 1 76 | for i in range(1,n+1): 77 | fact = fact * i 78 | print(fact) 79 | ``` 80 | ------------------- 81 | 82 | # Question 3 83 | 84 | ### **Question:** 85 | 86 | >***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*** 87 | 88 | >***Then, the output should be:*** 89 | ``` 90 | {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64} 91 | ``` 92 | ------------------ 93 | 94 | ### Hints: 95 | >***In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()*** 96 | 97 | ----------------- 98 | 99 | **Main author's Solution: Python 2** 100 | ```python 101 | n = int(raw_input()) 102 | d = dict() 103 | for i in range(1,n+1): 104 | d[i] = i * i 105 | print d 106 | ``` 107 | 108 | **My Solution: Python 3:** 109 | ```python 110 | n = int(input()) 111 | ans = {} 112 | for i in range (1,n+1): 113 | ans[i] = i * i 114 | print(ans) 115 | ``` 116 | **OR** 117 | ```python 118 | # This is done with dictionary comprehension method 119 | n = int(input()) 120 | ans={i : i*i for i in range(1,n+1)} 121 | print(ans) 122 | ``` 123 | ---------------------------------- 124 | 125 | ## Conclusion 126 | ***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.*** 127 | 128 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md "Next Day") 129 | 130 | 131 | 132 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day 2.md: -------------------------------------------------------------------------------- 1 | # Question 4 2 | 3 | ### **Question:** 4 | 5 | >***Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.Suppose the following input is supplied to the program:*** 6 | 7 | ``` 8 | 34,67,55,33,12,98 9 | ``` 10 | 11 | 12 | >***Then, the output should be:*** 13 | ``` 14 | ['34', '67', '55', '33', '12', '98'] 15 | ('34', '67', '55', '33', '12', '98') 16 | ``` 17 | 18 | ### Hints: 19 | >***In case of input data being supplied to the question, it should be assumed to be a console input.tuple() method can convert list to tuple*** 20 | 21 | ----------------------- 22 | 23 | **Main author's Solution: Python 2** 24 | ```python 25 | values = raw_input() 26 | l = values.split(",") 27 | t = tuple(l) 28 | print l 29 | print t 30 | ``` 31 | ------------------------- 32 | 33 | **My Solution: Python 3** 34 | ```python 35 | lst = input().split(',') # the input is being taken as string and as it is string it has a built in 36 | # method name split. ',' inside split function does split where it finds any ',' 37 | # and save the input as list in lst variable 38 | 39 | tpl = tuple(lst) # tuple method converts list to tuple 40 | 41 | print(lst) 42 | print(tpl) 43 | ``` 44 | -------------------------- 45 | # Question 5 46 | 47 | ### **Question:** 48 | 49 | >***Define a class which has at least two methods:*** 50 | >* ***getString: to get a string from console input*** 51 | >* ***printString: to print the string in upper case.*** 52 | 53 | >***Also please include simple test function to test the class methods.*** 54 | 55 | ### Hints: 56 | >***Use __init__ method to construct some parameters*** 57 | 58 | ---------------------------------- 59 | **Main author's Solution: Python 2** 60 | ```python 61 | class InputOutString(object): 62 | def __init__(self): 63 | self.s = "" 64 | 65 | def getString(self): 66 | self.s = raw_input() 67 | 68 | def printString(self): 69 | print self.s.upper() 70 | 71 | strObj = InputOutString() 72 | strObj.getString() 73 | strObj.printString() 74 | ``` 75 | ---------------------------------------- 76 | **My Solution: Python 3** 77 | ```python 78 | class IOstring(): 79 | def __init__(self): 80 | pass 81 | 82 | def getString(self): 83 | self.s = input() 84 | 85 | def printString(self): 86 | print(self.s.upper()) 87 | 88 | xx = IOstring() 89 | xx.getString() 90 | xx.printString() 91 | ``` 92 | -------------------------- 93 | # Question 6 94 | 95 | ### **Question:** 96 | 97 | >***Write a program that calculates and prints the value according to the given formula:*** 98 | 99 | >***Q = Square root of [(2 * C * D)/H]*** 100 | 101 | >***Following are the fixed values of C and H:*** 102 | 103 | >***C is 50. H is 30.*** 104 | 105 | >***D is the variable whose values should be input to your program in a comma-separated sequence.For example 106 | Let us assume the following comma separated input sequence is given to the program:*** 107 | ``` 108 | 100,150,180 109 | ``` 110 | >***The output of the program should be:*** 111 | ``` 112 | 18,22,24 113 | ``` 114 | -------------------------- 115 | 116 | ### Hints: 117 | >***If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26).In case of input data being supplied to the question, it should be assumed to be a console input.*** 118 | 119 | ---------------------------- 120 | 121 | **Main author's Solution: Python 2** 122 | ```python 123 | #!/usr/bin/env python 124 | import math 125 | c = 50 126 | h = 30 127 | value = [] 128 | items = [x for x in raw_input().split(',')] 129 | for d in items: 130 | value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) 131 | 132 | print ','.join(value) 133 | ``` 134 | -------------------------------- 135 | 136 | **My Solution: Python 3** 137 | ```python 138 | from math import * # importing all math functions 139 | 140 | C,H = 50,30 141 | 142 | def calc(D): 143 | return sqrt((2*C*D)/H) 144 | 145 | D = input().split(',') # splits in comma position and set up in list 146 | D = [int(i) for i in D] # converts string to integer 147 | D = [calc(i) for i in D] # returns floating value by calc method for every item in D 148 | D = [round(i) for i in D] # All the floating values are rounded 149 | D = [str(i) for i in D] # All the integers are converted to string to be able to apply join operation 150 | 151 | print(",".join(D)) 152 | ``` 153 | **OR** 154 | ```python 155 | from math import * # importing all math functions 156 | 157 | C,H = 50,30 158 | 159 | def calc(D): 160 | return sqrt((2*C*D)/H) 161 | 162 | D = input().split(',') # splits in comma position and set up in list 163 | D = [str(round(calc(int(i)))) for i in D] # using comprehension method. It works in order of the previous code 164 | print(",".join(D)) 165 | ``` 166 | **OR** 167 | ```python 168 | from math import * 169 | C,H = 50,30 170 | 171 | def calc(D): 172 | return sqrt((2*C*D)/H) 173 | 174 | print(",".join([str(int(calc(int(i)))) for i in input().split(',')])) 175 | ``` 176 | **OR** 177 | ```python 178 | from math import * # importing all math functions 179 | C,H = 50,30 180 | 181 | def calc(D): 182 | D = int(D) 183 | return str(int(sqrt((2*C*D)/H))) 184 | 185 | D = input().split(',') 186 | D = list(map(calc,D)) # applying calc function on D and storing as a list 187 | print(",".join(D)) 188 | ``` 189 | --------------------- 190 | # Question 7 191 | 192 | ### **Question:** 193 | 194 | >***Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i * j.*** 195 | 196 | >***Note: i=0,1.., X-1; j=0,1,¡­Y-1. Suppose the following inputs are given to the program: 3,5*** 197 | 198 | >***Then, the output of the program should be:*** 199 | ``` 200 | [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] 201 | ``` 202 | 203 | ------------------------------- 204 | 205 | ### Hints: 206 | >***Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form.*** 207 | 208 | ------------------ 209 | **Main author's Solution: Python 2** 210 | ```python 211 | input_str = raw_input() 212 | dimensions = [int(x) for x in input_str.split(',')] 213 | rowNum = dimensions[0] 214 | colNum = dimensions[1] 215 | multilist = [[0 for col in range(colNum)] for row in range(rowNum)] 216 | 217 | for row in range(rowNum): 218 | for col in range(colNum): 219 | multilist[row][col] = row * col 220 | 221 | print multilist 222 | ``` 223 | ----------------------- 224 | **My Solution: Python 3** 225 | ```python 226 | x,y = map(int,input().split(',')) 227 | lst = [] 228 | 229 | for i in range(x): 230 | tmp = [] 231 | for j in range(y): 232 | tmp.append(i*j) 233 | lst.append(tmp) 234 | 235 | print(lst) 236 | ``` 237 | **OR** 238 | ```python 239 | x,y = map(int,input().split(',')) 240 | lst = [[i*j for j in range(y)] for i in range(x)] 241 | print(lst) 242 | ``` 243 | --------------------------- 244 | # Question 8 245 | 246 | ### **Question:** 247 | 248 | >***Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.*** 249 | 250 | >***Suppose the following input is supplied to the program:*** 251 | ``` 252 | without,hello,bag,world 253 | ``` 254 | >***Then, the output should be:*** 255 | ``` 256 | bag,hello,without,world 257 | ``` 258 | 259 | ---------------------- 260 | ### Hints: 261 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 262 | 263 | ------------------- 264 | **Main author's Solution: Python 2** 265 | ```python 266 | items = [x for x in raw_input().split(',')] 267 | items.sort() 268 | print ','.join(items) 269 | ``` 270 | ---------------- 271 | **My Solution: Python 3** 272 | ```python 273 | lst = input().split(',') 274 | lst.sort() 275 | print(",".join(lst)) 276 | ``` 277 | ------------------------------- 278 | # Question 9 279 | 280 | ### **Question:** 281 | 282 | >***Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.*** 283 | 284 | >***Suppose the following input is supplied to the program:*** 285 | ``` 286 | Hello world 287 | Practice makes perfect 288 | ``` 289 | >***Then, the output should be:*** 290 | ``` 291 | HELLO WORLD 292 | PRACTICE MAKES PERFECT 293 | ``` 294 | 295 | ---------------------- 296 | ### Hints: 297 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 298 | 299 | ------------------- 300 | **Main author's Solution: Python 2** 301 | ```python 302 | lines = [] 303 | while True: 304 | s = raw_input() 305 | if s: 306 | lines.append(s.upper()) 307 | else: 308 | break; 309 | 310 | for sentence in lines: 311 | print sentence 312 | ``` 313 | ---------------- 314 | **My Solution: Python 3** 315 | ```python 316 | lst = [] 317 | 318 | while True: 319 | x = input() 320 | if len(x)==0: 321 | break; 322 | lst.append(x) 323 | 324 | for line in lst: 325 | print(line) 326 | ``` 327 | -------------------- 328 | 329 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%201.md "Day 1") 330 | 331 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%203.md "Day 3") 332 | 333 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day 3.md: -------------------------------------------------------------------------------- 1 | # Question 10 2 | 3 | ### **Question** 4 | 5 | >***Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.*** 6 | 7 | >***Suppose the following input is supplied to the program:*** 8 | ``` 9 | hello world and practice makes perfect and hello world again 10 | ``` 11 | >***Then, the output should be:*** 12 | ``` 13 | again and hello makes perfect practice world 14 | ``` 15 | 16 | ---------------------- 17 | 18 | ### Hints: 19 | >***In case of input data being supplied to the question, it should be assumed to be a console input.We use set container to remove duplicated data automatically and then use sorted() to sort the data.*** 20 | 21 | ------------------- 22 | **Main author's Solution: Python 2** 23 | ```python 24 | s = raw_input() 25 | words = [word for word in s.split(" ")] 26 | print " ".join(sorted(list(set(words)))) 27 | ``` 28 | ---------------- 29 | **My Solution: Python 3** 30 | ```python 31 | word = input().split() 32 | 33 | for i in word: 34 | if word.count(i) > 1: #count function returns total repeatation of an element that is send as argument 35 | word.remove(i) # removes exactly one element per call 36 | 37 | word.sort() 38 | print(" ".join(word)) 39 | ``` 40 | **OR** 41 | ```python 42 | word = input().split() 43 | [word.remove(i) for i in word if word.count(i) > 1 ] # removal operation with comprehension method 44 | word.sort() 45 | print(" ".join(word)) 46 | ``` 47 | **OR** 48 | ```python 49 | word = sorted(list(set(input().split()))) # input string splits -> converting into set() to store unique 50 | # element -> converting into list to be able to apply sort 51 | print(" ".join(word)) 52 | ``` 53 | --------------------------- 54 | 55 | # Question 11 56 | 57 | ### **Question** 58 | 59 | >***Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.*** 60 | 61 | >***Example:*** 62 | ``` 63 | 0100,0011,1010,1001 64 | ``` 65 | >***Then the output should be:*** 66 | ``` 67 | 1010 68 | ``` 69 | >***Notes: Assume the data is input by console.*** 70 | 71 | ---------------------- 72 | 73 | ### Hints: 74 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 75 | 76 | ------------------- 77 | **Main author's Solution: Python 2** 78 | ```python 79 | value = [] 80 | items=[x for x in raw_input().split(',')] 81 | for p in items: 82 | intp = int(p,2) 83 | if not intp % 5: 84 | value.append(p) 85 | 86 | print ','.join(value) 87 | ``` 88 | ---------------- 89 | **My Solution: Python 3** 90 | ```python 91 | def check(x): # converts binary to integer & returns zero if divisible by 5 92 | total,pw = 0,1 93 | reversed(x) 94 | 95 | for i in x: 96 | total+=pw * (ord(i) - 48) # ord() function returns ASCII value 97 | pw*=2 98 | return total % 5 99 | 100 | data = input().split(",") # inputs taken here and splited in ',' position 101 | lst = [] 102 | 103 | for i in data: 104 | if check(i) == 0: # if zero found it means divisible by zero and added to the list 105 | lst.append(i) 106 | 107 | print(",".join(lst)) 108 | ``` 109 | **OR** 110 | ```python 111 | def check(x): # check function returns true if divisible by 5 112 | return int(x,2)%5 == 0 # int(x,b) takes x as string and b as base from which 113 | # it will be converted to decimal 114 | data = input().split(',') 115 | 116 | data = list(filter(check,data)) # in filter(func,object) function, elements are picked from 'data' if found True by 'check' function 117 | print(",".join(data)) 118 | ``` 119 | **OR** 120 | ```python 121 | data = input().split(',') 122 | data = list(filter(lambda i:int(i,2)%5==0,data)) # lambda is an operator that helps to write function of one line 123 | print(",".join(data)) 124 | ``` 125 | ------------------------- 126 | 127 | # Question 12 128 | 129 | ### **Question:** 130 | 131 | >***Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.The numbers obtained should be printed in a comma-separated sequence on a single line.*** 132 | 133 | ---------------------- 134 | 135 | ### Hints: 136 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 137 | 138 | ------------------- 139 | **Main author's Solution: Python 2** 140 | ```python 141 | values = [] 142 | for i in range(1000, 3001): 143 | s = str(i) 144 | if (int(s[0])%2 == 0) and (int(s[1])%2 == 0) and (int(s[2])%2 == 0) and (int(s[3])%2 == 0): 145 | values.append(s) 146 | print ",".join(values) 147 | ``` 148 | ---------------- 149 | **My Solution: Python 3** 150 | ```python 151 | lst = [] 152 | 153 | for i in range(1000,3001): 154 | flag = 1 155 | for j in str(i): # every integer number i is converted into string 156 | if ord(j)%2 != 0: # ord returns ASCII value and j is every digit of i 157 | flag = 0 # flag becomes zero if any odd digit found 158 | if flag == 1: 159 | lst.append(str(i)) # i is stored in list as string 160 | 161 | print(",".join(lst)) 162 | ``` 163 | **OR** 164 | ```python 165 | def check(element): 166 | return all(ord(i)%2 == 0 for i in element) # all returns True if all digits i is even in element 167 | 168 | lst = [str(i) for i in range(1000,3001)] # creates list of all given numbers with string data type 169 | lst = list(filter(check,lst)) # filter removes element from list if check condition fails 170 | print(",".join(lst)) 171 | ``` 172 | **OR** 173 | ```python 174 | lst = [str(i) for i in range(1000,3001)] 175 | lst = list(filter(lambda i:all(ord(j)%2 == 0 for j in i),lst )) # using lambda to define function inside filter function 176 | print(",".join(lst)) 177 | ``` 178 | ------------------------- 179 | 180 | # Question 13 181 | 182 | ### **Question:** 183 | 184 | >***Write a program that accepts a sentence and calculate the number of letters and digits.*** 185 | 186 | >***Suppose the following input is supplied to the program:*** 187 | 188 | ``` 189 | hello world! 123 190 | ``` 191 | 192 | >***Then, the output should be:*** 193 | ``` 194 | LETTERS 10 195 | DIGITS 3 196 | ``` 197 | ---------------------- 198 | 199 | ### Hints: 200 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 201 | 202 | ------------------- 203 | **Main author's Solution: Python 2** 204 | ```python 205 | s = raw_input() 206 | d = {"DIGITS":0, "LETTERS":0} 207 | for c in s: 208 | if c.isdigit(): 209 | d["DIGITS"]+=1 210 | elif c.isalpha(): 211 | d["LETTERS"]+=1 212 | else: 213 | pass 214 | print "LETTERS", d["LETTERS"] 215 | print "DIGITS", d["DIGITS"] 216 | ``` 217 | ---------------- 218 | **My Solution: Python 3** 219 | ```python 220 | word = input() 221 | letter,digit = 0,0 222 | 223 | for i in word: 224 | if ('a'<=i and i<='z') or ('A'<=i and i<='Z'): 225 | letter+=1 226 | if '0'<=i and i<='9': 227 | digit+=1 228 | 229 | print("LETTERS {0}\nDIGITS {1}".format(letter,digit)) 230 | ``` 231 | **OR** 232 | ```python 233 | word = input() 234 | letter,digit = 0,0 235 | 236 | for i in word: 237 | letter+=i.isalpha() # returns True if alphabet 238 | digit+=i.isnumeric() # returns True if numeric 239 | 240 | print("LETTERS %d\nDIGITS %d"%(letter,digit)) # two different types of formating method is shown in both solution 241 | ``` 242 | ----------------- 243 | ## Conclusion 244 | ***All the above problems are mostly string related problems. Major parts of the solution includes string releted functions and comprehension method to write down the code in more shorter form.*** 245 | 246 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%202.md "Day 2") 247 | 248 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%204.md "Day 4") 249 | 250 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day 4.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 14 3 | 4 | ### **Question:** 5 | 6 | >***Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.*** 7 | 8 | >***Suppose the following input is supplied to the program:*** 9 | ``` 10 | Hello world! 11 | ``` 12 | >***Then, the output should be:*** 13 | ``` 14 | UPPER CASE 1 15 | LOWER CASE 9 16 | ``` 17 | --------------------- 18 | 19 | ### Hints: 20 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 21 | 22 | ------------------- 23 | **Main author's Solution: Python 2** 24 | ```python 25 | s = raw_input() 26 | d = {"UPPER CASE":0, "LOWER CASE":0} 27 | for c in s: 28 | if c.isupper(): 29 | d["UPPER CASE"]+=1 30 | elif c.islower(): 31 | d["LOWER CASE"]+=1 32 | else: 33 | pass 34 | print "UPPER CASE", d["UPPER CASE"] 35 | print "LOWER CASE", d["LOWER CASE"] 36 | ``` 37 | ---------------- 38 | **My Solution: Python 3** 39 | ```python 40 | word = input() 41 | upper,lower = 0,0 42 | 43 | for i in word: 44 | if 'a'<=i and i<='z' : 45 | lower+=1 46 | if 'A'<=i and i<='Z': 47 | upper+=1 48 | 49 | print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower)) 50 | ``` 51 | **OR** 52 | ```python 53 | word = input() 54 | upper,lower = 0,0 55 | 56 | for i in word: 57 | lower+=i.islower() 58 | upper+=i.isupper() 59 | 60 | print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower)) 61 | ``` 62 | **OR** 63 | ```python 64 | word = input() 65 | upper = sum(1 for i in word if i.isupper()) # sum function cumulatively sum up 1's if the condition is True 66 | lower = sum(1 for i in word if i.islower()) 67 | 68 | print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower)) 69 | ``` 70 | **OR** 71 | ```python 72 | # solution by Amitewu 73 | 74 | string = input("Enter the sentense") 75 | upper = 0 76 | lower = 0 77 | for x in string: 78 | if x.isupper() == True: 79 | upper += 1 80 | if x.islower() == True: 81 | lower += 1 82 | 83 | print("UPPER CASE: ", upper) 84 | print("LOWER CASE: ", lower) 85 | ``` 86 | ---------------------- 87 | # Question 15 88 | 89 | ### **Question:** 90 | 91 | >***Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.*** 92 | 93 | >***Suppose the following input is supplied to the program:*** 94 | 95 | ``` 96 | 9 97 | ``` 98 | 99 | >***Then, the output should be:*** 100 | ``` 101 | 11106 102 | ``` 103 | --------------------- 104 | ### Hints: 105 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 106 | 107 | ------------------- 108 | **Main author's Solution: Python 2** 109 | ```python 110 | a = raw_input() 111 | n1 = int( "%s" % a ) 112 | n2 = int( "%s%s" % (a,a) ) 113 | n3 = int( "%s%s%s" % (a,a,a) ) 114 | n4 = int( "%s%s%s%s" % (a,a,a,a) ) 115 | print n1+n2+n3+n4 116 | ``` 117 | ---------------- 118 | **My Solution: Python 3** 119 | ```python 120 | a = input() 121 | total,tmp = 0,str() # initialing an integer and empty string 122 | 123 | for i in range(4): 124 | tmp+=a # concatenating 'a' to 'tmp' 125 | total+=int(tmp) # converting string type to integer type 126 | 127 | print(total) 128 | ``` 129 | **OR** 130 | ```python 131 | a = input() 132 | 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" 133 | print(total) 134 | ``` 135 | 136 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%203.md "Day 3") 137 | 138 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%205.md "Day 5") 139 | 140 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) 141 | -------------------------------------------------------------------------------- /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.*** 6 | >***Suppose the following input is supplied to the program:*** 7 | 8 | ``` 9 | 1,2,3,4,5,6,7,8,9 10 | ``` 11 | 12 | >***Then, the output should be:*** 13 | 14 | ``` 15 | 1,3,5,7,9 16 | ``` 17 | 18 | ---------------------- 19 | 20 | ### Hints: 21 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 22 | 23 | ------------------- 24 | **Main author's Solution: Python 2** 25 | ```python 26 | values = raw_input() 27 | numbers = [x for x in values.split(",") if int(x)%2 != 0] 28 | print ",".join(numbers) 29 | ``` 30 | ---------------- 31 | **My Solution: Python 3** 32 | ```python 33 | lst = [i for i in input().split(',') if int(i) % 2] 34 | print(",".join(lst)) 35 | ``` 36 | ------------------------ 37 | 38 | 39 | # Question 17 40 | 41 | ### **Question:** 42 | 43 | >***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:*** 44 | ``` 45 | D 100 46 | W 200 47 | ``` 48 | * D means deposit while W means withdrawal. 49 | 50 | >***Suppose the following input is supplied to the program:*** 51 | ``` 52 | D 300 53 | D 300 54 | W 200 55 | D 100 56 | ``` 57 | >***Then, the output should be:*** 58 | ``` 59 | 500 60 | ``` 61 | ---------------------- 62 | 63 | ### Hints: 64 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 65 | 66 | ------------------- 67 | **Main author's Solution: Python 2** 68 | ```python 69 | import sys 70 | netAmount = 0 71 | while True: 72 | s = raw_input() 73 | if not s: 74 | break 75 | values = s.split(" ") 76 | operation = values[0] 77 | amount = int(values[1]) 78 | if operation=="D": 79 | netAmount+=amount 80 | elif operation=="W": 81 | netAmount-=amount 82 | else: 83 | pass 84 | print netAmount 85 | ``` 86 | ---------------- 87 | **My Solution: Python 3** 88 | ```python 89 | total = 0 90 | while True: 91 | s = input().split() 92 | if not s: # break if the string is empty 93 | break 94 | cm,num = map(str,s) # two inputs are distributed in cm and num in string data type 95 | 96 | if cm=='D': 97 | total+=int(num) 98 | if cm=='W': 99 | total-=int(num) 100 | 101 | print(total) 102 | ``` 103 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%204.md "Day 4") 104 | 105 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%206.md "Day 6") 106 | 107 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /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 | - ***At least 1 letter between [a-z]*** 9 | - ***At least 1 number between [0-9]*** 10 | - ***At least 1 letter between [A-Z]*** 11 | - ***At least 1 character from [$#@]*** 12 | - ***Minimum length of transaction password: 6*** 13 | - ***Maximum length of transaction password: 12*** 14 | 15 | >***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.*** 16 | 17 | >***Example*** 18 | 19 | >***If the following passwords are given as input to the program:*** 20 | ``` 21 | ABd1234@1,a F1#,2w3E*,2We3345 22 | ``` 23 | >***Then, the output of the program should be:*** 24 | ``` 25 | ABd1234@1 26 | ``` 27 | 28 | ---------------------- 29 | 30 | ### Hints: 31 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 32 | 33 | ------------------- 34 | **Main author's Solution: Python 2** 35 | ```python 36 | import re 37 | value = [] 38 | items = [x for x in raw_input().split(',')] 39 | for p in items: 40 | if len(p) < 6 or len(p) > 12: 41 | continue 42 | else: 43 | pass 44 | if not re.search("[a-z]",p): 45 | continue 46 | elif not re.search("[0-9]",p): 47 | continue 48 | elif not re.search("[A-Z]",p): 49 | continue 50 | elif not re.search("[$#@]",p): 51 | continue 52 | elif re.search("\s",p): 53 | continue 54 | else: 55 | pass 56 | value.append(p) 57 | print ",".join(value) 58 | ``` 59 | ---------------- 60 | **My Solution: Python 3** 61 | ```python 62 | def is_low(x): # Returns True if the string has a lowercase 63 | for i in x: 64 | if 'a'<=i and i<='z': 65 | return True 66 | return False 67 | 68 | def is_up(x): # Returns True if the string has a uppercase 69 | for i in x: 70 | if 'A'<= i and i<='Z': 71 | return True 72 | return False 73 | 74 | def is_num(x): # Returns True if the string has a numeric digit 75 | for i in x: 76 | if '0'<=i and i<='9': 77 | return True 78 | return False 79 | 80 | def is_other(x): # Returns True if the string has any "$#@" 81 | for i in x: 82 | if i=='$' or i=='#' or i=='@': 83 | return True 84 | return False 85 | 86 | s = input().split(',') 87 | lst = [] 88 | 89 | for i in s: 90 | length = len(i) 91 | 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 92 | lst.append(i) 93 | 94 | print(",".join(lst)) 95 | ``` 96 | **OR** 97 | ```python 98 | def check(x): 99 | cnt = (6<=len(x) and len(x)<=12) 100 | for i in x: 101 | if i.isupper(): 102 | cnt+=1 103 | break 104 | for i in x: 105 | if i.islower(): 106 | cnt+=1 107 | break 108 | for i in x: 109 | if i.isnumeric(): 110 | cnt+=1 111 | break 112 | for i in x: 113 | if i=='@' or i=='#'or i=='$': 114 | cnt+=1 115 | break 116 | return cnt == 5 # counting if total 5 all conditions are fulfilled then returns True 117 | 118 | s = input().split(',') 119 | lst = filter(check,s) # Filter function pick the words from s, those returns True by check() function 120 | print(",".join(lst)) 121 | ``` 122 | **OR** 123 | ```python 124 | import re 125 | 126 | s = input().split(',') 127 | lst = [] 128 | 129 | for i in s: 130 | cnt = 0 131 | cnt+=(6<=len(i) and len(i)<=12) 132 | cnt+=bool(re.search("[a-z]",i)) # here re module includes a function re.search() which returns the object information 133 | 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 134 | cnt+=bool(re.search("[0-9]",i)) # if not a single match found then returns NONE which converts to False in boolean 135 | cnt+=bool(re.search("[@#$]",i)) # expression otherwise True if found any. 136 | if cnt == 5: 137 | lst.append(i) 138 | 139 | print(",".join(lst)) 140 | ``` 141 | 142 | -------------------------- 143 | 144 | # Question 19 145 | 146 | ### **Question:** 147 | 148 | >***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:*** 149 | - ***1: Sort based on name*** 150 | - ***2: Then sort based on age*** 151 | - ***3: Then sort by score*** 152 | 153 | >***The priority is that name > age > score.*** 154 | 155 | >***If the following tuples are given as input to the program:*** 156 | ``` 157 | Tom,19,80 158 | John,20,90 159 | Jony,17,91 160 | Jony,17,93 161 | Json,21,85 162 | ``` 163 | >***Then, the output of the program should be:*** 164 | ``` 165 | [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')] 166 | ``` 167 | 168 | ---------------------- 169 | 170 | ### Hints: 171 | >***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.*** 172 | 173 | ------------------- 174 | **Main author's Solution: Python 2** 175 | ```python 176 | from operator import itemgetter, attrgetter 177 | 178 | l = [] 179 | while True: 180 | s = raw_input() 181 | if not s: 182 | break 183 | l.append(tuple(s.split(","))) 184 | 185 | print sorted(l, key=itemgetter(0,1,2)) 186 | ``` 187 | -------------------------- 188 | **My Solution: Python 3** 189 | ```python 190 | lst = [] 191 | while True: 192 | s = input().split(',') 193 | if not s[0]: # breaks for blank input 194 | break 195 | lst.append(tuple(s)) 196 | 197 | lst.sort(key= lambda x:(x[0],x[1],x[2])) # here key is defined by lambda and the data is sorted by element priority 0>1>2 in accending order 198 | print(lst) 199 | ``` 200 | ----------------------- 201 | ## Conclusion 202 | ***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.*** 203 | 204 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%205.md "Day 5") 205 | 206 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%207.md "Day 7") 207 | 208 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /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 | ---------------------- 8 | 9 | ### Hints: 10 | >***Consider use class, function and comprehension.*** 11 | 12 | ------------------- 13 | **Main author's Solution: Python 2** 14 | #### ***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.*** 15 | 16 | ---------------- 17 | **My Solution: Python 3** 18 | ```python 19 | class Test: 20 | def generator(self,n): 21 | return [i for i in range(n) if i%7==0] # returns the values as a list if an element is divisible by 7 22 | 23 | n = int(input()) 24 | num = Test() 25 | lst = num.generator(n) 26 | print(lst) 27 | 28 | ``` 29 | ---------------------- 30 | # Question 21 31 | 32 | ### **Question:** 33 | 34 | >***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:*** 35 | ``` 36 | UP 5 37 | DOWN 3 38 | LEFT 3 39 | RIGHT 2 40 | ``` 41 | >***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.*** 42 | ***Example:*** 43 | ***If the following tuples are given as input to the program:*** 44 | ``` 45 | UP 5 46 | DOWN 3 47 | LEFT 3 48 | RIGHT 2 49 | ``` 50 | >***Then, the output of the program should be:*** 51 | ``` 52 | 2 53 | ``` 54 | 55 | ---------------------- 56 | ### Hints: 57 | >***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.*** 58 | 59 | ----------------------- 60 | **Main author's Solution: Python 2** 61 | ```python 62 | import math 63 | pos = [0,0] 64 | while True: 65 | s = raw_input() 66 | if not s: 67 | break 68 | movement = s.split(" ") 69 | direction = movement[0] 70 | steps = int(movement[1]) 71 | if direction=="UP": 72 | pos[0]+=steps 73 | elif direction=="DOWN": 74 | pos[0]-=steps 75 | elif direction=="LEFT": 76 | pos[1]-=steps 77 | elif direction=="RIGHT": 78 | pos[1]+=steps 79 | else: 80 | pass 81 | 82 | print int(round(math.sqrt(pos[1]**2+pos[0]**2))) 83 | ``` 84 | ---------------- 85 | **My Solution: Python 3** 86 | ```python 87 | import math 88 | 89 | x,y = 0,0 90 | while True: 91 | s = input().split() 92 | if not s: 93 | break 94 | if s[0]=='UP': # s[0] indicates command 95 | x-=int(s[1]) # s[1] indicates unit of move 96 | if s[0]=='DOWN': 97 | x+=int(s[1]) 98 | if s[0]=='LEFT': 99 | y-=int(s[1]) 100 | if s[0]=='RIGHT': 101 | y+=int(s[1]) 102 | # N**P means N^P 103 | dist = round(math.sqrt(x**2 + y**2)) # euclidean distance = square root of (x^2+y^2) and rounding it to nearest integer 104 | print(dist) 105 | ``` 106 | ------------------ 107 | 108 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%206.md "Day 6") 109 | 110 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%208.md "Day 8") 111 | 112 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /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 | New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3. 10 | ``` 11 | >***Then, the output should be:*** 12 | ``` 13 | 2:2 14 | 3.:1 15 | 3?:1 16 | New:1 17 | Python:5 18 | Read:1 19 | and:1 20 | between:1 21 | choosing:1 22 | or:2 23 | to:1 24 | ``` 25 | 26 | ---------------------- 27 | 28 | ### Hints 29 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 30 | 31 | ------------------- 32 | **Main author's Solution: Python 2** 33 | ```python 34 | freq = {} # frequency of words in text 35 | line = raw_input() 36 | for word in line.split(): 37 | freq[word] = freq.get(word,0)+1 38 | 39 | words = freq.keys() 40 | words.sort() 41 | 42 | for w in words: 43 | print "%s:%d" % (w,freq[w]) 44 | ``` 45 | ---------------- 46 | **My Solution: Python 3** 47 | ```python 48 | ss = input().split() 49 | word = sorted(set(ss)) # split words are stored and sorted as a set 50 | 51 | for i in word: 52 | print("{0}:{1}".format(i,ss.count(i))) 53 | ``` 54 | **OR** 55 | ```python 56 | ss = input().split() 57 | dict = {} 58 | for i in ss: 59 | i = dict.setdefault(i,ss.count(i)) # setdefault() function takes key & value to set it as dictionary. 60 | 61 | dict = sorted(dict.items()) # items() function returns both key & value of dictionary as a list 62 | # and then sorted. The sort by default occurs in order of 1st -> 2nd key 63 | for i in dict: 64 | print("%s:%d"%(i[0],i[1])) 65 | ``` 66 | **OR** 67 | ```python 68 | ss = input().split() 69 | dict = {i:ss.count(i) for i in ss} # sets dictionary as i-> split word & ss.count(i) -> total occurrence of i in ss 70 | dict = sorted(dict.items()) # items() function returns both key & value of dictionary as a list 71 | # and then sorted. The sort by default occurs in order of 1st -> 2nd key 72 | for i in dict: 73 | print("%s:%d"%(i[0],i[1])) 74 | ``` 75 | **OR** 76 | ```python 77 | from collections import Counter 78 | 79 | ss = input().split() 80 | ss = Counter(ss) # returns key & frequency as a dictionary 81 | ss = sorted(ss.items()) # returns as a tuple list 82 | 83 | for i in ss: 84 | print("%s:%d"%(i[0],i[1])) 85 | ``` 86 | --------------- 87 | 88 | # Question 23 89 | 90 | ### **Question:** 91 | 92 | >***Write a method which can calculate square value of number*** 93 | 94 | ---------------------- 95 | 96 | ### Hints: 97 | ``` 98 | Using the ** operator which can be written as n**p where means n^p 99 | ``` 100 | 101 | ------------------- 102 | **Main author's Solution: Python 2** 103 | ```python 104 | def square(num): 105 | return num ** 2 106 | 107 | print square(2) 108 | print square(3) 109 | ``` 110 | ---------------- 111 | **My Solution: Python 3** 112 | ```python 113 | n=int(input()) 114 | print(n**2) 115 | ``` 116 | --------------------- 117 | # Question 24 118 | 119 | ### **Question:** 120 | 121 | >***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.*** 122 | 123 | >***Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input()*** 124 | 125 | >***And add document for your own function*** 126 | 127 | ### Hints: 128 | ``` 129 | The built-in document method is __doc__ 130 | ``` 131 | 132 | ---------------------- 133 | **Main author's Solution: Python 2** 134 | ```python 135 | print abs.__doc__ 136 | print int.__doc__ 137 | print raw_input.__doc__ 138 | 139 | def square(num): 140 | '''Return the square value of the input number. 141 | 142 | The input number must be integer. 143 | ''' 144 | return num ** 2 145 | 146 | print square(2) 147 | print square.__doc__ 148 | ``` 149 | ---------------- 150 | **My Solution: Python 3** 151 | ```python 152 | print(str.__doc__) 153 | print(sorted.__doc__) 154 | 155 | def pow(n,p): 156 | ''' 157 | param n: This is any integer number 158 | param p: This is power over n 159 | return: n to the power p = n^p 160 | ''' 161 | 162 | return n**p 163 | 164 | print(pow(3,4)) 165 | print(pow.__doc__) 166 | ``` 167 | --------------------- 168 | # Question 25 169 | 170 | ### **Question:** 171 | 172 | >***Define a class, which have a class parameter and have a same instance parameter.*** 173 | 174 | ---------------------- 175 | 176 | ### Hints: 177 | ``` 178 | Define an instance parameter, need add it in __init__ method.You can init an object with construct parameter or set the value later 179 | ``` 180 | 181 | ------------------- 182 | **Main author's Solution: Python 2** 183 | ```python 184 | class Person: 185 | # Define the class parameter "name" 186 | name = "Person" 187 | 188 | def __init__(self, name = None): 189 | # self.name is the instance parameter 190 | self.name = name 191 | 192 | jeffrey = Person("Jeffrey") 193 | print "%s name is %s" % (Person.name, jeffrey.name) 194 | 195 | nico = Person() 196 | nico.name = "Nico" 197 | print "%s name is %s" % (Person.name, nico.name) 198 | ``` 199 | ---------------- 200 | **My Solution: Python 3** 201 | ```python 202 | class Car: 203 | name = "Car" 204 | 205 | def __init__(self,name = None): 206 | self.name = name 207 | 208 | honda=Car("Honda") 209 | print("%s name is %s"%(Car.name,honda.name)) 210 | 211 | toyota=Car() 212 | toyota.name="Toyota" 213 | print("%s name is %s"%(Car.name,toyota.name)) 214 | ``` 215 | --------------------- 216 | 217 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%207.md "Day 7") 218 | 219 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%209.md "Day 9") 220 | 221 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day 9.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 26 3 | 4 | ### **Question:** 5 | 6 | >***Define a function which can compute the sum of two numbers.*** 7 | 8 | ---------------------- 9 | 10 | ### Hints: 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 | ```python 17 | def SumFunction(number1, number2): 18 | return number1 + number2 19 | 20 | print SumFunction(1,2) 21 | ``` 22 | ---------------- 23 | **My Solution: Python 3** 24 | ```python 25 | sum = lambda n1,n2 : n1 + n2 # here lambda is use to define little function as sum 26 | print(sum(1,2)) 27 | ``` 28 | 29 | ---------------------------- 30 | # Question 27 31 | 32 | ### **Question:** 33 | 34 | >***Define a function that can convert a integer into a string and print it in console.*** 35 | 36 | ---------------------- 37 | ### Hints: 38 | >***Use str() to convert a number to string.*** 39 | 40 | ------------------- 41 | 42 | **Main author's Solution: Python 2** 43 | ```python 44 | def printValue(n): 45 | print str(n) 46 | 47 | printValue(3) 48 | ``` 49 | ---------------- 50 | 51 | **My Solution: Python 3** 52 | ```python 53 | conv = lambda x : str(x) 54 | n = conv(10) 55 | print(n) 56 | print(type(n)) # checks the type of the variable 57 | ``` 58 | --------------------- 59 | 60 | # Question 28 61 | 62 | ### **Question:** 63 | 64 | >***Define a function that can receive two integer numbers in string form and compute their sum and then print it in console.*** 65 | 66 | ---------------------- 67 | ### Hints: 68 | >***Use int() to convert a string to integer.*** 69 | 70 | ------------------- 71 | **Main author's Solution: Python 2** 72 | ```python 73 | def printValue(s1,s2): 74 | print int(s1) + int(s2) 75 | printValue("3","4") #7 76 | ``` 77 | ---------------- 78 | 79 | **My Solution: Python 3** 80 | ```python 81 | sum = lambda s1,s2 : int(s1) + int(s2) 82 | print(sum("10","45")) # 55 83 | ``` 84 | ------------------- 85 | 86 | # Question 29 87 | 88 | ### **Question:** 89 | 90 | >***Define a function that can accept two strings as input and concatenate them and then print it in console.*** 91 | 92 | ---------------------- 93 | 94 | ### Hints: 95 | >***Use + sign to concatenate the strings.*** 96 | 97 | ------------------- 98 | **Main author's Solution: Python 2** 99 | ```python 100 | def printValue(s1,s2): 101 | print s1 + s2 102 | 103 | printValue("3","4") #34 104 | ``` 105 | ---------------- 106 | **My Solution: Python 3** 107 | ```python 108 | sum = lambda s1,s2 : s1 + s2 109 | print(sum("10","45")) # 1045 110 | ``` 111 | ------------------ 112 | # Question 30 113 | 114 | ### **Question:** 115 | 116 | >***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.*** 117 | 118 | ---------------------- 119 | ### Hints: 120 | >***Use len() function to get the length of a string.*** 121 | 122 | ------------------- 123 | **Main author's Solution: Python 2** 124 | ```python 125 | def printValue(s1,s2): 126 | len1 = len(s1) 127 | len2 = len(s2) 128 | if len1 > len2: 129 | print s1 130 | elif len2 > len1: 131 | print s2 132 | else: 133 | print s1 134 | print s2 135 | 136 | printValue("one","three") 137 | 138 | ``` 139 | ---------------- 140 | **My Solution: Python 3** 141 | ```python 142 | def printVal(s1,s2): 143 | len1 = len(s1) 144 | len2 = len(s2) 145 | if len1 > len2: 146 | print(s1) 147 | elif len1 < len2: 148 | print(s2) 149 | else: 150 | print(s1) 151 | print(s2) 152 | 153 | s1,s2=input().split() 154 | printVal(s1,s2) 155 | ``` 156 | ------------ 157 | 158 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%208.md "Day 9") 159 | 160 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_10.md "Day 10") 161 | 162 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /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 | Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops. 12 | ``` 13 | ------------------- 14 | **Main Author's Solution: Python 2** 15 | ```python 16 | def printDict(): 17 | d=dict() 18 | for i in range(1,21): 19 | d[i]=i**2 20 | print d 21 | 22 | printDict() 23 | ``` 24 | ---------------- 25 | **My Solution: Python 3** 26 | ```python 27 | def printDict(): 28 | dict={i:i**2 for i in range(1,21)} # Using comprehension method and 29 | print(dict) 30 | 31 | printDict() 32 | ``` 33 | ---------------- 34 | 35 | # Question 32 36 | 37 | ### **Question:** 38 | 39 | >***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.*** 40 | 41 | ---------------------- 42 | 43 | ### Hints: 44 | ``` 45 | 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. 46 | ``` 47 | ------------------- 48 | **Main Author's Solution: Python 2** 49 | ```python 50 | def printDict(): 51 | d=dict() 52 | for i in range(1,21): 53 | d[i]=i**2 54 | for k in d.keys(): 55 | print k 56 | printDict() 57 | ``` 58 | ---------------- 59 | **My Solution: Python 3** 60 | ```python 61 | def printDict(): 62 | dict = {i: i**2 for i in range(1, 21)} 63 | print(dict.keys()) # print keys of a dictionary 64 | 65 | printDict() 66 | ``` 67 | --------------------- 68 | 69 | # Question 33 70 | 71 | ### **Question:** 72 | 73 | >***Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).*** 74 | 75 | ---------------------- 76 | 77 | ### Hints: 78 | ``` 79 | Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list. 80 | ``` 81 | 82 | ------------------- 83 | **Main Author's Solution: Python 2** 84 | ```python 85 | def printList(): 86 | li=list() 87 | for i in range(1,21): 88 | li.append(i**2) 89 | print li 90 | 91 | printList() 92 | ``` 93 | ---------------- 94 | **My Solution: Python 3** 95 | ```python 96 | def printList(): 97 | lst = [i ** 2 for i in range(1, 21)] 98 | print(lst) 99 | 100 | printList() 101 | ``` 102 | ------------------- 103 | 104 | # Question 34 105 | 106 | ### **Question:** 107 | 108 | >***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.*** 109 | 110 | ---------------------- 111 | 112 | ### Hints: 113 | ``` 114 | 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 115 | ``` 116 | 117 | ------------------- 118 | **Main Author's Solution: Python 2** 119 | ```python 120 | def printList(): 121 | li=list() 122 | for i in range(1,21): 123 | li.append(i**2) 124 | print li[:5] 125 | 126 | printList() 127 | ``` 128 | ---------------- 129 | 130 | **My Solution: Python 3** 131 | ```python 132 | def printList(): 133 | lst = [i ** 2 for i in range(1, 21)] 134 | 135 | for i in range(5): 136 | print(lst[i]) 137 | 138 | printList() 139 | ``` 140 | ------------- 141 | # Question 35 142 | 143 | ### **Question:** 144 | 145 | >***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.*** 146 | 147 | ---------------------- 148 | ### Hints: 149 | ``` 150 | 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 151 | ``` 152 | ------------------- 153 | **Main Author's Solution: Python 2** 154 | ```python 155 | def printList(): 156 | li=list() 157 | for i in range(1,21): 158 | li.append(i**2) 159 | print li[-5:] 160 | 161 | printList() 162 | ``` 163 | ---------------- 164 | **My Solution: Python 3** 165 | ```python 166 | def printList(): 167 | lst = [i ** 2 for i in range(1, 21)] 168 | for i in range(19,14,-1): 169 | print(lst[i]) 170 | 171 | printList() 172 | ``` 173 | ---------------------- 174 | # Question 36 175 | 176 | ### **Question:** 177 | 178 | >***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.*** 179 | 180 | ---------------------- 181 | ``` 182 | 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 183 | ``` 184 | 185 | ------------------- 186 | **Main Author's Solution: Python 2** 187 | ```python 188 | def printList(): 189 | li=list() 190 | for i in range(1,21): 191 | li.append(i**2) 192 | print li[5:] 193 | 194 | printList() 195 | ``` 196 | ---------------- 197 | **My Solution: Python 3** 198 | ```python 199 | def printList(): 200 | lst = [i ** 2 for i in range(1, 21)] 201 | for i in range(5,20): 202 | print(lst[i]) 203 | 204 | printList() 205 | ``` 206 | 207 | --------------------- 208 | # Question 37 209 | 210 | ### **Question:** 211 | 212 | >***Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).*** 213 | 214 | 215 | ---------------------- 216 | 217 | ### Hints: 218 | ``` 219 | 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. 220 | ``` 221 | 222 | ------------------- 223 | **Main Author's Solution: Python 2** 224 | ```python 225 | def printTuple(): 226 | li=list() 227 | for i in range(1,21): 228 | li.append(i**2) 229 | print tuple(li) 230 | 231 | printTuple() 232 | ``` 233 | ---------------- 234 | **My Solution: Python 3** 235 | ```python 236 | def printTupple(): 237 | lst = [i ** 2 for i in range(1, 21)] 238 | print(tuple(lst)) 239 | 240 | printTupple() 241 | ``` 242 | 243 | ----------------- 244 | 245 | ### Comment 246 | ***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.*** 247 | 248 | 249 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day%209.md "Day 9") 250 | 251 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_11.md "Day 11") 252 | 253 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /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 | >***Use [n1:n2] notation to get a slice from a tuple.*** 11 | 12 | ------------------- 13 | 14 | **Main Author's Solution: Python 2** 15 | ```python 16 | tp = (1,2,3,4,5,6,7,8,9,10) 17 | tp1 = tp[:5] 18 | tp2 = tp[5:] 19 | print tp1 20 | print tp2 21 | ``` 22 | ---------------- 23 | **My Solution: Python 3** 24 | ```python 25 | tpl = (1,2,3,4,5,6,7,8,9,10) 26 | 27 | for i in range(0,5): 28 | print(tpl[i],end = ' ') 29 | print() 30 | for i in range(5,10): 31 | print(tpl[i],end = ' ') 32 | ``` 33 | **OR** 34 | ```python 35 | tpl = (1,2,3,4,5,6,7,8,9,10) 36 | lst1,lst2 = [],[] 37 | 38 | for i in range(0,5): 39 | lst1.append(tpl[i]) 40 | 41 | for i in range(5,10): 42 | lst2.append(tpl[i]) 43 | 44 | print(lst1) 45 | print(lst2) 46 | ``` 47 | ------------------ 48 | 49 | # Question 39 50 | 51 | ### **Question:** 52 | 53 | >***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).*** 54 | 55 | ---------------------- 56 | 57 | ### Hints: 58 | >***Use "for" to iterate the tuple. Use tuple() to generate a tuple from a list.*** 59 | 60 | ------------------- 61 | 62 | **Main Author's Solution: Python 2** 63 | ```python 64 | tp = (1,2,3,4,5,6,7,8,9,10) 65 | li = list() 66 | for i in tp: 67 | if tp[i]%2 == 0: 68 | li.append(tp[i]) 69 | 70 | tp2 = tuple(li) 71 | print tp2 72 | ``` 73 | ---------------- 74 | **My Solution: Python 3** 75 | ```python 76 | tpl = (1,2,3,4,5,6,7,8,9,10) 77 | tpl1 = tuple(i for i in tpl if i%2 == 0) 78 | print(tpl1) 79 | ``` 80 | **OR** 81 | ```python 82 | tpl = (1,2,3,4,5,6,7,8,9,10) 83 | tpl1 = tuple(filter(lambda x : x%2==0,tpl)) # Lambda function returns True if found even element. 84 | # Filter removes data for which function returns False 85 | print(tpl1) 86 | ``` 87 | ---------------- 88 | 89 | # Question 40 90 | 91 | ### **Question:** 92 | 93 | >***Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No".*** 94 | 95 | ---------------------- 96 | 97 | ### Hints: 98 | >***Use if statement to judge condition.*** 99 | 100 | ------------------- 101 | **Main Author's Solution: Python 2** 102 | ```python 103 | s= raw_input() 104 | if s=="yes" or s=="YES" or s=="Yes": 105 | print "Yes" 106 | else: 107 | print "No" 108 | ``` 109 | ---------------- 110 | **My Solution: Python 3** 111 | ```python 112 | s = input() 113 | if s.lower() == 'yes': # lower function returns all lowercase letters in the string 114 | print('Yes') 115 | else: 116 | print("No") 117 | ``` 118 | ---------------- 119 | 120 | # Question 41 121 | 122 | ### **Question:** 123 | 124 | >***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].*** 125 | 126 | ---------------------- 127 | 128 | ### Hints: 129 | >***Use map() to generate a list.Use lambda to define anonymous functions.*** 130 | 131 | ------------------- 132 | 133 | **Main Author's Solution: Python 2** 134 | ```python 135 | li = [1,2,3,4,5,6,7,8,9,10] 136 | squaredNumbers = map(lambda x: x**2, li) 137 | print squaredNumbers 138 | ``` 139 | ---------------- 140 | **My Solution: Python 3** 141 | ```python 142 | # No different way of code is written as the requirment is specificly mentioned in problem description 143 | 144 | li = [1,2,3,4,5,6,7,8,9,10] 145 | squaredNumbers = map(lambda x: x**2, li) # returns map type object data 146 | print(list(squaredNumbers)) # converting the object into list 147 | ``` 148 | -------------- 149 | 150 | # Question 42 151 | 152 | ### **Question:** 153 | 154 | >***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].*** 155 | 156 | ---------------------- 157 | ### Hints: 158 | >***Use map() to generate a list.Use filter() to filter elements of a list.Use lambda to define anonymous functions.*** 159 | 160 | ------------------- 161 | 162 | **Main Author's Solution: Python 2** 163 | ```python 164 | li = [1,2,3,4,5,6,7,8,9,10] 165 | evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li)) 166 | print evenNumbers 167 | ``` 168 | ---------------- 169 | 170 | **My Solution: Python 3** 171 | ```python 172 | def even(x): 173 | return x%2==0 174 | 175 | def squer(x): 176 | return x*x 177 | 178 | li = [1,2,3,4,5,6,7,8,9,10] 179 | li = map(squer,filter(even,li)) # first filters number by even number and the apply map() on the resultant elements 180 | print(list(li)) 181 | ``` 182 | --------------- 183 | # Question 43 184 | 185 | ### **Question:** 186 | 187 | >***Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included).*** 188 | 189 | ---------------------- 190 | ### Hints: 191 | >***Use filter() to filter elements of a list.Use lambda to define anonymous functions.*** 192 | 193 | ------------------- 194 | 195 | **Main Author's Solution: Python 2** 196 | ```python 197 | evenNumbers = filter(lambda x: x%2==0, range(1,21)) 198 | print evenNumbers 199 | ``` 200 | ---------------- 201 | **My Solution: Python 3** 202 | ```python 203 | def even(x): 204 | return x%2==0 205 | 206 | evenNumbers = filter(even, range(1,21)) 207 | print(list(evenNumbers)) 208 | ``` 209 | ------------------ 210 | 211 | 212 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_10.md "Day 10") 213 | 214 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_12.md "Day 12") 215 | 216 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) 217 | -------------------------------------------------------------------------------- /Status/Day_12.md: -------------------------------------------------------------------------------- 1 | # Question 44 2 | 3 | ### **Question:** 4 | >***Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included).*** 5 | 6 | --------------- 7 | 8 | ### Hints: 9 | >***Use map() to generate a list. Use lambda to define anonymous functions.*** 10 | 11 | --------------- 12 | 13 | **Main Author's Solution: Python 2** 14 | ```python 15 | squaredNumbers = map(lambda x: x**2, range(1,21)) 16 | print squaredNumbers 17 | ``` 18 | ---------------- 19 | **My Solution: Python 3** 20 | ```python 21 | def sqr(x): 22 | return x*x 23 | 24 | squaredNumbers = list(map(sqr, range(1,21))) 25 | print (squaredNumbers) 26 | ``` 27 | ---------------------------------------- 28 | 29 | # Question 45 30 | 31 | ### **Question:** 32 | >***Define a class named American which has a static method called printNationality.*** 33 | 34 | --------------------- 35 | ### Hints: 36 | >***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/).*** 37 | 38 | --------------------- 39 | **Main Author's Solution: Python 2** 40 | ```python 41 | class American(object): 42 | @staticmethod 43 | def printNationality(): 44 | print "America" 45 | 46 | anAmerican = American() 47 | anAmerican.printNationality() 48 | American.printNationality() 49 | ``` 50 | -------------------------- 51 | **My Solution: Python 3** 52 | ```python 53 | class American(): 54 | @staticmethod 55 | def printNationality(): 56 | print("I am American") 57 | 58 | american = American() 59 | american.printNationality() # this will not run if @staticmethod does not decorates the function. 60 | # Because the class has no inctance. 61 | 62 | American.printNationality() # this will run even though the @staticmethod 63 | # does not decorate printNationality() 64 | ``` 65 | ---------------------------------------- 66 | 67 | # Question 46 68 | 69 | ### **Question:** 70 | >***Define a class named American and its subclass NewYorker.*** 71 | 72 | ------------ 73 | 74 | ### Hints: 75 | >**Use class Subclass(ParentClass) to define a subclass.*** 76 | 77 | ------------ 78 | 79 | **Main Author's Solution: Python 2** 80 | ```python 81 | class American(object): 82 | pass 83 | 84 | class NewYorker(American): 85 | pass 86 | 87 | anAmerican = American() 88 | aNewYorker = NewYorker() 89 | print anAmerican 90 | print aNewYorker 91 | ``` 92 | ---------------- 93 | **My Solution: Python 3** 94 | ```python 95 | class American(): 96 | pass 97 | 98 | class NewYorker(American): 99 | pass 100 | 101 | american = American() 102 | newyorker = NewYorker() 103 | 104 | print(american) 105 | print(newyorker) 106 | ``` 107 | ---------------------------------------- 108 | 109 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_11.md "Day 11") 110 | 111 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_13.md "Day 13") 112 | 113 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_13.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 47 3 | 4 | ### **Question** 5 | 6 | > ***Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.*** 7 | 8 | ---------------------- 9 | 10 | ### Hints 11 | > ***Use def methodName(self) to define a method.*** 12 | 13 | --------------------- 14 | 15 | **Main author's Solution: Python 2** 16 | ```python 17 | class Circle(object): 18 | def __init__(self, r): 19 | self.radius = r 20 | 21 | def area(self): 22 | return self.radius**2*3.14 23 | 24 | aCircle = Circle(2) 25 | print aCircle.area() 26 | ``` 27 | ---------------- 28 | **My Solution: Python 3** 29 | ```python 30 | class Circle(): 31 | def __init__(self,r): 32 | self.radius = r 33 | 34 | def area(self): 35 | return 3.1416*(self.radius**2) 36 | 37 | 38 | circle = Circle(5) 39 | print(circle.area()) 40 | ``` 41 | ---------------- 42 | 43 | # Question 48 44 | 45 | ### **Question** 46 | 47 | > ***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.*** 48 | 49 | ---------------------- 50 | 51 | ### Hints 52 | > ***Use def methodName(self) to define a method.*** 53 | 54 | ---- 55 | 56 | **Main author's Solution: Python 2** 57 | ```python 58 | class Rectangle(object): 59 | def __init__(self, l, w): 60 | self.length = l 61 | self.width = w 62 | 63 | def area(self): 64 | return self.length*self.width 65 | 66 | aRectangle = Rectangle(2,10) 67 | print aRectangle.area() 68 | 69 | ``` 70 | ---------------- 71 | **My Solution: Python 3** 72 | ```python 73 | class Rectangle(): 74 | def __init__(self,l,w): 75 | self.length = l 76 | self.width = w 77 | 78 | def area(self): 79 | return self.length*self.width 80 | 81 | 82 | rect = Rectangle(2,4) 83 | print(rect.area()) 84 | 85 | ``` 86 | ---------------- 87 | 88 | # Question 49 89 | 90 | ### **Question** 91 | 92 | > ***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.*** 93 | 94 | ---------------------- 95 | 96 | ### Hints 97 | > ***To override a method in super class, we can define a method with the same name in the super class.*** 98 | 99 | ---------------------- 100 | 101 | **Main author's Solution: Python 2** 102 | ```python 103 | class Shape(object): 104 | def __init__(self): 105 | pass 106 | 107 | def area(self): 108 | return 0 109 | 110 | class Square(Shape): 111 | def __init__(self, l): 112 | Shape.__init__(self) 113 | self.length = l 114 | 115 | def area(self): 116 | return self.length*self.length 117 | 118 | aSquare= Square(3) 119 | print aSquare.area() 120 | ``` 121 | ---------------- 122 | **My Solution: Python 3** 123 | ```python 124 | class Shape(): 125 | def __init__(self): 126 | pass 127 | 128 | def area(self): 129 | return 0 130 | 131 | class Square(Shape): 132 | def __init__(self,length = 0): 133 | Shape.__init__(self) 134 | self.length = length 135 | 136 | def area(self): 137 | return self.length*self.length 138 | 139 | Asqr = Square(5) 140 | print(Asqr.area()) # prints 25 as given argument 141 | 142 | print(Square().area()) # prints zero as default area 143 | ``` 144 | --------------------- 145 | 146 | # Question 50 147 | 148 | ### **Question** 149 | 150 | > ***Please raise a RuntimeError exception.*** 151 | 152 | ---------------------- 153 | ### Hints 154 | > ***UUse raise() to raise an exception.*** 155 | 156 | ----------- 157 | 158 | **Solution:** 159 | ```python 160 | raise RuntimeError('something wrong') 161 | ``` 162 | ---------------- 163 | 164 | ## Conclusion 165 | 166 | ***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.*** 167 | 168 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_12.md "Day 12") 169 | 170 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_14.md "Day 14") 171 | 172 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /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 | 2 | 3 | # Question 54 4 | 5 | ### **Question** 6 | 7 | > ***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.*** 8 | 9 | > ***Example: 10 | If the following email address is given as input to the program:*** 11 | ``` 12 | john@google.com 13 | ``` 14 | > ***Then, the output of the program should be:*** 15 | ``` 16 | google 17 | ``` 18 | > ***In case of input data being supplied to the question, it should be assumed to be a console input.*** 19 | 20 | ---------------------- 21 | ### Hints 22 | > ***Use \w to match letters.*** 23 | 24 | ---------------------- 25 | 26 | **Main author's Solution: Python 2** 27 | ```python 28 | import re 29 | emailAddress = raw_input() 30 | pat2 = "(\w+)@(\w+)\.(com)" 31 | r2 = re.match(pat2,emailAddress) 32 | print r2.group(2) 33 | ``` 34 | ---------------- 35 | **My Solution: Python 3** 36 | ```python 37 | import re 38 | 39 | email = "john@google.com elise@python.com" 40 | pattern = "\w+@(\w+).com" 41 | ans = re.findall(pattern,email) 42 | print(ans) 43 | ``` 44 | --------------------- 45 | 46 | 47 | # Question 55 48 | 49 | ### **Question** 50 | 51 | >***Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.*** 52 | 53 | >***Example: 54 | If the following words is given as input to the program:*** 55 | ``` 56 | 2 cats and 3 dogs. 57 | ``` 58 | >***Then, the output of the program should be:*** 59 | ``` 60 | ['2', '3'] 61 | ``` 62 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 63 | 64 | 65 | ---------------------- 66 | ### Hints 67 | > ***Use re.findall() to find all substring using regex.*** 68 | 69 | ---------------------- 70 | 71 | **Main author's Solution: Python 2** 72 | ```python 73 | import re 74 | s = raw_input() 75 | print re.findall("\d+",s) 76 | ``` 77 | ---------------- 78 | **My Solution: Python 3** 79 | ```python 80 | import re 81 | 82 | email = input() 83 | pattern = "\d+" 84 | ans = re.findall(pattern,email) 85 | print(ans) 86 | ``` 87 | **OR** 88 | ```python 89 | email = input().split() 90 | ans = [] 91 | for word in email: 92 | if word.isdigit(): # can also use isnumeric() / isdecimal() function instead 93 | ans.append(word) 94 | print(ans) 95 | ``` 96 | **OR** 97 | ```python 98 | email = input().split() 99 | ans = [word for word in email if word.isdigit()] # using list comprehension method 100 | print(ans) 101 | ``` 102 | --------------------- 103 | 104 | 105 | 106 | # Question 56 107 | 108 | ### **Question** 109 | 110 | > ***Print a unicode string "hello world".*** 111 | 112 | ---------------------- 113 | ### Hints 114 | > ***Use u'strings' format to define unicode string.*** 115 | 116 | ---------------------- 117 | 118 | **Main author's Solution: Python 2** 119 | ```python 120 | unicodeString = u"hello world!" 121 | print unicodeString 122 | ``` 123 | ---------------- 124 | 125 | # Question 57 126 | 127 | ### **Question** 128 | 129 | > ***Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.*** 130 | 131 | ---------------------- 132 | ### Hints 133 | > ***Use unicode()/encode() function to convert.*** 134 | 135 | ---------------------- 136 | 137 | **Main author's Solution: Python 2** 138 | ```python 139 | s = raw_input() 140 | u = unicode( s ,"utf-8") 141 | print u 142 | ``` 143 | ---------------- 144 | **My Solution: Python 3** 145 | ```python 146 | s = input() 147 | u = s.encode('utf-8') 148 | print(u) 149 | ``` 150 | --------------------- 151 | 152 | # Question 58 153 | 154 | ### **Question** 155 | 156 | > ***Write a special comment to indicate a Python source code file is in unicode.*** 157 | 158 | ---------------------- 159 | ### Hints 160 | > ***Use unicode() function to convert.*** 161 | 162 | ---------------------- 163 | 164 | **Solution:** 165 | ```python 166 | # -*- coding: utf-8 -*- 167 | ``` 168 | ---------------- 169 | # Question 59 170 | 171 | ### **Question** 172 | 173 | >***Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).*** 174 | 175 | >***Example: 176 | If the following n is given as input to the program:*** 177 | ``` 178 | 5 179 | ``` 180 | >***Then, the output of the program should be:*** 181 | ``` 182 | 3.55 183 | ``` 184 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 185 | 186 | 187 | ---------------------- 188 | ### Hints 189 | > ***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*** 190 | 191 | ---------------------- 192 | 193 | **Main author's Solution: Python 2** 194 | ```python 195 | n=int(raw_input()) 196 | sum=0.0 197 | for i in range(1,n+1): 198 | sum += float(float(i)/(i+1)) 199 | print sum 200 | ``` 201 | ---------------- 202 | **My Solution: Python 3** 203 | ```python 204 | n = int(input()) 205 | sum = 0 206 | for i in range(1, n+1): 207 | sum+= i/(i+1) 208 | print(round(sum, 2)) # rounded to 2 decimal point 209 | ``` 210 | --------------------- 211 | 212 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_14.md "Day 14") 213 | 214 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_16.md "Day 16") 215 | 216 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_16.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Question 60 4 | 5 | ### **Question** 6 | 7 | >***Write a program to compute:*** 8 | ``` 9 | f(n)=f(n-1)+100 when n>0 10 | and f(0)=1 11 | ``` 12 | >***with a given n input by console (n>0).*** 13 | 14 | >***Example: 15 | If the following n is given as input to the program:*** 16 | ``` 17 | 5 18 | ``` 19 | >***Then, the output of the program should be:*** 20 | ``` 21 | 500 22 | ``` 23 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 24 | 25 | ---------------------- 26 | ### Hints 27 | > ***We can define recursive function in Python.*** 28 | 29 | ---------------------- 30 | 31 | **Main author's Solution: Python 2** 32 | ```python 33 | def f(n): 34 | if n==0: 35 | return 0 36 | else: 37 | return f(n-1)+100 38 | 39 | n=int(raw_input()) 40 | print f(n) 41 | ``` 42 | ---------------- 43 | **My Solution: Python 3** 44 | ```python 45 | def f(n): 46 | if n == 0: 47 | return 0 48 | return f(n-1) + 100 49 | 50 | n = int(input()) 51 | print(f(n)) 52 | ``` 53 | --------------------- 54 | 55 | # Question 61 56 | 57 | ### **Question** 58 | 59 | >***The Fibonacci Sequence is computed based on the following formula:*** 60 | ``` 61 | f(n)=0 if n=0 62 | f(n)=1 if n=1 63 | f(n)=f(n-1)+f(n-2) if n>1 64 | ``` 65 | >***Please write a program to compute the value of f(n) with a given n input by console.*** 66 | 67 | >***Example: 68 | If the following n is given as input to the program:*** 69 | ``` 70 | 7 71 | ``` 72 | >***Then, the output of the program should be:*** 73 | ``` 74 | 13 75 | ``` 76 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 77 | 78 | ---------------------- 79 | ### Hints 80 | > ***We can define recursive function in Python.*** 81 | 82 | ---------------------- 83 | 84 | **Main author's Solution: Python 2** 85 | ```python 86 | def f(n): 87 | if n == 0: return 0 88 | elif n == 1: return 1 89 | else: return f(n-1)+f(n-2) 90 | 91 | n=int(raw_input()) 92 | print f(n) 93 | ``` 94 | ---------------- 95 | **My Solution: Python 3** 96 | ```python 97 | def f(n): 98 | if n < 2: 99 | return n 100 | return f(n-1) + f(n-2) 101 | 102 | n = int(input()) 103 | print(f(n)) 104 | ``` 105 | --------------------- 106 | 107 | # Question 62 108 | 109 | ### **Question** 110 | 111 | >***The Fibonacci Sequence is computed based on the following formula:*** 112 | ``` 113 | f(n)=0 if n=0 114 | f(n)=1 if n=1 115 | f(n)=f(n-1)+f(n-2) if n>1 116 | ``` 117 | >***Please write a program to compute the value of f(n) with a given n input by console.*** 118 | 119 | >***Example: 120 | If the following n is given as input to the program:*** 121 | ``` 122 | 7 123 | ``` 124 | >***Then, the output of the program should be:*** 125 | ``` 126 | 0,1,1,2,3,5,8,13 127 | ``` 128 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 129 | 130 | ---------------------- 131 | ### Hints 132 | >***We can define recursive function in Python. 133 | Use list comprehension to generate a list from an existing list. 134 | Use string.join() to join a list of strings.*** 135 | 136 | ---------------------- 137 | 138 | **Main author's Solution: Python 2** 139 | ```python 140 | def f(n): 141 | if n == 0: return 0 142 | elif n == 1: return 1 143 | else: return f(n-1)+f(n-2) 144 | 145 | n=int(raw_input()) 146 | values = [str(f(x)) for x in range(0, n+1)] 147 | print ",".join(values) 148 | 149 | ``` 150 | ---------------- 151 | **My Solution: Python 3** 152 | ```python 153 | def f(n): 154 | if n < 2: 155 | fibo[n] = n 156 | return fibo[n] 157 | fibo[n] = f(n-1) + f(n-2) 158 | return fibo[n] 159 | 160 | n = int(input()) 161 | fibo = [0]*(n+1) # initialize a list of size (n+1) 162 | f(n) # call once and it will set value to fibo[0-n] 163 | fibo = [str(i) for i in fibo] # converting integer data to string type 164 | ans = ",".join(fibo) # joining all string element of fibo with ',' character 165 | print(ans) 166 | 167 | ``` 168 | --------------------- 169 | 170 | 171 | # Question 63 172 | 173 | ### **Question** 174 | 175 | >***Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console.*** 176 | 177 | >***Example: 178 | If the following n is given as input to the program:*** 179 | ``` 180 | 10 181 | ``` 182 | >***Then, the output of the program should be:*** 183 | ``` 184 | 0,2,4,6,8,10 185 | ``` 186 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 187 | 188 | ---------------------- 189 | ### Hints 190 | > ***Use yield to produce the next value in generator.*** 191 | 192 | ---------------------- 193 | 194 | **Solution:** 195 | ```python 196 | def EvenGenerator(n): 197 | i=0 198 | while i<=n: 199 | if i%2==0: 200 | yield i 201 | i+=1 202 | 203 | 204 | n=int(raw_input()) 205 | values = [] 206 | for i in EvenGenerator(n): 207 | values.append(str(i)) 208 | 209 | print ",".join(values) 210 | 211 | ``` 212 | ---------------- 213 | 214 | 215 | # Question 64 216 | 217 | ### **Question** 218 | 219 | >***Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console.*** 220 | 221 | >***Example: 222 | If the following n is given as input to the program:*** 223 | ``` 224 | 100 225 | ``` 226 | >***Then, the output of the program should be:*** 227 | ``` 228 | 0,35,70 229 | ``` 230 | >***In case of input data being supplied to the question, it should be assumed to be a console input.*** 231 | 232 | ---------------------- 233 | ### Hints 234 | > ***Use yield to produce the next value in generator.*** 235 | 236 | ---------------------- 237 | 238 | **Main author's Solution: Python 2** 239 | ```python 240 | def NumGenerator(n): 241 | for i in range(n+1): 242 | if i%5==0 and i%7==0: 243 | yield i 244 | 245 | n=int(raw_input()) 246 | values = [] 247 | for i in NumGenerator(n): 248 | values.append(str(i)) 249 | 250 | print ",".join(values) 251 | ``` 252 | ---------------- 253 | **My Solution: Python 3** 254 | ```python 255 | def generate(n): 256 | for i in range(n+1): 257 | if i % 35 == 0: # 5*7 = 35, if a number is divisible by a & b then it is also divisible by a*b 258 | yield i 259 | 260 | n = int(input()) 261 | resp = [str(i) for i in generate(n)] 262 | print(",".join(resp)) 263 | 264 | ``` 265 | --------------------- 266 | 267 | 268 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_15.md "Day 15") 269 | 270 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_17.md "Day 17") 271 | 272 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_17.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 65 3 | 4 | ### **Question** 5 | 6 | >***Please write assert statements to verify that every number in the list [2,4,6,8] is even.*** 7 | 8 | 9 | ---------------------- 10 | ### Hints 11 | > ***Use "assert expression" to make assertion.*** 12 | 13 | ---------------------- 14 | 15 | **Main author's Solution: Python 2** 16 | ```python 17 | li = [2,4,6,8] 18 | for i in li: 19 | assert i%2==0 20 | ``` 21 | ---------------- 22 | **My Solution: Python 3** 23 | ```python 24 | data = [2,4,5,6] 25 | for i in data: 26 | assert i%2 == 0, "{} is not an even number".format(i) 27 | ``` 28 | --------------------- 29 | 30 | 31 | # Question 66 32 | 33 | ### **Question** 34 | 35 | >***Please write a program which accepts basic mathematic expression from console and print the evaluation result.*** 36 | 37 | >***Example: 38 | If the following n is given as input to the program:*** 39 | ``` 40 | 35 + 3 41 | ``` 42 | >***Then, the output of the program should be:*** 43 | ``` 44 | 38 45 | ``` 46 | 47 | ---------------------- 48 | ### Hints 49 | > ***Use eval() to evaluate an expression.*** 50 | 51 | ---------------------- 52 | 53 | **Main author's Solution: Python 2** 54 | ```python 55 | expression = raw_input() 56 | print eval(expression) 57 | ``` 58 | ---------------- 59 | **My Solution: Python 3** 60 | ```python 61 | expression = input() 62 | ans = eval(expression) 63 | print(ans) 64 | ``` 65 | --------------------- 66 | 67 | # Question 67 68 | 69 | ### **Question** 70 | 71 | >***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.*** 72 | 73 | ---------------------- 74 | ### Hints 75 | >***Use if/elif to deal with conditions.*** 76 | 77 | ---------------------- 78 | 79 | **Main author's Solution: Python 2** 80 | ```python 81 | import math 82 | def bin_search(li, element): 83 | bottom = 0 84 | top = len(li)-1 85 | index = -1 86 | while top>=bottom and index==-1: 87 | mid = int(math.floor((top+bottom)/2.0)) 88 | if li[mid]==element: 89 | index = mid 90 | elif li[mid]>element: 91 | top = mid-1 92 | else: 93 | bottom = mid+1 94 | 95 | return index 96 | 97 | li=[2,5,7,9,11,17,222] 98 | print bin_search(li,11) 99 | print bin_search(li,12) 100 | 101 | ``` 102 | ---------------- 103 | **My Solution: Python 3** 104 | ```python 105 | #to be written 106 | 107 | ``` 108 | --------------------- 109 | 110 | 111 | # Question 68 112 | 113 | ### **Question** 114 | 115 | >***Please generate a random float where the value is between 10 and 100 using Python module.*** 116 | 117 | ---------------------- 118 | ### Hints 119 | > ***Use random.random() to generate a random float in [0,1].*** 120 | 121 | ---------------------- 122 | 123 | **Main author's Solution: Python 2** 124 | ```python 125 | import random 126 | print random.random()*100 127 | ``` 128 | ---------------- 129 | **My Solution: Python 3** 130 | ```python 131 | import random 132 | rand_num = random.uniform(10,100) 133 | print(rand_num) 134 | ``` 135 | --------------------- 136 | 137 | 138 | 139 | # Question 69 140 | 141 | ### **Question** 142 | 143 | >***Please generate a random float where the value is between 5 and 95 using Python module.*** 144 | 145 | 146 | ---------------------- 147 | ### Hints 148 | > ***Use random.random() to generate a random float in [0,1].*** 149 | 150 | ---------------------- 151 | 152 | **Main author's Solution: Python 2** 153 | ```python 154 | import random 155 | print random.random()*100-5 156 | ``` 157 | ---------------- 158 | **My Solution: Python 3** 159 | ```python 160 | import random 161 | rand_num = random.uniform(5,95) 162 | print(rand_num) 163 | ``` 164 | --------------------- 165 | 166 | 167 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_16.md "Day 16") 168 | 169 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_18.md "Day 18") 170 | 171 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_18.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 70 3 | 4 | ### **Question** 5 | 6 | >***Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension.*** 7 | 8 | 9 | ---------------------- 10 | ### Hints 11 | > ***Use random.choice() to a random element from a list.*** 12 | 13 | ---------------------- 14 | 15 | **Main author's Solution: Python 2** 16 | ```python 17 | li = [2,4,6,8] 18 | import random 19 | print random.choice([i for i in range(11) if i%2==0]) 20 | ``` 21 | ---------------- 22 | **My Solution: Python 3** 23 | ```python 24 | import random 25 | resp = [i for i in range(2,11,2)] 26 | print(random.choice(resp)) 27 | ``` 28 | --------------------- 29 | 30 | 31 | # Question 71 32 | 33 | ### **Question** 34 | 35 | >***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.*** 36 | 37 | ---------------------- 38 | ### Hints 39 | > ***Use random.choice() to a random element from a list.*** 40 | 41 | ---------------------- 42 | 43 | **Main author's Solution: Python 2** 44 | ```python 45 | import random 46 | print random.choice([i for i in range(10,151) if i%5==0 and i%7==0]) 47 | ``` 48 | ---------------- 49 | **My Solution: Python 3** 50 | ```python 51 | import random 52 | resp = [i for i in range(10,151) if i % 35 == 0 ] 53 | print(random.choice(resp)) 54 | ``` 55 | --------------------- 56 | 57 | # Question 72 58 | 59 | ### **Question** 60 | 61 | >***Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.*** 62 | 63 | ---------------------- 64 | ### Hints 65 | >***Use random.sample() to generate a list of random values.*** 66 | 67 | ---------------------- 68 | 69 | **Main author's Solution: Python 2** 70 | ```python 71 | 72 | import random 73 | print random.sample(range(100,201), 5) 74 | ``` 75 | ---------------- 76 | **My Solution: Python 3** 77 | ```python 78 | import random 79 | resp = random.sample(range(100,201),5) 80 | print(resp) 81 | ``` 82 | --------------------- 83 | 84 | 85 | # Question 73 86 | 87 | ### **Question** 88 | 89 | >***Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.*** 90 | 91 | ---------------------- 92 | ### Hints 93 | > ***Use random.sample() to generate a list of random values.*** 94 | 95 | ---------------------- 96 | 97 | **Main author's Solution: Python 2** 98 | ```python 99 | 100 | import random 101 | print random.sample([i for i in range(100,201) if i%2==0], 5) 102 | 103 | ``` 104 | ---------------- 105 | **My Solution: Python 3** 106 | ```python 107 | import random 108 | resp = random.sample(range(100,201,2),5) 109 | print(resp) 110 | ``` 111 | --------------------- 112 | 113 | 114 | 115 | # Question 74 116 | 117 | ### **Question** 118 | 119 | >***Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.*** 120 | 121 | 122 | ---------------------- 123 | ### Hints 124 | > ***Use random.sample() to generate a list of random values.*** 125 | 126 | ---------------------- 127 | 128 | **Main author's Solution: Python 2** 129 | ```python 130 | 131 | import random 132 | print random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5) 133 | ``` 134 | ---------------- 135 | **My Solution: Python 3** 136 | ```python 137 | import random 138 | lst = [i for i in range(1,1001) if i%35 == 0] 139 | resp = random.sample(lst,5) 140 | print(resp) 141 | ``` 142 | --------------------- 143 | 144 | 145 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_17.md "Day 17") 146 | 147 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_19.md "Day 19") 148 | 149 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_19.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 75 3 | 4 | ### **Question** 5 | 6 | >***Please write a program to randomly print a integer number between 7 and 15 inclusive.*** 7 | 8 | ---------------------- 9 | ### Hints 10 | > ***Use random.randrange() to a random integer in a given range.*** 11 | 12 | ---------------------- 13 | 14 | **Solution:** 15 | ```python 16 | import random 17 | print random.randrange(7,16) 18 | ``` 19 | ---------------- 20 | 21 | # Question 76 22 | 23 | ### **Question** 24 | 25 | >***Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!".*** 26 | 27 | ---------------------- 28 | ### Hints 29 | > ***Use zlib.compress() and zlib.decompress() to compress and decompress a string.*** 30 | 31 | ---------------------- 32 | 33 | **Solution:** 34 | ```python 35 | import zlib 36 | s = 'hello world!hello world!hello world!hello world!' 37 | t = zlib.compress(s) 38 | print t 39 | print zlib.decompress(t) 40 | ``` 41 | ---------------- 42 | 43 | # Question 77 44 | 45 | ### **Question** 46 | 47 | >***Please write a program to print the running time of execution of "1+1" for 100 times.*** 48 | 49 | ---------------------- 50 | ### Hints 51 | >***Use timeit() function to measure the running time.*** 52 | 53 | ---------------------- 54 | 55 | **Main author's Solution: Python 2** 56 | ```python 57 | 58 | from timeit import Timer 59 | t = Timer("for i in range(100):1+1") 60 | print t.timeit() 61 | ``` 62 | ---------------- 63 | **My Solution: Python 3** 64 | ```python 65 | import datetime 66 | 67 | before = datetime.datetime.now() 68 | for i in range(100): 69 | x = 1 + 1 70 | after = datetime.datetime.now() 71 | execution_time = after - before 72 | print(execution_time.microseconds) 73 | ``` 74 | **OR** 75 | ```python 76 | import time 77 | 78 | before = time.time() 79 | for i in range(100): 80 | x = 1 + 1 81 | after = time.time() 82 | execution_time = after - before 83 | print(execution_time) 84 | ``` 85 | --------------------- 86 | 87 | # Question 78 88 | 89 | ### **Question** 90 | 91 | >***Please write a program to shuffle and print the list [3,6,7,8].*** 92 | 93 | ---------------------- 94 | ### Hints 95 | > ***Use shuffle() function to shuffle a list.*** 96 | 97 | ---------------------- 98 | 99 | **Main author's Solution: Python 2** 100 | ```python 101 | 102 | from random import shuffle 103 | li = [3,6,7,8] 104 | shuffle(li) 105 | print li 106 | 107 | ``` 108 | ---------------- 109 | **My Solution: Python 3** 110 | ```python 111 | import random 112 | 113 | lst = [3,6,7,8] 114 | random.shuffle(lst) 115 | print(lst) 116 | ``` 117 | **OR** 118 | ```python 119 | import random 120 | 121 | # shuffle with a chosen seed 122 | lst = [3,6,7,8] 123 | seed = 7 124 | random.Random(seed).shuffle(lst) 125 | print(lst) 126 | ``` 127 | --------------------- 128 | 129 | # Question 79 130 | 131 | ### **Question** 132 | 133 | >***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"].*** 134 | 135 | 136 | ---------------------- 137 | ### Hints 138 | > ***Use list[index] notation to get a element from a list.*** 139 | 140 | ---------------------- 141 | 142 | **Main author's Solution: Python 2** 143 | ```python 144 | 145 | subjects=["I", "You"] 146 | verbs=["Play", "Love"] 147 | objects=["Hockey","Football"] 148 | for i in range(len(subjects)): 149 | for j in range(len(verbs)): 150 | for k in range(len(objects)): 151 | sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k]) 152 | print sentence 153 | ``` 154 | ---------------- 155 | **My Solution: Python 3** 156 | ```python 157 | subjects=["I", "You"] 158 | verbs=["Play", "Love"] 159 | objects=["Hockey","Football"] 160 | 161 | for sub in subjects: 162 | for verb in verbs: 163 | for obj in objects: 164 | print("{} {} {}".format(sub,verb,obj)) 165 | ``` 166 | --------------------- 167 | 168 | 169 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_18.md "Day 18") 170 | 171 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_20.md "Day 20") 172 | 173 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_20.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 80 3 | 4 | ### **Question** 5 | 6 | >***Please write a program to print the list after removing even numbers in [5,6,77,45,22,12,24].*** 7 | 8 | 9 | ---------------------- 10 | ### Hints 11 | > ***Use list comprehension to delete a bunch of element from a list.*** 12 | 13 | ---------------------- 14 | 15 | **Main author's Solution: Python 2** 16 | ```python 17 | li = [5,6,77,45,22,12,24] 18 | li = [x for x in li if x%2!=0] 19 | print li 20 | ``` 21 | ---------------- 22 | **My Solution: Python 3** 23 | ```python 24 | def isEven(n): 25 | return n%2!=0 26 | 27 | li = [5,6,77,45,22,12,24] 28 | lst = list(filter(isEven,li)) 29 | print(lst) 30 | ``` 31 | **OR** 32 | ```python 33 | li = [5,6,77,45,22,12,24] 34 | lst = list(filter(lambda n:n%2!=0,li)) 35 | print(lst) 36 | ``` 37 | --------------------- 38 | 39 | 40 | # Question 81 41 | 42 | ### **Question** 43 | 44 | >***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].*** 45 | 46 | ---------------------- 47 | ### Hints 48 | > ***Use list comprehension to delete a bunch of element from a list.*** 49 | 50 | ---------------------- 51 | 52 | **Main author's Solution: Python 2** 53 | ```python 54 | li = [12,24,35,70,88,120,155] 55 | li = [x for x in li if x%5!=0 and x%7!=0] 56 | print li 57 | ``` 58 | ---------------- 59 | **My Solution: Python 3** 60 | ```python 61 | li = [12,24,35,70,88,120,155] 62 | li = [x for x in li if x % 35!=0] 63 | print(li) 64 | ``` 65 | --------------------- 66 | 67 | # Question 82 68 | 69 | ### **Question** 70 | 71 | >***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].*** 72 | 73 | ---------------------- 74 | ### Hints 75 | >***Use list comprehension to delete a bunch of element from a list. 76 | Use enumerate() to get (index, value) tuple.*** 77 | 78 | ---------------------- 79 | 80 | **Main author's Solution: Python 2** 81 | ```python 82 | 83 | li = [12,24,35,70,88,120,155] 84 | li = [x for (i,x) in enumerate(li) if i%2!=0] 85 | print li 86 | ``` 87 | ---------------- 88 | **My Solution: Python 3** 89 | ```python 90 | li = [12,24,35,70,88,120,155] 91 | li = [li[i] for i in range(len(li)) if i%2 != 0] 92 | print(li) 93 | ``` 94 | --------------------- 95 | 96 | 97 | # Question 83 98 | 99 | ### **Question** 100 | 101 | >***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].*** 102 | 103 | ---------------------- 104 | ### Hints 105 | > ***Use list comprehension to delete a bunch of element from a list. 106 | Use enumerate() to get (index, value) tuple.*** 107 | 108 | ---------------------- 109 | 110 | **Main author's Solution: Python 2** 111 | ```python 112 | 113 | li = [12,24,35,70,88,120,155] 114 | li = [x for (i,x) in enumerate(li) if i<3 or 4 ***By using list comprehension, please write a program generate a 3\*5\*8 3D array whose each element is 0.*** 135 | 136 | 137 | ---------------------- 138 | ### Hints 139 | > ***Use list comprehension to make an array.*** 140 | 141 | ---------------------- 142 | 143 | **Solution:** 144 | ```python 145 | array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)] 146 | print array 147 | ``` 148 | ---------------- 149 | 150 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_19.md "Day 19") 151 | 152 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_21.md "Day 21") 153 | 154 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_21.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 85 3 | 4 | ### **Question** 5 | 6 | >***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].*** 7 | 8 | 9 | ---------------------- 10 | ### Hints 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 | ```python 17 | li = [12,24,35,70,88,120,155] 18 | li = [x for (i,x) in enumerate(li) if i not in (0,4,5)] 19 | print li 20 | ``` 21 | ---------------- 22 | **My Solution: Python 3** 23 | ```python 24 | li = [12,24,35,70,88,120,155] 25 | li = [li[i] for i in range(len(li)) if i not in (0,4,5)] 26 | print(li) 27 | ``` 28 | --------------------- 29 | 30 | 31 | # Question 86 32 | 33 | ### **Question** 34 | 35 | >***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].*** 36 | 37 | ---------------------- 38 | ### Hints 39 | > ***Use list's remove method to delete a value.*** 40 | 41 | ---------------------- 42 | 43 | **Main author's Solution: Python 2** 44 | ```python 45 | li = [12,24,35,24,88,120,155] 46 | li = [x for x in li if x!=24] 47 | print li 48 | ``` 49 | ---------------- 50 | **My Solution: Python 3** 51 | ```python 52 | li = [12,24,35,24,88,120,155] 53 | li.remove(24) # this will remove only the first occurrence of 24 54 | print(li) 55 | ``` 56 | --------------------- 57 | 58 | # Question 87 59 | 60 | ### **Question** 61 | 62 | >***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.*** 63 | 64 | ---------------------- 65 | ### Hints 66 | >***Use set() and "&=" to do set intersection operation.*** 67 | 68 | ---------------------- 69 | 70 | **Main author's Solution: Python 2** 71 | ```python 72 | 73 | set1=set([1,3,6,78,35,55]) 74 | set2=set([12,24,35,24,88,120,155]) 75 | set1 &= set2 76 | li=list(set1) 77 | print li 78 | ``` 79 | ---------------- 80 | **My Solution: Python 3** 81 | ```python 82 | list1 = [1,3,6,78,35,55] 83 | list2 = [12,24,35,24,88,120,155] 84 | set1= set(list1) 85 | set2= set(list2) 86 | intersection = set1 & set2 87 | print(intersection) 88 | ``` 89 | **OR** 90 | ```python 91 | list1 = [1,3,6,78,35,55] 92 | list2 = [12,24,35,24,88,120,155] 93 | set1= set(list1) 94 | set2= set(list2) 95 | intersection = set.intersection(set1,set2) 96 | print(intersection) 97 | ``` 98 | --------------------- 99 | 100 | 101 | # Question 88 102 | 103 | ### **Question** 104 | 105 | >***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.*** 106 | 107 | ---------------------- 108 | ### Hints 109 | > ***Use set() to store a number of values without duplicate.*** 110 | 111 | ---------------------- 112 | 113 | **Main author's Solution: Python 2** 114 | ```python 115 | def removeDuplicate( li ): 116 | newli=[] 117 | seen = set() 118 | for item in li: 119 | if item not in seen: 120 | seen.add( item ) 121 | newli.append(item) 122 | 123 | return newli 124 | 125 | li=[12,24,35,24,88,120,155,88,120,155] 126 | print removeDuplicate(li) 127 | 128 | ``` 129 | ---------------- 130 | **My Solution: Python 3** 131 | ```python 132 | li = [12,24,35,24,88,120,155,88,120,155] 133 | for i in li: 134 | if li.count(i) > 1: 135 | li.remove(i) 136 | print(li) 137 | ``` 138 | **OR** 139 | ```python 140 | def removeDuplicate( li ): 141 | seen = {} # dictionary 142 | for item in li: 143 | if item not in seen: 144 | seen[item] = True 145 | yield item 146 | 147 | li = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155] 148 | ans = list(removeDuplicate(li)) 149 | print(ans) 150 | ``` 151 | --------------------- 152 | 153 | 154 | # Question 89 155 | 156 | ### **Question** 157 | 158 | >***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.*** 159 | 160 | 161 | ---------------------- 162 | ### Hints 163 | > ***Use Subclass(Parentclass) to define a child class.*** 164 | 165 | ---------------------- 166 | 167 | **Solution:** 168 | ```python 169 | class Person(object): 170 | def getGender( self ): 171 | return "Unknown" 172 | 173 | class Male( Person ): 174 | def getGender( self ): 175 | return "Male" 176 | 177 | class Female( Person ): 178 | def getGender( self ): 179 | return "Female" 180 | 181 | aMale = Male() 182 | aFemale= Female() 183 | print aMale.getGender() 184 | print aFemale.getGender() 185 | ``` 186 | ---------------- 187 | 188 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_20.md "Day 20") 189 | 190 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_22.md "Day 22") 191 | 192 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_22.md: -------------------------------------------------------------------------------- 1 | 2 | # Question 90 3 | 4 | ### **Question** 5 | 6 | >***Please write a program which count and print the numbers of each character in a string input by console.*** 7 | 8 | >***Example: 9 | If the following string is given as input to the program:*** 10 | ``` 11 | abcdefgabc 12 | ``` 13 | >***Then, the output of the program should be:*** 14 | ``` 15 | a,2 16 | c,2 17 | b,2 18 | e,1 19 | d,1 20 | g,1 21 | f,1 22 | ``` 23 | ### Hints 24 | > ***Use dict to store key/value pairs. 25 | Use dict.get() method to lookup a key with default value.*** 26 | 27 | ---------------------- 28 | 29 | **Main author's Solution: Python 2** 30 | ```python 31 | dic = {} 32 | s=raw_input() 33 | for s in s: 34 | dic[s] = dic.get(s,0)+1 35 | print '\n'.join(['%s,%s' % (k, v) for k, v in dic.items()]) 36 | ``` 37 | ---------------- 38 | **My Solution: Python 3** 39 | ```python 40 | import string 41 | 42 | s = input() 43 | for letter in string.ascii_lowercase: 44 | cnt = s.count(letter) 45 | if cnt > 0: 46 | print("{},{}".format(letter,cnt)) 47 | ``` 48 | **OR** 49 | ```python 50 | s = input() 51 | for letter in range(ord('a'),ord('z')+1): # ord() gets the ascii value of a char 52 | letter = chr(letter) # chr() gets the char of an ascii value 53 | cnt = s.count(letter) 54 | if cnt > 0: 55 | print("{},{}".format(letter,cnt)) 56 | ``` 57 | --------------------- 58 | 59 | 60 | # Question 91 61 | 62 | ### **Question** 63 | 64 | >***Please write a program which accepts a string from console and print it in reverse order.*** 65 | 66 | >**Example: 67 | If the following string is given as input to the program:*** 68 | ``` 69 | rise to vote sir 70 | ``` 71 | >***Then, the output of the program should be:*** 72 | ``` 73 | ris etov ot esir 74 | ``` 75 | ### Hints 76 | > ***Use list[::-1] to iterate a list in a reverse order.*** 77 | 78 | ---------------------- 79 | 80 | **Main author's Solution: Python 2** 81 | ```python 82 | s=raw_input() 83 | s = s[::-1] 84 | print s 85 | ``` 86 | ---------------- 87 | **My Solution: Python 3** 88 | ```python 89 | s = input() 90 | s = ''.join(reversed(s)) 91 | print(s) 92 | ``` 93 | --------------------- 94 | 95 | # Question 92 96 | 97 | ### **Question** 98 | 99 | >***Please write a program which accepts a string from console and print the characters that have even indexes.*** 100 | 101 | >***Example: 102 | If the following string is given as input to the program:*** 103 | ``` 104 | H1e2l3l4o5w6o7r8l9d 105 | ``` 106 | >***Then, the output of the program should be:*** 107 | ``` 108 | Helloworld 109 | ``` 110 | ### Hints 111 | >***Use list[::2] to iterate a list by step 2.*** 112 | 113 | ---------------------- 114 | 115 | **Main author's Solution: Python 2** 116 | ```python 117 | s=raw_input() 118 | s = s[::2] 119 | print s 120 | ``` 121 | ---------------- 122 | **My Solution: Python 3** 123 | ```python 124 | s = "H1e2l3l4o5w6o7r8l9d" 125 | s = [ s[i] for i in range(len(s)) if i%2 ==0 ] 126 | print(''.join(s)) 127 | ``` 128 | **OR** 129 | ```python 130 | s = "H1e2l3l4o5w6o7r8l9d" 131 | ns ='' 132 | for i in range(len(s)): 133 | if i % 2 == 0: 134 | ns+=s[i] 135 | print(ns) 136 | ``` 137 | --------------------- 138 | 139 | 140 | # Question 93 141 | 142 | ### **Question** 143 | 144 | >***Please write a program which prints all permutations of [1,2,3]*** 145 | 146 | ---------------------- 147 | ### Hints 148 | > ***Use itertools.permutations() to get permutations of list.*** 149 | 150 | ---------------------- 151 | 152 | **Solution:** 153 | ```python 154 | 155 | import itertools 156 | print list(itertools.permutations([1,2,3])) 157 | ``` 158 | ---------------- 159 | 160 | 161 | # Question 94 162 | 163 | ### **Question** 164 | 165 | >***Write a program to solve a classic ancient Chinese puzzle: 166 | 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?*** 167 | 168 | 169 | ---------------------- 170 | ### Hints 171 | > ***Use for loop to iterate all possible solutions.*** 172 | 173 | ---------------------- 174 | 175 | **Solution:** 176 | ```python 177 | def solve(numheads,numlegs): 178 | ns='No solutions!' 179 | for i in range(numheads+1): 180 | j=numheads-i 181 | if 2*i+4*j==numlegs: 182 | return i,j 183 | return ns,ns 184 | 185 | numheads=35 186 | numlegs=94 187 | solutions=solve(numheads,numlegs) 188 | print solutions 189 | ``` 190 | ---------------- 191 | 192 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_21.md "Day 21") 193 | 194 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_23.md "Day 23") 195 | 196 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /Status/Day_23.md: -------------------------------------------------------------------------------- 1 | # The extended part of the repository starts from this page. Previous 94 problems were collected from the repository mentioned in intro. The following problems are collected from Hackerrank and other resources from internet.All the given solutions are in python 3. 2 | 3 | # Question 95 4 | 5 | ### **Question** 6 | 7 | >***Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.*** 8 | 9 | >***If the following string is given as input to the program:*** 10 | >``` 11 | >5 12 | >2 3 6 6 5 13 | >``` 14 | >***Then, the output of the program should be:*** 15 | >``` 16 | >5 17 | >``` 18 | ### Hints 19 | > ***Make the scores unique and then find 2nd best number*** 20 | 21 | ---------------------- 22 | **My Solution: Python 3** 23 | ```python 24 | n = int(input()) 25 | arr = map(int, input().split()) 26 | arr = list(set(arr)) 27 | arr.sort() 28 | print(arr[-2]) 29 | ``` 30 | --------------------- 31 | 32 | # Question 96 33 | 34 | ### **Question** 35 | 36 | >***You are given a string S and width W. 37 | Your task is to wrap the string into a paragraph of width.*** 38 | 39 | >***If the following string is given as input to the program:*** 40 | >``` 41 | >ABCDEFGHIJKLIMNOQRSTUVWXYZ 42 | >4 43 | >``` 44 | >***Then, the output of the program should be:*** 45 | >``` 46 | >ABCD 47 | >EFGH 48 | >IJKL 49 | >IMNO 50 | >QRST 51 | >UVWX 52 | >YZ 53 | >``` 54 | 55 | ### Hints 56 | > ***Use wrap function of textwrap module*** 57 | 58 | ---------------------- 59 | 60 | **My Solution: Python 3** 61 | ```python 62 | import textwrap 63 | 64 | def wrap(string, max_width): 65 | string = textwrap.wrap(string,max_width) 66 | string = "\n".join(string) 67 | return string 68 | 69 | if __name__ == '__main__': 70 | string, max_width = input(), int(input()) 71 | result = wrap(string, max_width) 72 | print(result) 73 | ``` 74 | --------------------- 75 | 76 | # Question 97 77 | 78 | ### **Question** 79 | 80 | >***You are given an integer, N. Your task is to print an alphabet rangoli of size N. (Rangoli is a form of Indian folk art based on creation of patterns.)*** 81 | 82 | >***Different sizes of alphabet rangoli are shown below:*** 83 | >``` 84 | >#size 3 85 | > 86 | >----c---- 87 | >--c-b-c-- 88 | >c-b-a-b-c 89 | >--c-b-c-- 90 | >----c---- 91 | > 92 | >#size 5 93 | > 94 | >--------e-------- 95 | >------e-d-e------ 96 | >----e-d-c-d-e---- 97 | >--e-d-c-b-c-d-e-- 98 | >e-d-c-b-a-b-c-d-e 99 | >--e-d-c-b-c-d-e-- 100 | >----e-d-c-d-e---- 101 | >------e-d-e------ 102 | >--------e-------- 103 | >``` 104 | ### Hints 105 | >***First print the half of the Rangoli in the given way and save each line in a list. Then print the list in reverse order to get the rest.*** 106 | 107 | ---------------------- 108 | **My Solution: Python 3** 109 | ```python 110 | 111 | import string 112 | def print_rangoli(size): 113 | n = size 114 | alph = string.ascii_lowercase 115 | width = 4 * n - 3 116 | 117 | ans = [] 118 | for i in range(n): 119 | left = '-'.join(alph[n - i - 1:n]) 120 | mid = left[-1:0:-1] + left 121 | final = mid.center(width, '-') 122 | ans.append(final) 123 | 124 | if len(ans) > 1: 125 | for i in ans[n - 2::-1]: 126 | ans.append(i) 127 | ans = '\n'.join(ans) 128 | print(ans) 129 | 130 | 131 | if __name__ == '__main__': 132 | n = int(input()) 133 | print_rangoli(n) 134 | ``` 135 | --------------------- 136 | 137 | 138 | # Question 98 139 | 140 | ### **Question** 141 | 142 | >***You are given a date. Your task is to find what the day is on that date.*** 143 | 144 | **Input** 145 | >***A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.*** 146 | >``` 147 | >08 05 2015 148 | >``` 149 | 150 | 151 | **Output** 152 | >***Output the correct day in capital letters.*** 153 | >``` 154 | >WEDNESDAY 155 | >``` 156 | 157 | 158 | ---------------------- 159 | ### Hints 160 | > ***Use weekday function of calender module*** 161 | 162 | ---------------------- 163 | 164 | **Solution:** 165 | ```python 166 | import calendar 167 | 168 | month, day, year = map(int, input().split()) 169 | 170 | dayId = calendar.weekday(year, month, day) 171 | print(calendar.day_name[dayId].upper()) 172 | ``` 173 | ---------------- 174 | 175 | 176 | # Question 99 177 | 178 | ### **Question** 179 | 180 | >***Given 2 sets of integers, M and N, print their symmetric difference in ascending order. The term symmetric difference indicates those values that exist in either M or N but do not exist in both.*** 181 | 182 | **Input** 183 | >***The first line of input contains an integer, M.The second line contains M space-separated integers.The third line contains an integer, N.The fourth line contains N space-separated integers.*** 184 | >``` 185 | >4 186 | >2 4 5 9 187 | >4 188 | >2 4 11 12 189 | >``` 190 | 191 | **Output** 192 | >***Output the symmetric difference integers in ascending order, one per line.*** 193 | >``` 194 | >5 195 | >9 196 | >11 197 | >12 198 | >``` 199 | 200 | 201 | ---------------------- 202 | ### Hints 203 | > ***Use \'^\' to make symmetric difference operation.*** 204 | 205 | ---------------------- 206 | 207 | **Solution:** 208 | ```python 209 | if __name__ == '__main__': 210 | n = int(input()) 211 | set1 = set(map(int,input().split())) 212 | 213 | m = int(input()) 214 | set2 = set(map(int, input().split())) 215 | 216 | ans = list(set1 ^ set2) 217 | ans.sort() 218 | for i in ans: 219 | print(i) 220 | ``` 221 | ---------------- 222 | 223 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_22.md "Day 22") 224 | 225 | [***go to next day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_24.md "Day 24") 226 | 227 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) -------------------------------------------------------------------------------- /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 | >4 10 | >bcdef 11 | >abcdefg 12 | >bcde 13 | >bcdef 14 | >``` 15 | >***Then, the output of the program should be:*** 16 | >``` 17 | >3 18 | >2 1 1 19 | >``` 20 | 21 | ### Hints 22 | > ***Make a list to get the input order and a dictionary to count the word frequency*** 23 | 24 | ---------------------- 25 | **My Solution: Python 3** 26 | ```python 27 | n = int(input()) 28 | 29 | word_list = [] 30 | word_dict = {} 31 | 32 | for i in range(n): 33 | word = input() 34 | if word not in word_dict: 35 | word_list.append(word) 36 | word_dict[word] = word_dict.get(word, 0) + 1 37 | 38 | print(len(word_list)) 39 | for word in word_list: 40 | print(word_dict[word], end=' ') 41 | ``` 42 | --------------------- 43 | 44 | # Question 101 45 | 46 | ### **Question** 47 | 48 | >***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.*** 49 | 50 | >***If the following string is given as input to the program:*** 51 | >``` 52 | >aabbbccde 53 | >``` 54 | >***Then, the output of the program should be:*** 55 | >``` 56 | >b 3 57 | >a 2 58 | >c 2 59 | >``` 60 | 61 | ### Hints 62 | > ***Count frequency with dictionary and sort by Value from dictionary Items*** 63 | 64 | ---------------------- 65 | 66 | **My Solution: Python 3** 67 | ```python 68 | word = input() 69 | dct = {} 70 | for i in word: 71 | dct[i] = dct.get(i,0) + 1 72 | 73 | dct = sorted(dct.items(),key=lambda x: (-x[1],x[0])) 74 | for i in dct[:3]: 75 | print(i[0],i[1]) 76 | ``` 77 | --------------------- 78 | 79 | 80 | # Question 102 81 | ### **Question** 82 | 83 | >***Write a Python program that accepts a string and calculate the number of digits and letters.*** 84 | 85 | **Input** 86 | >``` 87 | >Hello321Bye360 88 | >``` 89 | 90 | **Output** 91 | >``` 92 | >Digit - 6 93 | >Letter - 8 94 | >``` 95 | ---------------------- 96 | ### Hints 97 | > ***Use isdigit() and isalpha() function*** 98 | 99 | ---------------------- 100 | 101 | **Solution:** 102 | ```python 103 | word = input() 104 | digit,letter = 0,0 105 | for char in word: 106 | digit+=char.isdigit() 107 | letter+=char.isalpha() 108 | 109 | print('Digit -',digit) 110 | print('Letter -',letter) 111 | ``` 112 | ---------------- 113 | 114 | 115 | # Question 103 116 | 117 | ### **Question** 118 | 119 | >***Given a number N.Find Sum of 1 to N Using Recursion*** 120 | 121 | **Input** 122 | >``` 123 | >5 124 | >``` 125 | 126 | **Output** 127 | >``` 128 | >15 129 | >``` 130 | 131 | ---------------------- 132 | ### Hints 133 | > ***Make a recursive function to get the sum*** 134 | 135 | ---------------------- 136 | 137 | **Solution:** 138 | ```python 139 | def rec(n): 140 | if n == 0: 141 | return n 142 | return rec(n-1) + n 143 | 144 | 145 | n = int(input()) 146 | sum = rec(n) 147 | print(sum) 148 | ``` 149 | ---------------- 150 | 151 | [***go to previous day***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/blob/master/Status/Day_22.md "Day 23") 152 | 153 | [***Discussion***](https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3) 154 | 155 | # To Be Continue... -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate --------------------------------------------------------------------------------