Discussion on Ruby string and array maximization

Time:2022-1-8

Max method

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

For numerical data, Max will get the maximum value of the array and min will get the minimum value of the array

?
1
2
b.max => 777
b.min => 0

Comparing the size of string arrays has no practical significance,
The example given in ruby is

?
1
2
3
4
5
# 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"

Experiments in practice

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

It’s a little strange. What’s going on
For this purpose, a more intuitive numeric string is printed

?
1
2
3
4
5
6
7
8
9
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

function

Copy codeThe code is as follows:
a.max

The result is

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"---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"

If the problem arises, “8 > 777” = > true, test again

?
1
2
3
4
5
6
7
"7" > "777" => false
"6" > "777" => false
"9" > "777" => true
"10" > "777" => false
"11" > "777" => false
"70" > "777" => false
"80" > "777" => true

It’s time to crash. The law is not friendly, because max, the size comparison method, was thought to judge the size through ASCII coding. Today, I found this problem when looking at the problem of sort sorting. In fact, here is the maximum value obtained by dictionary sorting.

Let’s take a look at sort in Ruby:

Sort sort method

?
1
2
3
4
ary.sort          -> new_ary
ary.sort { |a, b| block } -> new_ary
enum.sort_by { |obj| block }  -> array
enum.sort_by          -> an_enumerator

example

?
1
2
3
4
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:

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

1. When no block is specified, sort is sorted by dictionary by default.
2. If a block is specified, sorting the values is based on the size of the values of the block formulation method.
3.sort_ The by method sorts the results generated by the code executing the block. It only calls the block once for each element, which is faster than B.

Recommended Today

The whole tutorial of docker installation and use, installation / complete command / dockerfile image production / docker container arrangement and one click installation of nginx + redis + MySQL / visualizer portal (version 2022)

官网: https://docs.docker.com/ 官网: https://www.docker.com/ docker 镜像市场: https://hub.docker.com/ 一、docker 说明 1.1、docker 核心 1、Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源,Docker 是一个 CS 架构软件。 2、Docker 是一个虚拟化轻量级linux服务器,可以解决我们在开发环境中运行配置问题 3.、Docker的主要目标是‘build ,ship and run any app,anywhere’,一次封装,到处运行 4、容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。 1.2、docker 版本问题 .Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。 1.3、docker 架构( 3大核心) · 1、Images 镜像 (等于软件) · 2、Registry […]