将递归函数的返回值存储在ruby中的变量中(Storing the return value of a recursive function in a variable in ruby)
我正在研究一个问题,找到一个数组中第一对与特定目标相加的数字。 我必须返回最右边的索引首先出现的那对。 所以说目标是
10
,发现的对是[5,5]
在指数1,5
和[3,7]
在指数3,4
我必须返回[3,7]
。 我有递归部分工作,问题是它返回一个散列,最左边的索引作为键,而对作为值。 我试图从另一个函数(#sum_pairs)调用这个递归函数,这样我就可以格式化我需要的输出,但是当我这样做并尝试将它存储在变量对中时,它返回的是一个空哈希,甚至虽然它只是在我从控制台调用它时返回完成的哈希。下面是我的代码:
$starts = [0] def sum_pairs(ints, s) pairs = find_pairs(ints,s) pairs[pairs.keys.min] end def find_pairs(ints, s) sums = {} ints.each_with_index do |n, idx| next if idx < $starts.last right_idx = completes(ints, idx, n, s) if right_idx.nil? return sums if idx == (ints.length - 1) next else sums[right_idx] = [n, (s - n)] $starts << idx sums.merge!(sum_pairs(ints[0...right_idx], s)) sums end end end def completes(ints, idx, n, s ) ints.each_with_index do |num, index| next if index <= idx return index if num == s - n end nil end
我知道我可以通过迭代来做到这一点,它可能会更有效或更高效,但我正在尝试使用它来更好地理解递归,谢谢!
I'm working on a problem to find the first pair of numbers in an array that sums to a specific target. I have to return the pair which its rightmost index appears first. So say target is
10
and pairs found are[5,5]
at indices1,5
and[3,7]
at indices3,4
I'd have to return[3,7]
. I have the recursive part working, problem is it returns a hash with the leftmost index as key and the pair as value. I'm trying to call this recursive function from another function (#sum_pairs) so that I can then format the output I need, but when I do that and try to store it in the variable pairs all it returns is an empty hash, even though it returns the completed hash when I just call it from the console.Heres my code:
$starts = [0] def sum_pairs(ints, s) pairs = find_pairs(ints,s) pairs[pairs.keys.min] end def find_pairs(ints, s) sums = {} ints.each_with_index do |n, idx| next if idx < $starts.last right_idx = completes(ints, idx, n, s) if right_idx.nil? return sums if idx == (ints.length - 1) next else sums[right_idx] = [n, (s - n)] $starts << idx sums.merge!(sum_pairs(ints[0...right_idx], s)) sums end end end def completes(ints, idx, n, s ) ints.each_with_index do |num, index| next if index <= idx return index if num == s - n end nil end
I know I could do this with iteration an it would probably be more or equally efficient, but I'm trying to use this to better understand recursion, thank you!
原文:https://stackoverflow.com/questions/39419518
最满意答案
你不明白的是从IIS返回浏览器只能是一个流 。
在你的情况下,你相信你可以发送两个。
实际发生了什么?
A. 您从网页发回一些数据。
B。 您将从用户返回一些文件以供下载。
C. 流结束和关闭 - 没有任何东西进入浏览器了。 所以页面无法拒绝,并且页面上的数据无法更改,因为您不再向页面发送任何内容。
怎么解决这个问题。
a )您可以创建一个下载文件的处理程序,并且只在页面中提供一个链接,该链接指向下载数据的处理程序。
b )您可以创建一些其他页面,您可以使用您要发送的消息重定向他,并自动使用一些javascript开始下载您的文件。
c )您可以使用ajax下载您的额外数据,并使用javascript来显示任何消息
类似的答案
What you do not have understand is that the return from IIS to the browser can be one stream only.
In your case, you believe that you can send two.
What is actually happened?
A. You post back some data from the web page.
B. You return some file for download from the user.
C. There the stream end and close – nothing is going to the browser any more. So there is no way the page to refress, and there is no way the data on the page to change because you send nothing any more to the page.
How to solve this.
a) You can make a handler that download the file and you only give a link in the page, a link to the handler that download the data.
b) you can make some other page that you redirect him with the message that you like to send, and there automatically with some javascript you start downloading your file.
c) you can use ajax to download your extra data, and javascript to show any message
similar answers
What is the best way to download file from server
file download by calling .ashx page
Updating a page before initiating a download?
相关问答
更多-
实际上我最终得到了一个不需要使用iFrame的解决方案。 相反,我使用了Cookie ,文件生成过程将其简单地写入名称值对。 然后可以稍后通过JavaScript从客户端检查此cookie,以确定响应(.xlsx文件)何时完成。 结果应该是加载微调器图像应该显示,直到生成 .xls文件并返回到客户端(Cookie包含DownloadComplete = true名称值对)。 在LinkButton的OnClientClick事件中: function startFileDownload(){ // Set ...
-
你不明白的是从IIS返回浏览器只能是一个流 。 在你的情况下,你相信你可以发送两个。 实际发生了什么? A. 您从网页发回一些数据。 B。 您将从用户返回一些文件以供下载。 C. 流结束和关闭 - 没有任何东西进入浏览器了。 所以页面无法拒绝,并且页面上的数据无法更改,因为您不再向页面发送任何内容。 怎么解决这个问题。 a )您可以创建一个下载文件的处理程序,并且只在页面中提供一个链接,该链接指向下载数据的处理程序。 b )您可以创建一些其他页面,您可以使用您要发送的消息重定向他,并自动使用一些javasc ...
-
在非IsPostBack块的Page_Load中添加另一个'if'语句,用于检查查询字符串(Request.QueryString)中是否存在'PayPeriodStartDate'。 如果是 - 使用该值设置隐藏字段和标签,如果它不使用代码示例中的'test1'值。 In Page_Load in Not IsPostBack block add another 'if' statement that checks if 'PayPeriodStartDate' is present in query s ...
-
所以它是一个回发与否(so is it a postback or not)[2022-12-27]
第一个链接点击是来自服务器的PostBack和正确的行为。 听起来第二个PostBack是由于Page.Redirect()或Server.Transfer()返回到同一页面而触发的。 在您提供其他详细信息之前,这是最佳猜测。 The first link click is a PostBack from the server and the correct behavior. It sounds like the second PostBack is triggered because of a Page ... -
问题是因为应用程序驻留在具有NLB的Web服务器场中,因此空白回发是由于NBL将用户从原始连接重定向到另一个服务器。 (不同的解决方案可能会应用链接NO Cache,Connection Affinity等)希望它能在将来帮助某人。 The issue was found as the application was residing in a Web Servers Farm with NLB, therefore the blank post-back was due to the NBL redire ...
-
事实证明MasterPage中有一个UpdatePanel,当我将按钮注册为后置控制时,问题就解决了......我知道这很痛。 所以我通过在Page_Load事件中使用这行代码解决了它: ScriptManager.GetCurrent(Me).RegisterPostBackControl(btnSubmit) Turns out there was an UpdatePanel in the MasterPage, the problem was solved when I registered th ...
-
问题在于浏览器实际上发出两个请求来获取文件。 这是典型的行为。 您可以在此页面上打开OutputCaching。 这应该意味着浏览器的第二个请求不会触及您的服务器,因此只记录一个条目,但您还需要确保您的web.config文件中没有设置调试标志。 另一种选择是检测请求类型。 我认为浏览器发送的第一个请求通常是HEAD请求,而不是GET或POST。 你可以适当地检测和处理这些,但我认为第一种方法可能更可靠。 另外,你应该把它编码成一个ASPX Handler而不是一个页面。 这意味着你可以避免response ...
-
所以主要的问题是在BUTTON.click(); 事件JS集中在其他地方。 因此,当点击后ELEMENT.focus();功能不好时,请尝试ELEMENT.focus(); 在想要的元素 - 之后,元素可以在gridview中再次点击。 So main problem was that after BUTTON.click(); event JS focused somewhere else. So when someon has bad functionality after click, try ELE ...
-
在做了更多的研究后,我想我可以提供一个可能的解释,说明为什么我们会得到上面的行为。 如此处所述 ,如何设置MIME类型以及浏览器如何解释它们,通过在我的文件上指定text/palin或text/csv ,浏览器将其解释为与text/html (已显示的页面)相同并且将当前页面内容添加到文件的HTTP响应中。 需要注意的是,附加的HTML实际上并不是作为文件响应的一部分传输的,但如果浏览器认为文件与页面的内容类型相同,浏览器仍将附加它。 至于解决方案和文章描述的后续内容,为了从HTTP响应的其余部分分离出文件 ...
-
在回发后首次单击按钮时OnClientClick不会触发(OnClientClick not triggering on first click on button after postback)[2021-08-10]
如果我理解你的问题,我认为正在发生的事情是你在历史按钮点击之后发回,这不是你想要的(因为没有点击处理程序)。 使用此代码对我有用:相关文章
更多- Why Ruby
- Spark - A Fault-Tolerant Abstraction for In-Memory Cluster Computing
- ruby 全局变量
- Ruby常用的内部变量
- Python内建函数(A)
- 两种js function 声明方式
- Becoming a data scientist
- error C2668: 'M' : ambiguous call to overloaded function
- ServletOutputStream cannot be resolved to a type
- jQuery EasyUI 如何获取当前单击标签(如<a>)所在行了数据
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)