Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week2/day3 #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions POD/week1-arrays/day4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

for _ in range(int(input())):
n,s = map(int,input().split())
arr = list(map(int,input().split()))
#before start setting the indices of the subarray
i0,j0=-1,-1
for i in range(n):
#setting sum as the first element of the subarray
su = arr[i]
try:
#checking for an index j so that sum of arr[i:j] is the req sum
for j in range(i+1,n):
su += arr[j]
#if the sum is required sum set the indices of i and j
if su == s:
i0 = i
j0 = j
break

except:
pass
if j0!= -1:
print(i0+1,j0+1)
break
else:
print(-1)
19 changes: 19 additions & 0 deletions POD/week1-arrays/day5_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int,input().split()))
c = 0
#finding the bigger of the first and second element
p = max(l[:1])
for i in range(n):
#condition for record breaking:
#index 0: day 1 visitors is more than day 2
#day 2 to day n-1(day i): visitors is max of the subarray [0:i], and visitors on day i is more than i+1
#day n(last day) (index n-1):visitors is max of the full array
if (i == 0 or l[i] > p) and (i == n-1 or l[i]> l[i+1]):
c+=1
#updating the maximum no of visitors from the subarray[0:i]
if l[i]>p:
p = l[i]

print("Case #{}: {}".format(_+1,c))
23 changes: 23 additions & 0 deletions POD/week2/day3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution(object):
def minimumSwap(self, s1, s2):
x,y = 0,0
swaps = 0
for i in range(len(s1)):
#ignore all the x and y which are equal
if s1[i]!=s2[i]:
if s1[i]=="x":
x+=1
else:
y+=1

#if the total number of x and y we need to swap is odd, we cant swap to form 2 equak strings
if ((x+y)%2 ==1):
return -1
else:
#we can group each to be swapped into pairs of xx or yy
#then it will be xx(s1) and yy(s2) or vice versa which requires 1 swap
#so for total xx(s1) and yy(s2) pairs we can get it as x//2 + x//y
#the remaining will be odd number of x(s1) and y(s2) or vice versa which requires 1 swap
#so for total xy and yx it is x%2(reminder when x//2) + y%2(reminder when y//2)

return y//2+y%2+x//2+x%2