recursion

Tips on finding the volume of water in a 3d chess board

北城以北 提交于 2020-01-01 03:22:12
问题 So I have an assignment where I have to recreate a 3d chessboard that is a RxC grid of squares each being a different height. If the chessboard is water tight, and someone pours water all over it until it can hold no more water, it will hold a fixed amount of water. If the board is already holding its maximum volume of water, any excess water poured onto the board will drain off the edges, there is no tall container surrounding the board. You can assume the squares on the chess board are one

Print all unique combination of factors of a given number

守給你的承諾、 提交于 2020-01-01 02:25:29
问题 What is the most efficient algorithm to print all unique combinations of factors of a positive integer. For example if the given number is 24 then the output should be 24*1 12*2 8*3 6*4 6*2*2 4*3*2 3*2*2*2 Here notice that when 6*4 gets printed then 4*6 doesn't get printed. So basically it's a problem of taking unique subsets without considering the order (one way to look at the problem). But the objective is to have a function that runs the fastest, so storing the factors in a data structure

Linq extension method, how to find child in collection recursive

佐手、 提交于 2020-01-01 02:17:13
问题 I'm already familiar with Linq but have little understanding of extension methods I'm hoping someone can help me out. So I have this hierarchical collection pseudo code ie: class Product prop name prop type prop id prop List<Product> children And I have a list of products List products. Is there any way I can look for product in this collection by the id with a extension method ? In other words I need one item somewhere within the hierarchy. 回答1: Here is a generic solution that will short

What are reasonable ways to improve solving recursive problems?

余生长醉 提交于 2020-01-01 01:56:07
问题 I like solving algorithm problems on TopCoder site. I can implement most of the basic recursive problems such as backtracking, dfs... However, whenever I encounter a complex recursion, it often takes me hours and hours. And when I check the solution of other coders, I feel so shame on myself. I've been programming for almost 5 years. I can see the significant improvement on other programming technique such as manipulating string, graphics, GUI ... but not recursion? Can anyone share some

recursive JSON.stringify implementation

自作多情 提交于 2020-01-01 00:22:26
问题 I am trying to learn recursion in Javascript, so I figured I'd rewrite the native JSON.stringify function using recursion as a challenge to myself. I almost got my code to work: var my_stringify = function(obj){ value = obj[ Object.keys(obj)[0] ]; index = Object.keys(obj)[0]; delete obj[ Object.keys(obj)[0] ]; // The value is just a simple string, not a nested object if (typeof value === 'string'){ if (Object.keys(obj).length !== 0){ // Continue recursion .. return '"' + index + '":"' + value

Find all files in directory with string or pattern in filename with PHP

六眼飞鱼酱① 提交于 2019-12-31 18:54:24
问题 I'm trying to list out files in a directory (recursive or non) with PHP where filename matches a certain pattern. I've never been too great with regex so any help you could offer would be great. I could search for a literal check on the filenames returned, but I think that's not such a great idea :) Update + Final Solution: 1/18/2011 @ 8:06 PM I found another way to do what I was looking for once I understood regex a bit more. Granted I was entirely frustrated about where I was with regex, I

Should I use recursion or memoization for an algorithm?

安稳与你 提交于 2019-12-31 13:14:52
问题 If I have a choice to use recursion or memoization to solve a problem which should I use? In other words if they are both viable solutions in that they give the correct output and can be reasonably expressed in the code I'm using, when would I use one over the other? 回答1: I pick memoization because it's usually possible to access more heap memory than stack memory. That is, if your algorithm is run on a lot of data, in most languages you'll run out of stack space recursing before you run out

Haskell filtering a nested list with specific data constructors

拜拜、爱过 提交于 2019-12-31 07:43:52
问题 Suppose I have the data type data Joke = Funny String | Lame String and say I have the following nested list [[Funny "Haha", Lame "boo"], [Funny "Haha"], [Lame "BOO"]] How would I go about filtering such a nested list so that any list within the nested list that contains Funny "Haha" is removed? In other words, I'm trying to filter the list so that I receive the following result: [[Lame "BOO"]] Any list that contains Funny "Haha" is removed. Would appreciate any help, I'm having a terrible

Recursion: Behind the Scenes [closed]

别来无恙 提交于 2019-12-31 07:40:09
问题 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 6 years ago . While it is common knowledge that recursion is "a method that calls itself", I am inclined to wonder what is actually happening. Take the classic factorial example: public static int fact(int n) { if(n == 0) return 1; else return n * fact(n - 1); } fact(5); I understand that it goes a little

Python palindrome program not working

放肆的年华 提交于 2019-12-31 07:00:49
问题 I've written a simple program in python which checks if the sentence is palindrome. But I can't figure out why isn't it working. The results is always False. Does anyone knows what's wrong? def isPalindrome(word): # Removes all spaces, and lowercase the word. word = word.strip().lower() word = word.replace(" ", "") # If the length of the word is less than 1, means its a palindrome if (len(word) <= 1): return True # Compares the first and the last character of the word. # If it is the same,