From 5585362fca3b53068f8f6182bf31c2bf85fc5ed1 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Wed, 6 Dec 2017 12:32:28 +0530 Subject: [PATCH 1/3] Code for Shell Sort in Python --- .../Sorting/Shell_Sort/shellsort.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Competitive Coding/Sorting/Shell_Sort/shellsort.py diff --git a/Competitive Coding/Sorting/Shell_Sort/shellsort.py b/Competitive Coding/Sorting/Shell_Sort/shellsort.py new file mode 100644 index 000000000..9c0011d6c --- /dev/null +++ b/Competitive Coding/Sorting/Shell_Sort/shellsort.py @@ -0,0 +1,29 @@ +""" +The algorithm uses a procedure called Shell Sort +to sort the lists. It breaks the list to smaller sublists +and each sublist is further sorted by insertion sort. +The sublists are made using a gap and elements at gap i +is often selected and forms a sublist. +""" + +def shellSort(test): + sublistcount = len(test)//2 #Finds gap to create sublists + while(sublistcount > 0): + for startposition in range(sublistcount): + gapInsertionSort(test,startposition,sublistcount) + print("After increments of size",sublistcount,"The list is",test) + sublistcount = sublistcount // 2 + +def gapInsertionSort(test,start,gap): + for i in range(start+gap,len(test),gap): + currentvalue = test[i] + position = i + while(position>=gap and test[position-gap]>currentvalue): + test[position]=test[position-gap] + position = position-gap + test[position]=currentvalue + +if __name__=="__main__": + test = [99,109,29,89,44,98,20,55,31] + shellSort(test) + print test From e29477960b58b84ea443a9e073284ea89b662e9b Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Wed, 6 Dec 2017 12:46:51 +0530 Subject: [PATCH 2/3] Caesarcipher in Python --- .../RSA Algortihm/Caesarcipher.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Security Algorithms/Cryptography/RSA Algortihm/Caesarcipher.py diff --git a/Security Algorithms/Cryptography/RSA Algortihm/Caesarcipher.py b/Security Algorithms/Cryptography/RSA Algortihm/Caesarcipher.py new file mode 100644 index 000000000..42919fb4a --- /dev/null +++ b/Security Algorithms/Cryptography/RSA Algortihm/Caesarcipher.py @@ -0,0 +1,34 @@ +""" +A Caesar Cipher function for both, encryption and decryption. +After giving the string that you want, encryption will take place by shifting +characters to the right and the decrypted string can be obtained by shifting +the encrypted string to the left. +The cipher is ready for Capital Letters only +""" + +def encrypt(message,key): + ascii_vals_enc=[ord(c) for c in message] #To get a list of ASCII Values of each character in string + for i in range(0,len(ascii_vals_enc)): + if(ascii_vals_enc[i]>=65 and ascii_vals_enc[i]<=90): + ascii_vals_enc[i] = 65 + (ascii_vals_enc[i] + key - 91)%26 #If the values in the list are greater than 65 and 90, we get it back b/w 65 and 90 + encrypt_string = ''.join(chr(i) for i in ascii_vals_enc) # To get the encrypted string using the list of manipulated ASCII vals + return encrypt_string + +def decrypt(message,key): + ascii_vals_dec=[ord(c) for c in message] + for i in range(0,len(ascii_vals_dec)): + if(ascii_vals_dec[i]>=65 and ascii_vals_dec[i]<=90): + ascii_vals_dec[i] = 90 - (90 - ascii_vals_dec[i] + key )%26 + decrypt_string = ''.join(chr(i) for i in ascii_vals_dec) # To get the decrypted string using the list of manipulated ASCII vals + return decrypt_string + +def caesar_cipher(): + message = raw_input("Enter text to be encrypted:") + encrypt_string = encrypt(message,key) + decrypt_string = decrypt(encrypt_string,key) + print encrypt_string + print decrypt_string + +if __name__=="__main__": + key = int(raw_input("By what value do you want to shift?")) + caesar_cipher() From b851bdcd29c84efaab511184e23f38eebe01581d Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Wed, 6 Dec 2017 12:51:18 +0530 Subject: [PATCH 3/3] Delete shellsort.py --- .../Sorting/Shell_Sort/shellsort.py | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 Competitive Coding/Sorting/Shell_Sort/shellsort.py diff --git a/Competitive Coding/Sorting/Shell_Sort/shellsort.py b/Competitive Coding/Sorting/Shell_Sort/shellsort.py deleted file mode 100644 index 9c0011d6c..000000000 --- a/Competitive Coding/Sorting/Shell_Sort/shellsort.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -The algorithm uses a procedure called Shell Sort -to sort the lists. It breaks the list to smaller sublists -and each sublist is further sorted by insertion sort. -The sublists are made using a gap and elements at gap i -is often selected and forms a sublist. -""" - -def shellSort(test): - sublistcount = len(test)//2 #Finds gap to create sublists - while(sublistcount > 0): - for startposition in range(sublistcount): - gapInsertionSort(test,startposition,sublistcount) - print("After increments of size",sublistcount,"The list is",test) - sublistcount = sublistcount // 2 - -def gapInsertionSort(test,start,gap): - for i in range(start+gap,len(test),gap): - currentvalue = test[i] - position = i - while(position>=gap and test[position-gap]>currentvalue): - test[position]=test[position-gap] - position = position-gap - test[position]=currentvalue - -if __name__=="__main__": - test = [99,109,29,89,44,98,20,55,31] - shellSort(test) - print test