-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathemojicipher.py
60 lines (50 loc) · 1.88 KB
/
emojicipher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#/usr/bin/env python
# -*- coding: utf-8 -*-
# Emoji cipher
from sys import argv, exit
# Function that returns the index of the word in all possible combinations
# of the given alphabet
def intify(string, alphabet):
number = sum([len(alphabet)**i for i in range(len(string))])
combinations = len(alphabet)**(len(string))
for letter in string:
combinations /= len(alphabet)
number += alphabet.index(letter) * combinations
return number
# Function that turns the index into a word using the alphabet
def wordify(number, alphabet):
out = []
while 1:
if number <= len(alphabet):
out.insert(0, alphabet[number-1])
break
loc = number % len(alphabet)
loc += len(alphabet) * (loc == 0)
out.insert(0, alphabet[loc-1])
number = (number - loc) / len(alphabet)
return out
# Functions that use the above functions to encipher/decipher words
def encipher(string):
integer = intify(string, alphabet1)
return wordify(integer, alphabet2)
def decipher(string):
integer = intify(string, alphabet2)
return wordify(integer, alphabet1)
# The first alphabet is just the latin alphabet with a space
alphabet1 = list("abcdefghijklmnopqrstuvwxyz ")
# Getting the second alphabet (1791 emojis) from an external file
with open("emojilist") as f:
emojilist = f.read().splitlines()
alphabet2 = [emoji.split("|")[0] for emoji in emojilist]
#meanings = [emoji.split("|")[1] for emoji in emojilist]
# Getting the input from the args
string1 = " ".join(argv[1:]).lower()
# Checking whether the inputs are valid and which alphabet the input is using
if not any(letter not in alphabet1 for letter in string1):
print " ".join(encipher(string1))
else:
string1 = string1.split()
if not any(letter not in alphabet2 for letter in string1):
print "".join(decipher(string1))
else:
print "Invalid input."