JavaBat
1
Name:
Jesus
2009-02-11 20:37
Hey /programing/ i need some help w/ javabat.(array 1)
it would really help if i could have the user/pass of someones. i will not screw it up.
We'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array.
unlucky1({1, 3, 4, 5}) → true
unlucky1({2, 1, 3, 4, 5}) → true
unlucky1({1, 1, 1}) → false
public boolean unlucky1(int[] nums) {
2
Name:
Anonymous
2009-02-11 20:39
>/programing/
Get out.
3
Name:
Anonymous
2009-02-11 20:42
>/programing/
Get out.
4
Name:
Anonymous
2009-02-11 20:43
wat?
5
Name:
Anonymous
2009-02-11 20:43
do you want /prog/?
6
Name:
Anonymous
2009-02-11 21:05
pro⋅gram /ˈproʊgræm, -grəm/
noun, verb, -grammed or -gramed , -gram⋅ming or -gram⋅ing .
7
Name:
Anonymous
2009-02-11 22:40
>>6
pro⋅gram /ˈproʊgrām, -grām/
noun, verb, -gramed , -gram⋅ing .
8
Name:
Anonymous
2009-02-11 22:51
Here you go OP, don't mind these trolls.
public boolean unlucky(int[] nums) {
if(nums.length<=1) return false;
int last = nums[0];
for(int i=1; i<nums.length; i++) {
int cur = nums[i];
if(cur==3 && last==1) {
return true;
}
last = cur;
}
}
Hasn't been checked so there might be something small wrong.
9
Name:
Anonymous
2009-02-11 23:10
>>8
Sorry OP, didn't read the second part of the specification. Have some more elegant regex instead.
public boolean unlucky1(int[] nums) {
String cat = "";
for(int i=0; i<nums.length; i++)
cat+=nums[i];
return java.util.regex.Pattern.matches("^(.?13.*|.*13)$",cat);
}
Hasn't been checked either; may be some compile time errors.
10
Name:
Anonymous
2009-02-11 23:21
unlucky a = elem [1, 3] [take 2 a, take 2 $ tail a, reverse $ take 2 $ reverse a]
11
Name:
Anonymous
2009-02-11 23:42
>>9
what happens if nums contains "13"? or "130"? or "113"?
12
Name:
Anonymous
2009-02-12 0:15
>>9
For efficiency you should be using
StringBuilder here... ಠ_ಠ
13
Name:
Anonymous
2009-02-12 0:25
>>11
It will work perfectly? Why, can you see an error?
14
Name:
Anonymous
2009-02-12 0:48
This assignment sounds like it's in the problem domain of a Touring Machine . Come back here when your septuples are in order.
15
Name:
Anonymous
2009-02-12 0:50
>>11
Those all contain "13" and are therefore unlucky.
16
Name:
Anonymous
2009-02-12 0:50
>>13
if the array is { 13 },
>>9 's function will return true, even thought the array contains neither a 1 nor a 3.
17
Name:
Anonymous
2009-02-12 0:53
My Touring Machine has more memory than I could ever use, sometimes I rip a little off the end when I run out of toilet paper.
18
Name:
Anonymous
2009-02-12 1:20
: unlucky? ( seq -- bool ) { [ 0 over length 3 min rot ] [ 2 from-end (tail) ] } [ call <slice> ] with map [ { 1 3 } swap subseq? ] any? ;
19
Name:
Anonymous
2009-02-12 1:41
20
Name:
Anonymous
2009-02-12 2:34
Use pattern matching .
21
Name:
Anonymous
2009-02-12 3:21
>>20
like this?
unlucky (1:3:xs) = True
unlucky (_:1:3:xs) = True
unlucky a = (take 2 $ reverse a) == [3,1]
22
Name:
Anonymous
2009-02-12 15:02
>>9
doesn't quite qualify as
EXPERT PROGRAMMER yet.
23
Name:
Anonymous
2009-02-12 15:05
Optimized:
unlucky (1:3:xs) = True
unlucky (_:1:3:xs) = True
unlucky (_:_:1:3:xs) = True
unlucky (_:_:_:1:3:xs) = True
unlucky (_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:1:3:xs) = True
24
Name:
Anonymous
2009-02-12 15:06
>>23
Except it doesn't quite fit the
requirements .
25
Name:
Anonymous
2009-02-12 15:09
How about now?
unlucky (1:3:xs) = True
unlucky (_:1:3:xs) = True
unlucky (_:_:1:3:xs) = True
unlucky (_:_:_:1:3:xs) = True
unlucky (_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
unlucky (_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs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_:_:_:_:_:_:_:_:_:_:_:_:_:_:_:1:3:xs) = True
26
Name:
Anonymous
2009-02-12 15:30
>>25
No, write a lazy code generator.
27
Name:
Anonymous
2009-02-12 16:02
>>26
/prog/ is a lazy code generator.
28
Name:
The Scatman
2009-02-12 17:03
use recursion shoobydooby beeepababadooooooowop
29
Name:
Anonymous
2009-02-12 17:06
unlucky list = runPROGRAM (while True $ match list with
(1:3:xs) -> return True
(_:xs) -> list := xs)
30
Name:
Anonymous
2009-02-12 19:43
>>23,25,29
Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array .
31
Name:
Anonymous
2009-02-12 19:44
>>30
Checking the leading 13 is trivial.
32
Name:
Anonymous
2009-02-12 20:07
>>31
Hax...
oh, what's the point
33
Name:
Anonymous
2009-02-12 20:20
public boolean unlucky1(int[] nums) { return true; }
This is a valuable lesson about practical software development.
34
Name:
Anonymous
2009-02-12 21:34
>>21
unlucky (1:3:xs) = True
unlucky (_:1:3:xs) = True
unlucky a = (take 2 $ reverse a) == [3,1] ||
(take 3 $ reverse a) == [_,3,1]
35
Name:
Anonymous
2009-02-12 21:43
>>34
only if the
1 is in the first 2 or last 2 positions. that means there are only three things you need to check for:
(1:3:xs),
(_:1:3:xs), and
(_++[1,3]).
36
Name:
Anonymous
2009-02-12 21:48
unlucky (1:3:_) = True
unlucky (_:1:3:_) = True
unlucky a = isSuffixOf [1,3] a
37
Name:
Anonymous
2009-02-12 22:03
I have just used 'qubits' to solve this problem in O(1) time.
38
Name:
Anonymous
2009-02-12 23:41
perfect!
public boolean unlucky1(int[] nums) {
int l = nums.length;
if (l < 2) return false;
if (nums[0] == 1 && nums[1] == 3 ||
nums[1] == 1 && nums[2] == 3 ||
nums[l-2] == 1 && nums[l-1] == 3)
return true;
return false;
}
unlucky1({1, 3, 4, 5}) → true true OK
unlucky1({2, 1, 3, 4, 5}) → true true OK
unlucky1({1, 1, 1}) → false false OK
unlucky1({1, 3, 1}) → true true OK
unlucky1({1, 1, 3}) → true true OK
unlucky1({1, 2, 3}) → false false OK
unlucky1({3, 3, 3}) → false false OK
unlucky1({1, 3}) → true true OK
unlucky1({1, 4}) → false false OK
unlucky1({1}) → false false OK
unlucky1({}) → false false OK
unlucky1({1, 1, 1, 3, 1}) → false false OK
unlucky1({1, 1, 3, 1, 1}) → true true OK
unlucky1({1, 1, 1, 1, 3}) → true true OK
unlucky1({1, 4, 1, 5}) → false false OK
unlucky1({1, 1, 2, 3}) → false false OK
unlucky1({2, 3, 2, 1}) → false false OK
unlucky1({2, 3, 1, 3}) → true true OK
unlucky1({1, 2, 3, 4, 1, 3}) → true true OK
39
Name:
Anonymous
2009-02-13 2:21
>>38
I hate you so much.
public boolean fuckyourshittyproblems(int[] nums) {
return (nums[0] == 1 && nums[1] == 3) ||
(nums[1] == 1 && nums[2] == 3) ||
(nums[nums.length - 2] == 1 && nums[nums.length - 1] == 3]);
}
If you're ever writing
return false or
return true you're doing something wrong.
40
Name:
Anonymous
2009-02-13 2:28
>>39
You best be
trolling ,
nigga .
41
Name:
Anonymous
2009-02-13 2:35
>>39
(nums[0] == 1 && nums[1] == 3) ...
Incoming
ArrayIndexOutOfBoundsException!
42
Name:
Anonymous
2009-02-13 2:41
>>41
>>38 has the same problem.
public boolean fuckyourshittyproblems(int[] nums) {
int l = nums.length
return l > 1 && (nums[0] == 1 && nums[1] == 3) ||
l > 2 && (nums[1] == 1 && nums[2] == 3) ||
(nums[nums.length - 2] == 1 && nums[nums.length - 1] == 3]);
}
43
Name:
Anonymous
2009-02-13 2:42
>>42
Nice job destroying the readability for no gain, faggot.
44
Name:
Anonymous
2009-02-13 5:32
>>41
Incoming
AnusOutOfBoundsException!
45
Name:
Anonymous
2009-02-13 7:29
>>41
public boolean fuckyourshittyproblems(int[] nums) {
try {
return fuckyourshittyproblem(nums);
} catch (ArrayIndexOutOfBoundsException e) {
return false;
}
}
Your move.
46
Name:
Anonymous
2009-02-13 7:56
>>45
Exceptions are meant to catch random runtime failures, not for you niggers too lazy to code proper array bound checking.
47
Name:
Anonymous
2009-02-13 8:05
>>46
Runtime exceptions aren't meant to be caught at all, but since they exist, you might as well use them.
48
Name:
Anonymous
2009-02-13 9:12
>>47
So you never catch
IOException when you try to open a file that could potentially be locked?
49
Name:
Anonymous
2009-02-13 10:42
>>46
It's
EAFP than to
LBYL .
50
Name:
Anonymous
2009-02-13 11:13
51
Name:
Anonymous
2009-02-13 12:09
>>50
It's easier to ask for permission than forgiveness than to look before you leap.
Anticudders prefer to ignore errors and slap a try/catch on as an afterthought.
52
Name:
Anonymous
2009-02-13 16:18
>>48
Runtime exception has a specific meaning in Java. IOException isn't a runtime exception.
53
Name:
Anonymous
2009-02-13 23:08
>>39
(nums[nums.length - 2] == 1 && nums[nums.length - 1] == 3]);
Syntax error on token "]", delete this token
54
Name:
Anonymous
2009-02-13 23:11
>>42
>>38 posted the testing results and they are all right. Plus I plugged it into Javabat to double check and it's correct. PS I hope you die of AIDS.
55
Name:
Anonymous
2009-02-25 6:54
The former case your program will include the bits of the library you need and will work in the post ID ranges Secondly by pure chance someone had posted before you just as you think are needed.
56
Name:
Anonymous
2010-11-13 22:28