
import random
from typing import TypeVar, List, Tuple
X = TypeVar('X')  # generic type to represent a data point


# split_data(data, prob):
#
#    Split "data" into 2 set [1], [2] 
#    Put a fraction p of "data " in set [1] and 1-p in set [2]
#

def split_data(data: List[X], prob: float) -> Tuple[List[X], List[X]]:
    """Split data into fractions [prob, 1 - prob]"""
    data = data[:]                    # Make a copy
    random.shuffle(data)              # because shuffle modifies its input.
    cut = int(len(data) * prob)       # Use prob to find a cutoff
    return data[:cut], data[cut:]     # and split the shuffled list there.

# def split_data(data, prob):
#     """split data into fractions [prob, 1 - prob]"""
#     results = [], []
#     for row in data:
#         results[0 if random.random() < prob else 1].append(row)
# #               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# #               This is an index(!!!): 0 with probab "prob"
#     return results

data = [n for n in range(100)]

print(data, "\n", len(data), "\n")

o1,o2 = split_data(data, 0.1)
print(o1, "\n", len(o1), "\n")
print(o2, "\n", len(o2), "\n")





