Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active February 14, 2024 10:13
Show Gist options
  • Save sdkfz181tiger/e6409f037a73282375a433cae613fb06 to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/e6409f037a73282375a433cae613fb06 to your computer and use it in GitHub Desktop.
基数変換問題ジェネレーター
# coding: utf-8
"""
基数変換問題ジェネレーター(10進整数 <-> 2進数)
"""
import os, math, random
from PIL import Image, ImageFont, ImageDraw
# D2B or B2D
flg_d2b = True
# Seed
seed = 0
# Directory
dir_name = "d2b" if flg_d2b else "b2d"
text_title = "BINARY \\(x_x;)/ HELL 100"
if flg_d2b == True:
text_explain = "次にある10進数の値を、2進数に変換しなさい"
text_example = "※桁数に制限は無いものとする\n\n例1: 4=100, 例2: 9=1001"
else:
text_explain = "次にある2進数の値を、10進数に変換しなさい"
text_example = "※桁数に制限は無いものとする\n\n例1: 100=4, 例2: 1001=9"
# Font
font_path_w4 = "/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc";
font_path_w6 = "/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc";
font_color = (0, 0, 0)
base_x = 250
base_y = 800
pad_x = 980
pad_y = 210
rows = 10
cols = 2
# Random
rdms = list(range(100))
random.seed(seed)
random.shuffle(rdms)
# Nums
#nums = rdms[0:rows*cols]# Random(Seed)
nums = list(range(0, 30))
nums.sort(reverse=False)
# Decimals
decimals = [0]# Easy
# decimals = [0, 0.5, 0.25, 0.125]# Normal
# decimals = [0, 0.5, 0.25, 0.125, 0.75, 0.625, 0.375, 0.875]# Hard
for i in range(len(nums)):
nums[i] += random.choice(decimals)
# Num to Binary
def num2binary(num):
print("num2binary:{0}".format(num))
integer = math.floor(num)
decimal = num - integer
a = getInteger(integer)
b = getDecimal(decimal)
if decimal == 0: return a
return a + "." + b
def getInteger(num):
nums = []
while(0 < num):
nums.append(num % 2)
num = int(num / 2)
nums.reverse()
if len(nums) <= 0: return "0"
return "".join(map(str, nums))
def getDecimal(num):
nums = []
while(num != 0):
num *= 2
modf = math.modf(num)
if(modf[1] == 1):
nums.append(1)
else:
nums.append(0)
num = modf[0]
return "".join(map(str, nums))
for i in range(2):
type_flg = i % 2
type_name = "mondai" if type_flg else "kaito"
images = []
# Image
image = Image.new("RGB", (2150, 3035), (255, 255, 255))
# Draw
draw = ImageDraw.Draw(image)
# Title
font = ImageFont.truetype(font_path_w6, 80)
draw.text((base_x, 200), text_title, font=font, fill=font_color)
# Name
font = ImageFont.truetype(font_path_w6, 40)
draw.text((image.size[0]/2 + 500, 250), "氏名:", font=font, fill=font_color)
# Explain
font = ImageFont.truetype(font_path_w6, 50)
draw.text((260, 420), text_explain, font=font, fill=font_color)
# Example
font = ImageFont.truetype(font_path_w4, 40)
draw.text((260, 540), text_example, font=font, fill=font_color)
for r in range(rows):
for c in range(cols):
i = c + r * cols
if(len(nums) <= i): break
num = nums[i]
if flg_d2b == True:
text = "(" + str(i+1) + ")" + "\n\n " + str(num) + " = "
if type_flg == 0: text += num2binary(num)
else:
text = "(" + str(i+1) + ")" + "\n\n " + num2binary(num) + " = "
if type_flg == 0: text += str(num)
pos_x = base_x + c * pad_x
pos_y = base_y + r * pad_y
pos = (pos_x, pos_y)
font = ImageFont.truetype(font_path_w6, 40)
draw.text(pos, text, font=font, fill=font_color)
# Directory
if(not os.path.isdir(dir_name)): os.mkdir(dir_name)
images.append(image)
# Save
image_out = "./" + dir_name + "/" + type_name + ".pdf"
images[0].save(image_out, "PDF",
quality=100, save_all=True, append_images=images[1:], optimize=True)
# coding: utf-8
"""
基数変換問題ジェネレーター(10進整数 <-> 2進数)
"""
import os, math, random
from PIL import Image, ImageFont, ImageDraw
# D2B or B2D
flg_d2b = True
# Seed
seed = 0
# Directory
dir_name = "d2b" if flg_d2b else "b2d"
text_title = "BINARY \\(x_x;)/ HELL 100"
if flg_d2b == True:
text_explain = "次にある10進数の値を、符号付き4bit2進数に変換しなさい"
text_example = "※マイナスの場合は2の補数表現にする事\n\n例1: 4=0100, 例2: -4=1100"
else:
text_explain = "次にある符号付き4bit2進数の値を、10進数に変換しなさい"
text_example = "※マイナスの場合は2の補数表現としている\n\n例1: 0100=4, 例2: 1100=-4"
# Font
font_path_w4 = "/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc";
font_path_w6 = "/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc";
font_color = (0, 0, 0)
base_x = 250
base_y = 800
pad_x = 980
pad_y = 210
rows = 10
cols = 2
# Random
rdms = list(range(100))
random.seed(seed)
random.shuffle(rdms)
# Nums
#nums = rdms[0:rows*cols]# Random(Seed)
nums = list(range(-8, 8))
nums.sort(reverse=True)
# Int to Binary
def int2binary(num, digits=4):
print("int2binary:{0}".format(num))
if 0 <= num: return getArray(num, digits)
return flipArray(getArray(-num, digits))
def getArray(num, digits):
nums = [0] * digits
arr = []
while(0 < num):
arr.append(num % 2)
num = int(num / 2)
for i in range(len(arr)):
nums[i] = arr[i]
nums.reverse()
return nums
def flipArray(nums):
for i in range(len(nums)):
nums[i] = 0 if nums[i] != 0 else 1
c = 1
for i in range(len(nums)):
r = len(nums) - 1 - i
n = c + nums[r]
if 1 < n:
c = 1
nums[r] = 0
else:
c = 0
nums[r] = n
return nums
for i in range(2):
type_flg = i % 2
type_name = "mondai" if type_flg else "kaito"
images = []
# Image
image = Image.new("RGB", (2150, 3035), (255, 255, 255))
# Draw
draw = ImageDraw.Draw(image)
# Title
font = ImageFont.truetype(font_path_w6, 80)
draw.text((base_x, 200), text_title, font=font, fill=font_color)
# Name
font = ImageFont.truetype(font_path_w6, 40)
draw.text((image.size[0]/2 + 500, 250), "氏名:", font=font, fill=font_color)
# Explain
font = ImageFont.truetype(font_path_w6, 50)
draw.text((260, 420), text_explain, font=font, fill=font_color)
# Example
font = ImageFont.truetype(font_path_w4, 40)
draw.text((260, 540), text_example, font=font, fill=font_color)
for r in range(rows):
for c in range(cols):
i = c + r * cols
if(len(nums) <= i): break
num = nums[i]
if flg_d2b == True:
text = "(" + str(i+1) + ")" + "\n\n " + str(num) + " = "
if type_flg == 0: text += "".join(map(str, int2binary(num)))
else:
text = "(" + str(i+1) + ")" + "\n\n " + "".join(map(str, int2binary(num))) + " = "
if type_flg == 0: text += str(num)
pos_x = base_x + c * pad_x
pos_y = base_y + r * pad_y
pos = (pos_x, pos_y)
font = ImageFont.truetype(font_path_w6, 40)
draw.text(pos, text, font=font, fill=font_color)
# Directory
if(not os.path.isdir(dir_name)): os.mkdir(dir_name)
images.append(image)
# Save
image_out = "./" + dir_name + "/" + type_name + ".pdf"
images[0].save(image_out, "PDF",
quality=100, save_all=True, append_images=images[1:], optimize=True)
# coding: utf-8
"""
基数変換問題ジェネレーター(10進整数 <-> 8進数)
"""
import os, math, random
from PIL import Image, ImageFont, ImageDraw
# D2O or O2D
flg_d2o = False
# Seed
seed = 0
# Directory
dir_name = "d2o" if flg_d2o else "o2d"
text_title = "BINARY \\(x_x;)/ HELL 100"
if flg_d2o == True:
text_explain = "次にある10進数の値を、8進数に変換しなさい"
text_example = "※桁数に制限は無いものとする\n\n例1: 12=14, 例2: 19=23"
else:
text_explain = "次にある8進数の値を、10進数に変換しなさい"
text_example = "※桁数に制限は無いものとする\n\n例1: 14=12, 例2: 23=19"
# Font
font_path_w4 = "/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc";
font_path_w6 = "/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc";
font_color = (0, 0, 0)
base_x = 250
base_y = 800
pad_x = 980
pad_y = 210
rows = 10
cols = 2
# Random
rdms = list(range(100))
random.seed(seed)
random.shuffle(rdms)
# Nums
#nums = rdms[0:rows*cols]# Random(Seed)
nums = list(range(0, 30))
nums.sort(reverse=False)
# Decimals
decimals = [0]# Easy
# decimals = [0, 0.5, 0.25, 0.125]# Normal
# decimals = [0, 0.5, 0.25, 0.125, 0.75, 0.625, 0.375, 0.875]# Hard
for i in range(len(nums)):
nums[i] += random.choice(decimals)
# Num to Binary
def num2binary(num):
print("num2binary:{0}".format(num))
integer = math.floor(num)
decimal = num - integer
a = getInteger(integer)
b = getDecimal(decimal)
if decimal == 0: return a
return a + "." + b
def getInteger(num):
nums = []
while(0 < num):
nums.append(num % 8)
num = int(num / 8)
nums.reverse()
if len(nums) <= 0: return "0"
return "".join(map(str, nums))
def getDecimal(num):
nums = []
while(num != 0):
num *= 8
modf = math.modf(num)
if(modf[1] == 1):
nums.append(1)
else:
nums.append(0)
num = modf[0]
return "".join(map(str, nums))
for i in range(2):
type_flg = i % 2
type_name = "mondai" if type_flg else "kaito"
images = []
# Image
image = Image.new("RGB", (2150, 3035), (255, 255, 255))
# Draw
draw = ImageDraw.Draw(image)
# Title
font = ImageFont.truetype(font_path_w6, 80)
draw.text((base_x, 200), text_title, font=font, fill=font_color)
# Name
font = ImageFont.truetype(font_path_w6, 40)
draw.text((image.size[0]/2 + 500, 250), "氏名:", font=font, fill=font_color)
# Explain
font = ImageFont.truetype(font_path_w6, 50)
draw.text((260, 420), text_explain, font=font, fill=font_color)
# Example
font = ImageFont.truetype(font_path_w4, 40)
draw.text((260, 540), text_example, font=font, fill=font_color)
for r in range(rows):
for c in range(cols):
i = c + r * cols
if(len(nums) <= i): break
num = nums[i]
if flg_d2o == True:
text = "(" + str(i+1) + ")" + "\n\n " + str(num) + " = "
if type_flg == 0: text += num2binary(num)
else:
text = "(" + str(i+1) + ")" + "\n\n " + num2binary(num) + " = "
if type_flg == 0: text += str(num)
pos_x = base_x + c * pad_x
pos_y = base_y + r * pad_y
pos = (pos_x, pos_y)
font = ImageFont.truetype(font_path_w6, 40)
draw.text(pos, text, font=font, fill=font_color)
# Directory
if(not os.path.isdir(dir_name)): os.mkdir(dir_name)
images.append(image)
# Save
image_out = "./" + dir_name + "/" + type_name + ".pdf"
images[0].save(image_out, "PDF",
quality=100, save_all=True, append_images=images[1:], optimize=True)
# coding: utf-8
"""
基数変換問題ジェネレーター(10進整数 <-> 16進数)
"""
import os, math, random
from PIL import Image, ImageFont, ImageDraw
# D2H or H2D
flg_d2h = False
# Seed
seed = 0
# Directory
dir_name = "d2h" if flg_d2h else "h2d"
text_title = "BINARY \\(x_x;)/ HELL 100"
if flg_d2h == True:
text_explain = "次にある10進数の値を、16進数に変換しなさい"
text_example = "※桁数に制限は無いものとする\n\n例1: 26=1A, 例2: 29=1C"
else:
text_explain = "次にある16進数の値を、10進数に変換しなさい"
text_example = "※桁数に制限は無いものとする\n\n例1: 1A=26, 例2: 1C=29"
# Font
font_path_w4 = "/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc";
font_path_w6 = "/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc";
font_color = (0, 0, 0)
base_x = 250
base_y = 800
pad_x = 980
pad_y = 210
rows = 10
cols = 2
# Random
rdms = list(range(100))
random.seed(seed)
random.shuffle(rdms)
# Nums
#nums = rdms[0:rows*cols]# Random(Seed)
nums = list(range(0, 30))
nums.sort(reverse=False)
# Decimals
decimals = [0]# Easy
# decimals = [0, 0.5, 0.25, 0.125]# Normal
# decimals = [0, 0.5, 0.25, 0.125, 0.75, 0.625, 0.375, 0.875]# Hard
for i in range(len(nums)):
nums[i] += random.choice(decimals)
# Alphabets
alphabets = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
# Num to Binary
def num2binary(num):
print("num2binary:{0}".format(num))
integer = math.floor(num)
decimal = num - integer
a = getInteger(integer)
b = getDecimal(decimal)
if decimal == 0: return a
return a + "." + b
def getInteger(num):
nums = []
while(0 < num):
nums.append(alphabets[num % 16])
num = int(num / 16)
nums.reverse()
if len(nums) <= 0: return "0"
return "".join(map(str, nums))
def getDecimal(num):
nums = []
while(num != 0):
num *= 16
modf = math.modf(num)
nums.append(alphabets[math.floor(modf[1])])
num = modf[0]
return "".join(map(str, nums))
for i in range(2):
type_flg = i % 2
type_name = "mondai" if type_flg else "kaito"
images = []
# Image
image = Image.new("RGB", (2150, 3035), (255, 255, 255))
# Draw
draw = ImageDraw.Draw(image)
# Title
font = ImageFont.truetype(font_path_w6, 80)
draw.text((base_x, 200), text_title, font=font, fill=font_color)
# Name
font = ImageFont.truetype(font_path_w6, 40)
draw.text((image.size[0]/2 + 500, 250), "氏名:", font=font, fill=font_color)
# Explain
font = ImageFont.truetype(font_path_w6, 50)
draw.text((260, 420), text_explain, font=font, fill=font_color)
# Example
font = ImageFont.truetype(font_path_w4, 40)
draw.text((260, 540), text_example, font=font, fill=font_color)
for r in range(rows):
for c in range(cols):
i = c + r * cols
if(len(nums) <= i): break
num = nums[i]
if flg_d2h == True:
text = "(" + str(i+1) + ")" + "\n\n " + str(num) + " = "
if type_flg == 0: text += num2binary(num)
else:
text = "(" + str(i+1) + ")" + "\n\n " + num2binary(num) + " = "
if type_flg == 0: text += str(num)
pos_x = base_x + c * pad_x
pos_y = base_y + r * pad_y
pos = (pos_x, pos_y)
font = ImageFont.truetype(font_path_w6, 40)
draw.text(pos, text, font=font, fill=font_color)
# Directory
if(not os.path.isdir(dir_name)): os.mkdir(dir_name)
images.append(image)
# Save
image_out = "./" + dir_name + "/" + type_name + ".pdf"
images[0].save(image_out, "PDF",
quality=100, save_all=True, append_images=images[1:], optimize=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment