-3
\$\begingroup\$

Introduction

In many programming languages, within syntax exceptions, it is often pointed out to the programmer exactly which character or line is the culprit. This challenge will be a bit of a modification of that - specifically, using an arrow to point out any specific character of an input string.

The challenge

Your job is to:

  • Define a function that takes two parameters.

  • This function will print the string to stdout (or an acceptable alternative), along with pointing out the index of the string requested.

  • The index is zero-based, meaning index 0 is actually he first character of the string.

  • If a half index is passed in, you are to use a double arrow (/\ instead of ^) to point out the middle of the two characters.

Don't quite get what I mean? Here's some test cases:

f("hello world", 3):

hello world
   ^
   l

f("Code Golf", 0):

Code Golf
^
C

f("MY STRING!", 4.5):

MY STRING!
    /\
    TR

You may use any language. No input from stdin - must be through a function. Do not use any of the common loopholes. And most importantly, have fun!

Leaderboard

You can view the leaderboard for this post by expanding the widget/snippet below. In order for your post to be included in the rankings, you need a header (# header text) with the following info:

  • The name of the language (end it with a comma , or dash -), followed by...

  • The byte count, as the last number to appear in your header.

For example, JavaScript (ES6), 72 bytes is valid, but Fortran, 143 bytes (8-bit) is invalid because the byte count is not the last number in the header (your answer will be recognized as 8 bytes - don't take advantage of this).

<iframe src="https://xmikee1.github.io/ppcg-leaderboard/?id=186018" width="100%" height="100%" style="border: none;">Oops, your browser is too old to view this content! Please upgrade to a newer version of your browser that supports HTML5.</iframe><style>html,body{margin:0;padding:0;height:100%;overflow:hidden}</style>

\$\endgroup\$
  • 8
    \$\begingroup\$ Will the index always be valid? (ie 0<=i<=len(string)) \$\endgroup\$ – TFeld May 23 '19 at 11:36
  • 14
    \$\begingroup\$ Why must it be a function? \$\endgroup\$ – AdmBorkBork May 23 '19 at 12:35
  • 14
    \$\begingroup\$ Please consider lifting the I/O restrictions. \$\endgroup\$ – Arnauld May 23 '19 at 13:10
  • 10
    \$\begingroup\$ Why must the index be zero-based? \$\endgroup\$ – Adám May 23 '19 at 13:12
  • 9
    \$\begingroup\$ You examples indicate that the pointed-out characters are to be printed on a third line, but your challenge spec doesn't actually state that. \$\endgroup\$ – Adám May 23 '19 at 13:15

10 Answers 10

2
\$\begingroup\$

Python 2, 78 77 75 74 bytes

def f(s,i):j=int(i);x='\n'+j*' ';print s+x+'/^\\'[i==j::2]+x+s[j:(i>j)-~j]

Try it online!

-1 byte, thanks to Arnauld

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 25 24 23 bytes

…^/\ć‚Y.ïè©XY.$®g£‚YúXš

Function taking X as string and Y as number, and returning a list of string-lines.

Try it online.

Explanation:

…^/\         # Push string "^/\"
    ć        # Extract head; pop and push remainder "/\" and head "^"
     ‚       # Pair them together: ["/\","^"]
      Y.ï    # Check if number `Y` is an integer (1 if truthy; 0 if falsey)
         è   # Use that to index into the pair ("^" if integer; "/\" if decimal)
          ©  # Store it in the register (without popping)
XY.$         # Remove the first `int(Y)` characters from string `X`
    ®g       # Get the length of the string we saved in the register
      £      # Leave that many leading characters from the string
‚            # Pair it with the earlier "^" or "/\"
 Yú          # Pad both with `int(Y)` amount of leading spaces
Xš           # Prepend string `X` to this list
             # (which is returned implicitly as result)

The function definition is:

"…^/\ć‚Y.ïè©XY.$®g£‚YúXš"ˆ

Which can then be called like this:

"MY STRING!"U  4.5V ¯`.V»,
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 165 125 101 99 90 85 bytes

-40 Bytes @Kevin Cruijssen
-24 Bytes @Arnauld
-2 Bytes
-9 Bytes @attinat
-5 Bytes @ceilingcat

t,u;a(char*s,float i){t=i;u=i>t;printf("%s\n%*s\n%*.*s",s,t+u,L" ^尯"+u,t+u,++u,s+t);}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I think you're missing a part of the challenge. a("MY STRING",4.5); gives an error in your TIO. \$\endgroup\$ – Kevin Cruijssen May 23 '19 at 13:44
  • \$\begingroup\$ @KevinCruijssen forgot about that, doesn't seem as elegant any more :) \$\endgroup\$ – peterzuger May 23 '19 at 14:04
  • 2
    \$\begingroup\$ Perhaps, but you can still save a bunch of bytes now. :) 125 bytes \$\endgroup\$ – Kevin Cruijssen May 23 '19 at 14:10
  • 2
    \$\begingroup\$ 101 bytes with some trailing spaces. \$\endgroup\$ – Arnauld May 23 '19 at 14:39
  • 2
    \$\begingroup\$ Note that my 101-byte version was based upon @KevinCruijssen 's 125-byte version, so I don't deserve all the credit for -64. :) \$\endgroup\$ – Arnauld May 25 '19 at 15:58
1
\$\begingroup\$

Pepe, 229 bytes

This took very long to make. Probably my longest Pepe code yet. Everything could be so much easier if not the third output line. I didn't write in Pepe for a long while so I'm proud of it.

Note: This is not a valid answer because the specs require it to be a function – but there are no functions in Pepe, officially.

REEeReeeReeERREeeRREeEEerREEreeeEeEEEEreeEeEEEeeREeREEEerREEreeEeEEEEeREerEeeEeeeeerrEEeeeErereeeREEEERREEEEEEeReEReeErereeeRREEEEEEEREEEeRrErrEEREEEEReREEEeREEEEereerreErEereeREEEEReEereRrEeEEeRrEEEEEEErEEEerrEEREEEEReReEerEeree

Try it online!

Non-minified code, with comments, if you can understand it...

# The line
REEe   # Take input
Reee   # Output it
ReeE   # End line

# Index
RREee    # Take the number [n] 
RREeEEe  # Floor in a copy

# Function [n]
rREE 

  reeeEeEEEE reeEeEEEee  # Output /\

REe  # End

# Move pointer to last item
REEEe

# Function floor[n], overrides [n] if it's an integer
rREE

  reeEeEEEEe  # Output ^

REe

# Line 2 – spaces
rEeeEeeeee  # A space
rrEEeeeE    # Repeat it [n] times
re          # Remove one
reee        # Output

# Line 2 – pointer
REEEE      # Go to first item
RREEEEEEe  # Copy to other stack
ReE        # Call it
ReeE       # End the line

# Line 3 – spaces
re     # Remove the item
reee   # Output

# Line 3 – letters
RREEEEEEE  # Move the index to other stack
REEEe      # Move R pointer to the end
RrE        # Prepend 0
rrEE       # Label 0
  REEEE      # Go to the first item
  Re         # Pop it
  REEEe      # Go to the last item
  REEEEe     # Decrement
  ree        # Recurse while unequal
  rreE       # Equal, end
    # This is a flag I've added because I thought that I should add something in its place
    # I never thought it will be useful. But apparently it is. For recursion.
rEe
ree        # Call if unequal
REEEE      # Move to first item
ReEe       # Output it
re         # Pop the 0
RrEeEEe    # Floor the first item in a copy
RrEEEEEEE  # Move the item to the other stack
rEEEe      # Move to the end
rrEE       # Label floor[n]
  REEEE      # Move to first item of R
  Re         # Remove it
  ReEe       # Output the next one
rEe        # End
ree        # Enter it if [n] is not an integer
\$\endgroup\$
  • 1
    \$\begingroup\$ Wow, haven't seen you do a Pepe answer in a long time \$\endgroup\$ – u_ndefined May 26 '19 at 13:12
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 100 98 bytes

a=>b=>a+(b>(i=(int)b)?"/\\":"^").PadLeft((v=b>i?2:1)+i)+'\n'+a.Substring(i,v).PadLeft(v+i);int i,v

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I'm not convinced having the new line character in the input to the function is ok. Basically your code does not work with "Hello World" it only works with "Hello World\n" (and arguably it doesnt work with that). \$\endgroup\$ – Expired Data May 23 '19 at 14:04
0
\$\begingroup\$

Java 11, 103 bytes

(s,n)->{int i=(int)n;var t="\n"+" ".repeat(i);return s+t+(n>i?"/\\":"^")+t+s.substring(i,n>i?i+2:i+1);}

Try it online.

\$\endgroup\$
0
\$\begingroup\$

Jelly, 22 bytes

⁾/\”^%?1,ịṭ€Ḟ’⁶xƲ{;@W}

Try it online!

A dyadic link which takes the position as its left argument and the string as its right. It returns a list of Jelly strings (presumed to be an ‘acceptable alternative’) representing the output. The TIO link outputs these to stdout separated by newlines.

This is currently using 1-indexing since that is the norm for Jelly. If 0-indexing is a must, here’s an alternative dyadic link:

Jelly, 23 bytes

⁾/\”^%?1,‘ị¥ṭ€Ḟ⁶xƊ{;@W}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I personally think the strict I/O rules are bs, but currently your answer is invalid. Yours is a full program instead of function; it's 1-indexed instead of 0-indexed; it returns a list instead of printing to STDOUT. I would keep it as is and mark it as non-competing (for now), or perhaps add a second version which does comply to these cumbersome rules.. Regardless, nice answer! 2 bytes shorter than what I had as full program in 05AB1E. \$\endgroup\$ – Kevin Cruijssen May 23 '19 at 13:02
  • \$\begingroup\$ @KevinCruijssen Regarding STDOUT, the challenge does mention "or an acceptable alternative". \$\endgroup\$ – Arnauld May 23 '19 at 13:08
  • \$\begingroup\$ @Arnauld Ah, you're right. Saved a byte in my 05AB1E 'function' by returning a string instead of printing. Thanks. :) \$\endgroup\$ – Kevin Cruijssen May 23 '19 at 13:12
  • \$\begingroup\$ @KevinCruijssen thanks. I’ve added a new version using 1-indexing. A single link is the Jelly equivalent of a function. The TIO link is to a full program that calls that link and formats the output for printing, but the link itself works as described taking two arguments and returning a list of Jelly strings. \$\endgroup\$ – Nick Kennedy May 23 '19 at 16:56
0
\$\begingroup\$

Python 3.8 (pre-release), 72 bytes

Very similar to @TFeld's answer, but makes use of Python 3.8's handy := lambda assignment operator.

f=lambda s,i:s+(x:='\n'+' '*(j:=int(i)))+'/^\\'[i==j::2]+x+s[j:(i>j)-~j]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES8),  65  63 bytes

Takes input as (string)(position).

s=>n=>s+(x=`
`.padEnd(n+1))+(n%1?'/\\'+x+s[~~n++]:'^'+x)+s[~~n]

Try it online!


JavaScript (ES8), 39 bytes

This version is based on the challenge description, without the 3rd line. The original version above includes it, like suggested by the test cases. Awaiting OP clarification.

Takes input as (string)(position).

s=>n=>s+`
`.padEnd(n+1)+(n%1?'/\\':'^')

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Stax, 21 bytes

ë└¥1ç4┴îdΩ8½öyε∙µ$%T▐

Run and debug it

\$\endgroup\$

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