On Project Euler Problem 11 (link: https://projecteuler.net/problem=11), there is a 20x20 grid of numbers. However, when I pasted it into Mathematica, I got this strange result where the times symbol 'x' is interlaced between the numbers.

Here is an image that shows this (again, this grid is directly copy/pasted from the website): http://i.gyazo.com/6336b0c44a1cb6afef7ee3f962c06522.png

Are there any in-line commands/functions you can use on that mass of numbers so that you can enter everything once and it will return a nicely formatted list/matrix? The only way I could think of, which did not make use of in-line commands, was very tedious: Ctrl+F --> Replace all "x" with "," and then manually entering curly brackets to turn the grid into a list/matrix.

Ideally, I want to turn it into something like {{1,2,3},{4,5,6},{7,8,9}}, but of course with the appropriate elements.

Edit: From this StackExchange post (Cut and paste data from a spreadsheet), I found that

data = ImportString["(copy/paste grid here)", "TSV"] 

works exactly as desired :)

Edit 2: Just kidding, the above doesn't work. It doesn't add commas.


Here is one way to import the grid into Mathematica

  Import["http://projecteuler.net/problem=11", "Plaintext"], 
  DigitCharacter ..][[10 ;; -8]], 
 20] // ToExpression
| improve this answer | |
  • $\begingroup$ Thank you for this :) Can you explain what the ". . " are for? I looked in the documentation for DigitCharacter, and that seems to be paired with the function, kind of like how pure functions involve the pairing of # and &. $\endgroup$ – A is for Ambition Jun 29 '15 at 3:35
  • 1
    $\begingroup$ @AisforAmbition It is a String Pattern. DigitCharacter .. means one or more digits. $\endgroup$ – Karsten 7. Jun 29 '15 at 3:39
  • $\begingroup$ A problem I ran into was that since this treats every digit as a string and not a standard number, the inside elements cannot be operated upon by the Times function. What can I do if I want to multiply the matrix's first diagonal, for example? $\endgroup$ – A is for Ambition Jun 29 '15 at 3:46
  • $\begingroup$ Ah, thank you very much :) $\endgroup$ – A is for Ambition Jun 29 '15 at 3:50
  • 1
    $\begingroup$ @Karsten7., output is the same w/o "Plaintext". $\endgroup$ – alancalvitti Jun 29 '15 at 17:38

Szabolcs's Past Tabular Data palette still works just like it should. This palette is so useful it is one of only two I always open on start-up.

If for some reason you are looking for a one-off solution here is mine:

Block[{Times = List}, (*paste here*) ] ~Partition~ 20

Reference: Convert head Times to List

| improve this answer | |
  • $\begingroup$ Very elegant method, Mr. Wizard. Once again, you've been a tremendous help :) Thank you! $\endgroup$ – A is for Ambition Jun 29 '15 at 9:05
  • $\begingroup$ Nice. With and Module also work. $\endgroup$ – alancalvitti Jun 29 '15 at 17:43

Highlight the list and paste into a text file and then

t=Import["put path of text file in here","Table"]

works for me. Does it work for anyone else?

| improve this answer | |
  • $\begingroup$ No, I get HTML, eventually containing ... {32, 98, 81, 28, 64, 23, 67, 10, "<span", \ "style=\"color:#ff0000;\"><b>26</b></span>", 38, 40, ... $\endgroup$ – alancalvitti Jun 29 '15 at 17:50
  • $\begingroup$ It certainly ought to. This is the basic method without direct copy-and-paste, but it's the one I used for years before the introduction of ImportString. $\endgroup$ – Mr.Wizard Jun 29 '15 at 17:51
  • $\begingroup$ @alancalvitti Is your browser really copying HTML code? Which do you use? $\endgroup$ – Mr.Wizard Jun 29 '15 at 17:51
  • $\begingroup$ @Mr.Wizard Possible confusion: bobbym wrote "put path..." so I used Import["http://projecteuler.net/problem=11","Table"] $\endgroup$ – alancalvitti Jun 29 '15 at 17:52
  • $\begingroup$ @alancalvitti but first Highlight the list and paste into a text file and then $\endgroup$ – Mr.Wizard Jun 29 '15 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.