17
\$\begingroup\$

Task

Given a letter (A, B, C), and a number (0-10), output the size of the matching standard paper size (Series A and B) or the matching standard envelope size (C series) in millimetres in the format aaaa x bbbb where aaaa and bbbb are the width and height measurements in millimetres as per ISO216 (Series A & B) or ISO296 (Series C)

To make things easier I will quote from Wikipedia's table of Paper Sizes

ISO paper sizes in portrait view
Format  A series    B series    C series
Size     mm × mm      mm × mm    mm × mm
0       841 × 1189  1000 × 1414 917 × 1297
1       594 × 841    707 × 1000 648 × 917
2       420 × 594    500 × 707  458 × 648
3       297 × 420    353 × 500  324 × 458
4       210 × 297    250 × 353  229 × 324
5       148 × 210    176 × 250  162 × 229
6       105 × 148    125 × 176  114 × 162
7        74 × 105     88 × 125   81 × 114
8        52 × 74      62 × 88    57 × 81
9        37 × 52      44 × 62    40 × 57
10       26 × 37      31 × 44    28 × 40

So examples of input and output:

**Test case 1**
Input: A4
Output: 210 x 297

**Test Case 2**
Input: B6
Output: 125 x 176

**Test Case 3**
Input: C2
Output: 458 x 648

Things to note:

  1. The format "210 x 297" or "1000 x 1414" etc. While this is the preferable format, You can choose to omit the " x " from your output, i.e. in the form of an array or two numbers or whatever tickles your fancy as long as the width measurement is outputted before the height.
  2. The ratio between the height and the width is roughly equivalent to the square root of 2, so in the calculation of the heights, the width is multiplied by sqrt(2), and then rounded up or down to the nearest millimetre, thus resulting in the measurements in the table above. This may help golf down your code.
  3. In successive sizes for a series as you go down the table, the width for one size becomes the height for the next. This may also help you golf down your code.

Rules:

  1. This is code-golf. Standard rules apply as a result. The score will be based on byte count. Lowest count will win.
  2. No silly loopholes, we've been there before... We shan't go through this again.
  3. If you can code it, then please also consider attaching a link to a working instance of your code so that other programmers and golfers can learn how your code works. This is not mandatory, but I'd like to encourage others to do this so we can all learn off one another. I certainly would love to learn more about other golfer's languages where possible.

Best of luck.

\$\endgroup\$
  • \$\begingroup\$ Bn is the geometric mean of An and An+1, and Cn is the geometric mean of An and Bn. \$\endgroup\$ – Adám Nov 15 '17 at 10:23
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ – Leo Nov 16 '17 at 4:20
9
\$\begingroup\$

JavaScript (ES7), 57 bytes

Saved 1 byte thanks to @WallyWest

s=>n=>[n,n-1].map(x=>.707**x*{A:841,B:1e3,C:917}[s]+.2|0)
\$\endgroup\$
  • 3
    \$\begingroup\$ Nice work... you can make it 57 with s=>n=>[n,n-1].map(x=>.707**x*{A:841,B:1e3,C:917}[s]+.2|0) and it will still have the same precision in your output. \$\endgroup\$ – WallyWest Nov 15 '17 at 5:44
  • \$\begingroup\$ @WallyWest Thanks for the tip. I'd played around with various replacements for .7071 and .2 for a while and just kinda gave up when I found something that worked :P \$\endgroup\$ – ETHproductions Nov 15 '17 at 16:42
  • \$\begingroup\$ No worries, all in the name of code golf... ;) I didn't realize map allowed one to reference unquoted keys within an object like that...? I'll have to use that sometime soon... \$\endgroup\$ – WallyWest Nov 15 '17 at 19:41
  • 1
    \$\begingroup\$ @WallyWest That has nothing to do with .map, you can use {key1:1,key2:2,key3:3}[myKey] anytime you wish. You only need to quote the keys if they are not valid variable names. \$\endgroup\$ – ETHproductions Nov 15 '17 at 19:58
  • \$\begingroup\$ Brilliant, thanks for the tip... \$\endgroup\$ – WallyWest Nov 15 '17 at 19:59
6
\$\begingroup\$

C (gcc), 113 111 90 78 70 bytes

It needs -lm to run on TIO, but works well on my computer without the option.

Saved 20 bytes thanks to pizzapants184.

Return values by pointer.

f(a,b,c,d)int*c,*d;{float x=1e3*pow(.707,b+a%3/4.)+.2;*c=x,*d=x/.707;}

Explanation:

f(a,b,c,d)int*c,*d;{          // calling by char, but receive as int
    float x = 1e3 * pow(.707, // float gives enough precision 
            a % 3 / 4.        // series: a%3/4.=.5,0,.25 when a=65,66,67
            + b) - .2;        // magic number to get correct roundings
    *c = x, *d = x / .707;
}
\$\endgroup\$
3
\$\begingroup\$

Python 2, 103 97 93 84 83 81 bytes

def f(l,n):w=[841,1000,917][ord(l)-65];h=int(w/.707);exec"w,h=h/2,w;"*n;print w,h

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Batch, 105 bytes

@set/aA=1189,B=1414,C=1297,h=%1,w=h*29/41
@for /l %%i in (1,1,%2)do @set/at=w,w=h/2,h=t
@echo %w% x %h%

41/29 ≅ √2

\$\endgroup\$
  • \$\begingroup\$ Are decimals not possible in Batch, @Neil? I would have thought h*.707 would be better than h*41/29? Mind you, ingenious use of approximation! \$\endgroup\$ – WallyWest Nov 15 '17 at 11:13
  • \$\begingroup\$ @WallyWest No, 32-bit signed integers only. \$\endgroup\$ – Neil Nov 15 '17 at 11:19
  • \$\begingroup\$ Oh, I see! Learn something new everyday! \$\endgroup\$ – WallyWest Nov 15 '17 at 11:20
3
\$\begingroup\$

JavaScript, 53 bytes

L=>N=>[N+1,N].map(p=>1091/2**(p/2+~{B:2,C:1}[L]/8)|0)

f=

L=>N=>[N+1,N].map(p=>1091/2**(p/2+~{B:2,C:1}[L]/8)|0)

document.write('<table><tr><th><th>A<th>B<th>C',[...Array(11)].map((_,i)=>'<tr><th>'+i+['A','B','C'].map(j=>'<td>'+f(j)(i)).join``).join``);

Save many bytes by using alternative output format, thanks to Neil.

\$\endgroup\$
  • 1
    \$\begingroup\$ 66 bytes: L=>N=>(g=p=>1000.2/2**(N/2+p-{A:2,B:4,C:3}[L]/8)|0)(.5)+' x '+g(0) \$\endgroup\$ – Neil Nov 15 '17 at 10:13
  • 1
    \$\begingroup\$ Or 58 bytes if you use the alternative output format: L=>N=>[N+1,N].map(p=>1000.2/2**(p/2-{A:2,B:4,C:3}[L]/8)|0) \$\endgroup\$ – Neil Nov 15 '17 at 10:16
  • \$\begingroup\$ @Neil edited. And also reduced to 56 bytes \$\endgroup\$ – tsh Nov 15 '17 at 11:01
3
\$\begingroup\$

Java (OpenJDK 8), 59 bytes

f->s->new double[]{s=1e3*Math.pow(.707,s+f%3/4d)-.2,s/.707}

Try it online!

Inspired by @Colera Su's C entry, then improved by myself.

  • 14 bytes saved thanks to Kevin Cruijssen
\$\endgroup\$
3
\$\begingroup\$

MATL, 34 33 30 bytes

917 841 1000.5vjo)t2X^*hi2/W/k

Try it online! or Verify all test cases

Saved 4 byte thanks to Luis Mendo

\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog), 31 28 bytes

-2 thanks to ngn.

Full program body. Assumes ⎕IO (Index Origin) to be 0, which is default on many systems. Prompts for number, then letter, both from STDIN. Prints to STDOUT.

⌊.2+1E3÷2*8÷⍨('BC'⍳⍞)+4×⎕-⍳2

Try it online!

A slightly modified version lets us test all possibilities at once: Try it online!

⍳2 first two ɩndices, i.e. 0 and 1

⎕- subtract that from numeric input

 multiply four with that

()+ add the following

 character input…

'BC'⍳ …'s ɩndex in this string ("A" will give 2, as the index beyond the last)

8÷⍨ divide that by 8

2* raise 2 to the power of that

1E3÷ 1000 divided by that

.2+ add ⅕ to that

 floor (round down)

\$\endgroup\$
  • \$\begingroup\$ @H.PWiz Yes. Typo. Thanks. \$\endgroup\$ – Adám Nov 15 '17 at 23:12
2
\$\begingroup\$

Befunge, 69 56 55 bytes

"}"8*~3%:"L"*7vv\p00-1_$..@
")"*":":-*!!\+<>2/00g:^:&\/

Try it online!

Explanation

We don't have the luxury of floating point or anything like a power function in Befunge, so we calculate the base size for the given format character, f, as follow:

fn = f % 3
width = 1000 - (n * 76 + 7) * !!n
height = width * 58 / 41

We then repeatedly rotate and divide these dimensions to get to the appropriate subdivision for the given size number.

"}"8*                         Push 1000 for later use in the width calculation.
     ~                        Read the format character from stdin.
      3%                      Convert into a number in the range 0 to 2.
        :"L"*7v               Calculate width = 1000 - (fn * 76 + 7) * !!fn
        -*!!\+<                  where fn is the format number derived above.
")"*":":                  /   Calculate height = width * 58 / 41.
                         \    Swap so the width is above the height on the stack.
                      v:&     Read the numeric size from stdin and duplicate for testing.
                      _       While not zero, go left.
                 p00-1        Decrement the size and move it from the stack into memory.
               v/             Swap the width and height.
               >2/            Divide the new width by 2.
                  00g:        Restore the size from memory and duplicate for testing.
                      _       While not zero, repeat this loop.
                       $      When zero, continue to the right and drop the size.
                        ..@   Output the width and height, then exit.
\$\endgroup\$

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.