last day (14 days later) » 

2:31 AM
1
Q: Folding paper strip and generate numbers while unfolding

NikhilI have an assignment question that I am struggling with and need some direction to solve. Suppose i have a strip of paper and i fold it from the center such that the left half goes behind the right half. Then i number the folded peices in sequence i get the numbers when i unfold as follows. 1 : 2...

 
Why did the answers below get -1
 
m69
@MichaelMarkidis Probably downvoted by people who thought the question didn't merit to be answered, because it's not much more than "here is my assignment, I don't know how to solve it, please help me."
 
@m69 I see. Good to know. Thanks.
 
m69
@MichaelMarkidis There are widely varying opinions among users about what SO is: a community of people helping each other with programming problems, or an archive of high-quality Q&A's that may be useful to future users. Where people stand on this issue often explains their voting.
The sequence looked familiar, and it is indeed related to the one you get with bit reversal, so there may be algorithms there that you could adapt.
I found an additional simplification using a bitwise NOT operation for the odd numbers, and tried my hand at some Java code.
@MichaelMarkidis I have a question about your suggested edit of the Java code: why did you change >>>1 into /2? Isn't the unsigned >>> operator safer in this case?
@MichaelMarkidis Also, is x & 1 not simpler than x % 2? Or would that be compiled to the same code?
 
@m69 You can absolutely can do it that way. I was just trying to make it a bit more readable. Also, the code you put does not compile.
@m69 Also, that code did not account for moving to zero-based and back to one-based.
 
m69
2:31 AM
@MichaelMarkidis I edited the edit a bit; I hope it still compiles. Any idea why my code didn't, btw? The integer as boolean? Too few brackets? Missing casts? Does this compile: return Long.reverse(((x & 1) != 0) ? (~(x >>> 1)) : (x >>> 1)) >>> (64 - n);?
 
@m69 For the edit you made to my "more readable" one you need to put parens around the bitwise "and" in the if stmt: Should be if ((x & 1) != 0) {...} For the second part of your question, look at the "more slim" version I put in. That one compiles.
That code works now. Thanks for taking my edits.
 
m69
We should probably delete the comments I started under the question; I couldn't invite you to a chat immediately.
 
We should.
 
m69
Only after a number of back-and-forth comments do you get the "continue in chat option"
Does my condensed version compile now too? I only changed the brackets around (x & 1)
 
BTW: I hope your answer gets voted #1. It is a very clever solution. My original one works, but yours runs in O(no time) :)
 
m69
2:33 AM
Maybe it needs to be (x & 1) != 0
But I found info that non-zero ints are considered "thruthy" in Java
 
Yeah, you need to wrap the bitwise in parens
 
m69
I'm no longer doing this for the upvotes and accepts; too much frustration if you count on it :-)
 
That's not true. Ints cannot be resolved as booleans in Java
You can't say if (0) in java. Not like C or C++
 
m69
Oh. That'll teach me for trusting stuff on the internet.
 
m69
2:36 AM
This claims non-zero numbers are true
Oh, it provides a Truth class of its own.
I didn't read the whole thing.
 
Yeah, it says you can do it in script langs, but not java.
Your slim version code is still wrong in the Answer
 
m69
I got used to doing that kind of never-check-any-types-ever stuff in JavaScript
 
JavaScript will do that. It's a fun language though
 
m69
I just changed it to "(x & 1) != 0" Still no success?
 
You need to have: return Long.reverse(((x & 1) != 0) ? ~(x >>> 1) : x >>> 1) >>> (64 - n);
 
m69
2:40 AM
If you're working on your own, JavaScript is great; I wouldn't want to trawl through someone else's code, though.
I don't even understand my own code after a few months :-)
 
m69
Aha, the condition in brackets before the ?
 
Also, don't forget the minus 1 and plus one to go from zero to one based.
 
m69
I left those out for simplicity; readers can choose whether to count from 0 or 1; who the hell counts from 1 anyway ? I suspect the assignment used counting from 1 to make it harder to Google the solution. The bit-reversal sequences are on hte oeis.org
 
Yeah, I'm still in that stage where you're excited to see your reputation go up on SO and you get frustrated at downvotes. That's why one of my early comments was "why the downvote"
It makes sense what you said about the two schools of thought on SO
 
m69
2:45 AM
Answering questions from low-rep users, or questions that are considered low-quelity by some can be risky. Often you put in the effort and then the question gets closed, or the asker never returns, or people downvote your correct answer...
 
That makes sense. I guess a lot of users just create an account to get help with a homework assignment, then never heard from again.
 
m69
I only tackle those if they look like fun and finding the solution will be reward enough.
I expect to get zero rep, then everthing more is a bonus
If no other solutions had been posted, i wouldn't have bothered probably, but looking at the longer sequence in one of the answers sucked me in.
 
Yeah, it's an interesting post. It's like a puzzle.
Well, I'm going to remove some of our chat like comments in the post. It was nice working on the problem with you and chatting with you.
 
m69
I wonder if I shouldn't just post my first code version as it was, like a sort of pseudo-code. I introduced ot with 'I don't speak java" anyway, so people are warned. Then they have your version that actually compiled. I'm not trying to pretend I know anything about Java, other than what is similar to C++.
 
Yeah, I think that's fine. That's what prompted me to convert it to Java.
It's more realistic as well.
 
m69
2:53 AM
Although now that I read that page better about the truthy values in Java, I can't just go back to not knowing about that :-)
I'll sleep on it and maybe edit it in the morning.
people seem to be using the ? without the putting the condition in brackets, like a = b > c ? d : e
59
Q: What is a Question Mark "?" and Colon ":" Operator Used for?

DeependTwo questions about using a question mark "?" and colon ":" operator within the parentheses of a print function: What do they do? Also, does anyone know the standard term for them or where I can find more information on their use? I've read that they are similar to an 'if' 'else' statement. in...

 
You can do that if it's a logical condition. like int i = 50 > 8 ? 1 : 4;
You can't do if it's just a bitwise operator: like int i = x & 1 ? 10 : 5;
You need to wrap the whole expression in a logical one:
 
m69
Anyway, I'm off to fall asleep in front of the tv. See you!
Oh, don't forget to delete the comment chat
 
3:14 AM
Will do. I'm sure I will catch you around SO.
 

last day (14 days later) »