Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Staircase builder | |
n=5 | |
startforhash=n-1 | |
str="" | |
for i in range(n): | |
for j in range(n): | |
if(j>=startforhash): | |
str+="#" | |
else: | |
str+=" " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def rand5(): | |
return random.randrange(0,6,1) | |
def rand7(): | |
diff=7-5 | |
x=rand5() | |
if x<=rand5(): | |
return x | |
else: | |
return x+diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Find product of all other numbers in the array for a given index except the number at that index.for ex. [1,2,3] = [6(3*2),3(1*3),2(1*2)] | |
def multiplier(arr): | |
result=[1 for i in arr] | |
#Brute force | |
for i in range(len(arr)): | |
for j in range(len(arr)): | |
if j==i: | |
continue | |
else: | |
result[i]=result[i]*arr[j] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#when you are not aware about number of arguments to a function | |
#args are passed in list | |
def multiplynumbers(*args): | |
count=1 | |
for i in args:count*=i | |
return count | |
print(multiplynumbers(1,2,3,4,5)) | |
#keyword arguments are passed as dict() | |
def addnumbers(**kwargs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Binary Search | |
def binarysearch(arr,searchterm): | |
start=0 #we need start and end to calculate pivot | |
end=len(arr)-1 | |
while(start<=end):#when to stop when start and end cross each other | |
pivot=int((start+end)/2) | |
if(arr[pivot])==searchterm: | |
return True | |
elif(arr[pivot]>searchterm): | |
end=pivot-1 #search left when pivot item is bigger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Recursive way of string reverse | |
def reversestring(str): | |
return str[len(str)-1::-1] | |
def recursivereversestr(str): | |
if len(str)<1: | |
return str | |
else: | |
return recursivereversestr(str[1:])+str[0] | |
print(reversestring("ADCA")) | |
print(recursivereversestr("charles")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Recursive way to convert integer to string | |
def int2string(num): | |
#Base case: | |
if num<10:##check if it is single digit number | |
return stringlookup(num) | |
else: | |
return int2string(int(num/(10**1)))+stringlookup(int(num%(10**1))) | |
def stringlookup(n): | |
d = {1:"1",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9",0:"0"} | |
return d[n] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Calculate the sum of list of numbers | |
def sumoflist(list,count): | |
if len(list)==1:#Escape Clause | |
print(count+list[0]) | |
return count+list[0] | |
else: | |
count+=list[0] | |
sumoflist(list[1:],count) | |
print(sumoflist([1,2,3,4,5],0)) | |
#Wrong way of using recursion above |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Test if strings are anagram provided their length is same. | |
def anagramtester(str1,str2): | |
#dictionary=dict(range(0,26)) | |
#CountDict=dict((el,0) for el in str1) | |
CountDictStr1=dict() | |
# i need dictionary to keep count | |
for i in range(len(str1)): | |
if str1[i] in CountDictStr1.keys(): | |
CountDictStr1[str1[i]]+=1 | |
else: |
NewerOlder