- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我需要根据相关元素对数组重新排序。如果一个元素是一个数组(具有依赖性),则该数组的第二个元素是依赖性,并且需要位于该数组之前。然后删除所有依赖信息,因为我们不再需要了,我们返回一个顺序正确的数组。
# array1 = [['a'], ['b','c'], ['c','a']]
# ordered = ['a', 'c', 'b']
# logic: c comes before b, a comes before c
这是我认为过度设计的方法:
array1.each_with_index do |ar, i|
# ignore elements without dependencies
if ar.count > 1
# get dependency
dep = ar[1]
# get index for element where this dependency is first
dep_index = array1.index { |a| a.first == dep }
# remove found dependency and store
dep_element = array1.delete_at(dep_index)
# insert found dependency to before current element
array1.insert(i, dep_element)
# delete processed dependency
ar.delete(dep)
end
end
上面的明显问题是,当我遍历数组时,具有我尚未处理的依赖项的元素将被移回,但循环只会执行一次。所以,我引入了一个while
:
while array1.flatten.count > array1.count
但是,我的结果是 ['c', 'a', 'b']
。
我还负责测试自引用和循环(无限)依赖循环。我应该使用枚举器吗?我是否应该将数组转换为不同的结构(对象)以便更轻松地管理顺序?
最佳答案
查看 TSort ,它与 Ruby 标准库一起提供。
它执行拓扑排序,这听起来正是您所需要的。使用上面的示例:
require 'tsort'
class Hash
include TSort
alias tsort_each_node each_key
def tsort_each_child(node, &block)
fetch(node).each(&block)
end
end
def deps arr
arr.map { |head, *tail| {head => tail} }.reduce(&:merge).tsort
end
deps [['a'], ['b','c'], ['c','a']]
#=> ['a', 'c', 'b']
关于ruby - 严重卡在 Ruby 数组重新排序上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16862843/
目前我正在调试我们的应用程序一个看似随机的错误。我将尝试提供尽可能多的细节。基本上过了一会儿,我们服务器上的应用程序的一个应用程序池停止了。我必须启动它,或者有时,重新启动 Coldfusion 或更
在寻找其他东西时,完全出于巧合,我偶然发现了一些关于案例类继承是多么邪恶的评论。有一个叫做 ProductN 的东西,可怜虫和国王, Sprite 和巫师,以及案例类继承如何丢失某种非常理想的属性。那
我正在从服务器下载媒体内容,并且应用程序在一定时间后崩溃,并给出以下错误: 收到内存压力通知(严重) 当我在 didReceiveMemoryWarning 函数中收到警告时删除了所有对象,但没有运气
为什么我会遇到这个错误 我开发了 Jax-Rs Web 服务 Jersey、Maven、Hibernate 和 Spring。如何解决该错误? Aug 03, 2015 2:23:17 PM com.
我是 Perl 新手。我收到此错误: CRITICAL : "file does not exist" 当我尝试运行脚本来通过 Nagios 监视 httpd 错误日志时。 #!/bin/sh # C
我最近一直在努力在 Kivy 中创建一个填充的 Circle,当窗口的大小调整为不同的宽度或高度时,它仍然是一个圆。我在这里看了问题: Centering an object in Kivy 但是当我
我想用实验编程语言实现的主要目的之一是:发生错误(语法,名称,类型等)时,无论程序有多严重或破坏性如何,都可以保持程序运行。 我知道这可能非常糟糕,但是我只想要一些不会在每个错误中都消失的东西-,我发
我使用 NetBeans 来制作我的 Java 项目。在我的数据库表或 DTO 中,它们都有一个名为“paymentDate ”的列。我不明白为什么它会发送此错误。奇怪的是,当我将“PAYMENTDA
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我正在使用Grails 1.3.4,后者使用了Hibernate,并且针对Oracle 11g数据库。我的应用程序启动失败,出现以下错误: 2010-11-04 09:45:34,671 INFO
自从我执行了 pip install google-api-python-client 后,我的 Gunicorn worker 在超时后停止了。 Django==1.5.3 unicorn ==
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Connecting to SQLite Database Fails 我是 Java 初学者 当我想从 SQLit
在我的实体类中,我有以下内容,调用一个返回 sys_refcursor 的 oracle 函数 @Entity @javax.persistence.NamedNativeQuery(
我们在生产环境中遇到了这个问题,但无法追踪。技术:JSP/Servlets,Jetty 服务器版本:jetty-distribution-8.1.15, 以下是异常(exception)情况,我们在
我有一个基于网络的应用程序,每三秒我连接到一个数据库并从 GPS 设备查询数据。 不幸的是,当应用程序工作超过几个小时时我无缘无故地得到了异常:“严重:没有找到适合jdbc的驱动程序......”。应
之前我的 tomcat 服务器运行良好。 但突然出现以下错误。 请帮忙解决这个问题 SEVERE: A child container failed during start java.util.co
请不要标记为重复,因为其他解决方案还没有帮助! 我在尝试运行本教程时在浏览器中收到 404: http://www.vogella.com/articles/REST/article.html 实际上
使用 Spring Tool Suite 3.6.3,我将以下内容添加到 Maven Web 项目的 POM.XML 中。 com.MyCompany.sei MyCompany-a
我在 tomcat 上部署了 RESTful Web 服务'该 Web 服务在同一域场景的客户端/服务器中工作正常'但我需要使 Web 服务可从跨域使用'为此我使用了 CORS 过滤器. 我在 myP
我第一次尝试使用 ServletContextListener 在每次部署应用程序时执行特定函数。为此,我采用了一个简单的 java 类文件并在其上实现了 ServletContextListener
我是一名优秀的程序员,十分优秀!