mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-12 01:29:49 +00:00
print() is a function just like every other function (#1101)
* print() is a function just like every other function
This commit is contained in:
parent
6654e1ec7d
commit
89acf5d017
@ -20,13 +20,13 @@ def NewtonRaphson(func, a):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Find root of trigonometric function
|
# Find root of trigonometric function
|
||||||
# Find value of pi
|
# Find value of pi
|
||||||
print ('sin(x) = 0', NewtonRaphson('sin(x)', 2))
|
print('sin(x) = 0', NewtonRaphson('sin(x)', 2))
|
||||||
|
|
||||||
# Find root of polynomial
|
# Find root of polynomial
|
||||||
print ('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
|
print('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
|
||||||
|
|
||||||
# Find Square Root of 5
|
# Find Square Root of 5
|
||||||
print ('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
|
print('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
|
||||||
|
|
||||||
# Exponential Roots
|
# Exponential Roots
|
||||||
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
|
print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
|
||||||
|
@ -41,12 +41,12 @@ def main():
|
|||||||
print("4.Quit")
|
print("4.Quit")
|
||||||
choice = input("What would you like to do?: ")
|
choice = input("What would you like to do?: ")
|
||||||
if choice not in ['1', '2', '3', '4']:
|
if choice not in ['1', '2', '3', '4']:
|
||||||
print ("Invalid choice, please enter a valid choice")
|
print("Invalid choice, please enter a valid choice")
|
||||||
elif choice == '1':
|
elif choice == '1':
|
||||||
strng = input("Please enter the string to be encrypted: ")
|
strng = input("Please enter the string to be encrypted: ")
|
||||||
key = int(input("Please enter off-set between 1-94: "))
|
key = int(input("Please enter off-set between 1-94: "))
|
||||||
if key in range(1, 95):
|
if key in range(1, 95):
|
||||||
print (encrypt(strng.lower(), key))
|
print(encrypt(strng.lower(), key))
|
||||||
elif choice == '2':
|
elif choice == '2':
|
||||||
strng = input("Please enter the string to be decrypted: ")
|
strng = input("Please enter the string to be decrypted: ")
|
||||||
key = int(input("Please enter off-set between 1-94: "))
|
key = int(input("Please enter off-set between 1-94: "))
|
||||||
@ -57,7 +57,7 @@ def main():
|
|||||||
brute_force(strng)
|
brute_force(strng)
|
||||||
main()
|
main()
|
||||||
elif choice == '4':
|
elif choice == '4':
|
||||||
print ("Goodbye.")
|
print("Goodbye.")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,11 +71,11 @@ def decrypt(message):
|
|||||||
def main():
|
def main():
|
||||||
message = "Morse code here"
|
message = "Morse code here"
|
||||||
result = encrypt(message.upper())
|
result = encrypt(message.upper())
|
||||||
print (result)
|
print(result)
|
||||||
|
|
||||||
message = result
|
message = result
|
||||||
result = decrypt(message)
|
result = decrypt(message)
|
||||||
print (result)
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -83,4 +83,4 @@ if __name__ == '__main__':
|
|||||||
msg = "DEFEND THE EAST WALL OF THE CASTLE."
|
msg = "DEFEND THE EAST WALL OF THE CASTLE."
|
||||||
encrypted = encryptMessage(msg,"EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
|
encrypted = encryptMessage(msg,"EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
|
||||||
decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
|
decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
|
||||||
print ("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted))
|
print("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted))
|
||||||
|
@ -188,22 +188,22 @@ class XORCipher(object):
|
|||||||
# key = 67
|
# key = 67
|
||||||
|
|
||||||
# # test enrcypt
|
# # test enrcypt
|
||||||
# print crypt.encrypt("hallo welt",key)
|
# print(crypt.encrypt("hallo welt",key))
|
||||||
# # test decrypt
|
# # test decrypt
|
||||||
# print crypt.decrypt(crypt.encrypt("hallo welt",key), key)
|
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))
|
||||||
|
|
||||||
# # test encrypt_string
|
# # test encrypt_string
|
||||||
# print crypt.encrypt_string("hallo welt",key)
|
# print(crypt.encrypt_string("hallo welt",key))
|
||||||
|
|
||||||
# # test decrypt_string
|
# # test decrypt_string
|
||||||
# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)
|
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))
|
||||||
|
|
||||||
# if (crypt.encrypt_file("test.txt",key)):
|
# if (crypt.encrypt_file("test.txt",key)):
|
||||||
# print "encrypt successful"
|
# print("encrypt successful")
|
||||||
# else:
|
# else:
|
||||||
# print "encrypt unsuccessful"
|
# print("encrypt unsuccessful")
|
||||||
|
|
||||||
# if (crypt.decrypt_file("encrypt.out",key)):
|
# if (crypt.decrypt_file("encrypt.out",key)):
|
||||||
# print "decrypt successful"
|
# print("decrypt successful")
|
||||||
# else:
|
# else:
|
||||||
# print "decrypt unsuccessful"
|
# print("decrypt unsuccessful")
|
||||||
|
@ -21,9 +21,9 @@ if __name__ == '__main__':
|
|||||||
f = FenwickTree(100)
|
f = FenwickTree(100)
|
||||||
f.update(1,20)
|
f.update(1,20)
|
||||||
f.update(4,4)
|
f.update(4,4)
|
||||||
print (f.query(1))
|
print(f.query(1))
|
||||||
print (f.query(3))
|
print(f.query(3))
|
||||||
print (f.query(4))
|
print(f.query(4))
|
||||||
f.update(2,-5)
|
f.update(2,-5)
|
||||||
print (f.query(1))
|
print(f.query(1))
|
||||||
print (f.query(3))
|
print(f.query(3))
|
||||||
|
@ -74,7 +74,7 @@ class SegmentTree:
|
|||||||
showList = []
|
showList = []
|
||||||
for i in range(1,N+1):
|
for i in range(1,N+1):
|
||||||
showList += [self.query(1, 1, self.N, i, i)]
|
showList += [self.query(1, 1, self.N, i, i)]
|
||||||
print (showList)
|
print(showList)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -82,10 +82,10 @@ if __name__ == '__main__':
|
|||||||
N = 15
|
N = 15
|
||||||
segt = SegmentTree(N)
|
segt = SegmentTree(N)
|
||||||
segt.build(1,1,N,A)
|
segt.build(1,1,N,A)
|
||||||
print (segt.query(1,1,N,4,6))
|
print(segt.query(1,1,N,4,6))
|
||||||
print (segt.query(1,1,N,7,11))
|
print(segt.query(1,1,N,7,11))
|
||||||
print (segt.query(1,1,N,7,12))
|
print(segt.query(1,1,N,7,12))
|
||||||
segt.update(1,1,N,1,3,111)
|
segt.update(1,1,N,1,3,111)
|
||||||
print (segt.query(1,1,N,1,15))
|
print(segt.query(1,1,N,1,15))
|
||||||
segt.update(1,1,N,7,8,235)
|
segt.update(1,1,N,7,8,235)
|
||||||
segt.showData()
|
segt.showData()
|
||||||
|
@ -55,17 +55,17 @@ class SegmentTree:
|
|||||||
showList = []
|
showList = []
|
||||||
for i in range(1,N+1):
|
for i in range(1,N+1):
|
||||||
showList += [self.query(i, i)]
|
showList += [self.query(i, i)]
|
||||||
print (showList)
|
print(showList)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8]
|
A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8]
|
||||||
N = 15
|
N = 15
|
||||||
segt = SegmentTree(A)
|
segt = SegmentTree(A)
|
||||||
print (segt.query(4, 6))
|
print(segt.query(4, 6))
|
||||||
print (segt.query(7, 11))
|
print(segt.query(7, 11))
|
||||||
print (segt.query(7, 12))
|
print(segt.query(7, 12))
|
||||||
segt.update(1,3,111)
|
segt.update(1,3,111)
|
||||||
print (segt.query(1, 15))
|
print(segt.query(1, 15))
|
||||||
segt.update(7,8,235)
|
segt.update(7,8,235)
|
||||||
segt.showData()
|
segt.showData()
|
||||||
|
@ -13,28 +13,28 @@ de = collections.deque([1, 2, 3,])
|
|||||||
de.extend([4,5,6])
|
de.extend([4,5,6])
|
||||||
|
|
||||||
# printing modified deque
|
# printing modified deque
|
||||||
print ("The deque after extending deque at end is : ")
|
print("The deque after extending deque at end is : ")
|
||||||
print (de)
|
print(de)
|
||||||
|
|
||||||
# using extendleft() to add numbers to left end
|
# using extendleft() to add numbers to left end
|
||||||
# adds 7,8,9 to right end
|
# adds 7,8,9 to right end
|
||||||
de.extendleft([7,8,9])
|
de.extendleft([7,8,9])
|
||||||
|
|
||||||
# printing modified deque
|
# printing modified deque
|
||||||
print ("The deque after extending deque at beginning is : ")
|
print("The deque after extending deque at beginning is : ")
|
||||||
print (de)
|
print(de)
|
||||||
|
|
||||||
# using rotate() to rotate the deque
|
# using rotate() to rotate the deque
|
||||||
# rotates by 3 to left
|
# rotates by 3 to left
|
||||||
de.rotate(-3)
|
de.rotate(-3)
|
||||||
|
|
||||||
# printing modified deque
|
# printing modified deque
|
||||||
print ("The deque after rotating deque is : ")
|
print("The deque after rotating deque is : ")
|
||||||
print (de)
|
print(de)
|
||||||
|
|
||||||
# using reverse() to reverse the deque
|
# using reverse() to reverse the deque
|
||||||
de.reverse()
|
de.reverse()
|
||||||
|
|
||||||
# printing modified deque
|
# printing modified deque
|
||||||
print ("The deque after reversing deque is : ")
|
print("The deque after reversing deque is : ")
|
||||||
print (de)
|
print(de)
|
||||||
|
@ -38,7 +38,7 @@ def calculateSpan(price, S):
|
|||||||
# A utility function to print elements of array
|
# A utility function to print elements of array
|
||||||
def printArray(arr, n):
|
def printArray(arr, n):
|
||||||
for i in range(0,n):
|
for i in range(0,n):
|
||||||
print (arr[i],end =" ")
|
print(arr[i],end =" ")
|
||||||
|
|
||||||
|
|
||||||
# Driver program to test above function
|
# Driver program to test above function
|
||||||
|
@ -65,8 +65,8 @@ def logistic_reg(
|
|||||||
return weights
|
return weights
|
||||||
|
|
||||||
if iterations == max_iterations:
|
if iterations == max_iterations:
|
||||||
print ('Maximum iterations exceeded!')
|
print('Maximum iterations exceeded!')
|
||||||
print ('Minimal cost function J=', J)
|
print('Minimal cost function J=', J)
|
||||||
converged = True
|
converged = True
|
||||||
return theta
|
return theta
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
alpha = 0.1
|
alpha = 0.1
|
||||||
theta = logistic_reg(alpha,X,y,max_iterations=70000,num_steps=30000)
|
theta = logistic_reg(alpha,X,y,max_iterations=70000,num_steps=30000)
|
||||||
print (theta)
|
print(theta)
|
||||||
|
|
||||||
|
|
||||||
def predict_prob(X):
|
def predict_prob(X):
|
||||||
|
@ -12,7 +12,7 @@ def QuadraticEquation(a,b,c):
|
|||||||
if Delta >= 0:
|
if Delta >= 0:
|
||||||
Solution1 = (-b + math.sqrt(Delta))/(2*a)
|
Solution1 = (-b + math.sqrt(Delta))/(2*a)
|
||||||
Solution2 = (-b - math.sqrt(Delta))/(2*a)
|
Solution2 = (-b - math.sqrt(Delta))/(2*a)
|
||||||
print ("The equation solutions are: ", Solution1," and ", Solution2)
|
print("The equation solutions are: ", Solution1," and ", Solution2)
|
||||||
else:
|
else:
|
||||||
"""
|
"""
|
||||||
Treats cases of Complexes Solutions(i = imaginary unit)
|
Treats cases of Complexes Solutions(i = imaginary unit)
|
||||||
|
@ -17,6 +17,6 @@ def FYshuffle(LIST):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
integers = [0,1,2,3,4,5,6,7]
|
integers = [0,1,2,3,4,5,6,7]
|
||||||
strings = ['python', 'says', 'hello', '!']
|
strings = ['python', 'says', 'hello', '!']
|
||||||
print ('Fisher-Yates Shuffle:')
|
print('Fisher-Yates Shuffle:')
|
||||||
print ('List',integers, strings)
|
print('List',integers, strings)
|
||||||
print ('FY Shuffle',FYshuffle(integers), FYshuffle(strings))
|
print('FY Shuffle',FYshuffle(integers), FYshuffle(strings))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user