Ruby string and array maximization problems, ruby Problems

Source: Internet
Author: User

Ruby string and array maximization problems, ruby Problems

Max Method

b=[1,3,55,777,2,4,6,8,0]

For numeric data, max obtains the maximum value of the array, and min obtains the minimum value of the array.

b.max => 777
b.min => 0

It has no practical significance for comparing the size of string arrays,
The example in ruby is

# enum.max         -> obj
# enum.max { |a, b| block } -> obj
#a = %w(albatross dog horse)
#a.max          => "horse"
# a.max { |a, b| a.length <=> b.length } #=> "albatross"

Lab in practice

d=%w(albatross dog horse ddd dasgfds)
d.max => "horse"

A little strange. What's going on?
Therefore, a relatively intuitive numeric string is printed.

a = ["1", "3", "55", "777", "2", "4", "6", "8", "0"]
def max(first,*rest) 
 max=first 
 rest.each do |x|
  p "---#{x}---#{max}"
  max=x if x>max 
  p "---#{x}---#{max}"
  max 
end

Run
Copy codeThe Code is as follows: a. max
Result:

"---3---1"
"---3---3"
"---55---3"
"---55---55"
"---777---55"
"---777---777"
"---2---777"
"---2---777"
"---4---777"
"---4---777"
"---6---777"
"---6---777"
"---8---777"
"---8---8"
"---0---8"
"---0---8"
 => "8"

The problem arises. "8"> "777" => true, test again.

"7" > "777" => false
"6" > "777" => false
"9" > "777" => true
"10" > "777" => false
"11" > "777" => false
"70" > "777" => false
"80" > "777" => true

This crashes, and the rule is not friendly, because the max method is used to compare the size, which is previously considered to be determined by ASCII encoding, this problem was found today when I looked at the sort Sorting Problem. In fact, this is the maximum value obtained by the dictionary sorting method.

Next let's take a look at the sort in Ruby:

Sort sorting method

ary.sort          -> new_ary
ary.sort { |a, b| block } -> new_ary
enum.sort_by { |obj| block }  -> array
enum.sort_by          -> an_enumerator

Instance

h=['1','3','13','10','7']
 A. p h.sort
 B. p h.sort{|a,b| a.to_i <=> b.to_i}
 C. p h.sort_by{|x| x.to_i}

The output is as follows:

A. ["1", "10", "13", "3", "7"] 
B. ["1", "3", "7", "10", "13"] 
C. ["1", "3", "7", "10", "13"]

1. When no block is specified, the default sort of sort is ordered by the dictionary.
2. If a block is specified, sorting the values is the size of the value of the block-making method.
3. The sort_by method sorts the results generated by the code that executes the block. Each element is called only once, which is faster than B.

Articles you may be interested in:
  • Various sorting algorithms implemented by Ruby
  • Merge Sorting Algorithm implemented by Ruby
  • Three quick sorting algorithms implemented by Ruby
  • Quick sorting by Ruby code line
  • Insert sorting and Bubble sorting algorithms implemented by ruby
  • Longest Common subsequence algorithm implemented by Ruby
  • Ruby's shortest editing Distance Calculation Method
  • Ruby-implemented optimal Binary Search Tree Algorithm
  • Ruby's simplest message server code


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.