Skip to content

Instantly share code, notes, and snippets.

@akirap3
Last active December 18, 2020 11:39
Show Gist options
  • Save akirap3/1a853f43531d7b6cd61a079fa29ca1a0 to your computer and use it in GitHub Desktop.
Save akirap3/1a853f43531d7b6cd61a079fa29ca1a0 to your computer and use it in GitHub Desktop.
from os import strerror
srcname = input("Open a file: ")
dstname = input("Select a dst file: ")
try:
    src = open(srcname, 'rt')
    dst = open(dstname, 'wt')
except IOError as e:
    print("Cannot open the file: ",strerror(e.errno))
    
alpha = 'abcdefghijklmnopqrstuvwxyz'
dataDict ={}
for letter in alpha:
    dataDict.update({letter:0})
    
data = src.read().strip()
data2 = data.lower()

for ch in data2:
    if ch.isalpha():
        temp = {ch:dataDict[ch]+1}
        dataDict.update(temp)

# ==============================================================================

valueList = list(dataDict.values())
valueList.sort()
sortedValue = list(filter(lambda x: x > 0, valueList))

acendValue = []
for i in range(0,len(sortedValue)):
    acendValue.append(sortedValue.pop())

for item in acendValue:
    for letter in list(dataDict.keys()):
        if dataDict[letter] == item:
            print(letter, ' -> ', item)
            newData = letter + ' -> ' + str(item) + '\n'
            dst.write(newData)
            del dataDict[letter]
            
src.close()
dst.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment