1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでelixir その252

Last updated at Posted at 2023-09-09

概要

paiza.ioでelixirやってみた。
zundoko書いたけど、バグってる。
見逃すときが、ある。

サンプルコード


defmodule Zundoko do
	def start() do
		Stream.repeatedly(fn ->
			["ズン", "ドコ"]
			|> Enum.random
		end)
		|> run
	end
	def run(input) do
		match = ["ドコ", "ズン", "ズン", "ズン", "ドコ"]
		eol = "キ・ヨ・シ!"
		input
		|> Stream.transform([], fn item, acc ->
				if List.last(acc) == eol do
					{:halt, nil}
				else
					if Enum.count(acc) < Enum.count(match) do
						{[item], acc ++ [item]}
					else
						if acc == match do
							{[eol], acc ++ [eol]}
						else
							{[item], [item]}
						end
					end
				end
				|> IO.inspect
			end)
		|> Enum.to_list
		|> Enum.reduce("", fn v, s ->
			s = s <> v <> " "
		end)
	end
end



defmodule Zundoko2 do
	def start() do
		Stream.repeatedly(fn ->
			["ズン", "ドコ"]
			|> Enum.random
		end)
		|> run
	end
	def run(input) do
		match = ["ドコ", "ズン", "ズン", "ズン"]
		eol = "ドコ キ・ヨ・シ!"
		doko = "ドコ"
		input
		|> Stream.transform([], fn item, acc ->
				if List.last(acc) == eol do
					{:halt, nil}
				else
				    if item == doko do
				    	{[item], [item]}
				    else
    					if Enum.count(acc) < Enum.count(match) do
	    					{[item], acc ++ [item]}
		    			else
			    			if acc == match do
				    			{[eol], acc ++ [eol]}
					    	else
						    	{[item], [item]}
					    	end
					    end
					end
				end
				|> IO.inspect
			end)
		|> Enum.to_list
		|> Enum.reduce("", fn v, s ->
			s = s <> v <> " "
		end)
	end
end


Zundoko.start
|> IO.inspect

Zundoko2.start
|> IO.inspect




実行結果

{["ズン"], ["ズン"]}
{["ドコ"], ["ズン", "ドコ"]}
{["ドコ"], ["ズン", "ドコ", "ドコ"]}
{["ドコ"], ["ズン", "ドコ", "ドコ", "ドコ"]}
{["ドコ"], ["ズン", "ドコ", "ドコ", "ドコ", "ドコ"]}
{["ズン"], ["ズン"]}
{["ズン"], ["ズン", "ズン"]}
{["ズン"], ["ズン", "ズン", "ズン"]}
{["ドコ"], ["ズン", "ズン", "ズン", "ドコ"]}
{["ズン"], ["ズン", "ズン", "ズン", "ドコ", "ズン"]}
{["ズン"], ["ズン"]}
{["ドコ"], ["ズン", "ドコ"]}
{["ドコ"], ["ズン", "ドコ", "ドコ"]}
{["ズン"], ["ズン", "ドコ", "ドコ", "ズン"]}
{["ドコ"], ["ズン", "ドコ", "ドコ", "ズン", "ドコ"]}
{["ドコ"], ["ドコ"]}
{["ズン"], ["ドコ", "ズン"]}
{["ズン"], ["ドコ", "ズン", "ズン"]}
{["ズン"], ["ドコ", "ズン", "ズン", "ズン"]}
{["ドコ"], ["ドコ", "ズン", "ズン", "ズン", "ドコ"]}
{["キ・ヨ・シ!"],
 ["ドコ", "ズン", "ズン", "ズン", "ドコ", "キ・ヨ・シ!"]}
{:halt, nil}
"ズン ドコ ドコ ドコ ドコ ズン ズン ズン ドコ ズン ズン ドコ ドコ ズン ドコ ドコ ズン ズン ズン ドコ キ・ヨ・シ! "
{["ドコ"], ["ドコ"]}
{["ドコ"], ["ドコ"]}
{["ドコ"], ["ドコ"]}
{["ドコ"], ["ドコ"]}
{["ズン"], ["ドコ", "ズン"]}
{["ドコ"], ["ドコ"]}
{["ドコ"], ["ドコ"]}
{["ズン"], ["ドコ", "ズン"]}
{["ドコ"], ["ドコ"]}
{["ドコ"], ["ドコ"]}
{["ズン"], ["ドコ", "ズン"]}
{["ズン"], ["ドコ", "ズン", "ズン"]}
{["ズン"], ["ドコ", "ズン", "ズン", "ズン"]}
{["ドコ キ・ヨ・シ!"],
 ["ドコ", "ズン", "ズン", "ズン", "ドコ キ・ヨ・シ!"]}
{:halt, nil}
"ドコ ドコ ドコ ドコ ズン ドコ ドコ ズン ドコ ドコ ズン ズン ズン ドコ キ・ヨ・シ! "

成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?