7
\$\begingroup\$

Input:

2 numbers, x and y.

Output:

Output a text block where x is the width of the text block and y is the height of the text block. Then there will be a straight line going from the top left to the bottom right and to show it, there should be a # where the line should cross.

The # should be generated using the straight line equation, y = mx + c where m is the gradient of the line which can be retrieved through making m as y divided by x. x is the current line index. c is the height intercept and can be found by multiplying the gradient by the x value and adding the y value. So, in short, it would be (y/x) * lineIndex + (y/x * x + h). This would all be rounded to the nearest position in the current line.

Rules:

  • there can only be 1 # per line
  • it has to be in the form of a string/whatever the equivalent in your language of choice. This has to be able to cover multiple lines as well.
  • in text block, after the # there does not need to be more spaces. Just initial ones to offset the #

Examples:

(don't mind the |, they are where the lines start and stop)

Where x is 9 and y is 1:

|        #|

Where x is 1 and y is 5:

|#|
|#|
|#|
|#|
|#|

where x is 9 and y is 5:

| #       |
|   #     |
|    #    |
|      #  |
|        #|

where x is 5 and y is 9:

|#    |
|#    |
| #   |
| #   |
|  #  |
|  #  |
|   # |
|   # |
|    #|

where x is 9 and y is 2:

|   #     |
|        #|

where x is 9 and y is 3:

|  #      |
|     #   |
|        #|

Edit: More detail in the output. Added 3rd rule. Added 5th and 6th example

Edit2: Fixed errors in the given examples (Sorry!). Revised over the output to make it much clearer v. sorry for any confusion :S

\$\endgroup\$
  • 3
    \$\begingroup\$ How exactly is the position of # per line determined? How would the output for 9 and 2 look? \$\endgroup\$ – Laikoni Apr 2 '17 at 20:30
  • \$\begingroup\$ Editted the main post but the position of the # should be determined by using the straight line equation, y = mx + c. you can find the examples for both 9, 2 and 9, 3 in the post \$\endgroup\$ – eniallator Apr 2 '17 at 20:40
  • 1
    \$\begingroup\$ You have four examples with x=9, and the first one (with y=1) is incompatible with the last three (and I'm not convinced that the second one, with y=5, is compatible with the last two). The instruction "The straight line should use the straight line equation, y = mx + c" is not sufficiently precise. \$\endgroup\$ – Greg Martin Apr 2 '17 at 21:12
  • \$\begingroup\$ very sorry about all the confusion, hopefully made it clearer now but do let me know if you find anything unclear so i can make it easy to understand. \$\endgroup\$ – eniallator Apr 2 '17 at 21:41
  • \$\begingroup\$ Can we choose to output more than 1 # per line if it helps save bytes? \$\endgroup\$ – user41805 Apr 10 '17 at 13:24
1
\$\begingroup\$

C, 74 70 bytes

Thanks to @Johan du Toit for saving 4 bytes!

i;f(x,y){for(i=y;i--;)printf("%*c\n",(int)round((i-y)/(-1.*y/x)),35);}

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Nice! You can save a couple of bytes by using -1.*y/x instead of -y/(float)x \$\endgroup\$ – Johan du Toit Apr 11 '17 at 10:50
1
\$\begingroup\$

PHP, 66 Bytes

for([,$x,$y]=$argv;$i<$y;)printf("%".round(++$i*$x/$y)."s\n","#");
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 1. no need to init $i. 2. save 1 byte (and fix) with printf("%".round(++$i*$x/$y)."s\n","#"); \$\endgroup\$ – Titus Apr 10 '17 at 11:33
1
\$\begingroup\$

JavaScript (ES6), 77 bytes

x=>y=>[...Array(y).keys()].map(a=>" ".repeat(0|((a+1)/(y/x))+.5)+"#").join`
`

f=x=>y=>[...Array(y).keys()].map(a=>" ".repeat(0|((a+1)/(y/x))+.5)+"#").join`
`

console.log(f(9)(5));

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Python, 64 63 bytes

1 byte saved thanks to @Cole

def f(x,y):[print(' '*round((i+1)*x/y-1)+'#')for i in range(y)]

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Using a list comprehension is 1 byte shorter (tested in Python 3.5.2): def f(x,y):[print(' '*round((i+1)*x/y-1)+'#')for i in range(y)] \$\endgroup\$ – cole Apr 10 '17 at 14:26
  • \$\begingroup\$ @Cole am I allowed to return garbage (Nones)? (I thought about yielding the lines, but the output rules seemed to be strictly stdout here) \$\endgroup\$ – Uriel Apr 10 '17 at 14:28
  • \$\begingroup\$ Does a function implicitly return the result of the list comprehension? I'm pretty sure if you saved it in a file and ran it (which afaik is the standard way; you'd specify if it had to be done in a REPL) it would only print out the desired output. If I'm wrong then by all means ignore my suggestion. \$\endgroup\$ – cole Apr 10 '17 at 14:46
  • \$\begingroup\$ @Cole Sorry, my bad. I guess I was confusing it with lambdas. \$\endgroup\$ – Uriel Apr 10 '17 at 14:49
0
\$\begingroup\$

Japt, 21 bytes

ÆSpºX+1 /(U/V  r)+"#

Try it online!

Input is in the format of y x. +1 byte for the -R flag.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

AWK, 55 44 bytes

 {for(;++j<=$2;)printf"%*c\n",$1/$2*j+0.5,35}

Using hints from @manatwork and the solution from @Steadybox

The previous code is handy as an example of using an sprintf within a printf

{for(;++j<=$2;)printf sprintf("%%%.0fs\n",$1/$2*j),"#"}

Example usage:

awk '{for(;++j<=$2;)printf"%*c\n",$1/$2*j+0.5,35}' <<< "9 3"
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Not verified all test cases, but wouldn't {for(;++j<=$2;)printf"%*s\n",$1/$2*j+.5,"#"} do it? \$\endgroup\$ – manatwork Apr 10 '17 at 19:30
  • \$\begingroup\$ What case did it fail on? And I've never tried using the * with printf in AWK \$\endgroup\$ – Robert Benson Apr 10 '17 at 19:36
  • \$\begingroup\$ @manatwork It's good to learn new things :) \$\endgroup\$ – Robert Benson Apr 10 '17 at 19:45

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.