Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the Concepts of Ruby Special Grammar

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report