Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I increase performance of the following code snippet in assembly? Please suggest what ways I have in order to do this?
void changeDirection(char key) {
/*
W
A + D
S
*/
switch (key) {
case 'w':
if (direction != 2) direction = 0;
break;
case 'd':
if (direction != 3) direction = 1;
break;
case 's':
if (direction != 4) direction = 2;
break;
case 'a':
if (direction != 5) direction = 3;
break;
}
}/******increase performance*****/
Thanks
Actually in the end the 2-5 range vs 0-3 range turned out to be simple to abuse (although I'm afraid it's not what you wanted).
Plus normal games allow for key redefinition, which would break this completely. So this is more like "joke" than serious answer. But your question is on the brink of "joke" too, I mean: you have more serious problems if you really believe that the thing in your post is a problem.
// I expect "direction" to be int, otherwise change wantDir vars
void changeDirection2(char key) {
// a s d w
constexpr static int wantDir[] = { ~0, 3, ~0, 2, 1, ~0, ~0, 0 };
int wantedDir = wantDir[key&7];
if (wantedDir+2 == direction) return;
direction = wantedDir;
}
Plus this will react to many more (all of them) keys than a,w,s,d. It's up to caller call only with correct ones.
version 2, without LUT (still hardcoded to "awsd" and mangling any other key into some number):
void changeDirection3(char key) {
int wantedDir = (~key>>1)&3;
if (wantedDir+2 == direction) return;
direction = wantedDir;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
To reverse and return the original queue using an auxiliary queue is easy to do, but for the stack, to reverse and return the original one using an auxiliary one is easily doable with the help of a variable?
The only way I see this working is by using recursion, but to me this is like cheating, you could as well read the data in an array and reverse it. The code is pretty simple, to copy a stack you do the same as when reversing a list recursively, but because the elements in a stack are iterated in reverse order already when popping them, you create the original stack and not a reverse.
function move(from, to) {
const v = from.pop()
if (v !== undefined) {
move(from, to)
// from.push(v) // uncomment this to make it copy instead of move
to.push(v)
}
}
function reverse(stack) {
const aux = [] // aux stack
move(stack, aux)
let v = aux.pop()
while (v !== undefined) {
stack.push(v)
v = aux.pop()
}
}
const s = [0,1,2,3]
console.log(s)
reverse(s)
console.log(s)
Or here the (very evil and potentially failing) solution without recursion and aux (in other words, the rules have to be specified more clearly):
function reverse(stack, callback) {
let delay = 10
setTimeout(callback, stack.length * 10 + 10)
let v = stack.pop()
while (v !== undefined) {
(function(v, delay){setTimeout(() => stack.push(v), delay)})(v, delay)
delay += 10
v = stack.pop()
}
}
const s = [0,1,2]
reverse(s, () => console.log(s))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Jason rings every multiple of 13 less than 500. He then crosses every multiple of 17 less than 500. How many numbers get both ringed and crossed?
It is an entrance test question with the following answers.
10
0
1
4
As per the answer sheet the answer is 4 but we don't know how to get the mentioned answer.
Please refer the following table results. As per that following table results we could not cross any thing but we can round all the 13 multiples which is 38. However we couldn't even see that answer in the list.
It would be greatly helpful if you could help us get to get that answer?
May be a silly question but did we understand the question correctly?
A simple algorithmic solution could go something like this:
void PrintMultiplesOfXandYuptoZ(int x, int y, int limit)
{
int cx = x;
int cy = y;
while (cx < limit && cy < limit)
{
if (cx == cy)
{
Console.WriteLine(cx);
cx += x;
cy += y;
}
else if (cx > cy)
{
cy += y;
}
else
{
cx += x;
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How to approach for finding solution to subsequence and subsets problems like finding the subsequence or subsets of an array which satisfy particular condition and should be solved using bits manipulation and what is the best time complexity to which it can be reduced to.
I have started practicing a lot of coding questions these days.
Need little help.
I expect some proper approach which should be followed to solve that's kind of problems.
Count from 0 to (2set.size() - 1) (inclusive). Retrieve the elements corresponding to 1 bits in the current count. The set will have to be ordered to retrieve elements by index, of course.
The only tricky part is pulling out the elements corresponding to the 1 bits in the current count. Here's pseudocode for one way to do that:
for (int pos = 0, mask = 1; mask <= currentCount; mask <<= 1; ++pos) {
if ((currentCount & mask) != 0) {
include element at pos in the current subset
}
}
Note that this assumes that the original set size is no more than the number of bits available for whatever integer type you are using for the count and mask.
Implementation in Java will look like this:
private static void findSubsets(int array[]) {
int numOfSubsets = 1 << array.length;
for (int i = 0; i < numOfSubsets; i++) {
int pos = array.length - 1;
int bitmask = i;
System.out.print("{");
while (bitmask > 0) {
if ((bitmask & 1) == 1)
System.out.print(array[pos] + ",");
bitmask >>= 1;
pos--;
}
System.out.print("}");
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've started solving Euler's problems and there is an error in Euler 23 that DOES bother me.
What's the problem?
#include <iostream>
using namespace std;
int main() {
long long res=0;
for (int answer=1;answer<=28123;answer++){
bool tri=false;
if (answer%6==0 || answer%20==0) continue;
for (int i=12;i<=answer;i++){
if (abs(answer-i)%20==0) tri=true;break;
}
if (!tri) res+=answer;
}
cout << res << endl;
return 0;
}
The logic behind my program is : every abundant number is either divisible by 20 or 6 so I see if I can writ a number with a number divisible by 6 and a number divisible by 20.At the top of code :
if (answer%6==0 || answer%20==0) continue;
I use continue if there was a number divisible by 20 or 6 I use continue.
What's the problem?
Is it because of my solution?
The intent of the line
if (abs(answer-i)%20==0) tri=true;break;
is not correctly expressed in code. It is equivalent to:
if (abs(answer-i)%20==0)
{
tri=true;
}
break;
I suspect that is not what you meant. It needs to be:
if (abs(answer-i)%20==0)
{
tri=true;
break;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I had been asked to provide the java implementation of the following pseudo code:
function
{
Input n; //can have either of the two values : 10 or 20
if n == 10
return 20
else
return 10
}
I tried following :
int function(int n){
if (n == 10){
return 20;
}
return 10;
}
Now the next one:
int function (int n){
return n == 10 ? 20 : 10;
}
Here is another one (an extreme one-liner):
int function (int n){
return 30 - n;
}
But, the question asker had some more technique (in a single line), in his mind and I wonder what that might be!
Any idea?
You could use bit-shift operators :
return n==10?n<<1:n>>1;
or multiplication/division :
return n==10?n*2:n/2;