diff --git a/sorting/bucket_sort.py b/sorting/bucket_sort.py new file mode 100644 index 0000000..bb53c14 --- /dev/null +++ b/sorting/bucket_sort.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +__author__ = "Aditya Krishnakumar" + + +def bucket_sort(A): + buckets = [[] for x in range(10)] + for i, x in enumerate(A): + buckets[int(x * len(buckets))].append(x) + out = [] + for buck in buckets: + out += isort(buck) + return out + + +def isort(A): + if len(A) <= 1: return A + i = 1 + while i < len(A): + k = A[i] + j = i - 1 + while j >= 0 and A[j] > k: + A[j + 1] = A[j] + A[j] = k + j -= 1 + i += 1 + return A \ No newline at end of file diff --git a/strings/anagram_search.py b/strings/anagram_search.py new file mode 100644 index 0000000..fb902d2 --- /dev/null +++ b/strings/anagram_search.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +""" +Pythonic Implementation of Anagram search +""" + +__author__ = "Aditya Krishnakumar" + +import collections + +# remove whitespaces +def remove_whitespace(string): + return ''.join(string.split()) + +""" + Checks if two strings are anagrams of each other, ignoring any whitespace. + + First remove any whitespace and lower all characters of both strings. + Then create dictionaries of the counts of every character in each string. + As well as keep a set of all characters used in both strings. + Check to ensure every unique character are used in both strings the + same number of times. +""" + +def is_anagram(string1, string2): + charCount1 = collections.Counter(remove_whitespace(string1.lower())) + charCount2 = collections.Counter(remove_whitespace(string2.lower())) + + allChars = set(charCount1.keys()) + allChars = allChars.union(charCount2.keys()) + + for c in allChars: + if (charCount1[c] != charCount2[c]): + return False + + return True + +# Dry runs + +assert is_anagram("anagram", "not a gram") == False +assert is_anagram("anagram", "na a marg") == True +assert is_anagram("William Shakespeare", "I am \t a weakish speller") == True +assert is_anagram("Madam Curie", "Radium came") == True +assert is_anagramg("notagram", "notaflam") == False \ No newline at end of file diff --git a/strings/morse_code.py b/strings/morse_code.py new file mode 100644 index 0000000..4a48462 --- /dev/null +++ b/strings/morse_code.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +""" +Pythonic Implementation of Morse Code encoding +""" + +__author__ = "Aditya Krishnakumar" + + +# The alphabet dictionary for morse codes +morseAlphabet = { + "A": ".-", + "B": "-...", + "C": "-.-.", + "D": "-..", + "E": ".", + "F": "..-.", + "G": "--.", + "H": "....", + "I": "..", + "J": ".---", + "K": "-.-", + "L": ".-..", + "M": "--", + "N": "-.", + "O": "---", + "P": ".--.", + "Q": "--.-", + "R": ".-.", + "S": "...", + "T": "-", + "U": "..-", + "V": "...-", + "W": ".--", + "X": "-..-", + "Y": "-.--", + "Z": "--..", + "1": ".----", + "2": "..---", + "3": "...--", + "4": "....-", + "5": ".....", + "6": "-....", + "7": "--...", + "8": "---..", + "9": "----.", + "0": "-----" +} + +# Lambda function for decoding the code to alphabet +inverseAlphabet = reduce(lambda a, b: dict(a.items() + b.items()), + [{ + morseAlphabet[k]: k + } for k in morseAlphabet.keys()], {}) + + +def encode(_text): + return ' '.join([morseAlphabet[_c.upper()] for _c in _text[:]]) + + +def decode(_text): + return ''.join([inverseAlphabet[_c] for _c in _text.split(' ')]) + +# Dry runner +def test(): + print decode(encode("TEST")) == "TEST" + + +if __name__ == "__main__": + test() \ No newline at end of file diff --git a/strings/password_checker.py b/strings/password_checker.py new file mode 100644 index 0000000..8743ea5 --- /dev/null +++ b/strings/password_checker.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import re, pyperclip + +password = pyperclip.paste() + +eightLettersRegex = re.compile(r'\S{8,}') +oneUppercaseRegex = re.compile(r'[A-Z]') +oneNumberRegex = re.compile(r'\d') + +check = { + eightLettersRegex: 'Your password must be 8 letters', + oneUppercaseRegex: 'Your password must have at least one uppercase Letter.', + oneNumberRegex: 'Your password must have at least one number.' +} + +print('Analyzing your password.') + +count = 1 +for regex, msg in check.items(): + mo = regex.search(password) + if mo == None: + print(msg) + break + if count == len(check): + print('Good! Your password is strong enough!') + count += 1 + +print('End.') \ No newline at end of file diff --git a/strings/rabin_karp.py b/strings/rabin_karp.py new file mode 100644 index 0000000..721e890 --- /dev/null +++ b/strings/rabin_karp.py @@ -0,0 +1,46 @@ +#!/usr/local/bin/env python3 + +# Rabin Karp Algorithm in python using hash values +# d is the number of characters in input alphabet +d = 2560 + + +def search(pat, txt, q): + M = len(pat) + N = len(txt) + i = 0 + j = 0 + + p = 0 + t = 0 + h = 1 + + for i in range(M - 1): + h = (h * d) % q + + for i in range(M): + p = (d * p + ord(pat[i])) % q + t = (d * t + ord(txt[i])) % q + + for i in range(N - M + 1): + if p == t: + for j in range(M): + if txt[i + j] != pat[j]: + break + + j += 1 + if j == M: + print("Pattern found at index " + str(i)) + + if i < N - M: + t = (d * (t - ord(txt[i]) * h) + ord(txt[i + M])) % q + if t < 0: + t = t + q + + +# Driver program to test the above function +txt = "ALL WORLDS IS A STAGE AND ALL OF US ARE A PART OF THE PLAY" +pat = "ALL" + +q = 101 # A prime number +search(pat, txt, q) \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy