Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

10-10-2013 , 12:18 AM
well array slices are for people who...

yeah i can't do it. lol java, lol java.util.Arrays.copyOfRange, lol camelCase.

Last edited by tyler_cracker; 10-10-2013 at 12:20 AM. Reason: one regret from my 2012 job search: not managing to work into an interview the phrase "COBOL of the 2000s"
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 12:32 AM
"Thanks for coming in, daveT, but.... "

Yeah, that solution probably isn't very good. I'd still prefer a candidate with a single pass across the array instead of doing double comparisons across the array.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 12:51 AM
daveT, your solution is not bad at all, just in the wrong language. Are you a Lisp programmer by any chance?

I think that in java you can do the same by keeping two pointers. Or the following, which is very similar to what you posted and IMO more clear than the official solution:

Code:
public static int countRepeats(int[] a) 
  {
  if (a.length <= 1) 
    return 0;
			
  int count = 0; 
  for(int i = 1; i < a.length; i++) 
  {
    if (a[i] == a[i-1]) 
    {
      count++;
      int repeated = a[i];
      for( i++; i < a.length && a[i] == repeated; i++);
    }
  }
  return count;
}
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 12:57 AM
Quote:
Originally Posted by jaytorr
daveT, your solution is not bad at all, just in the wrong language. Are you a Lisp programmer by any chance?
Look at his location.

(I actually almost responded with something like, "We don't all program in languages where the default data structure is a linked list")
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 01:05 AM
Quote:
Originally Posted by jaytorr
daveT, your solution is not bad at all, just in the wrong language. Are you a Lisp programmer by any chance?

I think that in java you can do the same by keeping two pointers. Or the following, which is very similar to what you posted and IMO more clear than the official solution:

Code:
public static int countRepeats(int[] a) 
  {
  if (a.length <= 1) 
    return 0;
			
  int count = 0; 
  for(int i = 1; i < a.length; i++) 
  {
    if (a[i] == a[i-1]) 
    {
      count++;
      int repeated = a[i];
      for( i++; i < a.length && a[i] == repeated; i++);
    }
  }
  return count;
}
Yeah, that's pretty close to what I was thinking of, except that repeated would probably be initialized outside of the loop so that the if is more like if(repeated == a[i]]), but I'm not sure how well that would work in Java land.

I'm not sure what the second for loop is doing. Care to enlighten me?

Quote:
Originally Posted by Xhad
Look at his location.

(I actually almost responded with something like, "We don't all program in languages where the default data structure is a linked list")
Then it wouldn't surprise you to know that I thought of recursion first.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 01:27 AM
It's just incrementing the pointer while a[i] == repeated. It's the equivalent of
Code:
array = array[1:] until candidate != array[0]
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 01:49 AM
Ah, I see, except that the array isn't getting getting shortened, just pointing to the next index. I was thinking more along the lines of popping the top of the stack / dequeue, but I wasn't sure how to represent it. I implied copying when I didn't really mean to. Thanks for that food for thought.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 02:51 AM
Quote:
Originally Posted by DirtySprite
Anyway my teacher had a solution which I think is a bit more elegant
no, it's not.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 04:22 AM
Quote:
Originally Posted by tyler_cracker
well array slices are for people who...

yeah i can't do it. lol java, lol java.util.Arrays.copyOfRange, lol camelCase.
My hate for Java has gotten less and less over the years. To the point where I'm actually thinking about properly relearning it. NeoCobol is fairly accurate though lol but then again there's Android

I think what's helping it is even if you don't like Java the language the JVM is pretty nice.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 05:01 AM
Quote:
Originally Posted by Xhad
I don't think there's any convenient way to get array slices in Java

EDIT: oh, I just found it right after I posted:

http://stackoverflow.com/questions/1...-array-in-java

Doubt it's faster or even reasonably close b/c of all the copying overhead
I think one of the other answers is actually the more "java" way to do it:

Code:
List<MyClass> subArray = Arrays.asList(array).subList(index, array.length);
This will not copy the underlying array. Instead, we are essentially wrapping it with a decorator, adding to it more "list"-like behavior. The subList call is now a method on your object, so you are actually programming with an OO style rather than applying a global utility function to your array.
Spoiler:

This comment is not to be construed as a defense or endorsement of Java
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 08:19 AM
Also, I just realized I somehow missed the obvious (and I think best) solution of just counting the number of changes to get the number of unique values, then subtracting that from the total:
Code:
    public static int countRepititions(int[] a) {
        
        if (a.length <= 1) return 0;

        int numChanges = 0;
        for(int i=1; i<a.length; i++) {
            if (a[i] != a[i-1])
                numChanges++;
        }   
        int numUniques = numChanges +1;
        return a.length - numUniques;       
    }
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 09:19 AM
1, 2, 2, 2, 3, 3 has 2 changes but you would return 3 (if my paper computer is right). Also, isn't it counting elements with a count > 2? You are only counting changes not the number of elements that match.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 09:22 AM
This one should work some of the time:

Code:
public static int countRepititions(int[] a) {
    return new Random().nextInt(a.length/2)
}
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 09:27 AM
Would it be faster to look for array[i] != array[i+1] && array[i+1]==array[i+2]?

1 2 2 3 3 4 4 5 6
1t2t2 = 1
2f2f3 = 0
2t3t3 = 2
3f3f4 = 0
3t4t4 = 3
4f4f5 = 0
4t5f6 = 0
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 10:12 AM
Quote:
Originally Posted by kerowo
1, 2, 2, 2, 3, 3 has 2 changes but you would return 3 (if my paper computer is right). Also, isn't it counting elements with a count > 2? You are only counting changes not the number of elements that match.
right, 3 is the answer (as i understand it).
there are 2 extra twos and 1 extra 3, for a total of 3 repititions
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 10:14 AM
I interpreted the question as being 2, there are 2 distinct numbers that occur more than once

Quote:
Example given is for the input { 1; 1; 1; 2; 4; 4; 5; 6; 6 }  the answer would be 3 because three different numbers (1,4,6) appear more than once.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 10:27 AM
Quote:
Originally Posted by Gullanian
I interpreted the question as being 2, there are 2 distinct numbers that occur more than once
yeah you are right. i somehow misremembered the question between yesterday and today. but your interpretation is how my previous answer and the teacher's answer work, so that's correct. so... nm my latest post
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 10:44 AM
Quote:
Originally Posted by clowntable
I think what's helping it is even if you don't like Java the language the JVM is pretty nice.
i agree with this. in the early 2000s when perl6 was still vaporware[1] and people were crowing about ParrotVM[2], i was like who gives a ****? why do i care that my interpreted language gets compiled to some bytecode situation before going off to the hardware.

nowadays, the rise of meaningfully useful languages which leverage the jvm (e.g. clojure) have proven me wrong. turns out larry wall knows more than me about language design[3].


[1] O WATE

[2] cwidt?

[3] drudgesiren.gif
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 11:31 AM
I guess the code would look like this for my above idea:

Code:
public static int checkRepetition(int[] a)
	{
		int repetitionCount = 0;
	
		for(int i=0; i<(a.length-2); i++)
		{
			if (a[i] != a[i+1] && a[i+1]==a[i+2]
	                {
                              repetitionCount++;
                         }
		}		
		return repetitionCount;	
	}
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 12:07 PM
That is basically like my solution from yesterday: http://forumserver.twoplustwo.com/sh...ostcount=10468

except you're looking forward and you miss the edge case. For example that code fails on:

int[] a = {1,1,2,2,3,4,4,6};
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 03:23 PM
While we're still skinning this cat, here are a couple more ways.

First a modified version of what I posted earlier. It's forward looking and that eliminated the need for the initial array length check - the main loop does the check. Also changed the for loop that increments the pointer to a while loop to make it more clear.

Code:
public static int countRepeats(int[] a) 
{
  int repeated, count = 0; 
  for(int i = 0; i < a.length-1; ) 
  {
	if (a[i] == a[++i]) 
	{
	  count++;
	  repeated = a[i++];
	  while(i < a.length-1 && a[i] == repeated) i++;
	}
  }
  return count;
}
Can someone with more Java/C experience comment on the use of postfix and prefix operators above? I'm learning C++ at the moment and see them all the time in others' code, but I'm not sure how idiomatic they are. I'm particularly leery about the line:

Code:
if (a[i] == a[++i])
Here's a partially recursive version (it still has a while loop).

Code:
public static int countRepeats3_r(int[] a, int start, int end, int count)
{
  if (end-start <= 1) 
    return count;
  if (a[start] == a[start+1]) 
  {
    int next = start+2;
    while(next < end && a[next] == a[start]) next++;
    return countRepeats3_r(a, next, end, count+1);
  }
  else
    return countRepeats3_r(a, start+1, end, count);
}	
	
public static int countRepeats3(int[] a) 
{
    return countRepeats3_r(a, 0, a.length, 0);
}
I tried a fully recursive version, but it was a bit ugly for my liking.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 03:31 PM
If you really want to show off, do:

A = evenElements(array)
B = oddElements(array)
ans = len(intersection(A, B))
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 04:38 PM
In case its not clear, A and B are sets.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 04:52 PM
What's the consensus on C#?

I'm a CS major graduating this winter and just got a job offer doing C# on a .NET stack. I really like the company and the people I'd be working with, the salary is pretty much just what I was looking for, but working in a non open source language is slightly less than ideal to me. Although I'm a noob so I figured I'd ask here.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-10-2013 , 05:17 PM
C# the language is fairly well designed but Windows sucks (imo) and the entire environment while theoretically independent of OS (there's Mono for Linux etc.) is not particularly awesome on other operating systems. If you end up working there and are in the environment anyways make sure to check out F# as a side project...neat language.

C# and .net are technically* open but surrounded by closed stuff.
*iirc the MS implementation doesn't exactly follow the open standard but it's been a while since I used C#.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m