It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
So, I have created a program that runs in two parts. Firstly, I generate a list of 500 random numbers and store them in a text file. Secondly, I am required to find the max, min, average, etc. However, I am also supposed to find how many consecutive numbers come up. This is where my problem lies. I am not sure how to approach it.
Looking for a place to start.
Thank you
As you go along the array you will keep the list of the following things: current largest seen element, current smallest seen, total of all elements seen so far, ..., the last element seen & total count of consecutive numbers. If current number == last element seen + 1, you increase the consecutive count. There should also be a flag for the first element in the consecutive sequence, so that, say, {1, 2} counted 2 occurrences instead of one
last_seen = -1
previous_consecutive = false
for(x in numbers_list):
if x == last_seen + 1:
if not previous_consecutive:
number_of_consecutive_elements += 1
number_of_consecutive_elements += 1
previous_consecutive = true
else:
previous_consecutive = false
last_seen = x
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i need help in solving the following test question (it's not a homework or assignment)
the user can input from the console values from 1-9 and the program accepts 3 numbers one number per line.
if the user inputs the following:
3
2
4
the output should be:
1
2 2
3 3 3
1
2 2
1
2 2
3 3 3
4 4 4 4
i don't have any experience with trees, so please advise about what kind of tree is that and where can i start to accomplish the above program (i need some hints and advises that will help me to make this program)
thanks in advance.
No tree data structure is required. The general features of your program would be the following:
Read the three inputs
For each input n, use a for loop from 1 to n to print n lines using n as the output values
(The bit harder part:) On each line, you will also need to generate the required spacing. This will involve a calculation on your part based on n and the max value of n. Since n is a single digit, you won't have to take into account n taking up more characters once it's >= 10.
Define a 2D char array that will hold the digits. Start from the top middle. Use recursion.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need help thinking up a formula for finding the factors of a number:
Write a method named printFactors that accepts an integer as its
parameter and uses a fencepost loop to print the factors of that
number, separated by the word " and ". For example, the number 24's
factors should print as:
1 and 2 and 3 and 4 and 6 and 8 and 12 and 24
You may assume that the number parameter's value is greater than 0.
Please don't give me a COMPLETE program as I would like to try it out myself.
The current code I have has a for loop to control the number of "and's" appearing, however, I printed out the last number by itself since I don't want a "24 and" attached to it...
So the output looks something like this at the moment:
"1 and 2 and 3" (I haven't yet thought up the equation hence the 1,2,3...)
I'm currently thinking that the factors requires a % kind of formula right? Will I need division? I was also thinking of printing out 1 and whatever the number (in this case, 24) you are finding factors for, since 1 and the number itself are always factors of itself.
What else am I missing??
Thanks in advance!! :)
I'm currently thinking that the factors requires a % kind of formula right?
Yes.
I was also thinking of printing out 1 and whatever the number (in this case, 24) you are finding factors for, since 1 and the number itself are always factors of itself.
If you test every number from 1 to n (e.g. from 1 to 24) then 1 and the number itself don't need to be special cases (because they'll simply satisfy your ordinary "% kind of formula").
Maybe 1 is a special case because it doesn't have the word "and" in front of it.
What else am I missing??
This may be more complicated than you want, but to find all the factors of n you only need to loop up to the square root of n.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Suppose Alice has picked three distinct integers and placed them into a stack S in random order. Write a short, straightline piece of pseudo-code (with no loops or recursion) that uses only one comparison and only one variable x, yet guarantees with probability 2/3 that at the end of this code the variable x will store the largest of Alice's three integers. Argue why your method is correct.
Data Structure on Java
x = S.pop()
return max(S.pop(), x)
explanation:
I got a S stack [A, B, C]
x = S.pop() // x points to A
return max(S.pop(), x) // compares B to A
once I know that the greatest is returned from B and A there is one possibility that the greatest integer is not between those two integers (B and A) that is C
so.. I have two possibilities of getting the greaters number between three -> 2/3
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to check to see if a player can no longer make moves but i cant seem to figure out how to check for it.
Say i have 5 tiles or less and each tile has a value between 1 and 3 on it. I want to run through the tiles and check if there are any possible combinations that will add up to 10. Its not as simple as just checking the total because there could be 3 tile with 3's on them and one with a 2. I have spent hours trying to figure this out....
Any ideas on how i could check for all possible combinations? I only need to see if one combination is possible. As soon as one is found i would break the loop to reduce the number of checks.
Edit: If it helps you can kinda see what im talking about if you download it. Its Numero: 21 on the android.
The object of the game is to add tiles to 21. So when the user gets down to a few tiles, sometimes a combination of 21 cant be acheived even when the total of all the tiles is above 21 because the numbers dont add up to exactly 21. This creates a problem because i am unable to check for that and tell the user that they have lost.
I really dont even know where to begin when it comes to checking for it. i can loop through all the tiles multiple times but the thhing is a combination could be as many tiles are on the board. So i have to check for combinations of 3, then 4 , then 5 and so on until the number of tiles left is reached. Its kind of hard for me to explain this exactly
Corrective Edit For Future Reference:
I apologize for my initial question for being so ambiguous. Happened to be the first question I ever asked on here... This is a much better description of the problem and the solution. I decided to keep the previous text for record.
The game has numerous numbered tile and I you have to add them up to 21 to remove. You can not go over 21. It has to be exact.
What I was wanting to check is if there was still a combination of tiles that could be used to add up to 21 exactly. A basic sum check doesn't work because you could have 5 number 5 tiles and be more than 21 but not possible to eliminate anymore.
Solution
As #mellamokb answered subset sum recursion needed to be used. Basically you loop through the tiles and on each tile you call the same function twice. One call adds the current tile and the other continues to the next iteration without adding the current tile. If any return true then the function is true. Basically a binary tree.
Code
boolean validate(tiles, index, subtotal, total){
if index >= tiles.length return false;
if subtotal == total return true;
return validate(tiles, index + 1, subtotal + tiles[index].number, total) ||
validate(tiles, index + 1, subtotal, total);
}
Call it with
validate(tiles, 0, 0, 21)
That about does it.
This sounds like a variation of the subset sum problem. The simplest algorithm is O(2^n), iterating over every possible subset combination by using bit flags, for example.
for i = 0 to 2^n - 1
set subtotal = 0
for each bit in i
if bit i is set, add ith element to subtotal
check subtotal against desired total (i.e., 10)
Or, alternatively using recursion:
validate(set, index, subtotal, total)
if index >= set.length return false;
if subtotal == total return true;
return validate(set, index + 1, subtotal + set[index], total) ||
validate(set, index + 1, subtotal, total);
Usage:
validate(set, 0, 0, 10);
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Given an array of integers, give two integers from the array whose addition gives a number N.
This was recently covered on the ihas1337code blog. See the comments section for solutions.
Essentially the most efficient way to solve this is to put the numbers in a hash_map and then loop through the array a second time checking each element x if element (N - x) exists in the hash_map.
You can optimize a bit from there, but that is the general idea.
Follow these steps:
1.Sort the numbers using merge sort in O(n logn) in descending order(can be ascending also but for this text assumed them to be sorted in desceending order).
2.Use two pointer variables one pointing to starting element (say p1) and other pointing to last element( say p2).
3.Now add *p1 + *p2 ( temp_sum= *p1 + *p2 ) and compare it with required sum
Repeat these steps untill p1>p2
i.If sum ==temp_sum then our job is over .
ii.If sum > temp_sum then decrease p2 to make it point to a bigger value then its current value so that temp_sum can increase.
iii.If sum < temp_sum then decrease p1 to make it point to a smaller value then its current value so that temp_sum can decrease.