<jupyter_start><jupyter_text>Week4 Code1 2Sum using HashtableThis week I implemented a 2Sum algorithm using hashtable. This algorithm takes 1 hour to runCoursera link:https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/1Y8t1/hash-tables-implementation-details-part-ii<jupyter_code>from Model import *
from DataLoader import *
from Setting import *
import collections
import os
import numpy as np
import pickle
import heapq
import math
class Hash2Ssum(Model):
    def __init__(self):
        super().__init__()

        
    def preprocess(self):
        self.data = self.dataLoader.data
        self.data = [i[0] for i in self.data if i!=[]]
        self.data = list(set(self.data))
        self.codedict = {}
    
        
    def model(self):
        dataset = set(self.data)
        bllist = {}
        for i in range(low,high):
            if i%500 == 0:
                print(i)
            for item in dataset:
                if i-item in dataset:
                    if i-item!=item:
                        bllist[i] = True
                        break
        
        return sum(bllist.values())

processLine = lambda x : list(map(int,map(str.strip,x.split())))
arg = {'fileName':'./data/prob-2sum.txt','numLines':None ,  'processLine' : processLine  }

d = DataLoader(**arg)
m = Hash2Ssum()
s = Setting(d,m,False)
s.run()<jupyter_output>Loading data ...
Total 1000000 lines read
--------------------------------------------------
-10000
-9500
-9000
-8500
-8000
-7500
-7000
-6500
-6000
-5500
-5000
-4500
-4000
-3500
-3000
-2500
-2000
-1500
-1000
-500
0
500
1000
1500
2000
2500
3000
3500
4000
4500
5000
5500
6000
6500
7000
7500
8000
8500
9000
9500
10000
result:427
{'fraction_non_alphanumeric': 0.13620283018867924, 'fraction_numerical': 0.11025943396226415, 'mean_word_length': 2.1779026217228465, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '125492', 'license': 'MIT', 'n_tokens_mistral': 695, 'n_tokens_neox': 564, 'n_words': 153, 'path': 'course2/Week4Code .ipynb', 'repo_name': 'Cuilie/Code-for-algorithm-stanford-coursera', 'size': '1696'}