Last active
April 29, 2024 00:48
-
-
Save sdkfz181tiger/ac59d75b764f48ac2e8f4d83d9bdfc81 to your computer and use it in GitHub Desktop.
基数変換で遊ぼう_その4_基数変換問題ジェネレーター(10進数<->2進数,8進数,16進数)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
""" | |
基数変換問題ジェネレーター(10進整数 <-> 2進数) | |
""" | |
import os, math, random | |
from PIL import Image, ImageFont, ImageDraw | |
# Seed | |
seed = 0 | |
# Random | |
rdms = list(range(64))# 0->99 | |
#rdms = list(range(20, 100))# 20->99 | |
random.seed(seed) | |
random.shuffle(rdms) | |
# Decimals | |
# decimals = [0]# Easy | |
decimals = [0.5, 0.25, 0.125]# Normal | |
# decimals = [0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875]# Hard | |
# D2B or B2D | |
flg_d2b = seed % 2 == 0 | |
# Directory | |
dir_name = "d2b" if flg_d2b else "b2d" | |
text_title = "BINARY \\(x_x;)/ HELL No:" + str(seed) | |
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 | |
# Nums | |
nums = rdms[0:rows*cols]# Random(Seed) | |
nums.sort(reverse=False)# Sort(If you neeed...) | |
# Decimals | |
for i in range(len(nums)): | |
index = nums[i] % len(decimals) | |
nums[i] += decimals[index] | |
#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 0 < modf[1]: | |
nums.append(1) | |
else: | |
nums.append(0) | |
num = modf[0] | |
return "".join(map(str, nums))# 2進文字列 | |
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 + "_No" + str(seed) + ".pdf" | |
images[0].save(image_out, "PDF", | |
quality=100, save_all=True, append_images=images[1:], optimize=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
""" | |
基数変換問題ジェネレーター(10進整数 <-> 8進数) | |
""" | |
import os, math, random | |
from PIL import Image, ImageFont, ImageDraw | |
# Seed | |
seed = 7 | |
# Random | |
rdms = list(range(99))# 0->99 | |
#rdms = list(range(20, 100))# 20->99 | |
random.seed(seed) | |
random.shuffle(rdms) | |
# Decimals | |
# decimals = [0]# Easy | |
# decimals = [0.5, 0.25, 0.125]# Normal | |
decimals = [0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875]# Hard | |
# D2O or O2D | |
flg_d2o = seed % 2 == 0 | |
# Directory | |
dir_name = "o2d" if flg_d2o else "d2o" | |
text_title = "BINARY \\(x_x;)/ HELL No:" + str(seed) | |
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 | |
# Nums | |
nums = rdms[0:rows*cols]# Random(Seed) | |
nums.sort(reverse=False)# Sort(If you neeed...) | |
# Decimals | |
for i in range(len(nums)): | |
index = nums[i] % len(decimals) | |
nums[i] += decimals[index] | |
#nums[i] += random.choice(decimals) | |
# Num to Octa | |
def num2octa(num): | |
print("num2octa:{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 *= 2 | |
modf = math.modf(num) | |
if(0 < modf[1]): | |
nums.append(1) | |
else: | |
nums.append(0) | |
num = modf[0] | |
arr = parseDecimal(nums, 3) # 8進数に変換(3桁区切り) | |
return "".join(map(str, arr)) # 8進文字列 | |
def parseDecimal(nums, dig): | |
# dig桁数に変換(3桁なら8進数, 4桁なら16進数) | |
odd = len(nums) % dig | |
for o in range(dig-odd): nums.append("0") | |
s_bin = "".join(map(str, nums))# 2進文字列 | |
arr = [int(s_bin[i: i+dig], 2) for i in range(0, len(s_bin), dig)] | |
while 1 < len(arr) and arr[len(arr)-1] == 0: arr.pop() # 最後の桁0を削除 | |
return arr | |
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 += num2octa(num) | |
else: | |
text = "(" + str(i+1) + ")" + "\n\n " + num2octa(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 + "_No" + str(seed) + ".pdf" | |
images[0].save(image_out, "PDF", | |
quality=100, save_all=True, append_images=images[1:], optimize=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
""" | |
基数変換問題ジェネレーター(10進整数 <-> 16進数) | |
""" | |
import os, math, random | |
from PIL import Image, ImageFont, ImageDraw | |
# Seed | |
seed = 0 | |
# Random | |
rdms = list(range(128))# 0->99 | |
#rdms = list(range(20, 100))# 20->99 | |
random.seed(seed) | |
random.shuffle(rdms) | |
# Decimals | |
# decimals = [0]# Easy | |
decimals = [0.5, 0.25, 0.125]# Normal | |
# decimals = [0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875]# Hard | |
# D2H or H2D | |
flg_d2h = seed % 2 == 0 | |
# Directory | |
dir_name = "h2d" if flg_d2h else "d2h" | |
text_title = "BINARY \\(x_x;)/ HELL No:" + str(seed) | |
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 | |
# Alphabets | |
alphabets = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"] | |
# Nums | |
nums = rdms[0:rows*cols]# Random(Seed) | |
nums.sort(reverse=False)# Sort(If you neeed...) | |
# Decimals | |
for i in range(len(nums)): | |
index = nums[i] % len(decimals) | |
nums[i] += decimals[index] | |
#nums[i] += random.choice(decimals) | |
# Num to Hex | |
def num2hex(num): | |
print("num2hex:{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 *= 2 | |
modf = math.modf(num) | |
if 0 < modf[1]: | |
nums.append(1) | |
else: | |
nums.append(0) | |
num = modf[0] | |
arr = parseDecimal(nums, 4) # 16進数に変換(4桁区切り) | |
return "".join(map(str, arr)) # 16進文字列 | |
def parseDecimal(nums, dig): | |
# dig桁数に変換(3桁なら8進数, 4桁なら16進数) | |
odd = len(nums) % dig | |
for o in range(dig-odd): nums.append("0") | |
s_bin = "".join(map(str, nums))# 2進文字列 | |
arr = [alphabets[int(s_bin[i: i+dig], 2)] for i in range(0, len(s_bin), dig)] | |
while 1 < len(arr) and arr[len(arr)-1] == 0: arr.pop() # 最後の桁0を削除 | |
return arr | |
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 += num2hex(num) | |
else: | |
text = "(" + str(i+1) + ")" + "\n\n " + num2hex(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 + "_No" + str(seed) + ".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