Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit32e088e

Browse files
author
Eric Steen
committed
Bank
1 parentf0d6c07 commit32e088e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+843
-6
lines changed

‎python_algorithms/.DS_Store‎

2 KB
Binary file not shown.

‎python_algorithms/algorithms/search/bsearch.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ def binary_search_iter(data, target):
1515
high=len(data)-1
1616

1717
whilelow<=high:
18-
mid=(low+high)//2
18+
mid=low+(high-low)//2
1919
iftarget==data[mid]:
20-
returnTrue
20+
returnmid
2121
eliftarget<data[mid]:
2222
high=mid-1
2323
else:
@@ -28,11 +28,11 @@ def binary_search_iter(data, target):
2828
# Recursive Binary Search
2929
defbinary_search_recursive(data,target,low,high):
3030
iflow>high:
31-
returnFalse
31+
returnlow-1
3232
else:
3333
mid= (low+high)//2
3434
iftarget==data[mid]:
35-
returnTrue
35+
returnmid
3636
eliftarget<data[mid]:
3737
returnbinary_search_recursive(data,target,low,mid-1)
3838
else:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Uses python3
2+
importsys
3+
importrandom
4+
5+
defpartition3(a,l,r):
6+
#write your code here
7+
pass
8+
9+
defpartition2(a,l,r):
10+
x=a[l]
11+
j=l
12+
foriinrange(l+1,r+1):
13+
ifa[i]<=x:
14+
j+=1
15+
a[i],a[j]=a[j],a[i]
16+
a[l],a[j]=a[j],a[l]
17+
returnj
18+
19+
20+
defrandomized_quick_sort(a,l,r):
21+
ifl>=r:
22+
return
23+
k=random.randint(l,r)
24+
a[l],a[k]=a[k],a[l]
25+
#use partition3
26+
m=partition2(a,l,r)
27+
randomized_quick_sort(a,l,m-1);
28+
randomized_quick_sort(a,m+1,r);
29+
30+
31+
if__name__=='__main__':
32+
input=sys.stdin.readline()
33+
n,*a=list(map(int,input.split()))
34+
randomized_quick_sort(a,0,n-1)
35+
forxina:
36+
print(x,end=' ')

‎python_algorithms/etc/misc/bank.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
defdetails(fn):
2+
defwrapper(self,*args):
3+
fn(self,*args)
4+
print(repr(self))
5+
returnwrapper
6+
7+
classAccount:
8+
def__init__(self):
9+
self.balance=0
10+
11+
@details
12+
defwithdraw(self,amt):
13+
self.balance-=amt
14+
15+
@details
16+
defdeposit(self,amt):
17+
self.balance+=amt
18+
19+
classMinBalAccount(Account):
20+
21+
@details
22+
def__init__(self,min_balance):
23+
Account.__init__(self)
24+
self.min_balance=min_balance
25+
26+
27+
@details
28+
defwithdraw(self,amt):
29+
ifself.balance-amt<self.min_balance:
30+
print("Sorry, min balance must be maintained")
31+
else:
32+
Account.withdraw(self,amt)
33+
34+
def__repr__(self):
35+
returnf'| Bal:{self.balance} |\n| MinBal:{self.min_balance}|\n'
36+
37+
# account = MinBalAccount(100)
38+
# print(account.deposit(1000))
39+
# print(account.withdraw(50))
40+
41+
account=MinBalAccount(25)
42+
# print(repr(account))
43+
account.deposit(1000)
44+
# print(repr(account))
45+
46+
account.withdraw(50)
47+
# print(repr(account))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Uses python3
2+
importsys
3+
importmath
4+
5+
defbinary_search(arr,low,high,x):
6+
ifhigh>=low:
7+
8+
mid=math.floor(low+ ((high-low)//2))
9+
# If element is present at the middle itself
10+
ifarr[mid]==x:
11+
12+
returnmid
13+
14+
# If element is smaller than mid, then it can only
15+
# be present in left subarray
16+
elifarr[mid]>x:
17+
returnbinary_search(arr,low,mid-1,x)
18+
19+
# Else the element can only be present in right subarray
20+
else:
21+
returnbinary_search(arr,mid+1,high,x)
22+
else:
23+
return-1
24+
25+
26+
27+
28+
# def linear_search(a, x):
29+
# for i in range(len(a)):
30+
# if a[i] == x:
31+
# return i
32+
# return -1
33+
34+
if__name__=='__main__':
35+
input=sys.stdin.read()
36+
data=list(map(int,input.split()))
37+
n=data[0]
38+
m=data[n+1]
39+
a=data[1 :n+1]
40+
# print(data,n,m, a, len(a)-1)
41+
forxindata[n+2:]:
42+
# replace with the call to binary_search when implemented
43+
print(binary_search(a,0,len(a)-1,x),end=' ')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# python3
2+
importsys
3+
4+
5+
defcompute_min_refills(distance,tank,stops):
6+
# write your code here
7+
return-1
8+
9+
if__name__=='__main__':
10+
d,m,_,*stops=map(int,sys.stdin.readline().split())
11+
print(compute_min_refills(d,m,stops))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defget_change(m):
2+
denominations= [1,3,4]
3+
minCoins= [0]+ [math.inf]*m
4+
5+
foriinrange(1,m+1):
6+
forjindenominations:
7+
ifi>=j:
8+
coins=minCoins[i-j]+1
9+
ifcoins<minCoins[i]:
10+
minCoins[i]=coins
11+
returnminCoins[m]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Uses python3
2+
importsys
3+
4+
defget_change(m):
5+
# If n is 0 then there is 1
6+
# solution (do not include any coin)
7+
ifn==0:
8+
return1
9+
10+
# If n is less than 0 then no
11+
# solution exists
12+
ifn<0:
13+
return0
14+
15+
# If there are no coins and n
16+
# is greater than 0, then no
17+
# solution exist
18+
ifm<=0andn>=1:
19+
return0
20+
21+
# count is sum of solutions (i)
22+
# including S[m-1] (ii) excluding S[m-1]
23+
returncount(S,m-1,n)+count(S,m,n-S[m-1])
24+
25+
if__name__=='__main__':
26+
m=int(sys.stdin.readline())
27+
print(get_change(m))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp