Skip to content

Commit 7253402

Browse files
Merge pull request #5 from HighnessAtharva/sourcery/master
Sourcery refactored master branch
2 parents 267e8eb + 8270d44 commit 7253402

30 files changed

+587
-997
lines changed

Python/Basic Exercises I.py

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
# Area = 3.8013271108436504
3535
from math import pi
3636
r = float(input("Input the radius of the circle : "))
37-
print("The area of the circle with radius " + str(r) + " is: " + str(pi * r**2))
37+
print(f"The area of the circle with radius {r} is: {str(pi * r**2)}")
3838

3939

4040
# 5. Write a Python program which accepts the user's first and last name and print them in reverse order with a space between them.
4141
fname = input("Input your First Name : ")
4242
lname = input("Input your Last Name : ")
43-
print("Hello " + lname + " " + fname)
43+
print(f"Hello {lname} {fname}")
4444

4545

4646
# 6. Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.
@@ -50,20 +50,20 @@
5050
# Tuple : ('3', ' 5', ' 7', ' 23')
5151

5252
color_list = ["Red", "Green", "White", "Black"]
53-
print("%s %s" % (color_list[0], color_list[-1]))
53+
print(f"{color_list[0]} {color_list[-1]}")
5454

5555
# 7. Write a Python program to accept a filename from the user and print the extension of that.
5656
# Sample filename : abc.java
5757
# Output : java
5858
filename = input("Input the Filename: ")
5959
f_extns = filename.split(".")
60-
print("The extension of the file is : " + repr(f_extns[-1]))
60+
print(f"The extension of the file is : {repr(f_extns[-1])}")
6161

6262

6363
# 8. Write a Python program to display the first and last colors from the following list.
6464
# color_list = ["Red","Green","White" ,"Black"]
6565
color_list = ["Red", "Green", "White", "Black"]
66-
print("%s %s" % (color_list[0], color_list[-1]))
66+
print(f"{color_list[0]} {color_list[-1]}")
6767

6868

6969
# 9. Write a Python program to display the examination schedule. (extract the date from exam_st_date).
@@ -78,9 +78,9 @@
7878
# Expected Result : 615
7979

8080
a = int(input("Input an integer : "))
81-
n1 = int("%s" % a)
82-
n2 = int("%s%s" % (a, a))
83-
n3 = int("%s%s%s" % (a, a, a))
81+
n1 = int(f"{a}")
82+
n2 = int(f"{a}{a}")
83+
n3 = int(f"{a}{a}{a}")
8484
print(n1 + n2 + n3)
8585

8686
# 11. Write a Python program to print the documents (syntax, description etc.) of Python built-in function(s).
@@ -132,10 +132,7 @@
132132

133133

134134
def difference(n):
135-
if n <= 17:
136-
return 17 - n
137-
else:
138-
return (n - 17) * 2
135+
return 17 - n if n <= 17 else (n - 17) * 2
139136

140137

141138
print(difference(22))
@@ -168,9 +165,7 @@ def sum_thrice(x, y, z):
168165

169166
# 19. Write a Python program to get a new string from a given string where "Is" has been added to the front. If the given string already begins with "Is" then return the string unchanged.
170167
def new_string(str):
171-
if len(str) >= 2 and str[:2] == "Is":
172-
return str
173-
return "Is" + str
168+
return str if len(str) >= 2 and str[:2] == "Is" else f"Is{str}"
174169

175170

176171
print(new_string("Array"))
@@ -203,12 +198,11 @@ def list_count_4(nums):
203198

204199
def substring_copy(str, n):
205200
flen = 2
206-
if flen > len(str):
207-
flen = len(str)
201+
flen = min(flen, len(str))
208202
substr = str[:flen]
209203

210204
result = ""
211-
for i in range(n):
205+
for _ in range(n):
212206
result = result + substr
213207
return result
214208

@@ -232,9 +226,7 @@ def is_vowel(char):
232226
# 3 -> [1, 5, 8, 3] : True
233227
# -1 -> [1, 5, 8, 3] : False
234228
def is_group_member(group_data, n):
235-
if n in group_data:
236-
return True
237-
return False
229+
return n in group_data
238230

239231
print(is_group_member([1, 5, 8, 3], 3))
240232
print(is_group_member([5, 8, 3], -1))
@@ -243,9 +235,7 @@ def is_group_member(group_data, n):
243235

244236
def histogram(items):
245237
for n in items:
246-
str = ''
247-
for i in range(n):
248-
str += '*'
238+
str = ''.join('*' for _ in range(n))
249239
print(str)
250240

251241
histogram([2, 3, 6, 5])
@@ -255,10 +245,7 @@ def histogram(items):
255245

256246

257247
def concatenate_list_data(list):
258-
result = ''
259-
for element in list:
260-
result += str(element)
261-
return result
248+
return ''.join(str(element) for element in list)
262249

263250

264251
print(concatenate_list_data([1, 5, 12, 2]))
@@ -294,8 +281,8 @@ def concatenate_list_data(list):
294281
# color_list_2 = set(["Red", "Green"])
295282
# Expected Output :
296283
# {'Black', 'White'}
297-
color_list_1 = set(["White", "Black", "Red"])
298-
color_list_2 = set(["Red", "Green"])
284+
color_list_1 = {"White", "Black", "Red"}
285+
color_list_2 = {"Red", "Green"}
299286

300287
print(color_list_1-color_list_2)
301288

@@ -308,16 +295,12 @@ def concatenate_list_data(list):
308295

309296
# 31. Write a Python program to compute the greatest common divisor (GCD) of two positive integers.
310297
def gcd(x, y):
311-
gcd = 1
312-
313298
if x % y == 0:
314299
return y
315300

316-
for k in range(int(y / 2), 0, -1):
317-
if x % k == 0 and y % k == 0:
318-
gcd = k
319-
break
320-
return gcd
301+
return next(
302+
(k for k in range(int(y / 2), 0, -1) if x % k == 0 and y % k == 0), 1
303+
)
321304

322305

323306
print(gcd(12, 17))
@@ -327,11 +310,7 @@ def gcd(x, y):
327310

328311

329312
def lcm(x, y):
330-
if x > y:
331-
z = x
332-
else:
333-
z = y
334-
313+
z = max(x, y)
335314
while(True):
336315
if((z % x == 0) and (z % y == 0)):
337316
lcm = z
@@ -348,11 +327,7 @@ def lcm(x, y):
348327
# 33. Write a Python program to sum of three given integers. However, if two values are equal sum will be zero.
349328

350329
def sum(x, y, z):
351-
if x == y or y == z or x == z:
352-
sum = 0
353-
else:
354-
sum = x + y + z
355-
return sum
330+
return 0 if x == y or y == z or x == z else x + y + z
356331

357332

358333
print(sum(2, 1, 2))
@@ -364,21 +339,15 @@ def sum(x, y, z):
364339

365340
def sum(x, y):
366341
sum = x + y
367-
if sum in range(15, 20):
368-
return 20
369-
else:
370-
return sum
342+
return 20 if sum in range(15, 20) else sum
371343

372344
print(sum(10, 6))
373345
print(sum(10, 2))
374346
print(sum(10, 12))
375347

376348
# 35. Write a Python program that will return true if the two given integer values are equal or their sum or difference is 5.
377349
def test_number5(x, y):
378-
if x == y or abs(x-y) == 5 or (x+y) == 5:
379-
return True
380-
else:
381-
return False
350+
return x == y or abs(x-y) == 5 or (x+y) == 5
382351

383352
print(test_number5(7, 2))
384353
print(test_number5(3, 2))
@@ -407,8 +376,8 @@ def personal_details():
407376
# Expected Output : (4 + 3) ^ 2) = 49
408377

409378
x, y = 4, 3
410-
result = x * x + 2 * x * y + y * y
411-
print("({} + {}) ^ 2) = {}".format(x, y, result))
379+
result = x**2 + 2 * x * y + y**2
380+
print(f"({x} + {y}) ^ 2) = {result}")
412381

413382
# 39. Write a Python program to compute the future value of a specified principal amount, rate of interest, and a number of years.
414383
# Test Data : amt = 10000, int = 3.5, years = 7
@@ -451,7 +420,7 @@ def personal_details():
451420
print(platform.release())
452421

453422
# 44. Write a Python program to locate Python site-packages.
454-
import site;
423+
import site;
455424
print(site.getsitepackages())
456425

457426

@@ -480,13 +449,13 @@ def personal_details():
480449
from os.path import isfile, join
481450
files_list = [f for f in listdir('/home/students') if isfile(join('/home/students', f))]
482451
print(files_list);
483-
452+
484453

485454
# 50. Write a Python program to print without newline or space.
486-
for i in range(0, 10):
455+
for _ in range(10):
487456
print('*', end="")
488457
print("\n")
489-
458+
490459

491460
# 51. Write a Python program to determine profiling of Python programs.
492461
# Note: A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module.

0 commit comments

Comments
 (0)