Get calculations from a JAVA hashset [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have java hashset like this
Java Hashset
7327896542976267 B ON 17 1381960719243
4116229919133022 B ON 7 1381960779618
2467710535609327 T OFF 5 1381960865253
7327896542976267 B OFF 16 1381961008020
7690519232957130 T ON 1 1381961124863
7690519232957130 T OFF 1 1381961364863
4144171091392103 F ON 1 1381961433257
2332355946816642 B ON 2 1381961601782
4270931484327447 B ON 10 1381961881239
6048000070479064 B ON 3 1381961959977
4270931484327447 B OFF 10 1381962121239
6056389598100654 T ON 13 1381962380866
5270503505599671 B ON 8 1381962381419
6048000070479064 B OFF 2 1381962588710
6056389598100654 T OFF 13 1381962620866
3873532220632004 T ON 1 1381962807491
3873532220632004 T OFF 1 1381963047491
3495435380122245 B ON 10 1381963314807
3495435380122245 B OFF 10 1381963554807 ................
Explanation -
This is information about passenger travelling of a public transport system,
3495435380122245(card number) B(transport type) ON(start travelling) 10 (travelling zone) 1381963314807 (time)
3495435380122245(card number) B(transport type) OFF(finish travelling) 15 (travelling zone) 1381963314807 (time)
QUESTION
This hashset has multiple entries for each passenger card number,
As a example, in above explanation that person travel 5 zones. How can I calculate this for each passenger? And how can I make a new hashset with new calculated values?

Working with objects will make your life much easier. For example a Trip object can hold information like card number, transport type, start time, finish time, start zone, finish zone, zone difference etc.
It will take one pass to your hashset to create another collection of such Trip objects. This collection can be a map (with card number as keys and Trip as values) if you want to search trips using card numbers later.

You need to sort the entries by time and replay them to build a state for each customer. With each event you add any charges incurred and at the end you know how much each customer's fee were.
BTW You can't sort a HashSet but you can sort an array or ArrayList.

Related

How to find the position of single bit set in a long very fast [closed]

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 5 years ago.
Improve this question
I have a long with a single bit set and I need to know which it is, e.g. the index. I need to calculate this as fast as possible.
The naive idea is to divide by 2 and check whether the result is 1. But this would need up to 63 iterations (worst case).
My next idea was to make like a binary search, e.g. to look wether it is bit 63-32 or 31-0 then 63 - 48, 47 - 31, 31 - 16, 15 - 0 and so on having many if-else statements, but this gives me hell of a bunch of code...
Furthermore I'd like to minimize object creation and memory used. You might suggest that I'm wrong then with Java and should use perhaps C/C++. Well it's for a school competition and I don't have a choice :)
I'd like to see some sample code!
Use Long.numberOfTrailingZeros - this will be exactly the index you are looking for.
Long.numberOfLeadingZeros can be also useful if you count bits starting from the highest one.
Both methods are JVM intrinsics, i.e. they are treated specially by JIT compiler. These methods are translated to a special CPU instruction (TZCNT / LZCNT) and thus are very efficient.
You could prepare a Map<Int, Int>, holding the number of the set bit for each possible value, but I'm not sure if it is really faster than a loop.
Maybe bit shifting is faster than dividing by 2.

Algorithm for finding a single number to represent two different numerical scales? [closed]

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'm looking for an algorithm to combine two values;
Where the first value indicates a positive result when the value is higher. For example, 0.98 is 'good' and 0.15 is 'bad'.
Where the second value indicates a positive result when the value is lower. For example, 10,000 is 'bad', whereas 1000 is 'good'.
I need a method of determining a value that can represent both of these scales with one number, so that I can sort my findings on my application from high to low accordingly. I'm not sure if anyone knows of such an algorithm, or any advice, but any help is greatly appreciated. Thank you.
P.S. I am aware I can 'negate' one of the two values, to have them appear on a similar scale, however I'm not sure how this would work in Java.
EDIT: Sorry, so to elaborate, I'm sorting images based on similarity to a user input image. Each of my algorithms that I'm using to return a value of similarity, function on a different scale. The first being a value between 0.00 and 1.00, with numbers being closer to 1.00, indicating the image is more similar to the original. Whereas, my second algorithm returns values from 1000+, with higher values indicating the image is less similar to the original. I need to take these two values and combine them to allow me to sort the resulting images in order of similarity, with the most similar image being shown at the top of my list, and the least similar at the bottom. Hopefully this helps clear up any confusion. Thanks again.
If your only goal is sorting, you need to come up with a function g(x,y) that represents the "goodness" of your pair of values. A pair (x1,y1) is better than (x2,y2) if and only if g(x1,y1) > g(x2,y2).
The function must represent what you consider "good". A simple example would be:
g(x,y) = x - y / 10000

calculate the prices for Holiday depends on adults and children [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have asked to build booking holiday system and I have faced a problem with calculating the prices. I do not know how to find the price per room per either adult or child. The system requirements are as the following:
The system allows the user to enter the number of adults and/or children going on the holiday with children charged at half price. the cost per night per adult is 50. what is the equation to find the prices?
For example, if two adults with two children books two rooms for two nights?
So the user enters the number of adults and children into the system. Use variables A and C to represent the two respectively. If it is $50 per adult and $25 per child, you end up with the equation total cost = (50A + 25C)*number of nights.
So if two adults and two children book for two nights you get total cost = (50*2+25*2)*2 nights = $300.

Bayesian networks implementation in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Bayesian network: Please am currently doing a project on bayesian networks in java and am stuck on how to calculate p(a|b) because from a questionnaire, i only have the values of p(a), p(b). Please anyone with experience in this field can help me out. websites that i checked required that i have the value of p(anb). How do i calculate p(anb) in this case since the probabilities if a, b were calculated from a questionnaire Please anybody help me out and i have to calculate this p(a|b) for several variables. I would really appreciate if anyone helps me out. Thanks
Bayes' theorem states that the probability of A given B, P(A|B), is equal to the probability of both A and B occurring, P(A∩B), divided by the probability that B occurs, P(B).
That is,
p(A|B) = P(A∩B) / P(B)
Now, P(A∩B) is equal to the probability of B given A, P(B|A), times the probability of A, P(A).
That is,
P(A∩B) = P(B|A) x P(A)
In your experiments, P(B|A) should already be known from your sample. That is, of the total population of A, how many samples correspond to B.
This would give the final formula,
p(A|B) = [P(B|A) x P(A)] / P(B)

Which is better searching in string or array [closed]

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 8 years ago.
Improve this question
I need to store certain id's , and check if one exists there.
Either i can use concatenated string or array/List, which of them is a better and faster way.
This is how actually data is organized :
Year 1
Month 1
Day 1
Day 2
Day 3
Month 2
Day 6
Day 2
Day 3
Year 2
Month 3
Day 1
Day 3
Day 7
Month 6
Day 6
Day 2
Day 3
I would definitely use a collection of some form. If you only care about containment, you should use a Set<String> of some kind (e.g. HashSet<String> or LinkedHashSet<String>, which will both give O(1) complexity unless you have a significant number of hash collisions) but for goodness' sake don't use a concatenated string.
Your data isn't naturally a concatenated string - it's a collection of strings. Always keep your data in the most natural representation unless you have really good evidence that some alternative form (such as a single string) will bring you a meaningful benefit. Keeping your data in a natural representation almost always leads to clearer code which is easier to work with - and easier to optimize later, when you've found where the real bottlenecks are.
Create a HashSet a use contains method. String or ArrayList will have O(n) complexity where as HashSet will be O(1) complexity.

Categories

Resources