You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 KiB

In [ ]:
# program to counter letter occurances in a string, or a word
# methods modified from 
# https://www.geeksforgeeks.org/python-program-to-find-occurrence-to-each-character-in-given-string/
In [16]:
# 法一
def set_counter(inp_str):
    # using set() + count() to get count
    # of each element in string
    out = {x : inp_str.count(x) for x in set(inp_str )}
    # printing result
    print ("{} is composed of these letters and occurances :\n  ".format(inp_str)+ str(out))

set_counter("tulip")
set_counter("pulpit")
tulip is composed of these letters and occurances :
  {'u': 1, 't': 1, 'i': 1, 'p': 1, 'l': 1}
pulpit is composed of these letters and occurances :
  {'u': 1, 't': 1, 'i': 1, 'p': 2, 'l': 1}
In [15]:
# 法二
# initializing string
def freq_dict(inp_str):
    # frequency dictionary
    freq = {}
    
    for ele in inp_str:
        if ele in freq:
            freq[ele] += 1
        else:
            freq[ele] = 1
    # printing result
    print ("{} composed of these letters and occurances : \n".format(inp_str)+ str(freq))

freq_dict("tulip")
freq_dict("pulpit")
tulip composed of these letters and occurances : 
{'t': 1, 'u': 1, 'l': 1, 'i': 1, 'p': 1}
pulpit composed of these letters and occurances : 
{'p': 2, 'u': 1, 'l': 1, 'i': 1, 't': 1}
In [17]:
# 法三
from collections import Counter

def collection_counter(inp_str):

    # using collections.Counter() to get
    # count of each element in string
    oup = Counter(inp_str)

    # printing result
    print ("{} is composed of these letters and occurances :\n ".format(inp_str)+ str(oup))

collection_counter("tulip")
collection_counter("pulpit")
    
tulip is composed of these letters and occurances :
 Counter({'t': 1, 'u': 1, 'l': 1, 'i': 1, 'p': 1})
pulpit is composed of these letters and occurances :
 Counter({'p': 2, 'u': 1, 'l': 1, 'i': 1, 't': 1})
In [ ]:
# this program can take two input words can see if they are anagrams? 
# isAnagram 
# hester mofet 
# are there anagrams in non-alphabetical word arrangements? yes, but in scalable units
In [26]:
# method to see if a pair of words are anagrams to each other
def set_counter(inp_str):
    return {x : inp_str.count(x) for x in set(inp_str )}

def areAnagrams(str1, str2):
    out1 = {}
    out2 = {}
    #apply set counter method to each of the words and store the output 
    out1 = set_counter(str1)
    out2 = set_counter(str2)
    # compare output 
    if out1 == out2:
        return True
    else: 
        return False 
In [36]:
# Python program to read
# image using matplotlib

# importing matplotlib modules
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

# Read Images
img = mpimg.imread('therestofme.jpg')

# Output Images
print("~~~~~~~~~~~~~~~~~~~~movie still from The Silence of Lamb to for a pair of anagrams ~~~~~~~~~~~~")
plt.imshow(img)
~~~~~~~~~~~~~~~~~~~~movie still from The Silence of Lamb to for a pair of anagrams ~~~~~~~~~~~~
Out[36]:
<matplotlib.image.AxesImage at 0x7f14448dbd00>
In [27]:
areAnagrams("hestermofet","therestofme")
Out[27]:
True
In [28]:
areAnagrams("hester mofet","the rest of me")
Out[28]:
False

in this case the empty strings are also counted.