From 68b980b5ee224387f29f25c281bf81f715536991 Mon Sep 17 00:00:00 2001 From: beingadityak Date: Tue, 2 Oct 2018 11:07:24 +0530 Subject: [PATCH 1/3] Adding Anagram Search --- strings/anagram_search.py | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 strings/anagram_search.py diff --git a/strings/anagram_search.py b/strings/anagram_search.py new file mode 100644 index 0000000..3f4a297 --- /dev/null +++ b/strings/anagram_search.py @@ -0,0 +1,42 @@ +""" +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_anagram("notagram", "notaflam") == False \ No newline at end of file From d3d6613cf9c4762b0479d2c8ca59a8e0fe0768c4 Mon Sep 17 00:00:00 2001 From: beingadityak Date: Tue, 2 Oct 2018 14:27:05 +0530 Subject: [PATCH 2/3] Adding 4 String algorithms --- strings/anagram_search.py | 4 ++- strings/morse_code.py | 69 +++++++++++++++++++++++++++++++++++++ strings/password_checker.py | 29 ++++++++++++++++ strings/rabin_karp.py | 46 +++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 strings/morse_code.py create mode 100644 strings/password_checker.py create mode 100644 strings/rabin_karp.py diff --git a/strings/anagram_search.py b/strings/anagram_search.py index 3f4a297..fb902d2 100644 --- a/strings/anagram_search.py +++ b/strings/anagram_search.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + """ Pythonic Implementation of Anagram search """ @@ -39,4 +41,4 @@ def is_anagram(string1, string2): 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_anagram("notagram", "notaflam") == False \ No newline at end of file +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 From faa79880289f4611704331884f161156bab55473 Mon Sep 17 00:00:00 2001 From: beingadityak Date: Tue, 2 Oct 2018 15:15:05 +0530 Subject: [PATCH 3/3] Adding Bucket Sort Algorithm --- sorting/bucket_sort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 sorting/bucket_sort.py 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 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