r/dailyprogrammer • u/Cosmologicon 2 3 • Apr 04 '16
[2016-04-04] Challenge #261 [Easy] verifying 3x3 magic squares
Description
A 3x3 magic square is a 3x3 grid of the numbers 1-9 such that each row, column, and major diagonal adds up to 15. Here's an example:
8 1 6
3 5 7
4 9 2
The major diagonals in this example are 8 + 5 + 2 and 6 + 5 + 4. (Magic squares have appeared here on r/dailyprogrammer before, in #65 [Difficult] in 2012.)
Write a function that, given a grid containing the numbers 1-9, determines whether it's a magic square. Use whatever format you want for the grid, such as a 2-dimensional array, or a 1-dimensional array of length 9, or a function that takes 9 arguments. You do not need to parse the grid from the program's input, but you can if you want to. You don't need to check that each of the 9 numbers appears in the grid: assume this to be true.
Example inputs/outputs
[8, 1, 6, 3, 5, 7, 4, 9, 2] => true
[2, 7, 6, 9, 5, 1, 4, 3, 8] => true
[3, 5, 7, 8, 1, 6, 4, 9, 2] => false
[8, 1, 6, 7, 5, 3, 4, 9, 2] => false
Optional bonus 1
Verify magic squares of any size, not just 3x3.
Optional bonus 2
Write another function that takes a grid whose bottom row is missing, so it only has the first 2 rows (6 values). This function should return true if it's possible to fill in the bottom row to make a magic square. You may assume that the numbers given are all within the range 1-9 and no number is repeated. Examples:
[8, 1, 6, 3, 5, 7] => true
[3, 5, 7, 8, 1, 6] => false
Hint: it's okay for this function to call your function from the main challenge.
This bonus can also be combined with optional bonus 1. (i.e. verify larger magic squares that are missing their bottom row.)
15
u/perry_the_blu_herron Apr 05 '16
Extremely verbose and explicit, as there are no arrays (as far as I can find) or anything similar. It's just done for fun anyway.
LOLCODE
Output
6
u/Jc_croft1 Apr 04 '16
In Matlab. Works for any sized matrix. First line calculates Magic number, defined by M = n(n2 +1)/2, where n is the size of the matrix. Then creates an array of all the sums (cols, rows, diags). Finally, it checks they're all equal to M.
Code:
Test:
6
u/smapti Apr 05 '16
C++, no bonus. Decided to write the ugliest thing possible.
11
u/Philboyd_Studge 0 1 Apr 05 '16
Without the bonuses is easy, there's only 8 valid ones. :P
5
u/deadlypanda4 Apr 04 '16 edited Apr 04 '16
Python 2.7 - All bonuses
Output
Edit: Added comments for clarification
5
u/fphat Apr 04 '16 edited Apr 05 '16
Dart
I wanted to try to do this as easy-to-follow as possible, the way you'd expect mature source code to be (as opposed to clever hack).
Options 1 and 2 implemented.
The source code is its own unit test.
Results:
Feedback (especially wrt readability) welcome.
5
u/porphyro Apr 05 '16 edited Apr 05 '16
CJam
Try it online!
With Bonus 1:
Try it online!
4
u/franza73 Apr 05 '16
Python 2.7
4
u/deB4SH Apr 05 '16
Clojure
feel free to say something about this code :3
https://gist.github.com/deB4SH/e68be82e0c0413b94ee499244ff3cbb4
4
u/4Q0olIk Apr 06 '16 edited Apr 06 '16
MS SQL 2008 & 2016
While this is not a solution possible within a function (UDF), I figured it was worth a (first) post.
3
u/Godspiral 3 3 Apr 04 '16 edited Apr 04 '16
in J,
(3rd one seems to be true)
0
oops with diagonals, (3rd is false now)
0
should work for any size,
bonus #2: hard coded to 3
4 9 2
variable sized, but passing square size parameter as x.
8 6 7 16
3
u/Helix_Molordo Apr 04 '16
GNU Prolog solution using the finite domain solver
Automagically solves the two bonuses by how Prolog works too!
3
u/Garth5689 Apr 04 '16 edited Apr 04 '16
python 3.5
doing it as simply as possible, nothing tricky here. Only works for 3x3, no bonus.
update: added variable size square bonus
code:
tests:
results:
3
u/StriveForMore Apr 05 '16
My submission, done in C#. I'm looking for ways to optimize this, also, first submission to the sub. No Bonuses.
3
u/thorwing Apr 05 '16 edited Apr 05 '16
JAVA
with both boni, pretty proud of this one.
OUTPUT
3
u/rufio1 Apr 07 '16
In Java. Had some fun with this one.
3
u/ocus Apr 15 '16 edited Apr 15 '16
Javascript (ES6)
With bonus 1.
Takes an array as input.
Test:
Output:
Edit: added test for 4x4 and 5x5.
2
u/cheers- Apr 04 '16 edited Apr 04 '16
Scala
Quickly put togheter
Edit: validates any magic square with numbers in range [1; n * n]
2
u/casualfrog Apr 04 '16
JavaScript (including first bonus, feedback welcome)
Usage:
2
u/hbeggs Apr 04 '16
Here's the shortest I could do in Python 2.7.
I'm sure there are better ways to do it, so lemme know if there is anything horrendously wrong.
2
u/[deleted] Apr 04 '16
Not-so-readable Python
2
u/chunes 1 2 Apr 04 '16
Java using the power of magic numbers
2
u/[deleted] Apr 04 '16 edited Apr 04 '16
in Python, first submission and feedback would be greatly appreciate since I started programming about a week ago:
https://github.com/deamon32/DailyProgrammer-261-easy
2
u/perry_the_blu_herron Apr 04 '16 edited Apr 05 '16
Python, with bonuses
The following seem to all pass.
The inputs used, in case anyone wants to test theirs with some big magic squares.
input0 = [8, 1, 6, 3, 5, 7, 4, 9, 2]# => true
input1 = [2, 7, 6, 9, 5, 1, 4, 3, 8]# => true
input2 = [3, 5, 7, 8, 1, 6, 4, 9, 2]# => false
input3 = [8, 1, 6, 7, 5, 3, 4, 9, 2]# => false
bonusinput1= [1,15,14,4,12,6,7,9,8,10,11,5,13,3,2,1]# => false
bonusinput2= [8,11,14,1,13,2,7,12,3,16,9,6,10,5,4,15]# => true
bonusinput3= [73,77,11,66,25,107,4,112,12,99,85, 84,55,70,33,63,6,106,32,108,54,60, 18,76,47,110,35,96,36,78,46,116,13, 88,26,74,43,65,1,92,42,109,31,100, 75,82,34,69,51,119,50,72,37,58,24, 20,9,103,5,120,117,118,2,80,7,90, 22,81,57,71,48,121,49,61,38,59,64, 102,27,111,40,98,3,86,41,114,19,30, 17,95,44,101,56,83,39,115,45,62,14, 79,52,104,28,89,8,68,29,67,53,94, 93,91,16,105,21,10,23,87,15,113,97]# => true
bonusinput4= [37, 78, 29, 70, 21, 62, 13, 54, 5, 6, 38, 79, 30, 71, 22, 63, 14, 46, 47, 7, 39, 80, 31, 72, 23, 55, 15, 16, 48, 8, 40, 81, 32, 64, 24, 56, 57, 17, 49, 9, 41, 73, 33, 65, 25, 26, 58, 18, 50, 1, 42, 74, 34, 66, 67, 27, 59, 10, 51, 2, 43, 75, 35, 36, 68, 19, 60, 11, 52, 3, 44, 76, 77, 28, 69, 20, 61, 12, 53, 4, 45]# => true
bonusinput5= [1,120,121,48,85,72,73,60,97,24,25,144, 142,27,22,99,58,75,70,87,46,123,118,3, 11,110,131,38,95,62,83,50,107,14,35,134, 136,33,16,105,52,81,64,93,40,129,112,9, 8,113,128,41,92,65,80,53,104,17,32,137, 138,31,18,103,54,79,66,91,42,127,114,7, 5,116,125,44,89,68,77,56,101,20,29,140, 139,30,19,102,55,78,67,90,43,126,115,6, 12,109,132,37,96,61,84,49,108,13,36,133, 135,34,15,106,51,82,63,94,39,130,111,10, 2,119,122,47,86,71,74,59,98,23,26,143, 141,28,21,100,57,76,69,88,45,124,117,4]# => true
bonus2input1=[8, 1, 6, 3, 5, 7]# => true
bonus2input2=[3, 5, 7, 8, 1, 6]# => false
bonus2input3= [1,120,121,48,85,72,73,60,97,24,25,144, 142,27,22,99,58,75,70,87,46,123,118,3, 11,110,131,38,95,62,83,50,107,14,35,134, 136,33,16,105,52,81,64,93,40,129,112,9, 8,113,128,41,92,65,80,53,104,17,32,137, 138,31,18,103,54,79,66,91,42,127,114,7, 5,116,125,44,89,68,77,56,101,20,29,140, 139,30,19,102,55,78,67,90,43,126,115,6, 12,109,132,37,96,61,84,49,108,13,36,133, 135,34,15,106,51,82,63,94,39,130,111,10, 2,119,122,47,86,71,74,59,98,23,26,143]# => true
2
u/[deleted] Apr 04 '16
C++. This is my very first submission to this sub. No bonuses, but the function in my program can be modified to check a grid of arbitrary size.
Please let me know if this isn't formatted correctly! I welcome your comments/suggestions,
2
u/Scroph 0 0 Apr 04 '16 edited Apr 05 '16
D (dlang) solution, both bonuses included :
Edit : looks like I misunderstood the problem. I wrote overloads for the isPossible and isValid to automatically deduce the sum to which the rows, the columns and the diagonals must add to.
Usage example : e261.exe 8 1 6 3 5 7 4 9 2
2
u/a_Happy_Tiny_Bunny Apr 05 '16
Haskell
For now, it only does the optional bonus 1. It does no input validation. The input is a matrix: cells are integers, columns are separated by spaces, and rows by newlines.
2
u/Daanvdk 1 0 Apr 05 '16 edited Apr 05 '16
Haskell
Works with a list of lists that contain all groups of indices where the elements of should equal 15, it then maps these ranges to the element values, sums them up and filters the list on the fact if the value is not 15, it then checks the list to be empty as it should be. It also sorts the input and then compares it to a list containing 1 to 9 in order to see if it contains all digits from 1 to 9.
EDIT: Now with Bonus 1
EDIT2: Now with Bonus 2 (Combined with Bonus 1)
2
u/jnd-au 0 1 Apr 05 '16 edited Apr 05 '16
Scala with both bonuses, using the diagonal as the goal sum for all sizes. Tests:
Solution for Examples, Parser, Bonus 1 and Bonus 2:
[PS. Empty input is defined as non-magical, although others may say it’s super magical.]
2
u/ChazR Apr 05 '16
Haskell. Fairly verbose, and breaks it down into small functions.
Works on any size square. It can try to complete squares for bonus 2 with either a missing row or column.
Was fun.
2
u/[deleted] Apr 05 '16 edited Apr 05 '16
R
First post, would love to hear suggestions on how to make my function faster/better/stronger! The code should work for any square matrix.
Input:
Output:
Testing my code for a 1000 x 1000 magic square:
2
u/kenny11cz Apr 05 '16 edited Apr 05 '16
Javascript with one bonus
text is input, taken from textarea element and output is jQuery selector of output div
Code:
Output:
Edit: I'm sorry, my first version was bad because I misunderstood task.
2
u/svgwrk Apr 05 '16
Rust, with the first bonus.
main.rs:
square.rs:
2
u/LahteL Apr 06 '16
Written in Java with no bonus. My first submission!
2
u/jee_lement Apr 07 '16 edited Apr 07 '16
No bonus. Would love to hear where my C++ code sucks.
2
u/tcbenkhard Apr 07 '16
Java
2
u/Rubiksdarcy Apr 09 '16
Python 3 One Line
Takes the numbers through stdin left to right, top to bottom, separated with spaces.
Example: 8 1 6 3 5 7 4 9 2
Edit: to my great dismay, this submission is not PEP compliant.
2
u/boiling_tunic Apr 11 '16
Ruby
Not sure how happy I am but it works (I think). Includes bonus 1. Questions or comments welcome.
2
u/shayhtfc Apr 13 '16
Ruby
Could be refactored quite a bit further and is a bit longwinded, but does the job. Uses some example test cases from someone elses submission
2
u/nickadin Apr 13 '16 edited Apr 13 '16
This is my solution in clojure, based on '2 dimensional inputs'
2
u/561288511680985 Apr 25 '16
Python 3
My first submission, any suggestions are appreciated!
2
u/SuperSmurfen May 09 '16 edited May 09 '16
Python-solution (including bonus 1)
My first ever submission. I've just started exploring programming (through python) and I find it really fun and challenging. I tried to do the first bonus and I think my solution works for all NxN squares! I have no idea how code comments should work but I tried to include some. Feedback welcome!
I know this challenge is quite old but I've tried to find easy ones on here I think I can do but I figured I might as well submit it since I made it work.
1
u/goodevilgenius Apr 04 '16
In JavaScript, checked in node. I made my function require a 2D-array. Works for bonus 1.
Any suggestions for improvement appreciated.
1
u/pburns1587 Apr 04 '16
Here it is for the main challenge and optional bonus 1 - will potentially come back for bonus 2. Quite sure this isn't the best way to do this, any help greatly appreciated as I'm still learning!!
python3.5
1
u/CTMemorial Apr 04 '16 edited Apr 04 '16
C
A bit unnecessarily long, but at least I tried to do all the bonuses. Bonus 1 should work but I'm too tired right now to come up with tests for it.
Edit: Hopefully made it so that the check for whether or not you can make a grid with a missing last row will work for matrices larger than 3x3 now.
Results
1
u/[deleted] Apr 04 '16 edited Dec 10 '16
[deleted]
0.9205
1
u/savagenator Apr 04 '16
Python 3.5 with Bonus, and I even used the quadratic equation somewhere.
Solutions with Bonus 1:
Output:
Bonus 2:
Output:
1
u/esuito Apr 04 '16
C# Bonus 1 & 2
1
u/Grygon Apr 04 '16
Elegant solution in Python 3 that supports bonus 1:
1
u/Steve132 0 1 Apr 04 '16 edited Apr 05 '16
C++, bonus 1 included
1
u/bearific Apr 04 '16
Python 2.7: with bonus 1.
Tried making a oneliner, but decided to initialize m and n on separate lines to keep it somewhat readable.
1
u/pxan Apr 04 '16
Trying to practice my C++. I feel like my main function is cheating. I don't know how to reassign a vector. Yikes.
1
u/primaryobjects Apr 05 '16
R
Run | Gist
Validates magic squares of any size.
1
u/[deleted] Apr 05 '16 edited Jul 04 '17
deleted 0.9747 What is ^^^this?
1
u/Farmzenda Apr 05 '16
In Julia: Does the first bonus -- maybe I will do the second tomorrow and post on my Github
1
u/Dr_Niggle Apr 05 '16
Java - with Bonus 1. Any suggestions are very much welcomed. First time posting solution and not too advanced in programming.
1
u/smidqe Apr 05 '16 edited Apr 05 '16
Java - with bonus 1, might do the bonus 2 later. I'm a first time poster on this subreddit so feedback is always welcome.
Edit: Fixed a logic mistake
1
u/[deleted] Apr 05 '16
JavaScript
Feedback is welcome
JsFiddle
1
u/draegtun Apr 05 '16 edited Apr 05 '16
Rebol (with bonus 1)
Example usage (in Rebol console):
Here's a cheeky non bonus (3x3) magic square solution:
1
u/JulianDeclercq Apr 05 '16 edited Apr 05 '16
C++ - No bonus
edit: didn't know about std::boolalpha flag for std::cout when making this. So I overloaded operator <<= instead to print the info (a)
Output
1
u/glenbolake 2 0 Apr 05 '16
Python 3; both bonuses. Also doesn't require that the lowest number be 9 (e.g., 2-10 for a 3x3)
1
u/LiveOnTheSun Apr 05 '16
C# with first bonus.
Output:
1
u/marinated_pork Apr 05 '16
Ruby with bonus one. Haven't written Ruby in awhile:
1
u/FallingFist Apr 05 '16 edited Apr 05 '16
C#
Outputs
Some monotonous code, could probably be optimised slightly. Works all right.
1
u/stratfordfellow Apr 05 '16 edited Apr 05 '16
Python No Bonus.
1
u/SwampCabbageMan Apr 05 '16
Using Java, first bonus, added support for changing the sum as well. Diagonal feels a bit messy, feedback is appreciated! MagicSquareValidator.java
SquareValidator.java
1
u/tonycarl Apr 05 '16 edited Apr 05 '16
C# All bonus
Output on my Mac
EDIT Added output and fixed formatting.
1
u/kjr1995 Apr 05 '16
Here's my python solution.
1
u/[deleted] Apr 06 '16
Decided to start learning Python 3.5, this is the first thing i've done with it so far from the best posible design. However, should work squares of any size. Please tell me if you spot anything that is bad a programming/Python habit or have any questions.
If a square fails the magic line test, it will output debug data, otherwise just prints a message telling you that it passed.
1
u/josephalan4 Apr 06 '16
Java Solution. Did bonus 1.
1
u/NorthwestWolf Apr 06 '16
Python 2.7 Checks magic squares of all sizes
1
u/Gobbedyret 1 0 Apr 06 '16 edited Apr 06 '16
Python 3.5 With bonus 1 and bonus 2.
It's fast, and works for all sizes (although it keeps the square in memory). It's not extended to N-dimensional hypercubes, although that would be cool.
1
u/cheezew1zz Apr 06 '16
First post on daily programmer. Have been learning OCaml recently so thought I'd give it a go with that. This attempt doesn't do the bonuses.
called like so: is_magic_sq "276951438"
1
u/[deleted] Apr 06 '16 edited Apr 06 '16
C# with both bonuses. First submission so hopefully this turns out.
1
u/Kerow Apr 06 '16
C++ could get some cleaning
I should be do this more often I really enjoyed this exercise.
1
u/[deleted] Apr 06 '16
C++ rubbish code
include <iostream>
using namespace std;
int main() {
}
1
u/D0ct0rJ Apr 06 '16 edited Apr 06 '16
In C++, probably could've been more clever about bonus 2, but brute force works on such a small problem. Will come up with something general so I can combine bonuses 1 and 2.
EDIT: Created a general permutation generator, but in a dumb way. Can only handle up to 11x11 squares.
EDIT2: Smarter permutations now - no need to store 12*12! numbers at once, generate them on the fly. Now can do Bonus 1, Bonus 2, and Bonus 1+2
And the output:
1
u/voice-of-hermes Apr 06 '16 edited Apr 06 '16
Implements both optional bonuses, plus more: will solve any unambiguous partial solution using simple, deductive logic (repeatedly adding values to rows, columns, and diagonals when only a single value is missing).
EDIT: Refactored to simplify logic a bit and remove some redundancy.
1
u/kwill1429 Apr 07 '16 edited Apr 07 '16
Feedback would be appreciated:
This completes bonus 1, 2, 1+2
Language: C#
Edit: After looking at someone elses code replaced my while true with Console.ReadLine()
1
u/[deleted] Apr 07 '16 edited Apr 07 '16
Ruby with optional 1. Would appreciate feedback
1
u/a_th0m Apr 07 '16 edited Apr 07 '16
MATLAB 2010a with bonus 1. First time posting here :D
Script
Command Window
1
u/koolcaz Apr 07 '16 edited Apr 08 '16
Python 3
My first programming post on reddit. Learning python. Edit - edited to update the streamlined version of the code that does the optional content
1
u/GlowingChemist Apr 07 '16
quick attempt in C should work for any size magic square if you ajust the input argument for the function: #include<stdio.h>
1
u/taka- Apr 07 '16
** Javascript ** : No bonus
1
u/[deleted] Apr 07 '16
Short and sweet in Fortran; gotta love those array intrinsics and implied do-loops. The function should work for any size square array.
1
u/Unremarkable Apr 07 '16 edited Apr 08 '16
Python
Optional 1 & 2
Run it at rextester
stole some code from /u/kimmy314
1
u/[deleted] Apr 08 '16
In Java. I feel like I tried to get a little too clever with this. I was going to hard-code each square as a 2-dimensional array but I thought, man that's almost too easy. Takes a 1 dimensional array from the command line and verifies it using a procedural approach. Feedback, suggestions, and criticism always welcome :-)
1
u/_HyDrAg_ Apr 08 '16
Python 2.7 with bonus 1. Uses two loops, one for the columns and rows and one for the diagonals. I think there's a better way to do this, maybe even without loops. Any feedback would be appreciated!
1
u/naliuj2525 Apr 08 '16 edited Apr 08 '16
First time posting here. This is my solution in Python 3.4 with none of the bonuses. Probably the easiest one I've tried doing yet. I might give the bonuses a shot later when I have some time.
1
u/jonnywoh Apr 08 '16
Python 3.5, with bonus 1
1
u/TrueKhaos Apr 08 '16
First time first steps, done it elementary python3. Hope you enjoy the crawling
1
u/[deleted] Apr 08 '16
Golang
Without bonuses to keep thing simple :)
1
u/DanSensei Apr 09 '16
Here's my c++ implementation, halfway through a beginner course on UDemy
1
u/[deleted] Apr 09 '16 edited Apr 09 '16
Java
Yeah I have magic numbers, sue me! If you have any feedback, I'd love to hear it.
1
u/IceDane 0 0 Apr 09 '16
Haskell
Just uses lists and backtracking. Since the
genSquarefunction takes the current square being 'built' as an argument, we get the partial verification for free. Tried using some heuristics like checking complete rows and verifying that they sum up to the magic sum before continuing. Using anIntSetfor keeping track of numbers that are already in use because it turned out that doing that naively by using\\(which is set difference for lists) was taking up the majority of the execution time.Still pretty inefficient. Takes a while (~10s ?) on my machine to generate even the first 4x4 square. But of course, there are O(n!) squares, and 16! is pretty huge, so that's no surprise.
1
u/[deleted] Apr 09 '16 edited Apr 09 '16
Using JAVA Tried adding a few of my own little bonuses; You can fill in the values yourself and it locks the program once you complete it. Once you put a number in one cell it is not available in any other cell until it is removed from that original cell. Let me know thoughts/criticisms please.
CODE: https://gist.github.com/anonymous/20f1ed8e63674c6aabd4381be49fd355 Was longer than 10000 so could not just copy it here , sorry.
1
u/CaptainCa Apr 09 '16
Javascript
Had a bit of fun with this one, only uses two loops to calculate validity of a magic square. Includes bonus 1.
Output:
Just for fun, a minified version (thanks https://javascript-minifier.com/)
1
u/[deleted] Apr 09 '16
Scala, with bonus 2:
This is my first Scala program -- feedback welcome!
1
u/HDInfinity Apr 10 '16
Ruby feedback appreciated, couldn't find a slick way to do diagonals :/
1
u/[deleted] Apr 10 '16
In Factor:
1
u/[deleted] Apr 10 '16 edited Apr 10 '16
In C++ no bonus. This is my first time submitting a solution here. Its kind of messy imo but it gets the job done and has been tested with different values in the array. But I have a feeling there are better methods to do this. I would greatly appreciate critique/feedback!
1
u/Pootwoot Apr 10 '16
Fairly small solution, one print statement to return the result.
PYTHON - Bonus 1 only
1
u/Porso7 Apr 11 '16
Node.js (Javascript) no bonus. First submission here, confirmed working. Reads newline-seperated input and validates output from input.txt and output.txt.
main.js:
input.txt:
output.txt:
1
u/ludanortmun Apr 11 '16
Python 3, bonus 1 and 2. Expects a two-dimensional array as input and returns a boolean value. First submission here, any feedback is welcome!
Code:
Input:
Output:
1
u/KevinDidNothingWrong Apr 11 '16
Python 3
This is my first submission to DailyProgrammer so any feedback is most welcome :)
1
u/gravitationalBS Apr 11 '16 edited Apr 12 '16
Python with bonus 1
bonus 2 coming later
Please judge my code.
Edit: I realized it didn't work so I fixed it.
1
u/Menerva Apr 11 '16
Python 3, with bonus 1. Might do bonus 2 later (hence why I defined a function), but I'm not sure.
1
u/bibiteinfo Apr 12 '16
First program in Rust
Output
1
u/Adamtreepuncher Apr 12 '16
Python 3
Make of it what you will
1
u/EtDecius Apr 13 '16 edited Apr 13 '16
C++. 82 lines, includes Bonus 1.
Output:
1
u/sanadan Apr 13 '16
My first submission. I know it's a week old, but this was a bit of fun. C# includes Bonus 1.
Would love some feedback actually.
1
u/Ouss4 Apr 14 '16
Python 3 with bonus 1.
1
u/EtDecius Apr 14 '16 edited Apr 14 '16
C++. Second submission, this time with Bonus 1 & 2 combined. Huzzah!
Output:
1
u/totallyuneekname Apr 14 '16 edited Apr 14 '16
Java This is my first submission to this subreddit. This solution includes Bonus 1. I would appreciate any feedback very much.
1
u/Arrorn Apr 15 '16
PHP
Class Square. The constructor takes an single dimensional array of the numbers contained, evaluates them and then caches the results in the properties magical and potentiallyMagical, accessed through isMagical() and canBeMagical().
The static functions _isMagical and _canBeMagical take multi-dimensional arrays, and return a boolean result.
1
u/dpxvx Apr 17 '16
Ok! Here we go again, and hopefully I finally get it to format correctly. I made 3 different functions for all three bonuses (although they could easily just as be combined into one, and less ugly) written in C++. The code is kind of ugly though (and me formatting it on here isn't helping either). I had commented a lot more explaining things and algos and stuff but I thought it was too big. This is my first submission! If anyone wants me to explain what's happening just let me know.
1
u/LordJackass Apr 17 '16 edited Apr 17 '16
C++ solution. Implemented first bonus, the second is yet to be done:
1
u/peteyatwork Apr 18 '16 edited Apr 18 '16
C# solution with bonus 1 and 2
I'm pretty new and would love any feedback. I tried to keep it fairly verbose and scale-able besides bonus 2.
I still don't have a firm understanding if everything needs to be static or not and how my code could be better/different using other methods.
https://gist.github.com/bigolpete/82e3c3f216b62083143cabf00af2a50c
1
u/waterskier2007 Apr 21 '16
Swift with Bonus 1
1
u/mmstiver Apr 22 '16
F# - A little late to the party. I picked up F# a little over a week ago, and been noodling around with it. A bit of recursive fun. No bonus.
1
u/[deleted] Apr 26 '16
Go
Bonus 1, but no bonus 2... yet
1
u/youwantedmyrealuser Apr 30 '16
no bonus just a simple java solution
1
u/shiniko May 12 '16
C++ no bonus.
My first submission post to this subreddit! Plan on doing more to practice! My code may be a bit too simple, welp
1
u/kaktuszon May 15 '16
C++ no bonuses #include <iostream>
1
u/MyLettuce May 20 '16
Java solution for 3x3, no bonuses yet. Takes user input for the array
1
u/vishal_mum May 21 '16
Rust solution. Using for loop stumped me! I kept getting an error "use of moved variable", and then realized that for i in vector transfers ownership; nice lesson learned!
1
u/munkyeetr May 22 '16
VB.NET w/ Bonus 1
1
u/Tetris_King May 23 '16
C# - Works with any size magic square.
1
u/[deleted] May 23 '16
Java (Beginner) - Bonus 1 + 2 combined
validation in case array length doesnt match the question
public class DP_261 {
}
1
u/[deleted] May 30 '16
PYTHON 3 (Beginner, first submission, feedback welcome)
1
u/mavrickman9800 Jun 07 '16
Here's a probably overly long and complicated Java solution for both bonuses and any size for everything.
1
u/DXPower Jun 10 '16
Python 3, works for any size for both bonuses
1
u/jpan127 Jun 19 '16
Any feedback would be loved!
Python 31
u/Humble_Boy619 Jul 02 '16
JAVA
import java.util.Scanner; public class yo {
1
u/4kpics Jul 21 '16 edited Jul 21 '16
C89, compiles with
cc magic.c, both bonuses combined.Code: https://gist.github.com/anonymous/7737943c9db500b4a041439f63bf42d8
Output:
1 1 0 0 1 0. 1 is true, 0 is false.1
u/Sirflankalot 0 1 Sep 25 '16
Python implementation, trying to be terse.
0
u/[deleted] Apr 04 '16
[deleted] — view deleted comment
0
u/[deleted] Apr 05 '16
[deleted] — view deleted comment
0
u/[deleted] Apr 07 '16
[deleted] — view deleted comment
0
u/[deleted] May 22 '16
[deleted] — view deleted comment