Skip to content

Commit 7784b51

Browse files
committed
Structuring Things
1 parent 9277f79 commit 7784b51

29 files changed

+87
-146
lines changed

Adding_Ones.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
link = "https://practice.geeksforgeeks.org/problems/adding-ones3628/1#"
33
"""
44

5+
56
c = [0,0,0] ##### Optimized code for below program
67
updates = [1,1,2,3]
78
k = 4
89
n = 3
9-
i = 0
10-
while (i<k):
10+
for i in range(k):
1111
c[updates[i]-1] +=1
12-
i +=1
13-
i = 1
14-
while (i<n):
12+
for i in range(1, n):
1513
c[i]+=c[i-1]
16-
i +=1
1714
print(c)
1815

1916

@@ -22,11 +19,9 @@
2219
l1 = inp1[0] + 1
2320
l2 = inp1[1]
2421
final_arr = inp2[:l2]
25-
arr = []
2622
count = 0
2723

28-
for x in range(l1):
29-
arr.append(0)
24+
arr = [0 for _ in range(l1)]
3025
for y in final_arr:
3126
for x in range(1,l1):
3227
if y <= x:

Advance_String.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ def subString(s):
1212
for x in final:
1313
length = len(x)
1414
disticnt_char = len(set(x))
15-
eq = eq + pow(length,disticnt_char)%100000007
15+
eq += pow(length,disticnt_char)%100000007
1616

1717
return int(eq%100000007)
18-
1918

2019
s = "ddddddddddddddddddeeeeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiiiyyyyyyyyyyyyyyyyyyiyd"
2120
ans = subString(s)

Apples_Orange_Problem.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"""
44

55
def method_3(s,t,a,b,apples,oranges): #### Optimized Code ####
6-
print(sum([1 for x in apples if (x + a) >= s and (x + a) <= t]))
7-
print(sum([1 for x in oranges if (x + b) >= s and (x + b) <= t]))
6+
print(sum((x + a) >= s and (x + a) <= t for x in apples))
7+
print(sum((x + b) >= s and (x + b) <= t for x in oranges))
88

99
def method_4(s,t,a,b,apples,oranges): #### Optimized Code ####
1010
print(sum(s <= a + d <= t for d in apples))
@@ -16,12 +16,9 @@ def method_1(s,t,a,b,apples,oranges): #### time Complexity exceeds in
1616
count_apples =0
1717
count_oragnes =0
1818
y = (t-s)+1
19-
le_app = 0
19+
le_app = 0
2020
x = 0
21-
if len(apples) >= len(oranges):
22-
le_app = len(apples)
23-
else:
24-
le_app = len(oranges)
21+
le_app = max(len(apples), len(oranges))
2522
while(y!=0):
2623
house_arr.append(temp)
2724
temp +=1
@@ -36,7 +33,7 @@ def method_1(s,t,a,b,apples,oranges): #### time Complexity exceeds in
3633
if oranges[x] in house_arr:
3734
count_oragnes +=1
3835
x +=1
39-
le_app -=1
36+
le_app -=1
4037
print(count_apples)
4138
print(count_oragnes)
4239

@@ -45,14 +42,14 @@ def method_2(s,t,a,b,apples,oranges): #### time Complexity exceeds
4542
temp = s
4643
count_apples =0
4744
count_oragnes =0
48-
for x in range((t-s)+1):
45+
for _ in range(t - temp + 1):
4946
house_arr.append(temp)
5047
temp +=1
51-
print(house_arr)
48+
print(house_arr)
5249
for x in range(len(apples)):
5350
apples[x] = apples[x] + a
5451
if apples[x] in house_arr:
55-
count_apples +=1
52+
count_apples +=1
5653
for x in range(len(oranges)):
5754
oranges[x] = oranges[x] + b
5855
if oranges[x] in house_arr:

Between Two Set.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@ def getTotalX(a, b):
44
prepre = []
55
lis = [x for x in range(a[-1],b[0]+1)]
66
for x in lis:
7-
c=0
8-
for y in a:
9-
if x%y==0:
10-
c+=1
7+
c = sum(x%y==0 for y in a)
118
if c == len(a):
129
s=0
1310
for x in pre:
14-
c=0
15-
for y in b:
16-
if y%x==0:
17-
c+=1
11+
c = sum(y%x==0 for y in b)
1812
if c==len(b):
19-
s+=1
20-
13+
s+=1
2114
print(s)
2215

2316

Count_Sum.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313

1414
def processing(n,sum,arr):
1515
temp = 0
16-
final = []
16+
final = []
1717
t = 0
1818
for y in arr:
1919
t = y
20-
for x in range(n):
21-
temp = temp + (int(t)%10)
20+
for _ in range(n):
21+
temp += int(t)%10
2222
t = int(t/10)
23-
t = 0
23+
t = 0
2424
if temp == sum:
2525
final.append(y)
26-
temp = 0
26+
temp = 0
2727
print(final)
2828
print("Count = {}".format(len(final)))
2929
final.clear()
3030

3131
def getCount(n,sum):
32-
arr = []
32+
arr = []
3333
if n == 2:
3434
arr = range(11,100)
35-
processing(n,sum,arr)
3635
else:
3736
start = math.pow(10,n-1)
38-
end = start * 10
37+
end = start * 10
3938
arr = range(int(start),int(end))
40-
processing(n,sum,arr)
39+
40+
processing(n,sum,arr)
4141

4242
getCount(2,3)
4343
getCount(3,3)

Counting_Vally.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ def countingValleys(steps, path):
33
count = 0
44
for x in range(steps):
55
if path[x]=='D' and sealevel ==0:
6-
sealevel = sealevel - 1
6+
sealevel -= 1
77
count +=1
88
elif path[x] == 'D':
9-
sealevel = sealevel - 1
9+
sealevel -= 1
1010
elif path[x] == 'U':
11-
sealevel = sealevel + 1
11+
sealevel += 1
1212
return count
1313

1414
a = "UDDDUDUU"

Find_Digits.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,15 @@ def caesarCipher(s, k):
4444
e = []
4545
for item in l:
4646
z = ord(item)
47-
if (z >= 97 and z<=122):
48-
if (z+k) > 122:
49-
e.append(chr(96 + (z+k-122)))
50-
else:
51-
e.append(chr(z+k))
52-
elif (z>=65 and z<=90):
53-
if (z+k) > 90:
54-
e.append(chr(64 + (z+k-90)))
55-
else:
56-
e.append(chr(z+k))
47+
if (z >= 97 and z <= 122) and (z + k) > 122:
48+
e.append(chr(96 + (z+k-122)))
49+
elif z >= 97 and z <= 122 or z >= 65 and z <= 90 and z + k <= 90:
50+
e.append(chr(z+k))
51+
elif z >= 65 and z <= 90:
52+
e.append(chr(64 + (z+k-90)))
5753
else:
5854
e.append(item)
59-
print("".join([x for x in e]))
55+
print("".join(e))
6056

6157

6258
s ="middle-Outz"

Grading_Problem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
if grades[x]<38:
66
continue
77
while t%5!=0:
8-
t +=1
8+
t +=1
99
if (t - temp) < 3:
1010
grades[x]=t
1111
print(grades)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Halloween Sale.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ def howManyGames(p, d, m, s):
44
deduction = d
55
minprice = m
66
c=0
7-
while(1):
7+
while 1:
88
budget = budget - price
99
if budget>=0:
1010
price = price -deduction
1111
if price>minprice:
1212
c+=1
1313
continue
14-
elif price<=minprice:
14+
else:
1515
if budget >= price-minprice:
1616
c+=1
1717
price = minprice
1818
else:
19-
break
19+
break
2020
else:
21-
break
21+
break
2222
print(c)
2323

2424
p = 20

Kangaroo_problem.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@
22
link = https://www.hackerrank.com/challenges/kangaroo/problem
33
"""
44
def kangaroo_optimization(x1, v1, x2, v2):
5-
y=0
6-
while (y<10000):
5+
for y in range(10000):
76
if x1 + y * v1 == x2 + y * v2:
87
return "YES"
9-
y+=1
108
return "NO"
11-
129

1310
def kangaroo(x1, v1, x2, v2):
1411
diff1 =v1
1512
diff2 = v2
16-
k=0
1713
flag = 0
18-
while k<10000:
14+
for k in range(10000):
1915
if (x2 > x1 and v2>v1) or (x1>x2 and v1>v2):
2016
return "NO"
21-
break
2217
a = x1+v1
2318
b = x2+v2
2419
if a==b:
2520
return "YES"
26-
break
2721
v1 = v1 + diff1
2822
v2 = v2 + diff2
2923
if k == 9999:
30-
flag=1
31-
k+=1
24+
flag=1
3225
if flag ==1:
3326
return "NO"
3427

Luck Balance.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ def luckBalance(k, contests):
2121

2222
k = int(nk[1])
2323

24-
contests = []
25-
26-
for _ in range(n):
27-
contests.append(list(map(int, input().rstrip().split())))
24+
contests = [list(map(int, input().rstrip().split())) for _ in range(n)]
2825

2926
result = luckBalance(k, contests)
3027

MIgratory_Birds.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
arr = [1 ,2, 3, 4, 5, 4, 3, 2, 1, 3, 4]
2-
my_dict = {}
32
uni = set(arr)
4-
for x in uni:
5-
my_dict[x] = arr.count(x)
6-
3+
my_dict = {x: arr.count(x) for x in uni}
74
my_dict.fo
85
print(my_dict.values())
96

107

118

12-
13-
14-
15-
16-
17-
18-
199
# for x in uni:
2010
# a.append(arr.count(x))
2111
# for x in range(len(uni)):

Numbers.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
def cutTheSticks(arr):
2-
ans = []
3-
ans.append(len(arr))
2+
ans = [len(arr)]
43
while 1:
54
z = min(arr)
65
for x in range(len(arr)):
76
arr[x] = arr[x]-z
8-
arr = list(filter(lambda x: x != 0, arr))
9-
if len(arr) == 0:
10-
break
11-
ans.append(len(arr))
7+
arr = list(filter(lambda x: x != 0, arr))
8+
if not arr:
9+
break
10+
ans.append(len(arr))
1211
print(ans)
1312

1413
arr = [1 ,2 ,3 ,4, 3, 3, 2, 1]

Palindrom.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ def isPalindrome(s): #### Optimized way ####
33

44
def isPalindrome_third(s): #### Optimized way ####
55
rev = ''.join(reversed(s))
6-
if (s == rev):
7-
return True
8-
return False
6+
return (s == rev)
97

108
def isPalindrom_fifth(s): #### for numbers only ####
119
temp=s
@@ -30,10 +28,7 @@ def isPalindrom_one(i): #### Un-Optimized way Strings only####
3028
print("Not")
3129

3230
def isPalindrome_second(str): #### Un-Optimized way ####
33-
for i in range(0, int(len(str)/2)):
34-
if str[i] != str[len(str)-i-1]:
35-
return False
36-
return True
31+
return all(str[i] == str[len(str)-i-1] for i in range(int(len(str)/2)))
3732

3833
def isPalindrom_fourth(s): #### Un-Optimized way Strings only ####
3934
w = ""
@@ -44,7 +39,6 @@ def isPalindrom_fourth(s): #### Un-Optimized way Strings only ####
4439
else:
4540
print("No")
4641

47-
4842
s = "malayalam"
4943

5044
isPalindrom_one(s)

Reverse_Arry.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
######################################### For Numbers Only #########################################
22

33
def reverseNumber(s):
4-
st = 0
5-
en = -1
6-
for x in range(int(len(s)/2)):
4+
en = -1
5+
for st, _ in enumerate(range(int(len(s)/2))):
76
s[st],s[en] = s[en],s[st]
8-
st = st + 1
9-
en = en - 1
7+
en -= 1
108
print(s)
119

1210
######################################### For Strings Only #########################################
1311

1412
def reverseWord(s):
1513
j = -1
1614
k = ""
17-
for x in range(len(s)-1):
18-
k = k + s[j]
19-
j = j - 1
20-
k = k + s[-len(s)]
15+
for _ in range(len(s)-1):
16+
k += s[j]
17+
j -= 1
18+
k += s[-len(s)]
2119
print(k)
2220

2321
def reverse(s):

0 commit comments

Comments
 (0)