Título robado inspirado en la respuesta de Greg Hewgill a ¿Cuál es la diferencia entre JavaScript y Java?
Introducción
Java y JavaScript son lenguajes de uso común entre los programadores, y actualmente son las etiquetas más populares en Stack Overflow. Sin embargo, como todos sabemos, aparte de nombres similares, los dos no tienen casi nada en común.
En honor a uno de los debates más infames de la programación, e inspirado por mis recientes frustraciones en la búsqueda de etiquetas , propongo lo siguiente:
Reto
Escriba un programa que tome una cadena como entrada. Regrese car
si la cadena comienza con "Java" y no incluye "JavaScript". De lo contrario, regrese carpet
.
Ejemplo de entrada y salida
coche:
java
javafx
javabeans
java-stream
java-script
java-8
java.util.scanner
java-avascript
JAVA-SCRIPTING
javacarpet
alfombra:
javascript
javascript-events
facebook-javascript-sdk
javajavascript
jquery
python
rx-java
java-api-for-javascript
not-java
JAVASCRIPTING
Notas
- La coincidencia de entrada no debe ser sensible a mayúsculas y minúsculas
- Solo las posibilidades de salida deben ser
car
ocarpet
- Puntos de bonificación imaginarios si su respuesta usa Java, JavaScript o Regex
- Título alternativo: Java es a JavaScript como jamón es a hámster
code-golf
string
decision-problem
pattern-matching
Stevoisiak
fuente
fuente
javacarpet
podrían detectarse errores que los casos de prueba existentes no detectan.imaginary bonus points if your answer uses Java, Javascript, or Regex
, ¿eso hace complejo el bytecount de tal solución? ;)Alternate Title: Java is to JavaScript as ham is to hamster
Actually, the "ham" in "hamster" is cognate to the food "ham". The food "ham" is pig meat, and the term "hamster" is derived from the related animal, the guinea pig, whose meat replaced pigs meat on long sea voyages as the animals were easier to raise on a ship.Respuestas:
Java / JavaScript políglotas,
108107106 bytesEjecutar como Java
Pruébalo en línea!
Nota: no confíe en el resaltado ya que es incorrecto. El verdadero Java, correctamente interpretado, se ve a continuación porque
\u000A
se interpreta en el primer paso de la compilación como\n
, de facto, terminando el comentario que comenzó con el comentario de línea (//
).Ejecutar como JavaScript
Créditos a @CowsQuak para la versión JS.
¿Cuántos puntos de bonificación imaginarios para esta respuesta?
-1 byte gracias a @Nevay en la respuesta de Java.
fuente
a
and=>
. I tried to stay in the spirit of golfing. If I did something wrong, please tell me?JavaScript,
5049 bytesSaved 1 byte thanks to @ValueInk by rearranging the regex
Test snippet
fuente
/^java(?!script)/i
Java (OpenJDK 8),
9282725857 bytesTry it online!
1 byte saved thanks to @Nevay!
fuente
C (only calling puts), 131 bytes
It does have its problems, but it passes all of the testcases provided :)
well... no thanks
fuente
05AB1E, 21 bytes
Try it online!
fuente
lD“¦‚“åi“¾„“ë“îá“åi…carë
3 bytes more and 1 hour late :(. Nice job.Python 2, 68 bytes
Try it online!
-11 bytes thanks to notjagan
-2 bytes thanks to Dennis
fuente
;
is required so I since I have to use it anyway the newline is unnecessary :P That's why I used it.C#,
8078 bytesfuente
EXCELGoogle Sheets,8986 BytesSaved 3 bytes thanks to Taylor Scott
Takes an input on A1
Explanation
fuente
Search
es withFind
s for -2 bytes and that that could be further translated to a google sheets formula for -3 bytes by not closing the last three parensvim, 58 bytes
Try it online!
fuente
g
command might be a little shorter, something like:g/\cjavascript/d
:g!/^\cjava/d
icarpet␛:s/pet..*
.Jelly, 27 bytes
Try it online!
fuente
Ruby, 42+1 = 43 bytes
Uses the
-p
flag.Try it online!
fuente
Retina,
4437 bytesThanks to @MartinEnder for golfing off 7 bytes!
Try it online!
fuente
Common Lisp,
131125 bytesTry it online!
Size reduced thanks to the #n= “trick” of Common Lisp.
Explanation
fuente
C (tcc),
144136 bytesTry it online!
Unrolled:
fuente
Excel, 84 bytes
fuente
Excel VBA, 76 Bytes
Anonymous VBE immediate window function that takes input from range
[A1]
and outputs iscar
/carpet
status to the VBE immediate windowDoes not use RegExp
fuente
Python 3, 95 bytes
Try it online!
Yeah, it could be shorter but I wanted to try using a nested lambda!
fuente
and
,or
,if
,else
Perl, 42 bytes
I believe the answer by stevieb has an incorrect output (tried that one myself - it returns car for 'javajavascript'). This should work:
fuente
Mathematica, 82 bytes
regex
fuente
JAISBaL, 36 bytes
Verbose/explanation:
JAISBaL was my first attempt at designing a golfing language, so it's rather quirky... there's no matches or contains, regex or otherwise, so instead we have to split and check the resulting array length... because JAISBaL has a split-by-regex... but no other regex support.... because reasons.
Regex
stolenborrowed from @Cows Quack's answer.fuente
Python 2, 69 bytes
Currently 1 byte longer than the shortest Python 2 solution.
Try it online!
fuente
Perl, 36 bytes
Run it as such:
fuente
Batch, 91 bytes
Takes input on STDIN. Batch doesn't have a case insensitive comparison operator but it does have case insensitive string replacement so I can assign a temporary to the first four characters and then case insensitively replace java, which should then result in the empty string. Meanwhile I case insensitively replace javascript in the original string, which should leave it unchanged.
fuente
Lua, 96 bytes
fuente
Perl,
988462 BytesTry it online!
Thanks to bytepusher
fuente
return $b;
with just$b;
. Perl always returns the last evaluated statement. Since we don't care about warnings, you can even drop the ';' to$b}
. You don't need the brackets around the if. If you use||
instead ofor
, you can save a whitespace after the regex.!~
instead of! =~
the second condition can belc$_[0]!~
. ` ->sub a{$b="car";$b.="pet"if lc$_[0]=~/javascript/||lc$_[0]!~/^java/;$b}
. Using the ternary ops brings it down one moresub a{$b="car";$b.="pet"if lc$_[0]=~/javascript/||lc$_[0]!~/^java/;$b}
$_[0] =~//i||$_[0]!~//i
. And finally, why a variable?sub a{"car".($_[0]=~/javascript/i||$_[0]!~/^java/i?'pet':'');}
should do fine :). And now: perl will be nice and let you use $_[0] w/o specifying it (though not with!~
):sub a{"car".(/javascript/i||!/^java/i?'pet':'')}
-> 48 :)Dart VM,
104 bytes102 bytesExplanation:
Degolfed:
We have our usual main function
We replace
p
withp[0].toLowerCase();
so that we don't have to declare a new variable (var
plus a space would be 4 extra bytes)We then proceed to do the actual printing
We print
car
unconditionally and we use inline statements for checking whether to printpet
after it or not. If it has the string 'java' at index 0 and does not have 'javascript' in it, we do nothing (we actually append an empty string but it does not have any effect) and otherwise we appendpet
.fuente
Rust, 97 bytes
I'm pretty sure that there is a shorter solution but it's my first try :)
fuente
Bracmat, 66 bytes
Try it online!
fuente