Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Quick Question

Name: Anonymous 2009-11-03 2:36


public class MostOftenOccurring{
  public static int occursMostOften(int[] a){
    int[] b = new int[a.length];
    for (int i = 0; i < a.length; i++){
      b[i] = CountOccurrences.countOccurrences(a, a[i]);
    }
    for (int j = 0; j < b.length; j++){
      if (Maximum.maximum(b) == b[j]){
        return b[j];
      }
    }
    return 666; //I did this to make the compiler happy
  }
  public static void main(String[] args){
    int[] testArray = {1};
    System.out.println(occursMostOften(testArray));
  }
}



Is there any way that return 666; could ever be executed?

Name: Anonymous 2009-11-03 2:38

edit: testArray should be something like {1, 3, 4, 2, 6, 2, 1} and the method will return 2 because it is the element that occurs most often in this array

Name: Anonymous 2009-11-03 2:45

What the FUCK are you doing.  Did you just write some On code to find the most common number in an array?

IHBT

Name: Anonymous 2009-11-03 3:33

There are a number of ways, for example if a is empty. You are retarded anyway. You do not need an O(n2) time and O(n) space method to find the most common element in an array. It can be done in linear time with constant space.

Name: Anonymous 2009-11-03 3:49

Maximum.maximum
I never knew the Java API had a Maximum class. That is insanely retarded.

Name: Anonymous 2009-11-03 3:53

>>5
AnusHaxer.HaxMyAnus

Name: Anonymous 2009-11-03 3:56

>>4 O(n2)

IHBT

Name: Anonymous 2009-11-03 8:21

Is there any way that return 666; could ever be executed?
a.length == 0

Name: Anonymous 2009-11-03 9:11

>>2,3
In Java it doesn't matter if you have On or O(n) code. It is still slow as fuck.

Name: Anonymous 2009-11-03 10:46

>>9
Holy shit, in my CSII class we had to build a table of processing times of different sorting algorithms using Java.

When our professor realized that all of his students were coming up with the same time for each sorting algorithm (due to Java's terrible overhead) he immediately switched all coursework requirements to C.

Name: Anonymous 2009-11-03 10:48

>>10
HA HA! OH WOW!
This is a good argument against java. One of the best I have encountered.

Name: Anonymous 2009-11-03 11:48

>>9
What the fuck is On?

Name: Anonymous 2009-11-03 11:49

>>1
public class MostOftenOccurring
...
CountOccurrences.countOccurrences
...
Maximum.maximum

Is this the fabled ENTERPRISE?? Oh wow.

Name: Anonymous 2009-11-03 13:12

>>5
I'm in an introductory course and I was doing the practice problems for my midterm. One of them asked me to write a class called Maximum which defines a method called maximum which finds the max method in the array.

>>3 I know what Big O Notation is from math but they don't care about speed of algorithms in this course.
I am interested though in how to make this quicker.

>>4 could you give me atleast a vague idea of how?

Name: Anonymous 2009-11-03 13:19

>>14

I wrote a class MaximumMaximum, with a method called MaximumMaximum.maximum, that calculate the MaximumMaximum.maximum or an array of Maximum.maximum!

Name: PHPAdvocate !MARtiNys66 2009-11-03 13:23

Try using a real language.


function most_often_occuring($array)
{
    return max( array_count_values($array) );
}

Name: Anonymous 2009-11-03 13:34

>>16

for a second i thought you meant a real language

Name: Anonymous 2009-11-03 13:39

>>14
Why would you need a class for that (°Д°)

>>16
Try using a real language.
mostCommon = snd . head . sort . (map length &&& head) . group . sort

Name: Anonymous 2009-11-03 13:41

>>18

for a second i thought you meant a real language

Name: Anonymous 2009-11-03 13:42

>>18
fuck, that should have been map (length &&& head).

Name: Anonymous 2009-11-03 13:45

>>20
You should use a real language, then.

Name: Anonymous 2009-11-03 13:45

And s/head/last/, too. I suck.

Name: Anonymous 2009-11-03 13:47

>>18
mostCommon = snd . maximum . (map length &&& head) . group . sort

Name: Anonymous 2009-11-03 14:33


(defun list->frequency-hash-table (list &key (test #'eql))
  "Given a list, returns a hashtable keyed by each element in the list,
    and the values are each element's frequency in the list."
  (let ((counts (make-hash-table :test test)))   
    (dolist (i list counts)
      (symbol-macrolet ((hash (gethash i counts)))
        (if hash (incf hash) (setf hash 1))))))

(defun hashtable->alist (ht)
  "Converts a hashtable to an association list"
  (let (alist)
    (maphash #'(lambda (key val)
                 (push (cons key val) alist))
             ht)
    alist))

(defun max-occurances-pair (list)
  "Returns a cons pair whose car is the most frequent element, and whose cdr is the number of occurances"
  (first (sort (hashtable->alist (list->frequency-hash-table list)) #'> :key #'cdr)))

(defun max-occurances (list)
  "Returns two values, the most frequent element, and the number of occurances"   
  (destructuring-bind (val . count) (max-occurances-pair list)
    (values val count)))


Anyone having a more elegant solution is welcome to post it. I can't think of one without writing some functions for mapping/searching over hash tables. I believe there's a few utility libraries which already have code for this, but I didn't bother looking them up now.

Name: Anonymous 2009-11-03 15:06

>>24
Oops, made a minor mistake there, even though the other functions would still return correct results since they don't depend on the order of the elements in the alist being correct:

(defun hashtable->alist (ht)
  "Converts a hashtable to an association list"
  (let (alist)
    (maphash #'(lambda (key val)
                 (push (cons key val) alist))
             ht)
    (nreverse alist)))

Name: Anonymous 2009-11-03 15:48

>>24
LISP - Taking half a page to do what most languages can do in half a line.

Name: Anonymous 2009-11-03 15:55

>>26
The first 2 are just util functions, you can find equivalents of them in various libraries. The actual code which performs the task is
(first (sort (hashtable->alist (list->frequency-hash-table list)) #'> :key #'cdr))
I could have written it more concise, but it would have been more inefficient/slower.

Name: Anonymous 2009-11-03 16:12

>>26
s/LISP/Lisp/
It's not the 1970's anymore

Name: Anonymous 2009-11-03 16:16

>>28
Let them keep it using it. It's a simple way of telling trolls from people who have studied Lisp.

Name: Anonymous 2009-11-03 21:07

>>10-11
You'd best be trolling

Name: Anonymous 2009-11-03 21:49

>>29
implying that Lispers aren't all trolls

Name: Anonymous 2009-11-03 22:23

>>28
[spoiler]Then why are you using the seventies [m]ed[/i]itor?[/spoiler]?

Name: Anonymous 2009-11-04 2:11

>>31
Uhh, he IS implying Lispers are trolls. Read closely

Don't change these.
Name: Email:
Entire Thread Thread List