在长生不老药中分组或计数重复的字母 [英] group or count duplicated letters in elixir

查看:68
本文介绍了在长生不老药中分组或计数重复的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算Elixir中String上的重复字母。
我确实尝试了一些尝试,但直到现在都没有成功。

I'm trying to count duplicated letters on a String in Elixir. I did try some attempts, but no success until now.

让我们以以下字符串为例: AAABBAAC

Let's take this string as example: "AAABBAAC"

所需的输出将是 3A2B2A1C;

将此字符串转换为列表,我能够计算出每个字母,结果是 5A2B1C ,但我必须计算以下内容

Converting this string to a List, I was able to count every letter, resulting in "5A2B1C", but I have to count following the order.

这是我正在执行的代码:

This is the code I was doing:

string
|> String.graphemes
|> Enum.reduce([], fn(letter, acc) -> Keyword.update(acc, letter, 1, &(&1 + 1)) end)

但是,在我的测试中,我试图生成一个列表,例如 [ AAA, BB, AA ;, C] ,所以我可以轻松地使用 String.lenght 来计数。

But, in my tests, I'm trying to produce a List, like this ["AAA", "BB", "AA", "C"], so I can easely count with String.lenght.

外观就像使用 Enum.chunk_by 一样,我正在接近解决方案。

Looks like using Enum.chunk_by I'm getting closer to a solution.

有没有办法产生这种结果?

Is there a way to produce this?

推荐答案

如果使用递归方法实现此功能,则可以轻松跟踪最后出现的字符及其当前计数以及一个累加器到目前为止保存的结果。如果当前字符等于最后一个字符,则只需增加计数即可。如果两者不同,则将最后一个字符及其计数添加到累加器,然后继续下一个字符,直到字符串为空。最后,对最终值进行编码,然后返回结果。

If you implement this using a recursive approach, you can easily keep track of the last occurred character and its current count, as well an accumulator that holds the result so far. If the current character equals the last character you just increase the count. If the two differ, you add the last character and its count to the accumulator and proceed with the next character until the string is empty. Finally, you encode the final value and return the result.

defmodule RunLengthEncoding do
  # public interface, take first char and remember it as the current value
  def encode(<<char::utf8, rest::binary>>) do
    do_encode(rest, char, 1, "")
  end

  # current == last, increase the count and proceed
  defp do_encode(<<char::utf8, rest::binary>>, char, count, acc) do
    do_encode(rest, char, count + 1, acc)
  end

  # current != last, reset count, encode previous values and proceed
  defp do_encode(<<char::utf8, rest::binary>>, last, count, acc) do
    do_encode(rest, char, 1, acc <> to_string(count) <> <<last::utf8>>)
  end

  # input empty, encode final values and return
  defp do_encode("", last, count, acc) do
    acc <> to_string(count) <> <<last::utf8>>
  end
end

这篇关于在长生不老药中分组或计数重复的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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