(silnie zainspirowany Elementem ciągu o określonym indeksie )
Biorąc pod uwagę ciąg s
i liczbę całkowitą n
reprezentującą indeks w s
, wyprowadzaj s
znak ze znakun
-tej pozycji usuniętej.
Dozwolone jest indeksowanie 0 i indeksowanie 1.
- W przypadku indeksowania 0 wartość
n
będzie nieujemna i mniejsza niż długośćs
. - Dla 1-indeksowania
n
będzie dodatnia i mniejsza lub równa długościs
.
s
będzie się składać wyłącznie z drukowalnych znaków ASCII ( \x20-\x7E
lub
przez ~
).
Wszelkie uzasadnione wejścia / wyjścia są dozwolone. Obowiązują standardowe luki .
Przypadki testowe (indeksowane 0):
n s output
0 "abcde" "bcde"
1 "abcde" "acde"
2 "a != b" "a = b"
3 "+-*/" "+-*"
4 "1234.5" "12345"
3 "314151" "31451"
Testcases (1-indeksowany):
n s output
1 "abcde" "bcde"
2 "abcde" "acde"
3 "a != b" "a = b"
4 "+-*/" "+-*"
5 "1234.5" "12345"
4 "314151" "31451"
To jest golf golfowy , więc wygrywa najkrótsza odpowiedź w bajtach.
3
,314151
->31451
. Zakładam, że nie.Odpowiedzi:
C#,
2019 bytesźródło
Alice,
1312 bytesThanks to Leo for saving 1 byte.
Try it online!
First line of the input is the string, second line is the 0-based index.
Explanation
źródło
Japt, 2 bytes
Try it online!
źródło
K (Kona), 1 byte
Gotta love builtins. 0-based indexing. Usage:
źródło
Haskell,
2824 Bytes-4 byte thanks to Laikoni, this version is 1-indexed.
Old answer:
A simple recursive function that takes the value, it's 0-indexed.
My first time code-golfing so maybe it's not the optimal solution. Oh well.
źródło
Mathematica, 18 bytes
1-indexed
input
thanks Martin Ender
źródło
["abcde", {1}]
, in which caseStringDrop
alone does the trick. What do you think? (You might want to explicitly mention that it's 1-indexed as well.) I'm always happy to see people posting Mathematica answers :)V, 3 bytes
Try it online!
This uses 1-indexing.
źródło
CJam, 4 bytes
Try it online!
Explanation
źródło
GCC c function, 25
1-based indexing.
Plenty of undefined behavior here so watch out for stray velociraptors:
strcpy()
man page says If copying takes place between objects that overlap, the behavior is undefined. Here there clearly is overlap of the src and dest strings, but it seems to work, so either glibc is more careful or I got lucky.s+=n
happens before thes-1
. The c standard gives no such guarantees, and in fact calls this out as undefined behaviour. Again, it seems to work as required with the gcc compiler on x86_64 Linux.Try it online.
źródło
strcpy
's arguments need to be pushed in right-to-left order, which would explain the behaviour, but you said you were usingx86_64
which uses registers... maybe the compiler decided to golf the generated code and decided that computing s+=n first was golfier!MATL, 3 bytes
Uses 1-based indexing.
Try it online! Or verify all test cases.
Explanation
In the modified version with all the test cases, the code is within an infinite loop
`...T
until no input is found. At the end of each iteration the display function (XD
) is explicitly called, and the stack is cleared (x
) to ready it for the next iteration.źródło
Vim, 7 bytes
How it works:
It expects two lines; one with the string and one with the number.
źródło
jD@"gox
Java 8, 39 bytes
Try it here.
Java 7, 67 bytes
Try it here.
źródło
s->n->new StringBuilder(s).deleteCharAt(n)+"";
though it is longer.StringBuffer
instead ofStringBuilder
in codegolf. ;)Ruby, 16 bytes
Try it online!
źródło
Haskell, 15 bytes
This requires the recently released GHC 8.4.1 (or higher). Now
<>
, as a function on Semigroups, is in Prelude. It is particularly useful on the function SemigroupTry it online!
Since tio is using an older bersion of GHC, I've imported
<>
in the header.źródło
R, 40 bytes
Just goes to show the variety of ways, none of which particularly compact, you can fiddle with strings in R.
źródło
05AB1E, 5 bytes
Try it online!
źródło
05AB1E, 6 bytes
Try it online!
Explanation
źródło
Pyth, 3 bytes
Try it here.
Takes index first.
źródło
PHP, 42 Bytes
0 indexed
Try it online!
źródło
JS (ES6),
413231 bytesBased on this. Takes input through currying, first is string, second is index.
-9 thanks to @JohanKarlsson
-1 thanks to @ETHproductions
źródło
Jelly, 3 bytes
A full program taking the (1-based) index and the string (in that order) and printing the result.
As a dyadic function it returns a list of the two parts.
In fact the index may be a list of n indices, in which case it returns a list of the n-1 parts.
Try it online!, or see a test suite.
How?
As an example of using multiple indexes:
źródło
vim,
107Takes 1-indexed input in the following format:
Thanks to @DJMcMayhem for 3 bytes!
źródło
Java 8,
4541 bytesSaved 4 bytes thanks to @OlivierGrégoire
My first code golf answer in something other than C#, even if it isn't the shortest for Java yet.
źródło
;
in lambda (-1 bytes). 2. In my eyes, you don't need to return aString
. I think that returning theStringBuffer
without the+""
would be perfectly valid (-3 bytes). Example?BigInteger
is a representation of an unboundedint
, in this caseStringBuffer
/StringBuilder
are representations of mutableString
s.Python 3, 24 bytes
Try it online!
źródło
JavaScript (ES6),
393433 bytes56 bytes saved thanks to Arnauld.źródło
brainfuck, 14 bytes
Try it online!
Reads zero-based one-byte index immediately followed by the string.
źródło
Befunge-98,
352725 bytes-4 bytes thanks to @eush77
Try it online!
1-indexed, note that the input has a trailing null-byte.
źródło
PHP, 41 bytes, 35 bytes excluding ?php
0-indexed
TIO
źródło
[$argv[2]]
index implicitly creating a range? Also, IIRC you can leave the<?php
off, because the PHP interpreter has a mode which doesn't need it, and because we don't normally penalise for that sort of indication in a file of what the language is.Japt,
32 bytesTry it online!
źródło
1
R,
4847 bytes(1 byte saved through use of
el()
thanks to Giuseppe)Split the string into its individual characters, remove the nth and then concatenate again.
There may well be a better solution, strsplit() is quite unwieldy as it returns a list.
źródło
pryr::f([function body])
saves a few bytes and usingel(strsplit(s,""))
saves a byte but also doesn't work on TIO for some reason.install.packages("pryr")
but maybe that's me being too precious!function(s,n)intToUtf8(utf8ToInt(s)[-n])
for 40 bytes.function(s,n)sub(sub(0,n,"(.{0})."),"\\1",s)
for 44.