import torch


def batchify(batch):
    """
    Gather a batch of individual examples into one batch, 
    which includes the question feature vec, question length, and labels 
    (feature vec and labels generated in create_feature_vecs_and_labels function)
    """
    #print("Here")
    question_len = list()
    label_list = list()
    for ex in batch:
        question_len.append(len(ex[0]))
        label_list.append(ex[1])
    
    '''
    Padding the labels - unequal length sequences for sequenial data like we have. 
    Since actual labels are 0/1 - we pad with -1, and will use this when 'masking' labels during loss and
    accuracy evaluation.
    '''
    target_labels = torch.nn.utils.rnn.pad_sequence([torch.tensor(y) for y in label_list], padding_value=-1).t()
    
    #dimension is dimension of every feature vector = n_guesses in this homework setting
    dim = batch[0][0].shape[1]
    
    #similar padding happens for the feature vectors, with vector of all zeros appended.
    x1 = torch.FloatTensor(len(question_len), max(question_len), dim).zero_()
    for i in range(len(question_len)):
        question_feature_vec = batch[i][0]
        vec = torch.FloatTensor(question_feature_vec)
        x1[i, :len(question_feature_vec)].copy_(vec)
    q_batch = {'feature_vec': x1, 'len': torch.FloatTensor(question_len), 'labels': target_labels}
    return q_batch