Advanced Usefulib Idea - String To Numpy
Opened this issue ยท 3 comments
Describe your idea
If you are new to contributing to usefulib, you can try this (maybe slightly advanced but a good challange ๐) idea! Create a translator that uses np.array
, np.zeros
and np.ones
to make a binary system for encrypting messages! Each row is a character and columns translate to ASCII letters. Here is a basic table to understand:
Translating 'hello!'
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 !
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
There is a one in the first row at H, indicating the first character, and so on with the rest! While this is just a bit of fun and not really useful ๐ , it's something great to get started with!
Good luck :)
Hey @hamdivazim , can I try this even though I have already contributed?
Hey @hamdivazim , can I try this even though I have already contributed?
@MKM12345 sure but maybe leave some of it for others to complete. If you want to paste starter code here that would be great. Thanks!
Starter Code
Here is some starter code from @MKM12345 (#18):
import numpy as np
def encrypt(message):
# Define the binary system as a numpy array
binary_system = np.array([
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
])
# Convert message to binary representation using the binary system
binary_message = []
for char in message:
binary_message.append(binary_system[ord(char)])
# Flatten binary message to a single list
binary_message = [bit for sublist in binary_message for bit in sublist]
# Add more.
return encrypted_message