From d716765266cd607923a7f684990b2efb44e198c4 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Wed, 3 Apr 2024 10:40:14 -0400 Subject: [PATCH] remove some code --- labs/12_hash_tables/a.out | Bin 25584 -> 0 bytes .../happy_number_separate_chaining_sol.cpp | 95 ------- labs/12_hash_tables/happy_number_set_sol.cpp | 50 ---- labs/12_hash_tables/test/ds_hashset.h | 260 ------------------ labs/12_hash_tables/test/test_ds_hashset.cpp | 155 ----------- labs/12_hash_tables/test2/a.out | Bin 16840 -> 0 bytes .../test2/happy_number_separate_chaining.cpp | 95 ------- labs/12_hash_tables/test3/a.out | Bin 47760 -> 0 bytes .../test3/happy_number_set_sol.cpp | 50 ---- labs/12_hash_tables/test4/a.out | Bin 25584 -> 0 bytes .../test_longest_consecutive_sequence.cpp | 117 -------- ...test_longest_consecutive_sequence_sol1.cpp | 45 --- ...test_longest_consecutive_sequence_sol2.cpp | 119 -------- .../test_longest_consecutive_sequence_sol.cpp | 120 -------- 14 files changed, 1106 deletions(-) delete mode 100755 labs/12_hash_tables/a.out delete mode 100644 labs/12_hash_tables/happy_number_separate_chaining_sol.cpp delete mode 100644 labs/12_hash_tables/happy_number_set_sol.cpp delete mode 100644 labs/12_hash_tables/test/ds_hashset.h delete mode 100644 labs/12_hash_tables/test/test_ds_hashset.cpp delete mode 100755 labs/12_hash_tables/test2/a.out delete mode 100644 labs/12_hash_tables/test2/happy_number_separate_chaining.cpp delete mode 100755 labs/12_hash_tables/test3/a.out delete mode 100644 labs/12_hash_tables/test3/happy_number_set_sol.cpp delete mode 100755 labs/12_hash_tables/test4/a.out delete mode 100644 labs/12_hash_tables/test4/test_longest_consecutive_sequence.cpp delete mode 100644 labs/12_hash_tables/test4/test_longest_consecutive_sequence_sol1.cpp delete mode 100644 labs/12_hash_tables/test4/test_longest_consecutive_sequence_sol2.cpp delete mode 100644 labs/12_hash_tables/test_longest_consecutive_sequence_sol.cpp diff --git a/labs/12_hash_tables/a.out b/labs/12_hash_tables/a.out deleted file mode 100755 index de6db892b07240ef4221a593fffa61f8fc16d0ec..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 25584 zcmeHQ4|G)3nSYZ%5EMzUpcE{OiWa1dnGi5oS%PHnMgAD_$0CByVP-<6CNpvJ27?8K z4YW>&?BbtRUF})h+IHJ|R@-6^+e!_HM7OjXs#~dANp01MV4Fs((W3Nv?|Js#s3J zGL}`two+shY+e)gw=+`M*iYk1S5%8Ofr^IOA_c<21iszvh`ReCz2o(f?C+2zKE z#?%#Snlz~CAso9=E7&3HN*~dtFrE0%+PvtTZRgFLzu~8+`d&Nj=O3(f)z2gzvYTXx zhYIOq6Gfe_r{Y6A(jT{rYr;|K3;n!p>OB*e%i74l&jWSlNVK*k4}L`+{P$r%Ee-HRv z{=W~3&T}@3`ac3cS(~9%*GXa-HAuxUHn&A1Mu+K-nTDYmwT-KdU?>*a9PThfvBuTQ zTceRsqkmItNYZjs);E|H<%VIl#G*GDt)a+fv&9I-V$oRL%7A9954P5AX#-r-5N+-7 zg>ltjR)!+M*4jXW>GcL${4v9f`NL*MZNTTN2X4V8e@8fAL_5q_$lq4mXf&1^zJ^6G z5K7FdaJ0h!N2szk5;kkRzAcvWx)t>e(FVgO+VEbYY}(`x8vfSSXaJnX=FU!|JrwJR zM*OW|bE~nXTyR%ZWpT%1{;ftNr20cmG?=X&wSS$(fkxQ7(2)I)+$4MNFnqZ*9KHxd z8>R0~M_70Z_!3q0l86{~75Uv5-*Dy^#p znac{#LT&lFb;~ZVHN2h$woA`q$-ZtKlvH{uvZ^%93dDa!$d`rUY8?KK$EOf0Q2{d8 z1bkegF0&og{)jQE=26{+Td}fF&?ay)bU`xB^lIpz5)PlCLE~i5NoL>eUq*T?(q?cy zK0Q2vvgw7Ee3AAt%Y$8e=HVNV%1g8g=HGhkVYD|+TcYa2cjKy9yOQhX+&i#@7HHR? zPDQQnYF+K&$vKDDZ-qZKP4bsJ@H?2lgy*N?SM{f^l5&*uRjwyYpA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faz_kqfn$hzW_wUP3rkf*V)T6Ek)EkR|=Gzs?#Y-gj4OSbA_i>OAgX+FV*%6uFvQ8 z%N=+npD%t&EkPA~y9&Kp*4in#qtN33dd8>hSN)m*oU$F4R@uK&UXE8UEHFj2s~Bl| S=8}JWz2u - -// this table can have at most 1024 keys -#define TABLE_SIZE 1024 - -class Node { -public: - int number; - Node* next; -}; - -// search the hash table and see if we can find this num. -bool identify(int num, Node** table){ - int key = abs(num%TABLE_SIZE); - // search num in table[key]; - Node* node = table[key]; - while(node!=NULL){ - if(node->number == num){ - return true; - } - node = node->next; - } - // if not found, return false; - return false; -} - -// add num into the hash table -void add(int num, Node** table){ - int key = abs(num%TABLE_SIZE); - Node* node = new Node; - // insert num and index into table[key] - // if this is the first node - if(table[key]==NULL){ - node->number = num; - node->next = NULL; - table[key] = node; - }else{ - // if this is not the first node - node->number = num; - node->next = table[key]; - table[key] = node; - } -} - -int replace(int n){ - int digit; - int result=0; - while(n>0){ - digit = (n%10); - result += digit * digit; - n = n/10; - } - return result; -} - -bool isHappy(int n) { - int newN = n; - Node* hash_table[TABLE_SIZE]; - for(int i=0;i -#include - -int replace(int n){ - int digit; - int result=0; - while(n>0){ - digit = (n%10); - result += digit * digit; - n = n/10; - } - return result; -} - -bool isHappy(int n) { - int newN = n; - std::unordered_set set1; - while(1){ - newN = replace(newN); - if(newN==1){ - return true; - }else{ - // if we can find it, this is going to be an infinite loop - if(set1.find(newN)!=set1.end()){ - return false; - } - // can't find it, insert it in the set first - set1.insert(newN); - } - } -} - -int main() { - // Test cases - // 2, 4, 5, 6, 17, 18, 20 are not happy numbers. - // 1, 7, 10, 13, 19, 23, 28, 68 are not happy numbers. - - int testCases[] = {2,4,5,6,17,18,20,1,7,10,13,19,23,28,68}; - - for (int n : testCases) { - if (isHappy(n)) { - std::cout << n << " is a happy number." << std::endl; - } else { - std::cout << n << " is not a happy number." << std::endl; - } - } - - return 0; -} - diff --git a/labs/12_hash_tables/test/ds_hashset.h b/labs/12_hash_tables/test/ds_hashset.h deleted file mode 100644 index fe25a38..0000000 --- a/labs/12_hash_tables/test/ds_hashset.h +++ /dev/null @@ -1,260 +0,0 @@ -#ifndef ds_hashset_h_ -#define ds_hashset_h_ -// The set class as a hash table instead of a binary search tree. The -// primary external difference between ds_set and ds_hashset is that -// the iterators do not step through the hashset in any meaningful -// order. It is just the order imposed by the hash function. -#include -#include -#include -#include -#include - -// The ds_hashset is templated over both the type of key and the type -// of the hash function, a function object. -template < class KeyType, class HashFunc > -class ds_hashset { -private: - typedef typename std::list::iterator hash_list_itr; - -public: - // ================================================================= - // THE ITERATOR CLASS - // Defined as a nested class and thus is not separately templated. - - class iterator { - public: - friend class ds_hashset; // allows access to private variables - private: - - // ITERATOR REPRESENTATION - ds_hashset* m_hs; - int m_index; // current index in the hash table - hash_list_itr m_list_itr; // current iterator at the current index - - private: - // private constructors for use by the ds_hashset only - iterator(ds_hashset * hs) : m_hs(hs), m_index(-1) {} - iterator(ds_hashset* hs, int index, hash_list_itr loc) - : m_hs(hs), m_index(index), m_list_itr(loc) {} - - public: - // Ordinary constructors & assignment operator - iterator() : m_hs(0), m_index(-1) {} - iterator(iterator const& itr) - : m_hs(itr.m_hs), m_index(itr.m_index), m_list_itr(itr.m_list_itr) {} - iterator& operator=(const iterator& old) { - m_hs = old.m_hs; - m_index = old.m_index; - m_list_itr = old.m_list_itr; - return *this; - } - - // The dereference operator need only worry about the current - // list iterator, and does not need to check the current index. - const KeyType& operator*() const { return *m_list_itr; } - - // The comparison operators must account for the list iterators - // being unassigned at the end. - friend bool operator== (const iterator& lft, const iterator& rgt) - { return lft.m_hs == rgt.m_hs && lft.m_index == rgt.m_index && - (lft.m_index == -1 || lft.m_list_itr == rgt.m_list_itr); } - friend bool operator!= (const iterator& lft, const iterator& rgt) - { return lft.m_hs != rgt.m_hs || lft.m_index != rgt.m_index || - (lft.m_index != -1 && lft.m_list_itr != rgt.m_list_itr); } - // increment and decrement - iterator& operator++() { - this->next(); - return *this; - } - iterator operator++(int) { - iterator temp(*this); - this->next(); - return temp; - } - iterator & operator--() { - this->prev(); - return *this; - } - iterator operator--(int) { - iterator temp(*this); - this->prev(); - return temp; - } - - private: - // Find the next entry in the table - void next() { - ++ m_list_itr; // next item in the list - - // If we are at the end of this list - if (m_list_itr == m_hs->m_table[m_index].end()) { - // Find the next non-empty list in the table - for (++m_index; - m_index < int(m_hs->m_table.size()) && m_hs->m_table[m_index].empty(); - ++m_index) {} - - // If one is found, assign the m_list_itr to the start - if (m_index != int(m_hs->m_table.size())) - m_list_itr = m_hs->m_table[m_index].begin(); - - // Otherwise, we are at the end - else - m_index = -1; - } - } - - // Find the previous entry in the table - void prev() { - // If we aren't at the start of the current list, just decrement - // the list iterator - if (m_list_itr != m_hs->m_table[m_index].begin()) - m_list_itr -- ; - - else { - // Otherwise, back down the table until the previous - // non-empty list in the table is found - for (--m_index; m_index >= 0 && m_hs->m_table[m_index].empty(); --m_index) {} - - // Go to the last entry in the list. - m_list_itr = m_hs->m_table[m_index].begin(); - hash_list_itr p = m_list_itr; ++p; - for (; p != m_hs->m_table[m_index].end(); ++p, ++m_list_itr) {} - } - } - }; - // end of ITERATOR CLASS - // ================================================================= -private: - // ================================================================= - // HASH SET REPRESENTATION - std::vector< std::list > m_table; // actual table - HashFunc m_hash; // hash function - unsigned int m_size; // number of keys - -public: - // ================================================================= - // HASH SET IMPLEMENTATION - - // Constructor for the table accepts the size of the table. Default - // constructor for the hash function object is implicitly used. - ds_hashset(unsigned int init_size = 10) : m_table(init_size), m_size(0) {} - - // Copy constructor just uses the member function copy constructors. - ds_hashset(const ds_hashset& old) - : m_table(old.m_table), m_size(old.m_size) {} - - ~ds_hashset() {} - - ds_hashset& operator=(const ds_hashset& old) { - if (&old != this) { - this->m_table = old.m_table; - this->m_size = old.m_size; - this->m_hash = old.m_hash; - } - return *this; - } - - unsigned int size() const { return m_size; } - - - // Insert the key if it is not already there. - std::pair< iterator, bool > insert(KeyType const& key) { - const float LOAD_FRACTION_FOR_RESIZE = 1.25; - - if (m_size >= LOAD_FRACTION_FOR_RESIZE * m_table.size()) - this->resize_table(2*m_table.size()+1); - - // Implement this function for Lab 11, Checkpoint 1 - - - - - - - - - - - - - - } - - // Find the key, using hash function, indexing and list find - iterator find(const KeyType& key) { - unsigned int hash_value = m_hash(key); - unsigned int index = hash_value % m_table.size(); - hash_list_itr p = std::find(m_table[index].begin(), - m_table[index].end(), key); - if (p == m_table[index].end()) - return this->end(); - else - return iterator(this, index, p); - } - // Erase the key - int erase(const KeyType& key) { - // Find the key and use the erase iterator function. - iterator p = find(key); - if (p == end()) - return 0; - else { - erase(p); - return 1; - } - } - - // Erase at the iterator - void erase(iterator p) { - m_table[ p.m_index ].erase(p.m_list_itr); - } - - // Find the first entry in the table and create an associated iterator - iterator begin() { - - // Implement this function for Lab 11, Checkpoint 2, Part 1 - - - - - - - - } - - // Create an end iterator. - iterator end() { - iterator p(this); - p.m_index = -1; - return p; - } - - // A public print utility. - void print(std::ostream & ostr) { - for (unsigned int i=0; i -#include -#include -#include - -#include "ds_hashset.h" - - -// Wrapping a class around a function turns a function into a functor -// (We'll talk about this more in Lecture 21. You can just ignore -// this wrapper part for now.) -class hash_string_obj { -public: - - // ---------------------------------------------------------- - // EXPERIMENT WITH THE HASH FUNCTION FOR CHECKPOINT 1, PART 2 - - unsigned int operator() ( const std::string& key ) const { - // This implementation comes from - // http://www.partow.net/programming/hashfunctions/ - // - // This is a general-purpose, very good hash function for strings. - unsigned int hash = 1315423911; - for(unsigned int i = 0; i < key.length(); i++) - hash ^= ((hash << 5) + key[i] + (hash >> 2)); - return hash; - } - -}; - - -typedef ds_hashset ds_hashset_type; - - -int main() { - - // --------------------------------- - // CODE TO TEST CHECKPOINT 1, PART 1 - ds_hashset_type a; - ds_hashset_type set1; - std::pair< ds_hashset_type::iterator, bool > insert_result; - - std::string to_insert = std::string("hello"); - insert_result = set1.insert( to_insert ); - assert( insert_result.second ); - - insert_result = set1.insert( std::string("good-bye") ); - assert( insert_result.second ); - - insert_result = set1.insert( std::string("friend") ); - assert( insert_result.second ); - - insert_result = set1.insert( std::string("abc") ); - assert( insert_result.second ); - - insert_result = set1.insert( std::string("puppy") ); - assert( insert_result.second ); - - insert_result = set1.insert( std::string("zebra") ); - assert( insert_result.second ); - - insert_result = set1.insert( std::string("daddy") ); - assert( insert_result.second ); - - insert_result = set1.insert( std::string("puppy") ); - assert( !insert_result.second && * insert_result.first == std::string("puppy") ); - - std::cout << "The set size is " << set1.size() << '\n' - << "Here is the table: \n"; - set1.print( std::cout ); - - ds_hashset_type::iterator p; - p = set1.find( "foo" ); - if ( p == set1.end() ) - std::cout << "\"foo\" is not in the set\n"; - else - std::cout << "\"foo\" is in the set\n" - << "The iterator points to " << *p << std::endl; - - p = set1.find("puppy"); - if ( p == set1.end() ) - std::cout << "\"puppy\" is not in the set\n"; - else - std::cout << "\"puppy\" is in the set\n" - << "The iterator points to " << *p << std::endl; - - p = set1.find("daddy"); - if ( p == set1.end() ) - std::cout << "\"daddy\" is not in the set\n"; - else - std::cout << "\"daddy\" is in the set\n" - << "The iterator points to " << *p << std::endl; - - - // --------------------------------- - // CODE TO TEST CHECKPOINT 2, PART 1 - /* - p = set1.begin(); - std::cout << "\nHere is the result of iterating: \n"; - for ( p = set1.begin(); p != set1.end(); ++p ) - std::cout << *p << '\n'; - */ - - - // --------------------------------- - // CODE TO TEST CHECKPOINT 2, PART 2 - /* - ds_hashset_type set2( set1 ); - std::cout << "set1.size() = " << set1.size() << ", set2.size() = " << set2.size() << std::endl; - - // Now add more stuff to set2. This should trigger a resize given the default settings. - insert_result = set2.insert( std::string("ardvark") ); - assert( insert_result.second ); - insert_result = set2.insert( std::string("baseball") ); - assert( insert_result.second ); - insert_result = set2.insert( std::string("football") ); - assert( insert_result.second ); - insert_result = set2.insert( std::string("gymnastics") ); - assert( insert_result.second ); - insert_result = set2.insert( std::string("dance") ); - assert( insert_result.second ); - insert_result = set2.insert( std::string("swimming") ); - assert( insert_result.second ); - insert_result = set2.insert( std::string("track") ); - assert( insert_result.second ); - - std::cout << "\nAfter seven more inserts:\n" - << "set1.size() = " << set1.size() << ", set2.size() = " << set2.size() << "\n" - << "\nThe contents of set2:" << std::endl; - set2.print(std::cout); - std::cout << "The results of iterating:\n"; - for ( p = set2.begin(); p != set2.end(); ++p ) - std::cout << *p << '\n'; - */ - - // --------------- - // OTHER TEST CODE - /* - // Now test erase - int num = set2.erase( std::string("hello") ); - std::cout << "Tried erase \"hello\" and got num (should be 1) = " << num << std::endl; - num = set2.erase( std::string("abc") ); - std::cout << "Tried erase \"abc\" and got num (should be 1) = " << num << std::endl; - num = set2.erase( std::string("hello") ); - std::cout << "Tried erase \"hello\" and got num (should be 0) = " << num << std::endl; - num = set2.erase( std::string("football") ); - std::cout << "Tried erase \"football\" and got num (should be 1) = " << num << std::endl; - num = set2.erase( std::string("friend") ); - std::cout << "Tried erase \"friend\" and got num (should be 1) = " << num - << "\nHere are the final contents of set2:" << std::endl; - set2.print(std::cout); - */ - - return 0; -} diff --git a/labs/12_hash_tables/test2/a.out b/labs/12_hash_tables/test2/a.out deleted file mode 100755 index f7961d21f01a1f17cd0ba6713f6804e6cbb90961..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16840 zcmeHOe{dAl9e|8j` zWpmhBz)#|skb4CIsYpi(GHJ2I3xT3tIcBoZiv-MwQVo%!U9RMvA|ymvnc&f`2(yxR z(j+zn5>b*{<+-^S0+UH6$@USo+o8+N3xxjW<(YJkv>TW0Cp(1Eu0z^&NIRlT+7YFA zVodN~BmFsPCCD%lb=tK_J14CZW{fD7fvM>OBXB0=HAuTkH+RFFG$3F`l*+pq>|_^7 zjV~tD&5g3WT>Y?1#*--aH>QOly=7_j!cbsANDn7E7If4sU9hy;6OVWnaXHq@4SzhR zR=VUGkj&ndwho^xe-ML>;U*+Mo_5 z^haHeJm#muA9X}L9^!e#FbxEM-#(+aq*idJ9~%HV?{t`53J$a3OLE}P=I}F;1Ai(9 zzA}gZu^jr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LWiwjdO$)eR@N`T zp@6Hm`QmNN6X*;>5rP^qA-OXci^GML!Dt|h1w%eGkhy5cU>@Ep9>{vOM1aLiJm_a0 z*bDv<1K$al(ff7K8=mp(tOw4$g!*^dj@Xc+?ITa+VSAj3AtU zz^xFTT|~n19TQQSe+X3QQXc80@W)4UvagZ$L@QCrmgqU3prw$p+dKC$;`bqYqUR67 z@cGbgPtT`B-J~d~)Bfv#!{@6A)t}~7M6Zx`G_P{j-v@G>Q<41<*?yv37+gqLN6eBv z&A)WO(1h%19!HesZJ@%FbVn@vCggBVMD{cfBI=f!RDaSVdN0Uvjzv7pvxsh%_7r2P zzl2#2WH6>=PxC&aZdqX}pX{mqk4pQM(jms^uXGDdq5;8LN2mSU zAhFx`x`isy5DIOH&L4t4fXr0*%gff9AJV>KiZkhxJ<(43upqr$_Md|Q z&s{2?=5e%-oC1T_A13lg_4*lL*hI@-zPIUEh}w)jAc%D&d!lE7vE80|rK;>WDu*19 z9r5^$8!;;1>lKnRXU#eSDvY&4uB17)%l+GQys#iK3lqbx^AjEfF nRDT+O@Vy4r-EQyZJ5FH*u|r7ZlJ2w|_8-;=jad!@2h08id*7V; diff --git a/labs/12_hash_tables/test2/happy_number_separate_chaining.cpp b/labs/12_hash_tables/test2/happy_number_separate_chaining.cpp deleted file mode 100644 index ff0c950..0000000 --- a/labs/12_hash_tables/test2/happy_number_separate_chaining.cpp +++ /dev/null @@ -1,95 +0,0 @@ -// Solve the problem using separate chaining. - -#include - -// this table can have at most 1024 keys -#define TABLE_SIZE 1024 - -class Node { -public: - int number; - Node* next; -}; - -// search the hash table and see if we can find this num. -bool identify(int num, Node** table){ - int key = abs(num%TABLE_SIZE); - // search num in table[key]; - Node* node = table[key]; - while(node!=NULL){ - if(node->number == num){ - return true; - } - node = node->next; - } - // if not found, return false; - return false; -} - -// add num into the hash table -void add(int num, Node** table){ - int key = abs(num%TABLE_SIZE); - Node* node = new Node; - // insert num and index into table[key] - // if this is the first node - if(table[key]==NULL){ - node->number = num; - node->next = NULL; - table[key] = node; - }else{ - // if this is not the first node - node->number = num; - node->next = table[key]; - table[key] = node; - } -} - -int replace(int n){ - int digit; - int result=0; - while(n>0){ - digit = (n%10); - result += digit * digit; - n = n/10; - } - return result; -} - -bool isHappy(int n) { - int newN = n; - Node* hash_table[TABLE_SIZE]; - for(int i=0;igusOhUw66&dUn%o-!$Q#LQqWzlfy=pJxh$qBNOjOv{dzp~Uh1pjawTPgjpl~0 zupA1i`aXtoq?Z%DR5vf<`ZD#yAnQ{>>TkL(k4Ea|lw2MS&5A~v+S+Hem(H0rr=*}Y zR!}VHB$p3=WK-AQP^*DW58+sREqlF8D}F>HQz1I#Z(j9-e|_q%#{xYo?!V{K&w8uI|F`e2K>GZ_%|}h zb3+C^*@F*%?yENgf1o|{&&+`TEbw0a^#&MS=&ls$?*f0CcCJ>|EQ$G4Azg;Csxj7N zw8jH1al_DzikhWHDBKcW6={u!TWXdrjK-S6HG#TlSkk;HH;1BCs~a`r<{PSG(N;Zz zyXtsJxG5B^2v*07ih>P+79-vgh{Rhfg1Wv8xY>1q)=1EZwZ>b*fyRm&qo&Z%tLH!j zFo~B&Vyy;pgi9)#BJuJfeYMHFYSFUlShb;xI*P6|jCey!Yz;Dpj6gIR3nHfxY!4W9 zXhV|`48(&C)XMgDqdDBt8fywfBk{Gy>OxVzxYSX;r6sV|XbP_}qT!}h@di=p5=dh} zC-FceT3l#UwL}`jMoYLM(Ar=$$D)zo+M*I;DN2PyjMf_)Nf*`eXluoZwLVybwqIdX zEeWDr(;BHsml75VC5WcAghiW09VNk7Tb!E=g{ubFHm!*uwJhA))`+eX#p)x}_&X^H zHyPwkZwy45I71*FZjZ#Z#&Bb6n0Ue}g0~yNhTDyLG+slY)_5p**=6KU=TLH05L_r# zM5&;}U%zmnF}t8pyS}pGx`jq@L5X=c+qx|-xI&_rFGuE*f}&Dw;qv7RuB$ML3T79~ zb1D`2B@+1{q5|y}#_hcSLAdepT0N)R&WW5tGx;56`SIE;rcr$j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@ z{2benFhFYwM*~!V@0z1=tw21eDZp*Psu+@V(;5zH1$a>dN%6*OK}$^hg0mppz&{~x z2!WeQp_*XO2)75r&2jv?ok9)4Rle9Kd8m6-9%zh!y<~_T1FZl*Yj4C4wAHWW-!r7o za^R=`_;#YarjBz${<%I=WnMFZ8~QB3MEt4prCeS?{``ztB-gx?H~74RqkIFGSCBui zVV24<@8nH|tGxO?rh-0Kes}p#fPM_$KvnXq@5Cy|pLsC96YG{gfbt)}pDM4uBdcID z6G%s-XH}j*%b-Poqb95J>btZGs_)Pui@1n~{kj7Q`o^m&pL>d=DyY6AHh~MNd@M_Ckqd}2ag4EeJ&jFhsP3EVu4KYMu}ms3!j(P%5+Vy}J0Rle{vnOQ-V zU$IbW1-o74%ecIPLO4|zP?b~gTki7wF^w{YKU5z&VIHcSV);E+dG-A~1wW`VGIWoh z=eWGt|7Dz!p|U^53R_Y41b>J>(!VNSQicRB1z%x*AhARH?`I&`%jcKNoC@yBQza!- z_M@<`<|(B1yGfjf9BwfLq_Heg(&&auQLMSKqOo%H>r) z$<4BVh0jBIvVB!veTUYk>Sx8$_@zsgQ*wO`IBLHtufAiAHMF=6NMQa~ -#include - -int replace(int n){ - int digit; - int result=0; - while(n>0){ - digit = (n%10); - result += digit * digit; - n = n/10; - } - return result; -} - -bool isHappy(int n) { - int newN = n; - std::unordered_set set1; - while(1){ - newN = replace(newN); - if(newN==1){ - return true; - }else{ - // if we can find it, this is going to be an infinite loop - if(set1.find(newN)!=set1.end()){ - return false; - } - // can't find it, insert it in the set first - set1.insert(newN); - } - } -} - -int main() { - // Test cases - // 2, 4, 5, 6, 17, 18, 20 are not happy numbers. - // 1, 7, 10, 13, 19, 23, 28, 68 are not happy numbers. - - int testCases[] = {2,4,5,6,17,18,20,1,7,10,13,19,23,28,68}; - - for (int n : testCases) { - if (isHappy(n)) { - std::cout << n << " is a happy number." << std::endl; - } else { - std::cout << n << " is not a happy number." << std::endl; - } - } - - return 0; -} - diff --git a/labs/12_hash_tables/test4/a.out b/labs/12_hash_tables/test4/a.out deleted file mode 100755 index 3a2f665710875d54c14aa9391becbf24c5aa0088..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 25584 zcmeHQeRNdSwZ9V*2`!RH!BU|xe)Az?CIk!?G)M+-G+-o&RSRB+nF*O^GLug3Kw?F) zf!29L6kA)d`mAm3m#=G;T3;8hPc(eUV3%!UODk0?(WiA%u#G9z)T;A-`<#95+_{-a zTf5#`{Ub*w`=0%E_Sxs0ea_r_XLFmcepQjnrKvJOyF{bfz-baw!-Szv@dZGQR;`_i zziw@gb_(jn0w?J;5`Zep(V|>f%J>O@BsYUD^1xXVttqH1BuH|F%AOBPih^3Xtdm>` zU2@$C>pmj+6jXGpy$VU!9PqXeNxXt~Ir5Erxum;S=fV;8OSS3@+eK)St7f@smQ%2t z5fl& zS7;mt*q;h=^V)@N;SEcwE^G@fYzs%?T?@Oamn>XT<>`!iDuoGTmm43N zQm8b>!i&a$I`exUR6Ur+%51K{)d z|9&Vs%h@REp9TIjZKhV!Ac^JFAQi*d*dC1-ou)r#8ir=nHLW&+p;%~RxYG>9npUrD zi$+3C{tay*Ny|^UzR|3#FbuOb7Ts*Lg(4fxRwERPMPm(D1~lXPU|YkccEB}_(Y8)s z7*~yERVWf{s|z%mUT>h)A2ZCDKWui^1$@4>z%AO~?+gcwXr~zq`P=K7jHU|1*SG`* zLWx-&j&>U02vyZZ!e*`4x5-l8uxf2%w9)X1HoO-pn>P4^hQF;X8UUxUv8&7I2*o<1 z5r13Q>^3%42=2=29PU`m-)%%fsz203quJJ3_xD*GXo9_q4cY(5X4!j(;VY%#@I@dR zH#MUzysFq4@a2MZ-%JJ+$#2S07qDPRu|`mH(z>fynwtvUth z;lUMFodWalpx3HXKpq}EQ`OPCy_Zn$YBkY6)P0Wnaz}l!qrT8lKi5(JxT8MZQ9spD zFLBi0!|edOeSdb;-*D7lv)8?E)Fqz1K~KD*_q;mP(B$*>d7svMm$zXUGShzv&XX;k z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_Ed8PtPNL+%s69lOLM)_Trs87`I< zCg&ziCOP`RDWe=cTO$-(V_7h5)hMw7i&JVcf}K$VuQUWHPZoKgdi1EvF}DvnSmg@H z7gR1%p@xErK~rCNqxO@YSg6HQAd6LDbynmeb<${x>=;T%J}GN7k@$(avZy^AscT0} z!{Vq^Yg??_d{!^QJPn4d2-Fbf%vX+4wgSlCNV;Vo1xhP5mcw#-hyY`Y!j9Il02+}Y zD}+2*6d@>MnXlbqzY?9p?!j9QLR%Yhp&>9XLdG%*)MD<-bX%6M3B|Q+aUd*%(EYF7V#0=0UnnRK*d@6N`#(Hl9!` zf1Pd(LYu0g8D#7Xbp=8lCXV!!idATp-?XI;b&baT?O~{w3K60-55D5JDjxgtLN_u z?q+$ipX^ljGg|A#1^HCTtM>sERPO_*_LaQq|Gg}K87oxp7bs|#r(0=G`L6*-K2iE{ zOHUSteA|_gqsRXNQ0($5x~?r4+<%)tus7KTol|8h`a1Qo%%ybawm{`lk+g_54~v zl{a4TJKKN3A>YL}Y!#&EN%o@VkHWo%3mSiAf78X1qTm9>$dGvLoB?6Gy!BkCMvAC)t`sOaRj0g3gj4OSyuveTBnRoY zml}Ho*JpG46%M?TFEoBiEkQN*b`^S!taVUuN1?|7^o&p0ulh9!IAuF7t+MAzd0DDn aSYV23S25D^%qQP>z2u;L`u5_eYX1jgz1yt- diff --git a/labs/12_hash_tables/test4/test_longest_consecutive_sequence.cpp b/labs/12_hash_tables/test4/test_longest_consecutive_sequence.cpp deleted file mode 100644 index 7dfd688..0000000 --- a/labs/12_hash_tables/test4/test_longest_consecutive_sequence.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include -#include - -#define TABLE_SIZE 1024 - -class Node{ -public: - int number; - Node* next; -}; - -// insert num into table -void insert(int num, Node** table){ - int key; - key = abs(num%TABLE_SIZE); // key will be something in between 0 and (TABLE_SIZE-1); num can be negative - if(table[key] == nullptr){ - // create the first node for this linked list - Node* node; - node = new Node; - node->number = num; - node->next = nullptr; - table[key] = node; - }else{ - // insert a node to the beginning of this linked list - Node* node; - node = new Node; - node->number = num; - node->next = table[key]; - table[key] = node; - } -} - -// search the hash table and see if we can find this num. -bool identify(int num, Node** table){ - int key = abs(num%TABLE_SIZE); - // search num in table[key]; - Node* node = table[key]; - while(node != nullptr){ - if(node->number == num){ - return true; - } - node = node->next; - } - // if not found, return false; - return false; -} - -// Question: why is this an O(n) solution when we have a nested loop? Because the inner while loop will only be used if *itr1 is the beginning of the sequence, which means each element will only be visited 2 or 3 times. -int longestConsecutive(std::vector& nums) { - int len=0; - Node* hash_table[TABLE_SIZE]; - // initialize the table - for(int i=0;inum-1) can't be found - if(identify(current->number - 1, hash_table)){ - int x = current->number + 1; - // now that current->num is the beginning of a sequence, how about current->num + 1? - while(identify(x, hash_table)){ - x++; - } - // when we get out of the above while loop, it's time to update len, if needed. - if( (x - current->number) > len){ - len = x - current->number; - } - } - current = current->next; - // we still need a while here, rather than an if. - // so that we can find the next non-empty bucket. - while(current == nullptr){ - i++; - if(i nums = {100, 4, 200, 1, 3, 2}; - std::vector nums = {0,3,7,2,5,8,4,6,0,1}; - //std::vector nums = {100, 4, 200, 201, 202, 203, 205, 204, 1, 3, 2}; - int size = nums.size(); - std::cout<< "for vector {"; - for(int i=0;i -#include -#include - - // Question: why is this an O(n) solution when we have a nested loop? Because the inner while loop will only be used if *itr1 is the beginning of the sequence, which means each element will only be visited 2 or 3 times. - int longestConsecutive(std::vector& nums) { - int len=0; - std::unordered_set set1; - int size = nums.size(); - // store unique elements in nums in set1 - for(int i=0;i::iterator itr1 = set1.begin(); - while(itr1!=set1.end()){ - // clearly *itr1 is in the set, because that's the meaning of iteration; and if *itr1-1 is not in the set, then we know *itr1 is the beginning of a sequence. - if(!set1.count(*itr1-1)){ - int x = *itr1+1; - // now that *itr1 is the beginning of a sequence, how about *itr1+1? - while(set1.count(x)){ - x++; - } - if(x-*itr1>len){ - len = x-*itr1; - } - } - itr1++; - } - return len; - } - -int main() { - //std::vector nums = {100, 4, 200, 1, 3, 2}; - std::vector nums = {0,3,7,2,5,8,4,6,0,1}; - //std::vector nums = {100, 4, 200, 201, 202, 203, 205, 204, 1, 3, 2}; - int size = nums.size(); - std::cout<< "for vector {"; - for(int i=0;i -#include - -#define TABLE_SIZE 1024 - -class Node{ -public: - int number; - Node* next; -}; - -// insert num into table -void insert(int num, Node** table){ - int key; - key = abs(num%TABLE_SIZE); // key will be something in between 0 and (TABLE_SIZE-1); num can be negative - if(table[key] == nullptr){ - // create the first node for this linked list - Node* node; - node = new Node; - node->number = num; - node->next = nullptr; - table[key] = node; - }else{ - // insert a node to the beginning of this linked list - Node* node; - node = new Node; - node->number = num; - node->next = table[key]; - table[key] = node; - } -} - -// search the hash table and see if we can find this num. -bool identify(int num, Node** table){ - int key = abs(num%TABLE_SIZE); - // search num in table[key]; - Node* node = table[key]; - while(node != nullptr){ - if(node->number == num){ - return true; - } - node = node->next; - } - // if not found, return false; - return false; -} - -// Question: why is this an O(n) solution when we have a nested loop? Because the inner while loop will only be used if *itr1 is the beginning of the sequence, which means each element will only be visited 2 or 3 times. -int longestConsecutive(std::vector& nums) { - int len=0; - Node* hash_table[TABLE_SIZE]; - // initialize the table - for(int i=0;inum-1) can't be found - if(!identify(current->number - 1, hash_table)){ - int x = current->number + 1; - // now that current->num is the beginning of a sequence, how about current->num + 1? - while(identify(x, hash_table)){ - x++; - } - // when we get out of the above while loop, it's time to update len, if needed. - if( (x - current->number) > len){ - len = x - current->number; - } - } - current = current->next; - // we still need a while here, rather than an if. - // so that we can find the next non-empty bucket. - while(current == nullptr){ - i++; - if(i nums = {100, 4, 200, 1, 3, 2}; - std::vector nums = {100, 4, 200, 1, 3, 2, 2, 2, 2, 3}; - //std::vector nums = {100, 4, 200, 1, 3, 2, 5, 6}; - //std::vector nums = {0,3,7,2,5,8,4,6,0,1}; - //std::vector nums = {100, 4, 200, 201, 202, 203, 205, 204, 1, 3, 2}; - int size = nums.size(); - std::cout<< "for vector {"; - for(int i=0;i -#include - -#define TABLE_SIZE 1024 - -class Node{ -public: - int number; - Node* next; -}; - -// insert num into table -void insert(int num, Node** table){ - int key; - key = abs(num%TABLE_SIZE); // key will be something in between 0 and (TABLE_SIZE-1); num can be negative - if(table[key] == nullptr){ - // create the first node for this linked list - Node* node; - node = new Node; - node->number = num; - node->next = nullptr; - table[key] = node; - }else{ - // insert a node to the beginning of this linked list - Node* node; - node = new Node; - node->number = num; - node->next = table[key]; - table[key] = node; - } -} - -// search the hash table and see if we can find this num. -bool identify(int num, Node** table){ - int key = abs(num%TABLE_SIZE); - // search num in table[key]; - Node* node = table[key]; - while(node != nullptr){ - if(node->number == num){ - return true; - } - node = node->next; - } - // if not found, return false; - return false; -} - -// Question: why is this an O(n) solution when we have a nested loop? Because the inner while loop will only be used if *itr1 is the beginning of the sequence, which means each element will only be visited 2 or 3 times. -int longestConsecutive(std::vector& nums) { - int len=0; - Node* hash_table[TABLE_SIZE]; - // initialize the table - for(int i=0;inum-1) can't be found - if(!identify(current->number - 1, hash_table)){ - int x = current->number + 1; - // now that current->num is the beginning of a sequence, how about current->num + 1? - while(identify(x, hash_table)){ - x++; - } - // when we get out of the above while loop, it's time to update len, if needed. - if( (x - current->number) > len){ - len = x - current->number; - } - } - current = current->next; - // we still need a while here, rather than an if. - // so that we can find the next non-empty bucket. - while(current == nullptr){ - i++; - if(i nums = {100, 4, 200, 1, 3, 2}; - //std::vector nums = {100, 4, 200, 1, 3, 2, 2, 2, 2, 3}; - //std::vector nums = {100, 4, 200, 1, 3, 2, 5, 6}; - //std::vector nums = {0,3,7,2,5,8,4,6,0,1}; - //std::vector nums = {100, 4, 200, 201, 202, 203, 205, 204, 1, 3, 2}; - std::vector nums = {-3,0,1,2,3,-2,-1,-5}; - int size = nums.size(); - std::cout<< "for vector {"; - for(int i=0;i