58
\$\begingroup\$

Introduction

In the error outputs of some languages such as Java, a pointer is shown to give the programmer an idea of exactly where the error went wrong.

Take this example on Ideone:

Main.java:12: error: ';' expected
    Invalid Java!
                ^

Notice the caret shows where the invalid code is?

Challenge

Your challenge is: given number N and string S, place a pointer on the Nth character in S.

Examples

Input: 2, "Lorem ipsum, dollar sit amet."

Output:

Lorem ipsum, dollar sit amet.
 ^

Rules

  • Input is received via STDIN or function parameters
  • Output is printed out to the console or returned
  • Trailing new lines, spaces etc are allowed in the output
  • The pointer character must be a ^ caret and must be on a new line.
  • This is code golf, so the shortest answer wins. Good luck!
\$\endgroup\$
16
  • 16
    \$\begingroup\$ I feel like this is an extremely simple problem, so I am not certain it will be received overly well. You might want to try the sandbox once you have enough rep. \$\endgroup\$ Jun 10, 2015 at 1:08
  • 9
    \$\begingroup\$ I think this could have been made a bit more interesting if the input had multiple lines, so that you had to insert a newline, spaces, and carat at the correct position(s). Honestly, the spec doesn't really say it will be a single line, but I think enforcing that now will invalidate a few answers unfairly, since there's no example that shows this. \$\endgroup\$
    – Geobits
    Jun 10, 2015 at 2:04
  • 3
    \$\begingroup\$ Like I said, the cat's probably out of the bag on this one. Rule changes after valid answers are posted usually don't work out well. Live and learn ;) \$\endgroup\$
    – Geobits
    Jun 10, 2015 at 3:06
  • 1
    \$\begingroup\$ @SohamChowdhury I'll just pretend that was intentional ;) Do you want me to fix it? \$\endgroup\$
    – Matt Y
    Jun 10, 2015 at 9:36
  • 11
    \$\begingroup\$ While this may be very simple, you've certainly done well for a first challenge! You have +16/-0 votes, 1,300 views, and 28 answers (as of this writing) and you've made the Hot Network Questions list. Nice job! \$\endgroup\$
    – Alex A.
    Jun 10, 2015 at 14:50

89 Answers 89

1 2
3
1
\$\begingroup\$

Jelly, 7 bytes

ṄṛṬị⁾^ 

Try it online!

Same length, but different to, Erik's existing answer

How it works

ṄṛṬị⁾^  - Main link. Takes S on the left and N on the right
Ṅ       - Print S with a newline
 ṛ      - Discard it and yield N
  Ṭ     - Yield a list with a 1 at the Nth index and 0s otherwise
   ị⁾^  - Index into "^ "
\$\endgroup\$
1
\$\begingroup\$

Vyxal o, 6 5 bytes

,‹\^꘍

Try it Online!

\$\endgroup\$
1
  • \$\begingroup\$ You can omit the trailing , with the o flag. \$\endgroup\$
    – naffetS
    Oct 11, 2022 at 4:24
0
\$\begingroup\$

rs, 35 bytes

(\d+), "(.*?)"/\2\n( )^^(\1)^
 \^/^

Unfortunately, the online demo has a bug that I need to fix with newlines, so I can't put a demo link.

EDIT: I fixed the bug in the demo page. So...

Live demo!

This is pretty simple:

  • Line 1 places the text, a newline, and a series of spaces repeated one more times than we want.
  • Line 2 gets rid of the extra space.
\$\endgroup\$
0
\$\begingroup\$

Lua, 58 53 bytes

a=" "function p(n,s)print(s,"\n"..a:rep(n-1).."^")end

Thanks to manatwork this just got shorter by 5 bytes. Looks like I need to refresh my Lua skills.

First version:

function p(n,s)print(s.."\n"..string.rep(" ",n-1).."^")end
\$\endgroup\$
2
  • 2
    \$\begingroup\$ That static method call is horribly long. function p(n,s)x=" ";print(s.."\n"..x:rep(n-1).."^")end \$\endgroup\$
    – manatwork
    Jun 10, 2015 at 17:24
  • 1
    \$\begingroup\$ Actually you can remove one more character. As “Trailing new lines, spaces etc are allowed in the output”, replace the first .. with ,. This way a trailing tab will appear in the first line, but is allowed. \$\endgroup\$
    – manatwork
    Jun 10, 2015 at 18:37
0
\$\begingroup\$

PHP 4.1, 35 31 bytes

This one was really simple, but still fun!

<?="$S
",str_pad('^',$P,' ',0);

You can pass the values over POST, GET, SESSIONS, ...

The key S must contain the string, while the key P must contain the character number where you want the ^.


Old answer (35 bytes):

<?="$S
",str_repeat(' ',--$P),'^';
\$\endgroup\$
0
\$\begingroup\$

Batch, 56 Bytes

@set/p=
@for /l %%a in (2,1,%1)do @cd|set/p=.<BS> 
@echo ^^

This code contains two points of interest. First, it takes input from both StdIn and command line arguments. The string is read from StdIn while the index of the caret is taken as a parameter. Second, Batch doesn't play nice when it come to outputting a single space character (U+0020) so a bit of a work around is required: cd|set/p=.<BS> In this line <BS> stands in for the backspace character (U+0008) and the last character is a space. This is accounted for in the byte count.

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

SAS, 22

macro a put &s/@&n'^'%

Usage:

%let s="Lorem ipsum dolor sit amet";
%let n=2;
data;a;run;

Output:

Lorem ipsum dolor sit amet
 ^

If running in 'batch' mode, the run is not required so the code in needed to produce the function and run it (just not setting the inputs) would be 30 bytes

macro a put &s/@&n'^'% data;a;

So, it may be cheating... this style macro(function) definition in SAS does not have parameters, so the inputs are defined separately, in Usage as two %let statements. Parsing from STDIN would substantially lengthen the solution and only works when running SAS in 'batch' mode, which is not necessarily commonplace.

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

Javascript, 50 bytes

Code

function f(n,s){return s+'\n'+s.substr(0,n-1)+'^'}

Output

console.log(f(2, "hello"));
hello
h^

This approach takes advantage of the fact that you do not specify an empty string preceding the caret, only a caret pointing to the index in the input string and anything following that. If you happen to have a caret in your input string and an error there then it might get a little bit confusing :)

For example

console.log(f(3, "he^lo"));
he^lo
he^

or

console.log(f(3, "he^"));
he^
he^
\$\endgroup\$
0
\$\begingroup\$

Pyth, 8 bytes

z+*dtQ\^

Takes inputs separated by a newline (i.e. 2\nLorem ipsum).
Try it online!

Explanation:

z           The second input, implicitly printed with trailing newline
  *d__      The space character, __ times
    tQ        where __ is the first input -1
 +    \^    Append a literal carat character and printed
\$\endgroup\$
0
\$\begingroup\$

Vim, 7 keystrokes (buffer input/output, excluding input keystrokes)

o<Esc> + input number + i <Esc>r^

Explanation

  • o<Esc> creates a new line
  • #i <Esc> creates # spaces
  • r^ replaces the last space by ^

Note: This answers does not write the output to stdout (:w !tee, + 6 keystrokes) - so maybe it will be considered invalid.

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

C++14, 45 bytes

As unnamed lambda modifying its input, assuming s is similar to std::string (has operator+=):

[](int n,auto&s){for(s+=10;--n;s+=32);s+=94;}

Usage:

#include<string>
#include<iostream>

auto f =
[](int n,auto&s){for(s+=10;--n;s+=32);s+=94;}
;

int main() {
 std::string s = "abcdefghijk";
 f(2,s);
 std::cout << s << std::endl;
}
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 26 bytes

INPUT N,S$?S$?" "*(N-1);"^

1-indexed arrow adds 4 bytes...

\$\endgroup\$
1
  • \$\begingroup\$ Note that you only want N-1 spaces before the caret. \$\endgroup\$ Feb 2, 2017 at 23:08
0
\$\begingroup\$

tcl, 47

gets stdin s
puts $s\n[format %[gets stdin]s ^]

demo

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

Ruby 2.7, 24 bytes

x=->{_1+'
'+" "*~-_2+?^}
\$\endgroup\$
0
\$\begingroup\$

Turtlèd, 17 bytes

r!-[*.-l_]' d?:'^

explanation

r!-               move right, take input into string variable, decrement string pointer
   [*    ]        while current char is not *
     .-           write pointed char from string, decrement pointer
       l_         move left, write * if the pointer is on the last char of string
          ' d     write space, move down
             ?:'^ Take input into register, move forward by the amount in register, write ^
\$\endgroup\$
0
\$\begingroup\$

Jelly, 8 7 bytes

Ṅ⁶ṁ”^⁹¦

Try it online!

That dollardolor makes me laugh!!

Thanks to @Dennis for -1.

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

Braingolf, 25 bytes

VV"^ "R3-M#
&@v[v.Rv]v<&@

Try it online!

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

V, 6 bytes

ïÀá r^

Try it online!

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

PowerShell Core, 29 27 bytes

$args[1]
'^'|% *ft $args[0]

Try it online!

-2 bytes thanks to Mazzy!

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

Japt -R, 5 bytes

-1 byte if we can take the string input as a singleton array.

·p^ùV

Try it

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

05AB1E, 6 bytes

,'^ú¦,

Try it online!

,       # output with trailing newline...
        # implicit input
     ,  # output with trailing newline...
 '^     # "^"...
   ú    # with...
        # implicit input...
    ¦   # minus 1...
   ú    # spaces in front
 
\$\endgroup\$
0
\$\begingroup\$

Haskell, 38 bytes

n!s=s++'\n':g n
g 1="^"
g n=' ':g(n-1)

Try it online!

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

Gol><> (Golfish), 14 bytes

`^IMR` aTiEHot

Try it here.

Explanation

`^             # push caret
  I            # read input as number
   M           # subtract 1
    R`         # pop N, and Repeatedly push a space, N times
       a       # push 10 (ASCII newline)
        T      # set teleport pad
         i     # input character
          EH   # if EOF, output stack and halt
            o  # otherwise, output the character just read
             t # teleport back to pad
\$\endgroup\$
3
  • \$\begingroup\$ Why is this non-competing? \$\endgroup\$ Nov 22, 2020 at 16:49
  • \$\begingroup\$ I think it used features which were added to the language after this question was asked. \$\endgroup\$ Nov 22, 2020 at 21:54
  • \$\begingroup\$ In that case the annotation is no longer necessary and I've removed it. The reason I asked is that "non-competing" has been historically misused to mean "this answer doesn't actually meet the challenge requirements and should be deleted". \$\endgroup\$ Nov 23, 2020 at 0:16
0
\$\begingroup\$

Red, 35 bytes

func[n s][prin[s lf pad/left"^^"n]]

Try it online!

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

Stax, 5 bytes

P'^)p

Run and debug it

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

APL (Dyalog Classic), 14 bytes

{↑⍺((-⍵)↑'^')}

Try it online!

A port of @FrownyFrog J answer

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

Thunno 2 N, 6 bytes

⁻ṣ'^+,

Attempt This Online!

Explanation

⁻ṣ'^+,  # Implicit input
⁻ṣ      # First input decremented spaces
  '^+   # With a "^" appended
     ,  # Paired with the second input
        # Implicit output joined by newlines
\$\endgroup\$
0
\$\begingroup\$

Java (JDK), 32 bytes

Speaking of which, the Java answer of this problem needed a little upgrade :)

a->b->b+"\n"+" ".repeat(a-1)+"^"

Try it online!

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

Go, 79 bytes

import."strings"
func f(s string,n int)string{return s+`
`+Repeat(" ",n-1)+"^"}

Attempt This Online!

\$\endgroup\$
1 2
3

Your Answer

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

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