मैं coursera स्कैला पाठ्यक्रम से तीसरे असाइनमेंट से निपटने की कोशिश कर रहा हूं। मैंने इसमें से कुछ पूरा कर लिया है, हालांकि मुझे लगता है कि जब मैं एक निश्चित कार्य की बात करता हूं तो मुझे उस बिंदु को याद आ रहा है। मुझे फ़िल्टर फ़ंक्शन को कार्यान्वित करना होगा जो दिए गए भविष्य को संतुष्ट करने वाले ट्वीट्स के सेट से सभी ट्वीट्स का एक सबसेट वापस कर देगा। मैंने कुछ कार्यों को लागू किया है जो मुझे लगता है कि इससे मुझे मदद मिलेगी हालांकि परीक्षणफ़िल्टर एल्गोरिदम में लापता तर्क
नोट कृपया मुझे बेक्ड कोड न दें क्योंकि यह coursera सम्मान कोड का उल्लंघन करेगा। मुझे बस इतना कुछ चाहिए जो मुझे मेरे तर्क को डीबग करने में मदद करेगा और मुझे यह देखने में मदद करेगा कि मैं कहां गड़बड़ कर रहा हूं और परीक्षण क्यों विफल हो जाते हैं।
abstract class TweetSet {
def isEmpty: Boolean
/**
* This method takes a predicate and returns a subset of all the elements
* in the original set for which the predicate is true.
*
* Question: Can we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def filter(p: Tweet => Boolean): TweetSet
/**
* This is a helper method for `filter` that propagetes the accumulated tweets.
*/
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet
/**
* Returns a new `TweetSet` that is the union of `TweetSet`s `this` and `that`.
*
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def union(that: TweetSet): TweetSet;
/**
* Returns the tweet from this set which has the greatest retweet count.
*
* Calling `mostRetweeted` on an empty set should throw an exception of
* type `java.util.NoSuchElementException`.
*
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def mostRetweeted: Tweet = ???
/**
* Returns a list containing all tweets of this set, sorted by retweet count
* in descending order. In other words, the head of the resulting list should
* have the highest retweet count.
*
* Hint: the method `remove` on TweetSet will be very useful.
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def descendingByRetweet: TweetList = ???
/**
* The following methods are already implemented
*/
/**
* Returns a new `TweetSet` which contains all elements of this set, and the
* the new element `tweet` in case it does not already exist in this set.
*
* If `this.contains(tweet)`, the current set is returned.
*/
def incl(tweet: Tweet): TweetSet
/**
* Returns a new `TweetSet` which excludes `tweet`.
*/
def remove(tweet: Tweet): TweetSet
/**
* Tests if `tweet` exists in this `TweetSet`.
*/
def contains(tweet: Tweet): Boolean
/**
* This method takes a function and applies it to every element in the set.
*/
def foreach(f: Tweet => Unit): Unit
}
class Empty extends TweetSet {
def union(that: TweetSet): TweetSet = that
def isEmpty = true
def filter(p: Tweet=> Boolean): TweetSet = new Empty()
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = new Empty()
/**
* The following methods are already implemented
*/
def contains(tweet: Tweet): Boolean = false
def incl(tweet: Tweet): TweetSet = new NonEmpty(tweet, new Empty, new Empty)
def remove(tweet: Tweet): TweetSet = this
def foreach(f: Tweet => Unit): Unit =()
}
class NonEmpty(elem: Tweet, left: TweetSet, right: TweetSet) extends TweetSet {
def union(that: TweetSet): TweetSet = (left.union(right)).union(that).incl(elem)
val isEmpty = false
def filter(p: Tweet => Boolean): TweetSet = filterAcc(p,left.union(right))
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {
if(acc.isEmpty) acc
else if(p(elem)) {acc.incl(elem); left.filterAcc(p,acc).union(right.filterAcc(p,acc))}
else new Empty
}
/**
* The following methods are already implemented
*/
def contains(x: Tweet): Boolean =
if (x.text < elem.text) left.contains(x)
else if (elem.text < x.text) right.contains(x)
else true
def incl(x: Tweet): TweetSet = {
if (x.text < elem.text) new NonEmpty(elem, left.incl(x), right)
else if (elem.text < x.text) new NonEmpty(elem, left, right.incl(x))
else this
}
def remove(tw: Tweet): TweetSet =
if (tw.text < elem.text) new NonEmpty(elem, left.remove(tw), right)
else if (elem.text < tw.text) new NonEmpty(elem, left, right.remove(tw))
else left.union(right)
def foreach(f: Tweet => Unit): Unit = {
f(elem)
left.foreach(f)
right.foreach(f)
}
}
मुझे लगता है कि इस बारे में प्रमुख गलत बात filterAcc के रूप में यह रिटर्न है empty
जब कोई भी मामलों लागू होते हैं लेकिन मुझे लगता है मैं वास्तव में और क्या कर सकता है यकीन नहीं है। क्या यह सब कुछ विफल रहता है? यदि हां, तो मुझे इसके आसपास कैसे जाना चाहिए?
अद्यतन इसके अलावा, मैं इस समस्या को इस तरह से हल करने के लिए कोशिश की थी:
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {
if(acc.isEmpty) acc
else if(p(elem)) {acc.incl(elem); left.filterAcc(p,acc).union(right.filterAcc(p,acc))}
else left.filterAcc(p,acc).union(right.filterAcc(p,acc))
}
इस विधि अब सुनिश्चित करती है कि यदि कुछ भी तो हालत का मिलान नहीं हुआ एक पुनरावर्ती कॉल अभी भी किया जाता है, लेकिन एक ही समय में संचयक वृद्धि नहीं करता है। परीक्षण अभी भी असफल हो जाते हैं। मेरे तर्क की दोष क्या है?
धन्यवाद
@lpiepiora और @Ende Neu सख्त मुझे बताने की कोशिश की के रूप में, एसीसी का प्रारंभिक खाली होना चाहिए। मैंने अभी भी एक संघ का उपयोग कर समाप्त कर दिया है क्योंकि मेरा दिमाग बस इस विचार से ढेर हो गया और मैं इसे बंद नहीं कर सका। कोड का अंतिम भाग यहां दिया गया है:
def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {
if(left.isEmpty && right.isEmpty) acc
else if(p(elem)){ left.filterAcc(p,acc.incl(elem)).union(right.filterAcc(p,acc.incl(elem)))}
else left.filterAcc(p,acc).union(right.filterAcc(p,acc))
}
एक संकेत पर filterAcc में पारित हो जाता, याद रखें कि अपने 'TweetSet' अपरिवर्तनीय है, इसलिए यदि आप एक विधि को कॉल किया , जो इसमें कुछ जोड़ता है, आपको एक नया सेट मिल जाएगा। आपको परिणाम के साथ कुछ करना है या यह खो गया है। और आपको 'filterAcc' लिखने के लिए संघ की आवश्यकता नहीं है - 'acc' accumulator और रिकर्सन का उपयोग करें। जब आप पहली बार विधि चलाते हैं तो क्या होता है? 'Acc' का मूल्य क्या है - क्या इसमें कोई तत्व है? अगर आप 'खाली' सेट दबाएंगे तो क्या होगा, क्या लौटाया जाता है? – lpiepiora
@lpiepiora हां। यह खाली होना चाहिए। मैं अभी भी एक संघ का उपयोग कर समाप्त हो गया लेकिन हे! इसने काम कर दिया। आपके स्पष्टीकरण के लिए धन्यवाद – Bula
बस अंतिम संकेत, शायद अगर आप सामान को थोड़ा सा सरल बना सकते हैं। यदि आपको 'एम्स्टसेट' मिलता है और 'filterAcc (_ => true, acc)' कहता है, जहां 'acc' में पहले से ही कुछ तत्व हैं, तो आप क्या वापस करेंगे, क्या आप सुनिश्चित हैं कि' एम्प्सेटसेट 'सही बात है? – lpiepiora