Skip to content

Instantly share code, notes, and snippets.

View shiracamus's full-sized avatar

@shiracamus shiracamus

View GitHub Profile
### tic-tac-toe game
from tkinter import *
from tkinter.messagebox import *
class Board:
def __init__(self, size=3):
self.size = size
import random
class Answer:
"""出題者"""
def __init__(self, digits: int) -> None:
"""digits桁の重複しない数を作って隠し持つ"""
self._number = ''.join(map(str, random.sample(range(0, 10), digits)))
public class ParkingSimulator {
public static void main(String[] args) {
Parking parking = new Parking("駅前駐車場", 2);
Car car1 = new Car("品川33 あ 1234");
Car car2 = new Car("横浜555 い 5678");
Car car3 = new Car("多摩345 う 9012");
car1.park(parking);
car2.park(parking);
car3.park(parking);
def signed(value, bits=8):
return (value & ((sign := (1 << (bits - 1))) - 1)) - (value & sign)
print(signed(0x7f)) # 127
print(signed(0x80)) # -128
print(signed(0x81)) # -127
print(signed(0x7fff)) # -1
print(signed(0x7fff, 16)) # 32767
print(signed(0x8000, 16)) # -32768
print(signed(0x8001, 16)) # -32767
@shiracamus
shiracamus / Digits.java
Created November 10, 2023 03:09
四則演算を使わない加算処理(Qiitaに投稿された記事にコメントしたが記事は削除された)
import java.util.HashMap;
import java.util.Map;
class Digit { // 1桁数字
static final Map<Integer, Integer> next = new HashMap<>() {{
put(0, 1);
put(1, 2);
put(2, 3);
put(3, 4);
put(4, 5);
import tkinter as tk
from datetime import datetime, timedelta, timezone
# サイズ設定
SEG_WIDTH = 12 # このサイズを変更するだけで全体サイズが連動して変更される
SEG_HEIGHT = SEG_WIDTH * 4
COLON_SIZE = SEG_WIDTH * 5 // 3
CANVAS_WIDTH_NUMBER = SEG_WIDTH * 5
CANVAS_WIDTH_COLON = CANVAS_WIDTH_NUMBER // 2
CANVAS_HEIGHT = CANVAS_WIDTH_NUMBER * 2 - SEG_WIDTH

情報処理用語の 参照渡し・参照呼出し (call by reference) の説明とは違っているので、違いを確認しておきましょう。

https://www.ap-siken.com/kakomon/25_haru/q20.html

Pythonは、すべての値がオブジェクトで、すべての変数がオブジェクトへの参照を保持し(参照型変数)、どんな変数や値であろうとメモリの参照を引数として渡し、値(オブジェクト)を共有します。

def sub(x):
    print("address in x =", id(x))  # 引数xが参照しているオブジェクトのアドレス
    print("x.real =", x.real)  # 整数オブジェクトもアトリビュートやメソッドを持っている
def solve(cards, numbers):
if not numbers:
yield cards[:]
return
left = cards.index(0)
for number in numbers:
if (right := left + number + 1) < len(cards) and cards[right] == 0:
cards[left] = cards[right] = number
yield from solve(cards, numbers - {number})
cards[left] = cards[right] = 0
const WORDS = {
"ポケモン": ['株式会社ポケモン', '任天堂', 'ゲームソフト'],
"株式会社ポケモン": ["登場する架空の生物", "アニメ", "メディアミックス"],
"任天堂": ["マリオ", "ゼルダ", "星のカービィ"],
};
function getAncWords(word) {
// 引数の文字列を元に検索でキーワードを文字列の配列で返す処理
// 例:['株式会社ポケモン', '任天堂', 'ゲームソフト']、空配列が返る事もある
console.log("getAncWords:", word);
def solution(garden):
days = 0
# 咲いている花(1)の周囲で咲いていない花(0)、つまり次に咲く花の位置を列挙
grows = {(ny, nx)
for y, row in enumerate(garden)
for x, flower in enumerate(row)
if flower == 1
for ny, nx in ((y-1, x), (y+1, x), (y, x-1), (y, x+1))
if 0 <= ny < len(garden) and 0 <= nx < len(garden[ny])
if garden[ny][nx] == 0}