<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python | You Look Too Cool</title>
	<atom:link href="https://stabucky.com/wp/archives/category/digital/python/feed" rel="self" type="application/rss+xml" />
	<link>https://stabucky.com/wp</link>
	<description>ゆるくつくる stabuckyのブログ</description>
	<lastBuildDate>Tue, 11 Nov 2025 09:07:40 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://stabucky.com/wp/wp-content/uploads/2024/05/cropped-stabucky-32x32.png</url>
	<title>Python | You Look Too Cool</title>
	<link>https://stabucky.com/wp</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>RPAの代替としてのPyAutoGUI</title>
		<link>https://stabucky.com/wp/archives/20808</link>
					<comments>https://stabucky.com/wp/archives/20808#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Tue, 11 Nov 2025 09:07:40 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[PyAutoGUI]]></category>
		<category><![CDATA[RPA]]></category>
		<category><![CDATA[ライブラリ]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=20808</guid>

					<description><![CDATA[PythonのPyAutoGUIを使うと、マウス移動、クリック、キーボード操作、画像認識が可能です。これにより、パソコン操作のほとんどすべてを自動化できます。 RPAの代替として使えます。 目次 インストール基本マウスマ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>PythonのPyAutoGUIを使うと、マウス移動、クリック、キーボード操作、画像認識が可能です。これにより、パソコン操作のほとんどすべてを自動化できます。<br />
RPAの代替として使えます。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">インストール</a></li><li><a href="#toc2" tabindex="0">基本</a></li><li><a href="#toc3" tabindex="0">マウス</a><ol><li><a href="#toc4" tabindex="0">マウス移動（絶対）</a></li><li><a href="#toc5" tabindex="0">マウス移動（相対）</a></li><li><a href="#toc6" tabindex="0">移動時間</a></li><li><a href="#toc7" tabindex="0">マウスドラッグ</a></li><li><a href="#toc8" tabindex="0">クリック</a></li><li><a href="#toc9" tabindex="0">画像をクリック</a></li><li><a href="#toc10" tabindex="0">ダウン、アップ</a></li></ol></li><li><a href="#toc11" tabindex="0">キーの入力</a><ol><li><a href="#toc12" tabindex="0">通常</a></li><li><a href="#toc13" tabindex="0">ホットキー</a></li></ol></li><li><a href="#toc14" tabindex="0">ダイアログ</a><ol><li><a href="#toc15" tabindex="0">アラート</a></li><li><a href="#toc16" tabindex="0">コンファーム</a></li><li><a href="#toc17" tabindex="0">プロンプト</a></li></ol></li><li><a href="#toc18" tabindex="0">スクリーンショット</a><ol><li><a href="#toc19" tabindex="0">通常</a></li><li><a href="#toc20" tabindex="0">保存</a></li></ol></li><li><a href="#toc21" tabindex="0">画像検索</a><ol><li><a href="#toc22" tabindex="0">画像の座標を取得</a></li><li><a href="#toc23" tabindex="0">画像の中心座標を取得</a></li><li><a href="#toc24" tabindex="0">画像の中心をクリック</a></li><li><a href="#toc25" tabindex="0">画像検索の挙動</a></li></ol></li><li><a href="#toc26" tabindex="0">テクニック</a><ol><li><a href="#toc27" tabindex="0">待機</a></li><li><a href="#toc28" tabindex="0">文字列の入力</a></li></ol></li></ol>
    </div>
  </div>

<h2><span id="toc1">インストール</span></h2>
<p>pip install pyautogui</p>
<h2><span id="toc2">基本</span></h2>
<p>次のようにして使用します。<br />
import pyautogui</p>
<h2><span id="toc3">マウス</span></h2>
<h3><span id="toc4">マウス移動（絶対）</span></h3>
<p>pyautogui.moveTo(x, y)</p>
<h3><span id="toc5">マウス移動（相対）</span></h3>
<p>pyautogui.move(x, y)<br />
pyautogui.moveRel(x, y)</p>
<h3><span id="toc6">移動時間</span></h3>
<p>「duration」で移動時間（秒）を指定できます。<br />
pyautogui.moveTo(x, y, duration=num_seconds)</p>
<h3><span id="toc7">マウスドラッグ</span></h3>
<p>ドラッグします。相対座標です。<br />
pyautogui.dragTo(x, y, button=&#8217;left&#8217;)</p>
<h3><span id="toc8">クリック</span></h3>
<p>クリックします。絶対座標です。引数なしの場合はその場でクリックします。<br />
pyautogui.click()<br />
pyautogui.click(x, y)<br />
pyautogui.rightClick(x, y)<br />
pyautogui.doubleClick(x, y)</p>
<h3><span id="toc9">画像をクリック</span></h3>
<p>画像検索には「locateOnScreen」などを使用しますが、「click」の引数に画像ファイルを指定するとその中心をクリックします。<br />
pyautogui.click(image_path)</p>
<h3><span id="toc10">ダウン、アップ</span></h3>
<p>pyautogui.mouseDown(x, y)<br />
pyautogui.mouseUp(x, y)</p>
<h2><span id="toc11">キーの入力</span></h2>
<h3><span id="toc12">通常</span></h3>
<p>順にキーを押します。日本語の場合はうまくいかないことがありますので、クリップボードに入れて貼り付けるのが良い方法です。<br />
pyautogui.typewrite([&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;])</p>
<h3><span id="toc13">ホットキー</span></h3>
<p>同時にキーを押します。<br />
pyautogui.hotkey(&#8216;ctrl&#8217;, &#8216;v&#8217;)</p>
<h2><span id="toc14">ダイアログ</span></h2>
<h3><span id="toc15">アラート</span></h3>
<p>pyautogui.alert(&#8216;文字列表示&#8217;)</p>
<h3><span id="toc16">コンファーム</span></h3>
<p>pyautogui.confirm(&#8216;OKとキャンセル&#8217;)</p>
<h3><span id="toc17">プロンプト</span></h3>
<p>キャンセルを押した場合は「None」が返ります。<br />
pyautogui.prompt(&#8216;文字列入力&#8217;)</p>
<h2><span id="toc18">スクリーンショット</span></h2>
<h3><span id="toc19">通常</span></h3>
<p>画面全体のスクリーンショットを撮り、Pillowのイメージオブジェクトを返します。<br />
im = pyautogui.screenshot()</p>
<p>「region」を使用すると範囲を指定できます。<br />
im = pyautogui.screenshot(region=(x0, y0, x1, y1))</p>
<h3><span id="toc20">保存</span></h3>
<p>画面全体のスクリーンショットを撮り、ファイル名を付けて保存します。拡張子に応じた形式で保存されます。<br />
pyautogui.screenshot(&#8220;all.png&#8221;)<br />
pyautogui.screenshot(&#8220;all.jpg&#8221;)</p>
<h2><span id="toc21">画像検索</span></h2>
<p>画面上にあるボタンなどをクリックする場合などに使用できます。<br />
ボタンなどのスクリーンショットを画像として保存しておき、これを指定することでクリックが可能です。<br />
通常は最初に見つけた画像が対象となります。</p>
<h3><span id="toc22">画像の座標を取得</span></h3>
<p>指定した画像ファイルと同じ画像を画面上から探し、上下左右の座標を返します。<br />
location = pyautogui.locateOnScreen(image_path)<br />
x, y = pyautogui.center(location)</p>
<h3><span id="toc23">画像の中心座標を取得</span></h3>
<p>中心の座標を返します。クリックする場合はこれを使用します。<br />
x, y = pyautogui.locateCenterOnScreen(image_path)</p>
<h3><span id="toc24">画像の中心をクリック</span></h3>
<p>画像の中心をクリックします。シンプルな記述方法です。<br />
pyautogui.click(image_path)</p>
<h3><span id="toc25">画像検索の挙動</span></h3>
<p>次のようにすると上下左右の座標を取得できます。<br />
location = pyautogui.locateOnScreen(&#8220;button.png&#8221;)<br />
print(location) # Box(left=106, top=185, width=96, height=50)<br />
次のようにすると中心座標を取得できます。<br />
location = pyautogui.locateCenterOnScreen(&#8220;button.png&#8221;)<br />
print(location) # Point(x=154, y=210)<br />
次のようにすると合致する画像全てを取得します。<br />
location = pyautogui.locateAllOnScreen(&#8220;button.png&#8221;)<br />
print(location) # <generator object _locateAll_pillow at 0x00000209077BD630><br />
print(list(location)) # [Box(left=106, top=185, width=96, height=50)]</p>
<h2><span id="toc26">テクニック</span></h2>
<h3><span id="toc27">待機</span></h3>
<p>処理が早すぎるとボタンのクリックを「空振り」してしまうことがありますので、「sleep」を使用して待機します。<br />
import time<br />
time.sleep(3)<br />
画像を検索してクリックするような処理の場合は、以下のような関数を使用すると「sleep」不要で素早く処理できます。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">def click_image(image_path, timeout=10, interval=0.5):<br />
&nbsp; &nbsp; &quot;&quot;&quot;<br />
&nbsp; &nbsp; 指定された画像を探索し、見つかったらクリックする。<br />
&nbsp; &nbsp; タイムアウトまでに画像が見つからなければ処理終了。<br />
&nbsp; &nbsp; &quot;&quot;&quot;<br />
&nbsp; &nbsp; start_time = time.time()<br />
&nbsp; &nbsp; print(f&quot;画像探索開始: {image_path}&quot;)<br />
&nbsp; &nbsp; while time.time() - start_time &lt; timeout:<br />
&nbsp; &nbsp; &nbsp; &nbsp; try:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; location = pyautogui.locateOnScreen(image_path) &nbsp;# 画像探索 (例外対策)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if location: &nbsp;# 画像が見つかった場合<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; center = pyautogui.center(location) &nbsp;# 中心座標を取得<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pyautogui.click(center) &nbsp;# 中心をクリック<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f&quot;画像が見つかりました。クリックしました: {center}&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &nbsp;# 処理成功で終了<br />
&nbsp; &nbsp; &nbsp; &nbsp; except pyautogui.ImageNotFoundException as e:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f&quot;画像探索失敗: {e}&quot;) &nbsp;# エラーを通知<br />
&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(interval) &nbsp;# 再探索まで待機<br />
&nbsp; &nbsp; print(f&quot;タイムアウトで画像が見つかりませんでした: {image_path}&quot;)</div></div>
<h3><span id="toc28">文字列の入力</span></h3>
<p>「typewrite」を使用すると文字の入力ができますが、日本語だとうまくいかないことがあります。クリップボードに入れてペーストすると良いでしょう。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">import pyautogui<br />
import time<br />
import pyperclip<br />
text = &quot;こんにちは&quot;<br />
pyperclip.copy(text)<br />
time.sleep(3)<br />
pyautogui.hotkey('ctrl', 'v')</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/20808/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>APIでGPT4.1を使う方法</title>
		<link>https://stabucky.com/wp/archives/19920</link>
					<comments>https://stabucky.com/wp/archives/19920#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Sat, 17 May 2025 02:37:06 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[OpenAI]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=19920</guid>

					<description><![CDATA[PythonでAPIを使ってGPT4.1を使う方法については公式がサンプルコードを載せているので簡単にできます。 Just a moment...platform.openai.com 目次 準備コード使用例 準備 ライ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>PythonでAPIを使ってGPT4.1を使う方法については公式がサンプルコードを載せているので簡単にできます。</p>

<a rel="noopener" href="https://platform.openai.com/docs/guides/text?api-mode=chat&#038;lang=python" title="Just a moment..." class="blogcard-wrap external-blogcard-wrap a-wrap cf" target="_blank"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img decoding="async" src="https://s.wordpress.com/mshots/v1/https%3A%2F%2Fplatform.openai.com%2Fdocs%2Fguides%2Ftext%3Fapi-mode%3Dchat%26lang%3Dpython?w=160&#038;h=90" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">Just a moment...</div><div class="blogcard-snippet external-blogcard-snippet"></div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=https://platform.openai.com/docs/guides/text?api-mode=chat&#038;lang=python" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">platform.openai.com</div></div></div></div></a>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-4" checked><label class="toc-title" for="toc-checkbox-4">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">準備</a></li><li><a href="#toc2" tabindex="0">コード</a></li><li><a href="#toc3" tabindex="0">使用例</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">準備</span></h2>
<p>ライブラリOpenAIを使います。次の通りインストールします。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">pip install openai</div></div>
<p>APIキーは取得済みのものがそのまま使えます。<br />
ターミナルで次のように入力しておきます。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">export OPENAI_API_KEY=sk-xxxxx</div></div>
<h2><span id="toc2">コード</span></h2>
<p>少しアレンジしました。</p>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #ff7700;font-weight:bold;">from</span> openai <span style="color: #ff7700;font-weight:bold;">import</span> OpenAI<br />
client <span style="color: #66cc66;">=</span> OpenAI<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
prompt <span style="color: #66cc66;">=</span> <span style="color: #008000;">input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;prompt:&quot;</span><span style="color: black;">&#41;</span><br />
<br />
completion <span style="color: #66cc66;">=</span> client.<span style="color: black;">chat</span>.<span style="color: black;">completions</span>.<span style="color: black;">create</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; model<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;gpt-4.1&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; messages<span style="color: #66cc66;">=</span><span style="color: black;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;role&quot;</span>: <span style="color: #483d8b;">&quot;user&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;content&quot;</span>: prompt<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: black;">&#93;</span><br />
<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>completion.<span style="color: black;">choices</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">message</span>.<span style="color: black;">content</span><span style="color: black;">&#41;</span></div></div>
<h2><span id="toc3">使用例</span></h2>
<p>「日本の首相を教えて」と入力してみました。</p>
<blockquote><p>2024年6月時点での日本の首相（内閣総理大臣）は、**岸田文雄（きしだ ふみお）**さんです。岸田氏は2021年10月4日に首相に就任しました。</p></blockquote>
<p>情報は相変わらず古いようです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/19920/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SymPyで方程式を解く方法</title>
		<link>https://stabucky.com/wp/archives/19267</link>
					<comments>https://stabucky.com/wp/archives/19267#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Sat, 11 Jan 2025 04:10:28 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[sympy]]></category>
		<category><![CDATA[方程式]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=19267</guid>

					<description><![CDATA[Pythonで方程式を解く方法は色々ありますが、今回はSymPyを使ってみます。 目次 基本解が複数ある場合虚数解がある場合複雑な数式の場合式がどうなっているか実際の値を求めたい場合 基本 x^2-2*x+1=0を解いて [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Pythonで方程式を解く方法は色々ありますが、今回はSymPyを使ってみます。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-6" checked><label class="toc-title" for="toc-checkbox-6">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">基本</a></li><li><a href="#toc2" tabindex="0">解が複数ある場合</a></li><li><a href="#toc3" tabindex="0">虚数解がある場合</a></li><li><a href="#toc4" tabindex="0">複雑な数式の場合</a><ol><li><a href="#toc5" tabindex="0">式がどうなっているか</a></li><li><a href="#toc6" tabindex="0">実際の値を求めたい場合</a></li></ol></li></ol>
    </div>
  </div>

<h2><span id="toc1">基本</span></h2>
<p>x^2-2*x+1=0を解いてみます。<br />
結果は配列で出力されます。解が複数ある場合に対応しています。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">import sympy<br />
x = sympy.symbols(&quot;x&quot;)<br />
equation = x**2 - 2 * x + 1<br />
solution = sympy.solve(equation, x)<br />
print(solution)</div></div>
<p>結果<br />
[1]</p>
<h2><span id="toc2">解が複数ある場合</span></h2>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">import sympy<br />
x = sympy.symbols(&quot;x&quot;)<br />
equation = x**2 - 1<br />
solution = sympy.solve(equation, x)<br />
print(solution)</div></div>
<p>結果<br />
[-1, 1]</p>
<h2><span id="toc3">虚数解がある場合</span></h2>
<p>虚数は「I」で表されます。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">import sympy<br />
x = sympy.symbols(&quot;x&quot;)<br />
equation = x**2 + 1<br />
solution = sympy.solve(equation, x)<br />
print(solution)</div></div>
<p>結果<br />
[-I, I]</p>
<h2><span id="toc4">複雑な数式の場合</span></h2>
<p>下のような複雑な数式の場合は、ループを使って書けます。<br />
<img fetchpriority="high" decoding="async" src="https://stabucky.com/wp/wp-content/uploads/2025/01/lagrida_latex_editor.png" alt="x+\dfrac{1}{x+\dfrac{1}{x+\dfrac{1}{x+\dfrac{1}{x+\dfrac{1}{x+\dfrac{1}{x+\dfrac{1}{x+\dfrac{1}{x+\dfrac{1}{x+\dfrac{1}{x}}}}}}}}}}=0" width="507" height="282" class="aligncenter size-full wp-image-19268" srcset="https://stabucky.com/wp/wp-content/uploads/2025/01/lagrida_latex_editor.png 507w, https://stabucky.com/wp/wp-content/uploads/2025/01/lagrida_latex_editor-500x278.png 500w, https://stabucky.com/wp/wp-content/uploads/2025/01/lagrida_latex_editor-120x68.png 120w, https://stabucky.com/wp/wp-content/uploads/2025/01/lagrida_latex_editor-160x90.png 160w" sizes="(max-width: 507px) 100vw, 507px" /></p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">import sympy<br />
<br />
x = sympy.symbols(&quot;x&quot;)<br />
y = x<br />
for i in range(10):<br />
&nbsp; &nbsp; y = x + 1 / y<br />
solution = sympy.solve(y, x)<br />
print(solution)</div></div>
<p>結果<br />
[-sqrt(-2 &#8211; sqrt(3)), sqrt(-2 &#8211; sqrt(3)), -sqrt(-2 + sqrt(3)), sqrt(-2 + sqrt(3))]</p>
<h3><span id="toc5">式がどうなっているか</span></h3>
<p>式は出力できます。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">print(y)</div></div>
<p>結果<br />
x + 1/(x + 1/(x + 1/(x + 1/(x + 1/(x + 1/(x + 1/(x + 1/(x + 1/(x + 1/x)))))))))</p>
<h3><span id="toc6">実際の値を求めたい場合</span></h3>
<p>上の結果はsqrtが使われていて実際の値が分かりません。<br />
evalfを使うと実際の値が求まります。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">for s in solution:<br />
&nbsp; &nbsp; print(s.evalf())</div></div>
<p>結果<br />
-1.93185165257814*I<br />
1.93185165257814*I<br />
-0.517638090205041*I<br />
0.517638090205041*I</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/19267/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MoviePyを使いGIFをMP4に変換する方法</title>
		<link>https://stabucky.com/wp/archives/19231</link>
					<comments>https://stabucky.com/wp/archives/19231#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Mon, 06 Jan 2025 03:18:58 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[MoviePy]]></category>
		<category><![CDATA[ライブラリ]]></category>
		<category><![CDATA[動画]]></category>
		<category><![CDATA[変換]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=19231</guid>

					<description><![CDATA[目次 概要準備サンプルコード 概要 MoviePyというPythonのライブラリがあります。 これを使ってアニメーションGIFを動画ファイルMP4に変換します。 MoviePy documentation &#8212; [&#8230;]]]></description>
										<content:encoded><![CDATA[
  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-8" checked><label class="toc-title" for="toc-checkbox-8">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">概要</a></li><li><a href="#toc2" tabindex="0">準備</a></li><li><a href="#toc3" tabindex="0">サンプルコード</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">概要</span></h2>
<p>MoviePyというPythonのライブラリがあります。<br />
これを使ってアニメーションGIFを動画ファイルMP4に変換します。</p>

<a rel="noopener" href="https://zulko.github.io/moviepy/index.html" title="MoviePy documentation &#8212; MoviePy  documentation" class="blogcard-wrap external-blogcard-wrap a-wrap cf" target="_blank"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img loading="lazy" decoding="async" src="https://s.wordpress.com/mshots/v1/https%3A%2F%2Fzulko.github.io%2Fmoviepy%2Findex.html?w=160&#038;h=90" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">MoviePy documentation &#8212; MoviePy  documentation</div><div class="blogcard-snippet external-blogcard-snippet"></div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://zulko.github.io/moviepy/index.html" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">zulko.github.io</div></div></div></div></a>
<h2><span id="toc2">準備</span></h2>
<p>ターミナルで次のようにします。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">pip install moviepy</div></div>
<h2><span id="toc3">サンプルコード</span></h2>
<p>src.gifをdst.mp4に変換します。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">from moviepy import *<br />
movie_file = VideoFileClip(&quot;src.gif&quot;)<br />
movie_file.write_videofile(&quot;dst.mp4&quot;)<br />
movie_file.close()</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/19231/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AIエージェントとブラウザの連携を簡単に ‐ Browser Use を使ってみた</title>
		<link>https://stabucky.com/wp/archives/19162</link>
					<comments>https://stabucky.com/wp/archives/19162#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Wed, 25 Dec 2024 14:05:29 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[browser_use]]></category>
		<category><![CDATA[OpenAI]]></category>
		<category><![CDATA[ライブラリ]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=19162</guid>

					<description><![CDATA[Pythonのライブラリbrowser_useを使ってみました。 browser-use/README.md at main · browser-use/browser-use🌐 Make websites access [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Pythonのライブラリbrowser_useを使ってみました。</p>

<a rel="noopener" href="https://github.com/browser-use/browser-use/blob/main/README.md" title="browser-use/README.md at main · browser-use/browser-use" class="blogcard-wrap external-blogcard-wrap a-wrap cf" target="_blank"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img loading="lazy" decoding="async" src="https://stabucky.com/wp/wp-content/uploads/cocoon-resources/blog-card-cache/9be8f43f15d4ee4c5ba8dfd60a417ef3." alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">browser-use/README.md at main · browser-use/browser-use</div><div class="blogcard-snippet external-blogcard-snippet">🌐 Make websites accessible for AI agents. Automate tasks online with ease. - browser-use/browser-use</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://github.com/browser-use/browser-use/blob/main/README.md" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">github.com</div></div></div></div></a>
<blockquote><p>Browser use は、AIエージェントをブラウザと接続する最も簡単な方法です。</p></blockquote>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-10" checked><label class="toc-title" for="toc-checkbox-10">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">導入</a></li><li><a href="#toc2" tabindex="0">サンプル</a></li><li><a href="#toc3" tabindex="0">実行結果</a></li><li><a href="#toc4" tabindex="0">gpt-4o-miniの場合</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">導入</span></h2>
<p>OpenAIのアカウントがあるのが大前提です。</p>
<p>Macの場合、ターミナルで次のようにします。<br />
公式サイトには「playwrightはオプショナル」と書かれていますが必須のようです。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">pip install browser-use<br />
playwright install</div></div>
<p>また次のように環境変数のセットをします。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">export OPENAI_API_KEY=sk-xxxxx</div></div>
<h2><span id="toc2">サンプル</span></h2>
<p>次のようにします。<br />
レプリコンワクチンに関しては、今日（12月25日）、製薬会社が国会議員を提訴したというニュースがありました。<br />
これが反映されるかどうか確認したいと思います。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">from langchain_openai import ChatOpenAI<br />
from browser_use import Agent<br />
import asyncio<br />
<br />
async def main():<br />
&nbsp; &nbsp; agent = Agent(<br />
&nbsp; &nbsp; &nbsp; &nbsp; task=&quot;レプリコンワクチンに関する最新情報をまとめて。日本語で。&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; llm=ChatOpenAI(model=&quot;gpt-4o&quot;),<br />
&nbsp; &nbsp; )<br />
&nbsp; &nbsp; result = await agent.run()<br />
&nbsp; &nbsp; print(result)<br />
<br />
asyncio.run(main())</div></div>
<h2><span id="toc3">実行結果</span></h2>
<p>結果は次の通りMarkdown形式で出力されます。<br />
国会議員が提訴されたニュースも反映されています。<br />
生成AIは情報が古いという弱点がありましたが、それを克服しています。<br />
これはブラウザを起動しニュースをスクレイピングしているからだと思われます。</p>
<blockquote><p>最新のレプリコンワクチンに関する情報は以下の通りです。</p>
<p>1. **法的問題**: 製薬会社がレプリコンワクチンをめぐり、立憲民主党の原口議員を誹謗中傷で提訴しています。これは複数のニュースソース（NHK、朝日新聞デジタル、Yahoo!ニュース）で報じられています。</p>
<p>2. **製造販売承認**: 明治製菓ファルマが、次世代mRNAワクチンであるレプリコンワクチンの製造販売承認を世界で初めて取得しました。これは2023年11月に行われたと報じられています。</p>
<p>3. **ワクチンの特徴と安全性**: レプリコンワクチンは自己増幅型mRNAワクチンで、細胞内で大量に複製することが特徴です。安全性に関しては、日本で初めて承認されたため、情報が限られていますが、これまでのmRNAワクチンよりも少ない接種者数が報告されています。</p></blockquote>
<h2><span id="toc4">gpt-4o-miniの場合</span></h2>
<p>上述のサンプルはモデルとしてgpt-4oを使っています。<br />
これをgpt-4o-miniにしてみました。</p>
<blockquote><p>最新のレプリコンワクチンに関する情報は以下の通りです：</p>
<p>&#8211; **訴訟の内容**: 2024年12月25日、Meiji Seika Pharma社が立憲民主党の原口一博衆院議員を訴えました。訴訟の理由は、原口議員が同社のワクチンを「生物兵器」と呼び、戦時中の人体実験部隊「731部隊」に例えたことによる名誉毀損です。<br />
&#8211; **損害賠償**: 同社は、原口氏に対して1千万円の損害賠償を求めています。<br />
&#8211; **影響**: 原口氏の発言により、同社には抗議の電話が多数寄せられ、120万円の損害が発生したとされています。また、ワクチンの売上で得られるはずだった55億円超の利益が失われたと主張しています。<br />
&#8211; **会社の見解**: 同社は、訴訟は原口氏の主張の科学的根拠を問うものではなく、国から承認されたワクチンに対する一連の発言は「意見や論評の範囲を超えている」と述べています。</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/19162/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Word、ExcelをMarkdownに変換するPythonライブラリ「MarkItDown」を使ってみた</title>
		<link>https://stabucky.com/wp/archives/19123</link>
					<comments>https://stabucky.com/wp/archives/19123#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Sun, 15 Dec 2024 12:36:21 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Markdown]]></category>
		<category><![CDATA[ライブラリ]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=19123</guid>

					<description><![CDATA[MarkItDownというPythonのライブラリがあります。 WordやExcelなどをMarkdown形式に変換します。 目次 導入サンプルコードWordExcel 導入 Macの場合、ターミナルで次の通り入力します [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>MarkItDownというPythonのライブラリがあります。<br />
WordやExcelなどをMarkdown形式に変換します。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-12" checked><label class="toc-title" for="toc-checkbox-12">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">導入</a></li><li><a href="#toc2" tabindex="0">サンプルコード</a></li><li><a href="#toc3" tabindex="0">Word</a></li><li><a href="#toc4" tabindex="0">Excel</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">導入</span></h2>
<p>Macの場合、ターミナルで次の通り入力します。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">pip install markitdown</div></div>
<p>詳細は次のとおりです。</p>

<a rel="noopener" href="https://github.com/microsoft/markitdown/" title="GitHub - microsoft/markitdown: Python tool for converting files and office documents to Markdown." class="blogcard-wrap external-blogcard-wrap a-wrap cf" target="_blank"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img loading="lazy" decoding="async" src="https://opengraph.githubassets.com/123b001bd04eb4c30104523191d7fc90d10783b015d8169e4146e4fa2a62fee5/microsoft/markitdown" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">GitHub - microsoft/markitdown: Python tool for converting files and office documents to Markdown.</div><div class="blogcard-snippet external-blogcard-snippet">Python tool for converting files and office documents to Markdown. - microsoft/markitdown</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://github.com/microsoft/markitdown" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">github.com</div></div></div></div></a>
<h2><span id="toc2">サンプルコード</span></h2>
<p>基本的にこれだけでMarkdown形式のテキストが出力されます。<br />
smple.xxxのところを適宜書き換えます。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">from markitdown import MarkItDown<br />
markitdown = MarkItDown()<br />
result = markitdown.convert(&quot;sample.xxx&quot;)<br />
print(result.text_content)</div></div>
<h2><span id="toc3">Word</span></h2>
<p>将軍について説明したWordファイルを作成しました。<br />
タイトルを「将軍」、見出し1を「将軍とは」「各時代」としています。<br />
これを変換するとタイトルは見出しとは認識されず一般のテキストになっています。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">将軍<br />
<br />
# 将軍とは<br />
<br />
将軍とは、日本の封建時代において、天皇から軍事と政治の最高権力を委任された武家の指導者のことです。実質的な国の支配者として、約700年にわたり日本の政治を統治しました。<br />
<br />
# 各時代<br />
<br />
## 鎌倉幕府<br />
<br />
鎌倉幕府の将軍 源頼朝により開かれた鎌倉幕府の初代将軍。武家社会の基礎を築き、武士階級の政治支配の礎を確立した歴史的な人物です。<br />
<br />
## 室町幕府<br />
<br />
室町幕府の将軍 足利尊氏が開いた幕府で、天皇と武家の権力バランスが特徴的でした。文化や芸術の発展にも大きな影響を与えた時代の指導者です。<br />
<br />
## 江戸幕府<br />
<br />
江戸幕府の将軍 徳川家康が創設した幕府で、260年以上続く平和な統治を実現。鎖国政策を敷き、安定した封建社会を長期間維持しました。</div></div>
<h2><span id="toc4">Excel</span></h2>
<p>一つのシートに足利15代、別のシートに徳川15代の一覧を載せたExcelファイルを変換してみました。<br />
各シートが見出し2となり、表はテーブルに変換されます。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;height:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">## 徳川<br />
| 代 | 漢字 | かな |<br />
| --- | --- | --- |<br />
| 1 | 家康 | いえやす |<br />
| 2 | 秀忠 | ひでただ |<br />
| 3 | 家光 | いえみつ |<br />
| 4 | 家綱 | いえつな |<br />
| 5 | 綱吉 | つなよし |<br />
| 6 | 家宣 | いえのぶ |<br />
| 7 | 家継 | いえつぐ |<br />
| 8 | 吉宗 | よしむね |<br />
| 9 | 家重 | いえしげ |<br />
| 10 | 家治 | いえはる |<br />
| 11 | 家斉 | いえなり |<br />
| 12 | 家慶 | いえよし |<br />
| 13 | 家定 | いえさだ |<br />
| 14 | 家茂 | いえもち |<br />
| 15 | 慶喜 | よしのぶ |<br />
<br />
## 足利<br />
| 代 | 漢字 | かな |<br />
| --- | --- | --- |<br />
| 1 | 尊氏 | たかうじ |<br />
| 2 | 義詮 | よしあきら |<br />
| 3 | 義満 | よしみつ |<br />
| 4 | 義持 | よしもち |<br />
| 5 | 義量 | よしかず |<br />
| 6 | 義教 | よしのり |<br />
| 7 | 義勝 | よしかつ |<br />
| 8 | 義政 | よしまさ |<br />
| 9 | 義尚 | よしひさ |<br />
| 10 | 義材 | よしき |<br />
| 11 | 義澄 | よしずみ |<br />
| 12 | 義晴 | よしはる |<br />
| 13 | 義輝 | よしてる |<br />
| 14 | 義栄 | よしひで |<br />
| 15 | 義昭 | よしあき |</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/19123/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Pythonで複数の画像をGIFに変換する方法</title>
		<link>https://stabucky.com/wp/archives/18957</link>
					<comments>https://stabucky.com/wp/archives/18957#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Sun, 24 Nov 2024 10:31:15 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[GIF]]></category>
		<category><![CDATA[Pillow]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=18957</guid>

					<description><![CDATA[PythonのPillowを使って万華鏡画像を大量に作りました。 動画にして見るためにPythonでGIFに変換してみました。 目次 サンプルコード出力例海鮮丼ケーキピザ サンプルコード imgというフォルダに000.p [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>PythonのPillowを使って万華鏡画像を大量に作りました。<br />
動画にして見るためにPythonでGIFに変換してみました。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-14" checked><label class="toc-title" for="toc-checkbox-14">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">サンプルコード</a></li><li><a href="#toc2" tabindex="0">出力例</a><ol><li><a href="#toc3" tabindex="0">海鮮丼</a></li><li><a href="#toc4" tabindex="0">ケーキ</a></li><li><a href="#toc5" tabindex="0">ピザ</a></li></ol></li></ol>
    </div>
  </div>

<h2><span id="toc1">サンプルコード</span></h2>
<p>imgというフォルダに000.pngから127.pngまでの画像が保存されています。<br />
これを繋いで、一つのGIFファイルdst.gifを生成します。</p>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #ff7700;font-weight:bold;">from</span> PIL <span style="color: #ff7700;font-weight:bold;">import</span> Image<br />
fn_gif <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;dst.gif&quot;</span><br />
images <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; number <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>.<span style="color: black;">zfill</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; fn_img <span style="color: #66cc66;">=</span> f<span style="color: #483d8b;">&quot;img/{number}.png&quot;</span><br />
&nbsp; &nbsp; im <span style="color: #66cc66;">=</span> Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>fn_img<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; images.<span style="color: black;">append</span><span style="color: black;">&#40;</span>im<span style="color: black;">&#41;</span><br />
images<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">save</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; fn_gif<span style="color: #66cc66;">,</span> save_all<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: #66cc66;">,</span> append_images<span style="color: #66cc66;">=</span>images<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> optimize<span style="color: #66cc66;">=</span><span style="color: #008000;">False</span><span style="color: #66cc66;">,</span> duration<span style="color: #66cc66;">=</span><span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> loop<span style="color: #66cc66;">=</span><span style="color: #ff4500;">0</span><br />
<span style="color: black;">&#41;</span></div></div>
<p>リストimagesに画像を格納します。<br />
images[0]で最初の画像を指定します。<br />
append_imagesで二枚目以降の画像を指定します。</p>
<h2><span id="toc2">出力例</span></h2>
<h3><span id="toc3">海鮮丼</span></h3>
<p><img loading="lazy" decoding="async" src="https://stabucky.com/wp/wp-content/uploads/2024/11/kaisendon.gif" alt="" width="256" height="256" class="aligncenter size-full wp-image-18971" /></p>
<h3><span id="toc4">ケーキ</span></h3>
<p><img loading="lazy" decoding="async" src="https://stabucky.com/wp/wp-content/uploads/2024/11/cake.gif" alt="" width="256" height="256" class="aligncenter size-full wp-image-18969" /></p>
<h3><span id="toc5">ピザ</span></h3>
<p><img loading="lazy" decoding="async" src="https://stabucky.com/wp/wp-content/uploads/2024/11/pizza.gif" alt="" width="256" height="256" class="aligncenter size-full wp-image-18970" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/18957/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>二つのコップの問題をPythonで解いてみた</title>
		<link>https://stabucky.com/wp/archives/18894</link>
					<comments>https://stabucky.com/wp/archives/18894#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Sat, 09 Nov 2024 15:33:22 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[アルゴリズム]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=18894</guid>

					<description><![CDATA[目次 問題と経緯コード実行結果 問題と経緯 以前、二つのコップの問題をプログラムで解きました。 3デシリットルのコップと5デシリットルのコップがあります。これらを使って1デシリットルの水をはかりとるにはどうしたらいいです [&#8230;]]]></description>
										<content:encoded><![CDATA[
  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-16" checked><label class="toc-title" for="toc-checkbox-16">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">問題と経緯</a></li><li><a href="#toc2" tabindex="0">コード</a></li><li><a href="#toc3" tabindex="0">実行結果</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">問題と経緯</span></h2>
<p>以前、<a href="https://stabucky.com/wp/archives/3911">二つのコップの問題</a>をプログラムで解きました。</p>
<blockquote><p>3デシリットルのコップと5デシリットルのコップがあります。これらを使って1デシリットルの水をはかりとるにはどうしたらいいですか。</p></blockquote>
<p>水差し問題とも呼ばれるそうです。<br />
以前、解いたときは、水の移し替えなどの操作をランダムに行い、目的の量になったら終了、これを何度も繰り返し、最小の手順を答えとするものでした。<br />
厳密には最小かどうかは怪しいです。</p>
<p>ところが<a href="https://stabucky.com/wp/archives/18889">「だんご屋のひまつぶし」をPythonで解いた</a>ところ、二つのコップの問題も同様に解けることに気付きました。</p>
<h2><span id="toc2">コード</span></h2>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;height:400px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">copy</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; cup_capacities <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; target <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;問題&quot;</span><span style="color: #66cc66;">,</span> cup_capacities<span style="color: #66cc66;">,</span> target<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; steps <span style="color: #66cc66;">=</span> solve_cup<span style="color: black;">&#40;</span>cup_capacities<span style="color: #66cc66;">,</span> target<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;正解&quot;</span><span style="color: #66cc66;">,</span> steps<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;️手順再現&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; show_steps<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> cup_capacities<span style="color: #66cc66;">,</span> steps<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> solve_cup<span style="color: black;">&#40;</span>cup_capacities<span style="color: #66cc66;">,</span> target<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; moves <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;left_spill&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;right_spill&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;left_pour&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;right_pour&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;left_right&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;right_left&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; state <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; states <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>state<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; states_done <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>state<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; steps <span style="color: #66cc66;">=</span> <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><br />
&nbsp; &nbsp; steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; states_new <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> state <span style="color: #ff7700;font-weight:bold;">in</span> states:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> i:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> step <span style="color: #ff7700;font-weight:bold;">in</span> moves:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state_next <span style="color: #66cc66;">=</span> transition_state<span style="color: black;">&#40;</span>state<span style="color: #66cc66;">,</span> cup_capacities<span style="color: #66cc66;">,</span> step<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> state_next <span style="color: #66cc66;">==</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> state_next <span style="color: #ff7700;font-weight:bold;">in</span> states_done:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state_next<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>:<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state_next<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>step<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; states_new.<span style="color: black;">append</span><span style="color: black;">&#40;</span>state_next<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> state_next<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">==</span> target <span style="color: #ff7700;font-weight:bold;">or</span> state_next<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">==</span> target:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state_next<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; states_done +<span style="color: #66cc66;">=</span> states_new<br />
&nbsp; &nbsp; &nbsp; &nbsp; states <span style="color: #66cc66;">=</span> states_new<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> transition_state<span style="color: black;">&#40;</span>state_previous<span style="color: #66cc66;">,</span> cup_capacities<span style="color: #66cc66;">,</span> step<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; state <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">copy</span>.<span style="color: black;">deepcopy</span><span style="color: black;">&#40;</span>state_previous<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> step <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;left_spill&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> step <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;right_spill&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> step <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;left_pour&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> cup_capacities<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> step <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;right_pour&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> cup_capacities<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> step <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;left_right&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span> cup_capacities<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> - cup_capacities<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> cup_capacities<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> step <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;right_left&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span> cup_capacities<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> - cup_capacities<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> cup_capacities<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> state<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> show_steps<span style="color: black;">&#40;</span>state_from<span style="color: #66cc66;">,</span> cup_capacities<span style="color: #66cc66;">,</span> steps<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; state <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">copy</span>.<span style="color: black;">deepcopy</span><span style="color: black;">&#40;</span>state_from<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;初期状態&quot;</span><span style="color: #66cc66;">,</span> state<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> step <span style="color: #ff7700;font-weight:bold;">in</span> steps:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;移動&quot;</span><span style="color: #66cc66;">,</span> step<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; state <span style="color: #66cc66;">=</span> transition_state<span style="color: black;">&#40;</span>state<span style="color: #66cc66;">,</span> cup_capacities<span style="color: #66cc66;">,</span> step<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;状態&quot;</span><span style="color: #66cc66;">,</span> state<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;回数&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>steps<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;__main__&quot;</span>:<br />
&nbsp; &nbsp; main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<h2><span id="toc3">実行結果</span></h2>
<p>問題 [3, 5] 1<br />
正解 [&#8216;left_pour&#8217;, &#8216;left_right&#8217;, &#8216;left_pour&#8217;, &#8216;left_right&#8217;]<br />
️手順再現<br />
初期状態 [0, 0]<br />
移動 left_pour<br />
状態 [3, 0]<br />
移動 left_right<br />
状態 [0, 3]<br />
移動 left_pour<br />
状態 [3, 3]<br />
移動 left_right<br />
状態 [1, 5]<br />
回数 4</p>
<p>left_spillは左のコップを空にする。<br />
left_pourは左のコップをいっぱいにする。<br />
left_rightは左のコップから右のコップに移し替える。<br />
という意味です。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/18894/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>だんご屋のひまつぶしをPythonで解いてみた</title>
		<link>https://stabucky.com/wp/archives/18889</link>
					<comments>https://stabucky.com/wp/archives/18889#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Sat, 09 Nov 2024 04:19:51 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[アルゴリズム]]></category>
		<category><![CDATA[パズル]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=18889</guid>

					<description><![CDATA[だんご屋のひまつぶしというパズルがあるそうです。 三本の串に刺さった、ある状態の赤、白、緑の団子を別の状態に移します。 このパズルを完全に解析したという記事がありました。 難しくて理解できなかったので、自分でも考えてみる [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a rel="noopener" href="https://amzn.to/3YWD2i4" target="_blank">だんご屋のひまつぶし</a>というパズルがあるそうです。<br />
三本の串に刺さった、ある状態の赤、白、緑の団子を別の状態に移します。</p>
<p>このパズルを完全に解析したという記事がありました。</p>
<p><iframe title="「だんご屋のひまつぶし」完全解析 - すぎゃーんメモ" src="https://hatenablog-parts.com/embed?url=https%3A%2F%2Fmemo.sugyan.com%2Fentry%2F2024%2F11%2F03%2F235814%23%25E6%259C%2580%25E9%2595%25B7%25E6%2589%258B%25E9%25A0%2586%25E3%2581%25AE%25E5%2595%258F%25E9%25A1%258C%25E3%2581%25AF" class="embed-card embed-blogcard" scrolling="no" frameborder="0" style="display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;"></iframe></p>
<p>難しくて理解できなかったので、自分でも考えてみることにしました。<br />
Pythonで与えられた状態から状態への最短手順を返します。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-18" checked><label class="toc-title" for="toc-checkbox-18">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">考え方</a></li><li><a href="#toc2" tabindex="0">コード</a></li><li><a href="#toc3" tabindex="0">実行結果</a></li><li><a href="#toc4" tabindex="0">答え合わせ</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">考え方</span></h2>
<p>はじめは、最初の状態から最後の状態になるまで、ひたすらランダムに団子を動かすということをやっていました。一応、答えは出ますが、時間がかかりますし、最短ではありません。<br />
この手の問題はダイクストラ法というのを使うらしいのですが、全く理解できなかったので自分で考えました。</p>
<p>最初の状態から動かすのは6パターンあります。団子は一本に三個までなので動かせない場合があり、そのパターンは無視します。動かした後の状態を全て保存します。<br />
次の状態から動かすのは6通りあります。同様に動かせないパターンを無視し、また過去にあった状態も無視します。動かした後の状態を全て保存します。<br />
これを最後の状態になるまで繰り返します。</p>
<h2><span id="toc2">コード</span></h2>
<p>上の記事で最短手順が長いと紹介されていた問題を解いてみました。<br />
赤白緑 緑白赤 なし → なし 赤緑緑 白赤白</p>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;height:400px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">copy</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; state_from <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; state_to <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;問題&quot;</span><span style="color: #66cc66;">,</span> state_from<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;&gt;&gt;&gt;&quot;</span><span style="color: #66cc66;">,</span> state_to<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; result <span style="color: #66cc66;">=</span> solve_dango<span style="color: black;">&#40;</span>state_from<span style="color: #66cc66;">,</span> state_to<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;正解&quot;</span><span style="color: #66cc66;">,</span> result<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;️手順再現&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>state_from<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;&gt;&gt;&gt;&quot;</span><span style="color: #66cc66;">,</span> state_to<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; show_steps<span style="color: black;">&#40;</span>state_from<span style="color: #66cc66;">,</span> result<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> solve_dango<span style="color: black;">&#40;</span>state_from<span style="color: #66cc66;">,</span> state_to<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; moves <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; steps <span style="color: #66cc66;">=</span> <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><br />
&nbsp; &nbsp; state_list_done <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; state <span style="color: #66cc66;">=</span> state_from<br />
&nbsp; &nbsp; state_list_done.<span style="color: black;">append</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; state_list <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>state<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; state_list_new <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> state <span style="color: #ff7700;font-weight:bold;">in</span> state_list:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> i:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> step <span style="color: #ff7700;font-weight:bold;">in</span> moves:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state_next <span style="color: #66cc66;">=</span> move_dango<span style="color: black;">&#40;</span>state<span style="color: #66cc66;">,</span> step<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> state_next <span style="color: #66cc66;">==</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> state_next <span style="color: #ff7700;font-weight:bold;">in</span> state_list_done:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state_next<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>:<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state_next<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>step<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state_list_new.<span style="color: black;">append</span><span style="color: black;">&#40;</span>state_next<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> state_next <span style="color: #66cc66;">==</span> state_to:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> steps<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>state_next<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; state_list_done +<span style="color: #66cc66;">=</span> state_list_new<br />
&nbsp; &nbsp; &nbsp; &nbsp; state_list <span style="color: #66cc66;">=</span> state_list_new<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> move_dango<span style="color: black;">&#40;</span>state_moto<span style="color: #66cc66;">,</span> step<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; state <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">copy</span>.<span style="color: black;">deepcopy</span><span style="color: black;">&#40;</span>state_moto<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#91;</span>step<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>state<span style="color: black;">&#91;</span>step<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">3</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; ball <span style="color: #66cc66;">=</span> state<span style="color: black;">&#91;</span>step<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; state<span style="color: black;">&#91;</span>step<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>.<span style="color: black;">insert</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> ball<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> state<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> show_steps<span style="color: black;">&#40;</span>state_from<span style="color: #66cc66;">,</span> steps<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; state <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">copy</span>.<span style="color: black;">deepcopy</span><span style="color: black;">&#40;</span>state_from<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;初期状態&quot;</span><span style="color: #66cc66;">,</span> state<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> step <span style="color: #ff7700;font-weight:bold;">in</span> steps:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;移動&quot;</span><span style="color: #66cc66;">,</span> step<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; state <span style="color: #66cc66;">=</span> move_dango<span style="color: black;">&#40;</span>state<span style="color: #66cc66;">,</span> step<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;状態&quot;</span><span style="color: #66cc66;">,</span> state<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;回数&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>steps<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;__main__&quot;</span>:<br />
&nbsp; &nbsp; main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<h2><span id="toc3">実行結果</span></h2>
<p>問題 [[1, 2, 3], [3, 2, 1], []] >>> [[], [1, 3, 3], [2, 1, 2]]<br />
正解 [[1, 2], [1, 2], [0, 1], [2, 1], [2, 0], [1, 2], [1, 2], [1, 2], [0, 1], [0, 1], [0, 1], [2, 0], [1, 0], [1, 2], [0, 1], [0, 1]]<br />
️手順再現<br />
[[1, 2, 3], [3, 2, 1], []] >>> [[], [1, 3, 3], [2, 1, 2]]<br />
初期状態 [[1, 2, 3], [3, 2, 1], []]<br />
移動 [1, 2]<br />
状態 [[1, 2, 3], [2, 1], [3]]<br />
移動 [1, 2]<br />
状態 [[1, 2, 3], [1], [2, 3]]<br />
移動 [0, 1]<br />
状態 [[2, 3], [1, 1], [2, 3]]<br />
移動 [2, 1]<br />
状態 [[2, 3], [2, 1, 1], [3]]<br />
移動 [2, 0]<br />
状態 [[3, 2, 3], [2, 1, 1], []]<br />
移動 [1, 2]<br />
状態 [[3, 2, 3], [1, 1], [2]]<br />
移動 [1, 2]<br />
状態 [[3, 2, 3], [1], [1, 2]]<br />
移動 [1, 2]<br />
状態 [[3, 2, 3], [], [1, 1, 2]]<br />
移動 [0, 1]<br />
状態 [[2, 3], [3], [1, 1, 2]]<br />
移動 [0, 1]<br />
状態 [[3], [2, 3], [1, 1, 2]]<br />
移動 [0, 1]<br />
状態 [[], [3, 2, 3], [1, 1, 2]]<br />
移動 [2, 0]<br />
状態 [[1], [3, 2, 3], [1, 2]]<br />
移動 [1, 0]<br />
状態 [[3, 1], [2, 3], [1, 2]]<br />
移動 [1, 2]<br />
状態 [[3, 1], [3], [2, 1, 2]]<br />
移動 [0, 1]<br />
状態 [[1], [3, 3], [2, 1, 2]]<br />
移動 [0, 1]<br />
状態 [[], [1, 3, 3], [2, 1, 2]]<br />
回数 16</p>
<h2><span id="toc4">答え合わせ</span></h2>
<p>上の記事では16手とあったので正解が出せました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/18889/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Pythonで読めるJSONを生成する方法</title>
		<link>https://stabucky.com/wp/archives/18837</link>
					<comments>https://stabucky.com/wp/archives/18837#respond</comments>
		
		<dc:creator><![CDATA[stabucky]]></dc:creator>
		<pubDate>Fri, 18 Oct 2024 03:55:38 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[JSON]]></category>
		<guid isPermaLink="false">https://stabucky.com/wp/?p=18837</guid>

					<description><![CDATA[JSONは扱いやすい形式ですが、文字列はUTF-8でエンコードされるため読めません。 読めるJSONを生成する方法を考えてみました。 import json def main(): &#160; &#160; recs  [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>JSONは扱いやすい形式ですが、文字列はUTF-8でエンコードされるため読めません。<br />
読めるJSONを生成する方法を考えてみました。</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:400px;height:400px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">import json<br />
<br />
def main():<br />
&nbsp; &nbsp; recs = {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;date&quot;: &quot;2020-1-15&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;data&quot;: [{&quot;id&quot;: 1, &quot;name&quot;: &quot;アフリカゾウ&quot;}, {&quot;id&quot;: 2, &quot;name&quot;: &quot;イノシシ&quot;}],<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; json_pure = json.dumps(recs)<br />
&nbsp; &nbsp; print(&quot;正しいJSON&quot;)<br />
&nbsp; &nbsp; print(json_pure)<br />
&nbsp; &nbsp; json_readable = json_dumps_readable(recs)<br />
&nbsp; &nbsp; print(&quot;読めるJSON&quot;)<br />
&nbsp; &nbsp; print(json_readable)<br />
&nbsp; &nbsp; recs_new = json.loads(json_readable)<br />
<br />
def json_dumps_readable(recs):<br />
&nbsp; &nbsp; json_pure = json.dumps(recs)<br />
&nbsp; &nbsp; json_readable = json_pure.encode().decode(&quot;unicode_escape&quot;)<br />
&nbsp; &nbsp; json_readable = (<br />
&nbsp; &nbsp; &nbsp; &nbsp; json_readable.replace(&quot; &quot;, &quot;&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; .replace(&quot;,&quot;, &quot;,\n&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; .replace(&quot;{&quot;, &quot;{\n&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; .replace(&quot;},\n{&quot;, &quot;},{&quot;)<br />
&nbsp; &nbsp; )<br />
&nbsp; &nbsp; return json_readable<br />
<br />
if __name__ == &quot;__main__&quot;:<br />
&nbsp; &nbsp; main()</div></div>
<p>実行結果</p>
<p>正しいJSON<br />
{&#8220;date&#8221;: &#8220;2020-1-15&#8221;, &#8220;data&#8221;: [{&#8220;id&#8221;: 1, &#8220;name&#8221;: &#8220;\u30a2\u30d5\u30ea\u30ab\u30be\u30a6&#8221;}, {&#8220;id&#8221;: 2, &#8220;name&#8221;: &#8220;\u30a4\u30ce\u30b7\u30b7&#8221;}]}<br />
読めるJSON<br />
{<br />
&#8220;date&#8221;:&#8221;2020-1-15&#8243;,<br />
&#8220;data&#8221;:[{<br />
&#8220;id&#8221;:1,<br />
&#8220;name&#8221;:&#8221;アフリカゾウ&#8221;},{<br />
&#8220;id&#8221;:2,<br />
&#8220;name&#8221;:&#8221;イノシシ&#8221;}]}</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stabucky.com/wp/archives/18837/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
