Get the App
SLTechnology News&Howtos  ›  Development  › 

How to analyze the Concepts of Ruby Special Grammar

Shulou Source: shulou.com Published: 2022-06-02 03:10:12 10月02日 Update

Today, the editor will show you how to parse the concepts of Ruby special grammar. The knowledge points in the article are introduced in great detail. Friends who think it is helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "how to analyze the concepts of Ruby special grammar".

There are many special functions in Ruby language worthy of our in-depth study. For example, those grammars that are different from other programming languages. Here we will learn some concepts related to the special syntax of Ruby.

The problem is change. Suppose we need to give someone a change of $39.

Then the result will be (US dollar coins have 25 coins, 10 coins, 5 coins, 1 coins):

It only takes two lines to solve the problem with Ruby special syntax.

Def make_change (amount

Coins = [])

Coins.sort! {| Xforme y | y x}

Return coins.map! {| coin |

F = amount/coin; amount

% = coin;Array.new (f) {coin}

}. Flatten

End

P make_change (39, [10, 10, 1, 5, 25])

The greedy algorithm is used here, which is always divided by the most coins each time, and then uses the next coin to do the same operation.

In a word, Ruby special grammar is the most agile language I have ever seen.

Thank you for your reading. The above is the whole content of "how to analyze the Special Grammar Concepts of Ruby". Friends who learn it, hurry up and do it. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!

Tags: Special grammar concept friends coins language content articles knowledge follow questions help agility one sentence function that is more come on knowledge points answers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Shulou Technology Apple macOS