排序的字符串,可以包含一个时间或距离 [英] Sorting a string that could contain either a time or distance

查看:114
本文介绍了排序的字符串,可以包含一个时间或距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现为重新presents时间或距离的数据跟踪及自定义字符串排序算法;田赛。下面是格式

'10:03.00 - 要么十分钟,三秒10英尺三寸

排序的结果是,田赛,最长的抛跳,或将是第一个元素,而运行的事件,在最快的时间将是第一个。下面是我目前正在使用的田赛的code。我没有贴 running_event_sort 既然是同样的逻辑与大于/小于交换。虽然它的工作原理,它只是似乎过于复杂,需要进行重构。我愿意接受建议。任何帮助将是巨大的。

  event_participants.sort {|!A,B | Participant.field_event_sort(A,B)}

班学员
高清self.field_event_sort(A,B)
  a_parts = a.time_distance.scan(/'([\ D] *):([\ D] *)([\ D] *)/)
  b_parts = b.time_distance.scan(/'([\ D] *):([\ D] *)([\ D] *)/)

  如果(a_parts.empty?|| b_parts.empty?)
    0
  ELSIF a_parts [0] [0] == b_parts [0] [0]
    如果a_parts [0] [1] == b_parts [0] [1]
      如果a_parts [0] [2]≥ b_parts [0] [2]
        -1
      ELSIF a_parts [0] [2]所述; b_parts [0] [2]
        1
      其他
        0
      结束
    ELSIF a_parts [0] [1]≥ b_parts [0] [1]
      -1
    其他
      1
    结束
  ELSIF a_parts [0] [0]≥ b_parts [0] [0]
    -1
  其他
    1
  结束
结束
结束
 

解决方案

这是一种情况, #sort_by 可以极大地简化您的code:

  event_participants = event_participants.sort_by做| S |
如果s =〜/'(\d+):(\d+)\.(\d+)/
[$ 1,$ 2,$ 3 .MAP {|位| digits.to_i}
	其他
[]
	结束
end.reverse
 

下面,我解析相关时间为一个整数数组,并用这些作为排序键为数据。数组比较完成后由入口进入,第一个是最显著,所以这个效果很好。

有一件事你没有做的是转换数字为整数,您很可能希望做的事情。否则,你必须与100和其中的问题; 2#=>真正的。这就是为什么我添加了 #map 一步。

此外,在您的正则表达式中,方括号 \ D 是不必要的,虽然你想逃避的期限,因此它不会匹配所有的字符。

的一种方式code我给了不符合code你给是在一个行不包含任何距离的情况。您的code将比较他们为等于周边线(可能让你陷入麻烦,如果排序算法假定平等是传递的。这就是 A == b B ==ç意味着 A ==ç,这是不是你的code的情况:例如 A ='10:00.1 B =蛙 C =' 9:99:9。

#sort_by 按升序排序,所以调用 #reverse 将其更改为降序排列。 #sort_by 也有只解析出的比较值一次,而你的算法将要分析每一行的每个比较的优势。

I have implemented a sorting algorithm for a custom string that represents either time or distance data for track & field events. Below is the format

'10:03.00 - Either ten minutes and three seconds or 10 feet, three inches

The result of the sort is that for field events, the longest throw or jump would be the first element while for running events, the fastest time would be first. Below is the code I am currently using for field events. I didn't post the running_event_sort since it is the same logic with the greater than/less than swapped. While it works, it just seems overly complex and needs to be refactored. I am open to suggestions. Any help would be great.

event_participants.sort!{ |a, b| Participant.field_event_sort(a, b) }

class Participant
def self.field_event_sort(a, b)
  a_parts = a.time_distance.scan(/'([\d]*):([\d]*).([\d]*)/)
  b_parts = b.time_distance.scan(/'([\d]*):([\d]*).([\d]*)/)

  if(a_parts.empty? || b_parts.empty?)
    0
  elsif a_parts[0][0] == b_parts[0][0]
    if a_parts[0][1] == b_parts[0][1]
      if a_parts[0][2] > b_parts[0][2]
        -1
      elsif a_parts[0][2] < b_parts[0][2]
        1
      else
        0
      end
    elsif a_parts[0][1] > b_parts[0][1]
      -1
    else
      1
    end  
  elsif a_parts[0][0] > b_parts[0][0] 
    -1
  else
    1
  end
end
end

解决方案

This is a situation where #sort_by could simplify your code enormously:

event_participants = event_participants.sort_by do |s|
	if s =~ /'(\d+):(\d+)\.(\d+)/
		[ $1, $2, $3 ].map { |digits| digits.to_i } 
	else
		[]
	end
end.reverse

Here, I parse the relevant times into an array of integers, and use those as a sorting key for the data. Array comparisons are done entry by entry, with the first being the most significant, so this works well.

One thing you don't do is convert the digits to integers, which you most likely want to do. Otherwise, you'll have issues with "100" < "2" #=> true. This is why I added the #map step.

Also, in your regex, the square brackets around \d are unnecessary, though you do want to escape the period so it doesn't match all characters.

One way the code I gave doesn't match the code you gave is in the situation where a line doesn't contain any distances. Your code will compare them as equal to surrounding lines (which may get you into trouble if the sorting algorithm assumes equality is transitive. That is a == b, b == c implies a ==c, which is not the case for your code : for example a = "'10:00.1", b = "frog", c="'9:99:9").

#sort_by sorts in ascending order, so the call to #reverse will change it into descending order. #sort_by also has the advantage of only parsing out the comparison values once, whereas your algorithm will have to parse each line for every comparison.

这篇关于排序的字符串,可以包含一个时间或距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆