(fortement inspiré par Element of string à l'index spécifié )
Avec une chaîne s
et un entier n
représentant un index s
, sortie s
avec le caractère à la n
-th position supprimée.
L'indexation 0 et l'indexation 1 sont autorisées.
- Pour l'indexation 0,
n
sera non négatif et inférieur à la longueur des
. - Pour 1-indexation,
n
sera positif et inférieur ou égal à la longueur des
.
s
consistera uniquement en caractères ASCII imprimables ( \x20-\x7E
, ou
par le biais ~
).
Toute entrée / sortie raisonnable est autorisée. Les failles standard s'appliquent.
Cas de test (indexé 0):
n s output
0 "abcde" "bcde"
1 "abcde" "acde"
2 "a != b" "a = b"
3 "+-*/" "+-*"
4 "1234.5" "12345"
3 "314151" "31451"
Cas de test (index 1):
n s output
1 "abcde" "bcde"
2 "abcde" "acde"
3 "a != b" "a = b"
4 "+-*/" "+-*"
5 "1234.5" "12345"
4 "314151" "31451"
C'est du code-golf , donc la réponse la plus courte en octets est gagnante.
3
,314151
->31451
. Je suppose que non.Réponses:
C #,
2019 octetsla source
Alice ,
1312 octetsMerci à Leo pour avoir sauvegardé 1 octet.
Essayez-le en ligne!
La première ligne de l'entrée est la chaîne, la deuxième ligne est l'index basé sur 0.
Explication
la source
Japt , 2 octets
Essayez-le en ligne!
la source
K (Kona), 1 octet
Je dois aimer construit. Indexation basée sur 0. Usage:
la source
Haskell ,
28 à24 octets-4 octets grâce à Laikoni, cette version est indexée 1.
Ancienne réponse:
Une fonction récursive simple qui prend la valeur, elle est indexée à 0.
Ma première fois, je jouais au code, alors ce n’est peut-être pas la solution optimale. Tant pis.
la source
Mathematica, 18 octets
1 indexé
contribution
merci Martin Ender
la source
["abcde", {1}]
, dans ce cas,StringDrop
seul fait l'affaire. Qu'est-ce que tu penses? (Vous voudrez peut-être mentionner explicitement qu'il est également indexé sur 1.) Je suis toujours heureux de voir des personnes publier des réponses Mathematica :)V , 3 octets
Essayez-le en ligne!
Ceci utilise 1-indexation.
la source
CJam, 4 bytes
Try it online!
Explanation
la source
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.
la source
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.la source
Vim, 7 bytes
How it works:
It expects two lines; one with the string and one with the number.
la source
jD@"gox
Java 8, 39 bytes
Try it here.
Java 7, 67 bytes
Try it here.
la source
s->n->new StringBuilder(s).deleteCharAt(n)+"";
though it is longer.StringBuffer
instead ofStringBuilder
in codegolf. ;)Ruby, 16 bytes
Try it online!
la source
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.la source
R, 40 bytes
Just goes to show the variety of ways, none of which particularly compact, you can fiddle with strings in R.
la source
05AB1E, 5 bytes
Try it online!
la source
05AB1E, 6 bytes
Try it online!
Explanation
la source
Pyth, 3 bytes
Try it here.
Takes index first.
la source
PHP, 42 Bytes
0 indexed
Try it online!
la source
JS (ES6),
413231 bytesBased on this. Takes input through currying, first is string, second is index.
-9 thanks to @JohanKarlsson
-1 thanks to @ETHproductions
la source
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:
la source
vim,
107Takes 1-indexed input in the following format:
Thanks to @DJMcMayhem for 3 bytes!
la source
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.
la source
;
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!
la source
JavaScript (ES6),
393433 bytes56 bytes saved thanks to Arnauld.la source
brainfuck, 14 bytes
Try it online!
Reads zero-based one-byte index immediately followed by the string.
la source
Befunge-98,
352725 bytes-4 bytes thanks to @eush77
Try it online!
1-indexed, note that the input has a trailing null-byte.
la source
PHP, 41 bytes, 35 bytes excluding ?php
0-indexed
TIO
la source
[$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!
la source
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.
la source
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.