LoginSignup
2
0

llama-2 で elixir を学ぶ (その3)掛け算九九の表を作成

Last updated at Posted at 2024-03-25

こちらと同じことを llama-2 で行いました。

Python のプログラム

multiplation.py
#! /usr/bin/python
#
import sys
#
sys.stderr.write("*** 開始 ***\n")

for it in range(1,10):
	for jt in range(1,10):
		kt = it * jt
		print("%2d" % kt,end = " ")
	print()

sys.stderr.write("*** 終了 ***\n")

llama-2 が作成したプログラム

ex03.exs
#! /usr/bin/elixir

import System

System.err.write("*** 開始 ***\n")

for it in 1..10 do
  for jt in 1..10 do
    kt = it * jt
    IO.puts("%2d" % kt, space: " ")
  end
end

System.err.write("*** 終了 ***\n")

実行結果

$ ./ex03.exs 
** (SyntaxError) invalid syntax found on ex03.exs:10:19:
    error: syntax error before: '%'
    │
 10 │     IO.puts("%2d" % kt, space: " ")
    │                   ^
    │
    └─ ex03.exs:10:19
    (elixir 1.16.2) lib/code.ex:1489: Code.require_file/2

修正

sed で修正

sed01
s/import System//
s/System.err.write/IO.puts :stderr,/
s/ in / <- /
sed -f sed01 ex03.exs > ex0301.exs
import System
#! /usr/bin/elixir

IO.puts :stderr,("*** 開始 ***\n")

for it <- 1..10 do
  for jt <- 1..10 do
    kt = it * jt
    IO.puts("%2d" % kt, space: " ")
  end
end

IO.puts :stderr,("*** 終了 ***\n")

更に手修正

次を

IO.puts("%2d" % kt, space: " ")

次に変換する

:io.format("~2s ",[Integer.to_string(kt)])
ex0302.exs
#! /usr/bin/elixir

IO.puts :stderr,("*** 開始 ***\n")

for it <- 1..10 do
  for jt <- 1..10 do
    kt = it * jt
    :io.format("~2s ",[Integer.to_string(kt)])
#    IO.puts("%2d" % kt, space: " ")
  end
end

IO.puts :stderr,("*** 終了 ***\n")

実行結果

$ ./ex0302.exs 
*** 開始 ***

 1  2  3  4  5  6  7  8  9 10  2  4  6  8 10 12 14 16 18 20  3  6  9 12 15 18 21 24 27 30  4  8 12 16 20 24 28 32 36 40  5 10 15 20 25 30 35 40 45 50  6 12 18 24 30 36 42 48 54 60  7 14 21 28 35 42 49 56 63 70  8 16 24 32 40 48 56 64 72 80  9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 10 *** 終了 ***

改行を入れる

ex0303.exs
#! /usr/bin/elixir

IO.puts :stderr,("*** 開始 ***\n")

for it <- 1..10 do
 for jt <- 1..10 do
   kt = it * jt
   :io.format("~2s ",[Integer.to_string(kt)])
#    IO.puts("%2d" % kt, space: " ")
 end
 IO.puts ""
end

IO.puts :stderr,("*** 終了 ***\n")

実行結果

$ ./ex0303.exs 
*** 開始 ***

 1  2  3  4  5  6  7  8  9 10 
 2  4  6  8 10 12 14 16 18 20 
 3  6  9 12 15 18 21 24 27 30 
 4  8 12 16 20 24 28 32 36 40 
 5 10 15 20 25 30 35 40 45 50 
 6 12 18 24 30 36 42 48 54 60 
 7 14 21 28 35 42 49 56 63 70 
 8 16 24 32 40 48 56 64 72 80 
 9 18 27 36 45 54 63 72 81 90 
10 20 30 40 50 60 70 80 90 10 
*** 終了 ***

ループの回数を修正

ex0304.exs
#! /usr/bin/elixir

IO.puts :stderr,("*** 開始 ***\n")

for it <- 1..9 do
  for jt <- 1..9 do
    kt = it * jt
    :io.format("~2s ",[Integer.to_string(kt)])
#    IO.puts("%2d" % kt, space: " ")
  end
  IO.puts ""
end

IO.puts :stderr,("*** 終了 ***\n")

実行結果

$ ./ex0304.exs 
*** 開始 ***

 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 
*** 終了 ***
2
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
2
0