7
$\begingroup$

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.

$\endgroup$
0

3 Answers 3

9
$\begingroup$

Here is one way to import the grid into Mathematica

Partition[
 StringCases[
  Import["http://projecteuler.net/problem=11", "Plaintext"], 
  DigitCharacter ..][[10 ;; -8]], 
 20] // ToExpression
$\endgroup$
6
  • $\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$
    – user155812
    Jun 29, 2015 at 3:35
  • 1
    $\begingroup$ @AisforAmbition It is a String Pattern. DigitCharacter .. means one or more digits. $\endgroup$
    – Karsten7
    Jun 29, 2015 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$
    – user155812
    Jun 29, 2015 at 3:46
  • $\begingroup$ Ah, thank you very much :) $\endgroup$
    – user155812
    Jun 29, 2015 at 3:50
  • 1
    $\begingroup$ @Karsten7., output is the same w/o "Plaintext". $\endgroup$ Jun 29, 2015 at 17:38
5
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Very elegant method, Mr. Wizard. Once again, you've been a tremendous help :) Thank you! $\endgroup$
    – user155812
    Jun 29, 2015 at 9:05
  • $\begingroup$ Nice. With and Module also work. $\endgroup$ Jun 29, 2015 at 17:43
1
$\begingroup$

Highlight the list and paste into a text file and then

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

works for me. Does it work for anyone else?

$\endgroup$
8
  • $\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$ Jun 29, 2015 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, 2015 at 17:51
  • $\begingroup$ @alancalvitti Is your browser really copying HTML code? Which do you use? $\endgroup$
    – Mr.Wizard
    Jun 29, 2015 at 17:51
  • $\begingroup$ @Mr.Wizard Possible confusion: bobbym wrote "put path..." so I used Import["http://projecteuler.net/problem=11","Table"] $\endgroup$ Jun 29, 2015 at 17:52
  • $\begingroup$ @alancalvitti but first Highlight the list and paste into a text file and then $\endgroup$
    – Mr.Wizard
    Jun 29, 2015 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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