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

You might also like...
You cant check for conflicts until course enrolment actually opens. I wanted to do it earlier.
You cant check for conflicts until course enrolment actually opens. I wanted to do it earlier.

AcornICS I noticed that Acorn it does not let you check if a timetable is valid based on the enrollment cart, it also does not let you visualize it ea

a decompilation of NAP36 the widevine removal software for amz and nf used by p2p groups until it stoped working due to it using expired cdm keys

NAP36 a decompilation of NAP36 the widevine removal software for amz and nf used by p2p groups until it stoped working due to it useing expired cdm ke

This is an app that allows users to upload photos and display and store the photos in a file until the user deletes them.
This is an app that allows users to upload photos and display and store the photos in a file until the user deletes them.

Qt Photo App This is an app that allows users to upload photos and display and store the photos in a file until the user deletes them. Setup python3 -

An iCal file to transport you to a new place every day until you die
An iCal file to transport you to a new place every day until you die

everydayvirtualvacation An iCal file to transport you to a new place every day until you die The library is closed πŸ˜” πŸ˜” including a video of the plac

generate HPC scheduler systems jobs input scripts and submit these scripts to HPC systems and poke until they finish

DPDispatcher DPDispatcher is a python package used to generate HPC(High Performance Computing) scheduler systems (Slurm/PBS/LSF/dpcloudserver) jobs in

Deleting someone else's Instagram account, repeat until the target account is blocked.
Deleting someone else's Instagram account, repeat until the target account is blocked.

Program Features πŸ“Œ Instagram report V4. πŸ“Œ Coded with the latest version of Python. πŸ“Œ Has automatic scheduling. πŸ“Œ Full account report. πŸ“Œ Report a

:game_die: Pytest plugin to randomly order tests and control random.seed

pytest-randomly Pytest plugin to randomly order tests and control random.seed. Features All of these features are on by default but can be disabled wi

Implementation of:
Implementation of: "Exploring Randomly Wired Neural Networks for Image Recognition"

RandWireNN Unofficial PyTorch Implementation of: Exploring Randomly Wired Neural Networks for Image Recognition. Results Validation result on Imagenet

A simple python script to fetch Bing daily images and set them randomly using hsetroot

Daily bing walls A script to set the daily bing image as the wallpaper. It can also set from a random set of previously fetched wallpapers. usage: dai

Randomly distribute members by groups making sure that every sector is represented

Generate Groups Randomly distribute members by groups making sure that every sector is represented The Scenario Imagine that you have a large group of

a simple function that randomly generates and applies console text colors
a simple function that randomly generates and applies console text colors

ChangeConsoleTextColour a simple function that randomly generates and applies console text colors This repository corresponds to my Python Functions f

Coffeematcher is a python library to randomly match participants for coffee meetings.

coffeematcher coffeematcher is a python library to randomly match participants for coffee meetings. Installation Clone the repository: git clone https

Add your new words to a text file and get them randomly.

Memorize-New-Words In this very very very little project, I've wrote a code to memorize new english words. Therefore you can add the words and their m

Blender pluggin (python script) that adds a randomly generated tree with random branches and bend orientations

Blender pluggin (python script) that adds a randomly generated tree with random branches and bend orientations

Randomly picks between your favourite meals for you when you're feeling indecisive.
Randomly picks between your favourite meals for you when you're feeling indecisive.

Food Recommendations Desktop application created with python and tkinter. The goal for this application is to provide a way for users to enter and sav

A Python script which randomly chooses and prints a file from a directory.

___ ____ ____ _ __ ___ / _ \ | _ \ | _ \ ___ _ __ | '__| / _ \ | |_| || | | || | | | / _ \| '__| | | | __/ | _ || |_| || |_| || __

Randomly selects two teams based on who is in a voice channel on Discord

TeamPickerDiscordBot Randomly selects two teams based on who is in a voice channel on Discord What I Learned The ins and outs of Python as this was my

Given a 2D triangle mesh, we could randomly generate cloud points that fill in the triangle mesh
Given a 2D triangle mesh, we could randomly generate cloud points that fill in the triangle mesh

generate_cloud_points Given a 2D triangle mesh, we could randomly generate cloud points that fill in the triangle mesh. Run python disp_mesh.py Or you

A simple API that will return a key-value pair of randomly generated UUID
A simple API that will return a key-value pair of randomly generated UUID

A simple API that will return a key-value pair of randomly generated UUID. Key will be a timestamp and value will be UUID. While the server is running, whenever the API is called, it should return all the previous UUIDs ever generated by the API alongside a new UUID.

Owner
Nick
Hey, welcome to my GitHub page! If you're interested, I accept discord bot commissions.
Nick
Maze generator with most popular shapes - hexagon, triangle, square

Maze-Generator Maze generator with most popular shapes - hexagon, triangle, square (sqaure not implemented yet): Theory: Planar Graph https://en.wikip

Kacper Plesiak 2 Dec 28, 2021
This Github Action automatically creates a GIF from a given web page to display on your project README

This Github Action automatically creates a GIF from a given web page to display on your project README

Pablo Lecolinet 28 Dec 15, 2022
FrostedGlass is a translucent frosted glass effect widget, that creates a context with the background behind it.

FrostedGlass FrostedGlass is a translucent frosted glass effect widget, that creates a context with the background behind it. The effect is drawn on t

Kivy Garden 24 Dec 10, 2022
This app finds duplicate to near duplicate images by generating a hash value for each image stored with a specialized data structure called VP-Tree which makes searching an image on a dataset of 100Ks almost instantanious

Offline Reverse Image Search Overview This app finds duplicate to near duplicate images by generating a hash value for each image stored with a specia

null 53 Nov 15, 2022
API to help generating QR-code for ZATCA's e-invoice known as Fatoora with any programming language

You can try it @ api-fatoora api-fatoora API to help generating QR-code for ZATCA's e-invoice known as Fatoora with any programming language Disclaime

نافع Ψ§Ω„Ω‡Ω„Ψ§Ω„ΩŠ 12 Oct 5, 2022
A minimal python script for generating bip39 seed phrases, and corresponding Seed Signer Seed seed phrase qr code ready for offline printing.

A minimal python script for generating bip39 seed phrases, and corresponding Seed Signer Seed seed phrase qr code ready for offline printing.

CypherToad 8 Sep 12, 2022
This tool allows the user to convert a 16 by 16 image into a list with numbers representing an object/character.

Room Formatter This tool allows the user to convert a 16 by 16 image into a list with numbers representing an object/character. There is cur

Thomas Landstra 1 Nov 13, 2021
Repo for investigation of timeouts that happens with prolonged training on clients

Flower-timeout Repo for investigation of timeouts that happens with prolonged training on clients. This repository is meant purely for demonstration o

null 1 Jan 21, 2022
Msos searcher - A half-hearted attempt at finding a magic square of squares

MSOS searcher A half-hearted attempt at finding (or rather searching) a MSOS (Magic Square of Squares) in the spirit of the Parker Square. Running I r

Niels MΓΌndler 1 Jan 2, 2022
DexterRedTool - Dexter's Red Team Tool that creates cronjob/task scheduler to consistently creates users

DexterRedTool Author: Dexter Delandro CSEC 473 - Spring 2022 This tool persisten

null 2 Feb 16, 2022