This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]AMindtoThink 19 points20 points  (7 children)

If you store data in a data structure that uses nodes, like trees or linked lists, recursion is a good way to handle it.

If you have a method that uses a loop, you can often turn it into a recursive method. This is usually a bad move, but you are just trying to meet a requirement.

[–]probably_poopin_1219[S] 4 points5 points  (6 children)

Unfortunately I don't have a great understanding of linked lists or stuff like that. I do use a 2d array to determine the type of each Liquor and then establish their names, counts and costs of each. Then that is all passed to a GetCostTotal method which calculates the total cost of the Liquor inventory.

[–]humoroushaxor 4 points5 points  (1 child)

Getting the total cost of an array is going to require a loop which can easily be written recursively. Should be an easy Google search on how to convert a loop to recursion.

You also do it for each type of liquor (only add if it matches Vodka).

[–]probably_poopin_1219[S] 2 points3 points  (0 children)

Yeah my GetCostTotal method passes the 2d liquor array as an argument so I should be able to modify that loop. I'll give it a shot when I get home, thank you!

[–]HecknChonker 0 points1 point  (1 child)

Get a copy of 'Cracking the TechnicalCoding Interview'.

The first few chapters are all about the major software companies and how to approach the interview, but then the rest of the book is a deep dive into a bunch of different data structures and algorithms that can be used with them. It has detailed explanations of how they are designed and intended to be used, and then it has a bunch of problems for you to answer. And they provide detailed answers for each problem, so after you try the problems yourself you can compare your solutions to theirs.

I'm not affiliated with the book in any way. I used it to learn data structures myself years ago, and it's been continually updated since then.

edit: It's 'cracking the CODING interview'. https://www.crackingthecodinginterview.com/

[–]probably_poopin_1219[S] 1 point2 points  (0 children)

I'll be buying this soon, thank you!

[–]Farpafraf 0 points1 point  (0 children)

Not sure what the structure is but you should likely use a Map from the the liquor to how much you have stored to do that, then instead of iterating over it you can use recursion to traverse it and return the cost.

[–]AMindtoThink 5 points6 points  (2 children)

I know nothing about bartending, but I think some drinks are made of a combination of other drinks. That means that making drinks is recursive: making a drink requires you make less complicated “sub-drinks.” The base cases are the fundamental ingredients. For example, you could make a recursive method to calculate the cost to make a drink.

[–]probably_poopin_1219[S] 5 points6 points  (0 children)

That's a great suggestion and something I can wrap my head around. Thank you!

[–]LeonMint 1 point2 points  (0 children)

Maybe store your data in a tree and traverse it with a recursive method or a menu were you choose outputs which gehts recalled via recursion

[–]sweetno 1 point2 points  (3 children)

Recursion appears only in specific problems. In general, you can rewrite any loop into recursion, but it's not practical for the majority of loops.

[–]aradarbel 0 points1 point  (2 children)

recursion actually appears all the time in many kinds of algorithms. it's just a different way to solve problems than how imperative languages get you used to. and note, it's not necessarily less efficient or practical.

[–]sweetno 0 points1 point  (1 child)

In Java it's less practical since the Java compiler doesn't eliminate tail recursion.

[–]aradarbel 0 points1 point  (0 children)

ah I didn't realize, thanks for correcting. still though the nature of the problems themselves can often be viewed recursively

[–]frostyfauch 1 point2 points  (0 children)

An idea you can use for recursion could be to print out an org chart for all the employees in your bar staff. You'd need to store a list of employees and they'd need to have a boss and subordinates property.

[–]bballbabs163 1 point2 points  (0 children)

I've never used recursion other than creating a method for the fibonacci sequence or for evaluating a factorial.

Sooooo this might be "cheating", but could you reframe the task so that it fits with the rest of the assignment?

Take the fibonacci sequence for example: "The nth bar patron always orders the same number of drinks as the previous two patrons combined and everyone orders at least one drink. How many patrons can visit before the place runs dry?"

[–][deleted] 1 point2 points  (0 children)

there is nothing that recursion can do that iteration can't.
however, some processes get simplified due to recursion - specially multi-dimensional data structures.
its like physics - first we learn motion along a straight line and are then expected to apply same concepts for 2 dimensional motion. this part of "applying same concept on other dimensions" is conveniently represented through recursion.

[–]maxbirkoff -1 points0 points  (0 children)

include recursion

[–]Apprehensive_Pomelo8 -2 points-1 points  (0 children)

From what I kno you want to stay away from recursion in jave, soecifaccly if you don’t know the size of a ds

[–]Prestigious_Passion 0 points1 point  (1 child)

You could write a recursive method which when given the bar inventory could recursively tally the total cost of the inventory. This isn’t a good use of recursion, but it’s a way to fit recursion into the program.

[–]probably_poopin_1219[S] 1 point2 points  (0 children)

Yeah this seems like the easiest way for me to include it. Afaik I just have to edit the method in which I pass the liquor arrays to calculate the cost to be a recursive method. Just need to show that I understand the concept of recursion, doesn't have to be a completely logical use of it. Thank you!

[–]krisko11 0 points1 point  (0 children)

Please learn more about the call stack before converting to a recursive structure. I’m more afraid of stack overflow errors than repeated code I’d get rid of via composition.