Advertisement Jump to content
  • Advertisement

Alatar

Member
  • Content Count

    228
  • Joined

  • Last visited

Community Reputation

303 Neutral

About Alatar

  • Rank
    Member
  1. Alatar

    Recursive Algorithm Help

    Thank you for the replies. Here is my updated program: [source lang=java] public static boolean groupExists(final List<Integer> numbers, final int target) { if (numbers == null) { return false; } else if (target == 0) { return true; } else if (numbers.size() == 0) { if (target == 0) { return true; } else { return false; } } else if (target == numbers.get(numbers.size()-1)) { return true; } else { if (target > numbers.get(numbers.size()-1)) { return groupExists(numbers.subList(0, numbers.size()-1), target - numbers.get(numbers.size()-1)); } else { if (numbers.size() > 3) { return groupExists(numbers.subList(0, numbers.size()-2), target); } else { return groupExists(numbers.subList(0, numbers.size()-1), target); } } } } [/source] It passes every single test I have written for it (about 20) except for one: assertTrue(Exercises.groupExists(list, 31)); The variable list is defined: list = java.util.Arrays.asList(2, 7, 13, 9, 11); Does anyone have any advice on how to change this so that the test does not fail? Thank you
  2. Hi everyone, I am trying to write a recurisve algoritm that, when given a list of numbers, checks to see if there is any combination of the numbers that will add up to a target value. I'm attempting to write it using tail recursion. The method works occasionally, but does not work every time. My problem is if I have a list {9, 7, 2, 5, 1} and the target is 11, obviously 2 and 9 will add together to become 11. BUT the way I have it written, it will add 1 + 5 (making 6). The next iteration will add the 2, making 8. After that, it will see the 7 and 9 and decide that the group does not exist. I do not see how to code it so that it can detect the 2 and 9 combination. Did that make sense? [source lang=java] public static boolean groupExists(final List<Integer> numbers, final int target) { if (numbers == null || numbers.isEmpty()) { return false; } else if (target == 0) { return true; } else if (numbers.size() == 1) { return (numbers.get(0) == target); } else if (target == numbers.get(numbers.size()-1)) { return true; } else { if (target > numbers.get(numbers.size()-1)) { return groupExists(numbers.subList(0, numbers.size()-1), target - (numbers.size()-1)); } else { return groupExists(numbers.subList(0, numbers.size()-1), target); } } } [/source] I do not see how to get the method to check ALL possible combinations. Can anyone give me some advice? Any help will be appreciated. Thank you.
  3. Thank you very much for your help. I was able to finally get it working with your pushes in the right direction.
  4. Thank you for the help you have given me so far. I really do appreciate it. I almost have it figured out, I am just running into an issue with the output. It runs through properly when there are two animals in the tree (say, lion and tiger). The problem is that once I try to add a third, it seems to forget about the others, and basically starts over. Here is what I have now: [source lang=java] public class Tree { public static void main(String[] args) { USERINTERFACE userInterface = new USERINTERFACE(); DTNode root = new TNode("Daisy"); do { root = traverse(root); } while (userInterface.confirmation("Would you like to play again?")); userInterface.outputInfo("Thank you for playing and helping me learn!"); } public static DTNode traverse(DTNode current) { if (current instanceof TNode) { if (userInterface.confirmation("Is it a " + current.getString() + "?")) { userInterface.outputInfo("I guessed it!"); return current; } else { String thing = userInterface.promptUser("I give up!\nWhat kind of flower were you thinking of?"); TNode tNode = new TNode(thing); String ques = userInterface.promptUser("Please enter a yes/no question that queses between a " + current.getString() + " and a " + tNode.getThing()); QNode qNode = null; if (userInterface.confirmation("What is the answer for a " + tNode.getThing() + "?")) { qNode = new QNode(ques, current, tNode); } else { qNode = new QNode(ques, tNode, current); } current = qNode; return traverse(current); } } else { if (userInterface.confirmation(current.getString())) { current = current.rLink(); } else { current = current.lLink(); } return traverse(current); } } } [/source] Can you help me narrow down where my new bug is? Again, thank you very much for all the help you have given me so far.
  5. Thank you for your reply. Parent Node -> Old Thing Node (which isn't what the user is thinking of) ...into this... New Question Node -> no -> Old Thing Node New Question Node -> yes -> New Thing Node [/quote] I thought that's what I was doing when I said QuestionNode qNode = new QuestionNode(distinguish, current, tNode) because the QuestionNode's constructor is (String question, DecisionTreeNode no, DecisionTreeNode yes). I take it that's not really what my code is doing?
  6. Well, I never actually said I wanted you to do it for me now did I? I said any advice on where to go from here would be appreciated. This is a forum to get help and pointers. If you would care to reread my post without getting high and mighty you may see that. Now as for my comment that you seem to think is sugar...I have not logged in and actively browsed the forums in quite a long time, but I recall a time when phrases like this showed up in quite a few posts. I meant no "sugar" from it. It was no bribe, just a way of showing respect to the people who would courteously help me. Perhaps that has died in the time since I last used this site. Again, not asking for the answer here, just some pointers. Offering to rate an answer is also not sucking up to the people who may try to help. If they do help, they should be appreciated for it. That's the reason there is a reputation scale here. If you would care to hover your cursor over those rating arrows, you would see that it says "this post provides useful information to the conversation and demonstrates knowledge of the subject matter." The other one says "this response is not useful and does not improve the conversation." Guess where yours falls? Did it ever cross your mind that I am trying to learn here? I am struggling with this assignment, and I turned to what I remember as being a helpful community. There comes a point when you need a pointer in the right direction, and I'm not getting that from my professor. Yes, I get that I am supposed to figure it out, and I am trying. BUT...there is absolutely nothing wrong with asking for advice from more experienced people. So thank you for your reply, but maybe you should pay more attention to what you read next time before you respond.
  7. Hi everyone, It has been years since I have actually tried to write any kind of somewhat complex program, and now that I'm back in school I;m finding those to be kind of frustrating. I have been assigned to write a guessing and learning program using a custom binary tree. The root of the tree is the very first guess. If the guess is incorrect, the program prompts the user to enter the correct thing and a yes/no question to ques between the two. Then it rotates the nodes so that the question is first, the yes branch has the correct answer, and the no branch originally is the wrong guess, but after the next cycle becomes the new quesing question. Hope that made sense. After it has this new information, it asks if the user wants to start over. If they do, it begins from the top, but this time the root is the first distinuserInterfacesing question. The way we have to set it up, we have a DTNode class, that then has two subclasses, TNode and QNode. Example run (with original root of TNode("Tiger"): Game asks: Is it a tiger? User enters: no. Game asks: What were you thinking of? User enters: lion Game asks: Please enter a yes/no question to ques between a tiger and a lion User enters: does it have a mane? Game asks: Would you like to play again? User says: yes Game asks: Does it have a mane? User says: yes Game asks: is it a lion? and so on. Hopefully that is clear enough. I have the Node classes all written and working, but my problem is in the implementation of the tree. I was trying to write a runner program to run a sample of the game and this is what I came up with so far. I am totally lost on where to go from here. [source lang="java"]import java.util.*; public class Tree { public static void main(String[] args) { USERINTERFACE userInterface = new USERINTERFACE(); TNode root = new TNode("Tiger"); do { traverse(root); } while (userInterface.confirmation("Would you like to play again?")); userInterface.outputInfo("Thank you for playing and helping me learn!"); } public static void traverse(DTNode current) { if (userInterface.confirmation("Is it a " + current.getString() + "?")) { userInterface.outputInfo("I guessed it!"); } else { String thing = userInterface.promptUser("I give up!\nWhat kind of animal were you thinking of?"); TNode tNode = new TNode(thing); String ques = userInterface.promptUser("Please enter a yes/no question that ques between a " + current.getString() + " and a " + tNode.getThing()); QNode qNode = new QNode(ques, current, tNode); current = qNode; } } } [/source] I have stared at it for hours and tried many things, and cannot for the life of me figure out where to go from here. I have spoken with my professor, and he has not helped much. Any advice would be greatly appreciated. I am on a time crunch, and helpful posts will be rated as such. Thank you mighty programmers of gamedev.net
  8. Alatar

    More Boolean Algebra

    Thanks. That helped a lot.
  9. Alatar

    More Boolean Algebra

    I'm still trying to understand how to simplify Boolean algebra expressions. Can someone help me understand how to simplify these pelase? 1. A.B + B.!C + !A.!C 2. D + !B.!C + C.!D + !A.!B.C Thanks. I would appreciate any help.
  10. Hi everyone, Does anyone have a link to a good intro to boolean algebra? I'm trying to do my homework, and my notes and the professor's powerpoints don't work. Our textbook is no help either. I'm trying to solve problems like: (A+B).(A+ !B) = A A.(A + B) = A A.(A +!A.B) = A A + A . B = A+B We are expected to prove them analytically using Boolean algebra laws. The ones with ! are supposed to have a bar above them according to my professor, but the ! (not) means the same thing. If someone has a good link, or could help me with this, I would really appreciate it. Thanks
  11. Alatar

    IEEE754

    Ok. Thanks for your help with this.
  12. Alatar

    IEEE754

    Thanks. So... it would be -1.001010000011011*2^1110... 1 10001101 0010100000110110...0? Is that closer?
  13. Alatar

    IEEE754

    Hi everyone, It's been forever since I've logged in, how is everyone? Anyway, I'm taking a computer architecture class and our professor does not speak English very well at all, and he does not know how to teach the material. I'm having trouble with presenting numbers as single-precision floating point numbers. I can do it ok when it's a positive number, but when it's a negative I have trouble. I'm trying to represent -4A0D.8 in this format. This is what I have: 4A0D.8 = 0100101000001101.1000 -4A0D.8 = 1011010111110010.0111 + 1 (using two's complement) = 1011010111110011.0111 So: the sign is 1 because it is a negative the exponent is 15, or 1111, which gets converted to Excess 127 (0111111 + 00001111 = 10001110) so it becomes 1.0110101111100110111, with 1 as the sign and 10001110 as the exponent. 1 10001110 01101011111001101110...0 (last section has 23 digits) Is this correct? Can anyone help me please?
  14. Alatar

    What do you listen to while programming?

    I generally listen to a mix of Breaking Benjamin, Evanescence, Linkin Park and 3 Doors Down. Sometimes I'll branch out and listen to game or movie soundtracks.
  15. Alatar

    Rotating a Rectangle

    In your create functions, you're rotating the rectangle, then setting the corner positions. Move the rotate call below where you set the corner positions.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!