Magic-Square - Creates a magic square by randomly generating a list until the list happens to be a magic square

Overview

Magic-Square

Creates a magic square by randomly generating a list until the list happens to be a magic square. Done as simply as possible...

Frequently Asked Questions

Why?

  • I wanted to remake a homework assignment I did two years ago, in one line of code

How long did this take you?

  • About an hour [but making this file is only adding to the amount of time wasted]

Does this even work?

  • Of course! TIAS!

How does it work?

  • I'm glad you asked. To be honest, I'm not sure I even know. But I split it up and added comments if that helps.
def is_magic_square(x): 
    """ There might be a more concise way to do this but it works """
    return sum([x[0],x[3],x[6]])==sum([x[1],x[4],x[7]])==sum([x[2],x[5],x[8]])==sum([x[0],x[1],x[2]])==sum([x[3],x[4],x[5]])==sum([x[6],x[7],x[8]])==sum([x[0],x[4],x[8]])==sum([x[2],x[4],x[6]])

#creates an infinite iterator that will always generate a new random list as long as you call x on it
infinite_iterator = (__import__("random").sample([1,2,3,4,5,6,7,8,9],9) for _ in __import__("itertools").repeat(1))

# the final list infinitely iterates as long as the boolean condition holds true 
# `https://stackoverflow.com/questions/2361426/get-the-first-item-from-an-iterable-that-matches-a-condition`
final_square= next(x for x in infinite_iterator if is_magic_square(x))
final_square = map(str, final_square) # just so it prints

# prints it out neatly 
# `https://stackoverflow.com/questions/35903828/how-to-print-3x3-array-in-python`
print("\n".join(map(" ".join, zip(*[iter(final_square)] * 3))))

Are you okay?

:)

Special Thanks

to Alex Martelli and Padraic Cunningham for the two stack overflow answers which made this monstrosity much more concise

Owner
Nick
Hey, welcome to my GitHub page! If you're interested, I accept discord bot commissions.
Nick
Python Interface of P3D

pyp3d 介绍: pyp3d是一个可在python上使用的工具包,它提供了一种可使用python脚本驱动创建模型的方法,为三维建模提供了全新的思路。 pyp3d中定义了一系列建模相关的必要的数据类型,例如球体、圆锥台、四棱锥台、 拉伸体、圆角管等几何体,pyp3d还提供了许多函数来实现放置集合体、

20 Sep 07, 2022
PIX is an image processing library in JAX, for JAX.

PIX PIX is an image processing library in JAX, for JAX. Overview JAX is a library resulting from the union of Autograd and XLA for high-performance ma

DeepMind 294 Jan 08, 2023
Pythonocc nodes for Ryven

Pythonocc-nodes-for-Ryven Pythonocc nodes for Ryven Here a way to work on Pythonocc with a node editor, Ryven in that case. To get it functional you w

Tanneguy 30 Dec 18, 2022
Photini - A free, easy to use, digital photograph metadata (Exif, IPTC, XMP) editing application for Linux, Windows and MacOS.

A free, easy to use, digital photograph metadata (Exif, IPTC, XMP) editing application for Linux, Windows and MacOS. "Metadata" is said to mea

Jim Easterbrook 120 Dec 20, 2022
MetaStalk is a tool that can be used to generate graphs from the metadata of JPEG, TIFF, and HEIC images

MetaStalk About MetaStalk is a tool that can be used to generate graphs from the metadata of JPEG, TIFF, and HEIC images, which are tested. More forma

Cyb3r Jak3 1 Jul 05, 2021
display: a browser-based graphics server

display: a browser-based graphics server Installation Quick Start Usage Development A very lightweight display server for Torch. Best used as a remote

Szymon Jakubczak 205 Oct 17, 2022
Scramb.py is a region based JPEG Image Scrambler and Descrambler written in Python

Scramb.py Scramb.py is a region based JPEG Image Scrambler and Descrambler written in Python. Main features Scramb.py can scramble images regions. So

47 Dec 25, 2022
Extract the temperature data of each wire from the thermal imager raw data.

Wire-Tempurature-Detection Extract the temperature data of each wire from the thermal imager raw data. The motivation of this computer vision project

JohanAckerman 1 Nov 03, 2021
This repository will help you get label for images in Stanford Cars Dataset.

STANFORD CARS DATASET stanford-cars "The Cars dataset contains 16,185 images of 196 classes of cars. The data is split into 8,144 training images and

Nguyễn Trường Lâu 3 Sep 20, 2022
Combinatorial image generator for generative NFT art.

ImageGen Stitches multiple image layers together into one image. Run usage: stitch.py [-h] backgrounds_dir dinos_dir traits_dir texture_file

Dinosols NFT 19 Sep 16, 2022
Pixel Brush Processing Unit

Pixel Brush Processing Unit The Pixel Brush Processing Unit (PBPU for short) is a simple 4-Bit CPU I designed in Logisim while I was still in school a

Pixel Brush 2 Nov 03, 2022
Short piece of code to create a rainbow gif of gradual contours from two shapefiles

rainbow-elevation-gif Short piece of code to create a rainbow gif of gradual con

Jess Roberts 6 Jan 17, 2022
Blender addon - convert empty image reference to image plane

Reference to image plane Convert reference images to a textured image mesh plane. As if it was imported with import image as plane Use on drag'n'dropp

Samuel Bernou 6 Nov 25, 2022
A Python package implementing various HDRI / Radiance image processing algorithms.

Colour - HDRI A Python package implementing various HDRI / Radiance image processing algorithms. It is open source and freely available under the New

colour-science 111 Dec 06, 2022
Create a 2D mesh for an airfoil in GMSH using python.

GMSHFoil A simple class to create a 2D mesh for an airfoil in GMSH using python. Requirements pip install airfoils

Charilaos Mylonas 1 May 16, 2022
Bringing vtk.js into Dash and Python

Dash VTK Dash VTK lets you integrate the vtk.js visualization pipeline directly into your Dash app. It is powered by react-vtk-js. Docs Demo Explorer

Plotly 88 Nov 29, 2022
An add to make adding screenshots and copied images to the scene easy

Blender Clipboard to Scene It doesn't work with version 2.93 and higher (I tested it on 2.91 and 2.83) There is an issue with importing the Pillow mod

Mohammad Mehdi Afkhami 3 Dec 29, 2021
HyperBlend is a new type of hyperspectral image simulator based on Blender.

HyperBlend version 0.1.0 This is the HyperBlend leaf spectra simulator developed in Spectral Laboratory of University of Jyväskylä. You can use and mo

SILMAE 2 Jun 20, 2022
Image comparison slider component for Streamlit

Streamlit Image Comparison Component A simple Streamlit Component to compare images with a slider in Streamlit apps using Knightlab's JuxtaposeJS. It

fatih 109 Dec 23, 2022
Sombra is simple Raytracer written in pure Python.

Sombra Sombra is simple Raytracer written in pure Python. It's main purpose is to help understand how raytracing works with a clean code. If you are l

Hernaldo Jesus Henriquez Nuñez 10 Jul 16, 2022