Hard exercises and solutions#

Exercise: Remove all outliers#

Remove all possible outliers from the given array, based on the cutoff (given cutoff value multiplied by standard deviation). Here, we want to remove all values that are more than three standard deviations from the mean. So,

abs(data_point - data_mean) > cutoff

Hint: Once you remove the first outlier(s), make sure that the resulting array does not have other outliers (e.g., use a loop).

Level: Medium.

import numpy as np
data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000])
cut_off = 3 * np.std(data)

# Your code here
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import numpy as np
      2 data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000])
      3 cut_off = 3 * np.std(data)

ModuleNotFoundError: No module named 'numpy'

Exercise: Binary to English translation#

You are provided with an English-to-binary code translation table and messages in binary code. You have to decode the given messages to find out what Chemistry Cat says.

Note that

  • the letters in the code are space delimited

  • the words are delimited by ‘/’

Level: Hard.

# we provide the translation table binary to english
binary_to_english = {
   "a" : "01100001", "b" : "01100010", "c" : "01100011", "d" : "01100100", "e" : "01100101", "f" : "01100110", "g" : "01100111", "h" : "01101000",
   "i" : "01101001", "j" : "01101010", "k" : "01101011", "l" : "01101100", "m" : "01101101", "n" : "01101110", "o" : "01101111", "p" : "01110000",
   "q" : "01110001", "r" : "01110010", "s" : "01110011", "t" : "01110100", "u" : "01110101", "v" : "01110110", "w" : "01110111", "x" : "01111000",
   "y" : "01111001", "z" : "01111010", "A" : "01000001", "B" : "01000010", "C" : "01000011", "D" : "01000100", "E" : "01000101", "F" : "01000110",
   "G" : "01000111", "H" : "01001000", "I" : "01001001", "J" : "01001010", "K" : "01001011", "L" : "01001100", "M" : "01001101", "N" : "01001110",
   "O" : "01001111", "P" : "01010000", "Q" : "01010001", "R" : "01010010", "S" : "01010011", "T" : "01010100", "U" : "01010101", "V" : "01010110",
   "W" : "01010111", "X" : "01011000", "Y" : "01011001", "Z" : "01011010", ",": "00101100", ".": "00101110", "'": "00100111", "?": "00111111"}

# to translate the sentences, you need to define a reversed table
# Your code here

# sentences to decode
sentences = ["01001001 / 01110111 01101111 01110101 01101100 01100100 / 01110100 01100101 01101100 01101100 / 01111001 01101111 01110101 / 01100001 / 01100011 01101000 01100101 01101101 01101001 01110011 01110100 01110010 01111001 / 01101010 01101111 01101011 01100101 / 00101100 / 01100010 01110101 01110100 / 01100001 01101100 01101100 / 01110100 01101000 01100101 / 01100111 01101111 01101111 01100100 / 01101111 01101110 01100101 01110011 / 01000001 01110010 01100111 01101111 01101110",
            "01001001 01100110 / 01111001 01101111 01110101 / 01100001 01110010 01100101 / 01101110 01101111 01110100 / 01110000 01100001 01110010 01110100 / 01101111 01100110 / 01110100 01101000 01100101 / 01110011 01101111 01101100 01110101 01110100 01101001 01101111 01101110 / 00101100 / 01111001 01101111 01110101 / 01100001 01110010 01100101 / 01110000 01100001 01110010 01110100 / 01101111 01100110 / 01110100 01101000 01100101 / 01110000 01110010 01100101 01100011 01101001 01110000 01101001 01110100 01100001 01110100 01100101",
            "01000001 / 01101110 01100101 01110101 01110100 01110010 01101111 01101110 / 01110111 01100001 01101100 01101011 01110011 / 01101001 01101110 01110100 01101111 / 01100001 / 01100010 01100001 01110010 / 01100001 01101110 01100100 / 01100001 01110011 01101011 01110011 / 00101100 / 00100111 / 01101000 01101111 01110111 / 01101101 01110101 01100011 01101000 / 01100110 01101111 01110010 / 01100001 / 01100010 01100101 01100101 01110010 / 00111111 / 00100111 / 01100001 01101110 01100100 / 01110100 01101000 01100101 / 01100010 01100001 01110010 01110100 01100101 01101110 01100100 01100101 01110010 / 01110011 01100001 01111001 01110011 / 00101100 / 00100111 / 01100110 01101111 01110010 / 01111001 01101111 01110101 / 00101100 / 01101110 01101111 / 01100011 01101000 01100001 01110010 01100111 01100101",
            "01001111 01101110 01100011 01100101 / 01001001 / 01110100 01101111 01101100 01100100 / 01100001 / 01100011 01101000 01100101 01101101 01101001 01110011 01110100 01110010 01111001 / 01101010 01101111 01101011 01100101 / 00101100 / 01110100 01101000 01100101 01110010 01100101 / 01110111 01100001 01110011 / 01101110 01101111 / 01110010 01100101 01100001 01100011 01110100 01101001 01101111 01101110"]

# decoding the sentences and printing the output
for sentence in sentences:
    words = sentence.split(' / ')
    for word in words:
        # Your code here
        # remove the next line to run your code
        pass 
    #print()

Possible solutions#

!!! Do not scroll down if you don’t want to see the solutions…you have been warned!.

Picture

string_a = 'a'
string_b = 'b'
integer_1 = 1
integer_2 = 2
string_4 = '4'
float_1 = 1.0
float_2_5 = 2.5

list_strings = [string_a, string_b, string_4]
list_numbers = [integer_1, integer_2, float_1, float_2_5]

def combinations(list_elements):
    list_combinations = []
    for element in list_elements:
        copy_list = list_elements.copy()
        copy_list.remove(element)
        for item in copy_list:
            list_combinations.append((element + item))
    return list_combinations

# print(combinations(list_strings))
# print(combinations(list_numbers))
import numpy as np
data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000])
cut_off = 3 * np.std(data)

def outliers(list_data):
    cut_off = 3 * np.std(list_data)
    no_outliers = [(abs(x - list_data.mean()) < cut_off) for x in list_data]
    list_data = list_data[no_outliers]
    while len(list_data) != len(no_outliers):
        cut_off = 3 * np.std(list_data)
        no_outliers = [(abs(x - list_data.mean()) < cut_off) for x in list_data]
        list_data = list_data[no_outliers]
    return list_data

outliers(data)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[4], line 1
----> 1 import numpy as np
      2 data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000])
      3 cut_off = 3 * np.std(data)

ModuleNotFoundError: No module named 'numpy'
english_to_binary = {v: k for k, v in binary_to_english.items()}

for sentence in sentences:
    words = sentence.split(" / ")
    temp = []
    for word in words:
        # your code here
        letters = word.split()
        list_lett = [english_to_binary[letter] for letter in letters]
        #print(''.join(list_lett))
        temp.append(''.join(list_lett))
    print(' '.join(temp))
I would tell you a chemistry joke , but all the good ones Argon
If you are not part of the solution , you are part of the precipitate
A neutron walks into a bar and asks , ' how much for a beer ? ' and the bartender says , ' for you , no charge
Once I told a chemistry joke , there was no reaction