Skip to content

Instantly share code, notes, and snippets.

@shello
Last active October 5, 2023 09:16
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shello/efa2655e8a7bce52f273 to your computer and use it in GitHub Desktop.
Save shello/efa2655e8a7bce52f273 to your computer and use it in GitHub Desktop.
Random Emoji! (for Python 3)
#!/usr/bin/env python3
from itertools import accumulate
from bisect import bisect
from random import randrange
from unicodedata import name as unicode_name
# Set the unicode version.
# Your system may not support Unicode 7.0 charecters just yet! So hipster.
UNICODE_VERSION = 6
# Sauce: http://www.unicode.org/charts/PDF/U1F300.pdf
EMOJI_RANGES_UNICODE = {
6: [
('\U0001F300', '\U0001F320'),
('\U0001F330', '\U0001F335'),
('\U0001F337', '\U0001F37C'),
('\U0001F380', '\U0001F393'),
('\U0001F3A0', '\U0001F3C4'),
('\U0001F3C6', '\U0001F3CA'),
('\U0001F3E0', '\U0001F3F0'),
('\U0001F400', '\U0001F43E'),
('\U0001F440', ),
('\U0001F442', '\U0001F4F7'),
('\U0001F4F9', '\U0001F4FC'),
('\U0001F500', '\U0001F53C'),
('\U0001F540', '\U0001F543'),
('\U0001F550', '\U0001F567'),
('\U0001F5FB', '\U0001F5FF')
],
7: [
('\U0001F300', '\U0001F32C'),
('\U0001F330', '\U0001F37D'),
('\U0001F380', '\U0001F3CE'),
('\U0001F3D4', '\U0001F3F7'),
('\U0001F400', '\U0001F4FE'),
('\U0001F500', '\U0001F54A'),
('\U0001F550', '\U0001F579'),
('\U0001F57B', '\U0001F5A3'),
('\U0001F5A5', '\U0001F5FF')
],
8: [
('\U0001F300', '\U0001F579'),
('\U0001F57B', '\U0001F5A3'),
('\U0001F5A5', '\U0001F5FF')
]
}
NO_NAME_ERROR = '(No name found for this codepoint)'
def random_emoji(unicode_version = 6):
if unicode_version in EMOJI_RANGES_UNICODE:
emoji_ranges = EMOJI_RANGES_UNICODE[unicode_version]
else:
emoji_ranges = EMOJI_RANGES_UNICODE[-1]
# Weighted distribution
count = [ord(r[-1]) - ord(r[0]) + 1 for r in emoji_ranges]
weight_distr = list(accumulate(count))
# Get one point in the multiple ranges
point = randrange(weight_distr[-1])
# Select the correct range
emoji_range_idx = bisect(weight_distr, point)
emoji_range = emoji_ranges[emoji_range_idx]
# Calculate the index in the selected range
point_in_range = point
if emoji_range_idx is not 0:
point_in_range = point - weight_distr[emoji_range_idx - 1]
# Emoji 😄
emoji = chr(ord(emoji_range[0]) + point_in_range)
emoji_name = unicode_name(emoji, NO_NAME_ERROR).capitalize()
emoji_codepoint = "U+{}".format(hex(ord(emoji))[2:].upper())
return (emoji, emoji_codepoint, emoji_name)
print(*random_emoji(UNICODE_VERSION), sep='\t')
@hubwoop
Copy link

hubwoop commented Aug 12, 2020

Nice 🎉!

Line 70 gives me the following warning: 'SyntaxWarning: "is not" with a literal. Did you mean "!="?' So i replaced is not with !=

@gvitalie
Copy link

gvitalie commented Nov 11, 2020

me@amadeus: ~_004

https://repl.it/@VitalieGhelbert/i-yi

from random import randint
from time import sleep


def o(a):

    print()  # una
    a = a.copy()
    for i in range(1, len(a)):
        pos = 0
        for j in range(0, i):
            state = int(a[-1] >= a[j])
            pos += state
            print(state, end=' ')
        a.insert(pos, a.pop())
        print("{:>3}".format(pos))  # unata
    print()  # una
    return a


a = [1]
while 1:  # len(a):
    N = randint(0, 255)
    n = randint(0, 32)
    sleep(randint(1, 7))
    a = [chr(abs(randint(-N, N))) for i in range(n)]
    print(a)
    print(o(a))
    print(a)
    print()
x15', 'S', 'T', '\r', 'k', '.', '\x0b', '\x8c', 'r', ',', 'N', '\x1b', 'm', '9', 'S', 'a', 'o', '\x01', '\x12', 'S', '\x91', 'D', ':', 'a', '\x02', 'd', 'D', '\r', 'R', '\x1a', '\x8d']

['\x0e', '\x01', 'j', '\x08', '?']

1   1
0 0   0
1 1 1   3
0 0 0 0   0

['\x01', '\x08', '\x0e', '?', 'j']
['\x0e', '\x01', 'j', '\x08', '?']

['W', '(', '4', 'a', 'u', 'f', '\x03', 'f', '1', 'V', '\x18', '/', '\x0f', '_', 'f', '\x08']

0   0
1 1   2
1 1 0   2
1 0 0 0   1
1 1 0 0 0   2
1 1 0 0 0 0   2
1 1 1 1 0 0 0   4
1 1 1 1 0 0 0 0   4
1 1 1 1 1 1 1 1 1   9
0 0 0 0 0 0 0 0 0 0   0
1 1 1 1 1 1 1 1 1 1 1  11
1 1 1 1 1 1 1 1 1 1 1 1  12
1 1 1 1 1 1 1 1 1 0 0 0 0   9
1 1 1 1 1 1 0 0 0 0 0 0 0 0   6
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0   4

['\x03', '\x08', '\x0f', '\x18', '(', '/', '1', '4', 'V', 'W', '_', 'a', 'f', 'f', 'f', 'u']
['W', '(', '4', 'a', 'u', 'f', '\x03', 'f', '1', 'V', '\x18', '/', '\x0f', '_', 'f', '\x08']

['I', 'î', '\\', '\x10', '\x1a', 'å', 'Ø', ',', '&', '4', '0', 'E', '§', 'Û', '\x8c', '\x98', '0', '\\', '\x8c', 'Ó', 'ñ', '¤']

1   1
1 1   2
1 1 0   2
1 0 0 0   1
1 0 0 0 0   1
0 0 0 0 0 0   0
1 1 1 1 0 0 0   4
1 1 1 1 0 0 0 0   4
1 1 1 1 1 1 1 1 0   8
1 1 1 1 1 1 1 0 0 0   7
1 0 0 0 0 0 0 0 0 0 0   1
1 0 0 0 0 0 0 0 0 0 0 0   1
1 1 0 0 0 0 0 0 0 0 0

Screenshot from 2020-11-11 21-58-12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment