| Bytes | Lang | Time | Link |
|---|---|---|---|
| 054 | AWK | 250825T131012Z | xrs |
| 044 | GolfScript | 231014T000123Z | noodle p |
| nan | Perl 5 p | 231113T182959Z | Xcali |
| 088 | Lua | 231111T215514Z | Kamila S |
| 081 | PowerShell | 231113T113818Z | Mark Har |
| 134 | Go | 230201T184755Z | bigyihsu |
| 066 | Bracmat | 191102T091133Z | Bart Jon |
| 097 | Rust | 170717T103702Z | f4bi4n |
| 131 | C only calling puts | 170713T210214Z | michi7x7 |
| 125 | Common Lisp | 170713T180428Z | Renzo |
| 136 | C tcc | 170713T182017Z | Steadybo |
| 102 | Dart VM | 170716T202549Z | Ice Flak |
| 057 | Java OpenJDK 8 | 170713T160236Z | Olivier |
| 106 | Java/JavaScript Polyglot | 170713T231720Z | Olivier |
| 062 | Perl | 170713T175637Z | Burgan |
| 042 | Perl | 170716T120747Z | bytepush |
| 096 | Lua | 170716T124021Z | IS4 |
| 091 | Batch | 170715T235724Z | Neil |
| 036 | Perl | 170715T210907Z | stevieb |
| 069 | Python 2 | 170714T190548Z | Dennis |
| 095 | Python 3 | 170714T174845Z | Kavi |
| 068 | Python 2 | 170713T155303Z | hyperneu |
| 076 | Excel VBA | 170713T210017Z | Taylor R |
| 084 | Excel | 170714T105553Z | Wernisch |
| 049 | JavaScript | 170713T155830Z | user4180 |
| 037 | Retina | 170713T211234Z | Dennis |
| nan | Ruby | 170713T233115Z | Value In |
| 036 | JAISBaL | 170713T231558Z | Socratic |
| 027 | Jelly | 170713T230707Z | Dennis |
| 086 | 86 Bytes | 170713T163836Z | Mark |
| 058 | vim | 170713T212317Z | Ray |
| 082 | Mathematica | 170713T165250Z | ZaMoC |
| 078 | C# | 170713T160311Z | TheLetha |
| 021 | 05AB1E | 170713T155342Z | Erik the |
AWK, 54 bytes
IGNORECASE=1&&$0=/^java/&&!/javascript/?"car":"carpet"
Tried a few ways and kept getting 54:
IGNORECASE=1&&$0="car"(/^java/&&!/javascript/?X:"pet")
GolfScript, 53 49 44 bytes
{32|}%"java"/(,\{6<"script"=+}/"carpet".3<if
This was my second GolfScript submission so after writing a couple more I was able to revisit this and shave a couple bytes.
Previously, this mapped over a list, then summed it, then added it to a base value. Now, it just loops over the list, adding to the base value on each iteration.
Here's an explanation of how the code works:
{32|}% # Convert the string to lowercase
"java"/ # Split on "java".
(, # Extract the first item, and push its length
# (0 iff string starts with java).
\ # Swap so the rest of the list is on top of the stack.
{6<"script"=+}/ # Check if any start with "script":
{ }/ # Loop over each item:
6< # Take the first 6 characters.
"script"= # Are they equal to "script"? (0 or 1)
+ # Add this to the top of the stack.
# After this loop, the stack will be 0 iff the string started
# with "java" and does not contain "javascript".
"carpet".3< # Push "carpet" and its first 3 characters.
if # If the value is 0, "car", otherwise "carpet".
Lua, 88 bytes
s=io.read():lower()print(s:sub(1,4)=='java'and not s:find'javascript'and'car'or'carpet')
PowerShell, 81 bytes
$a={param($s)$s.StartsWith('java')-and(!s.contains('javascript'))?'car':'carpet'}
Go, 134 bytes
import."strings"
func f(s string)string{T,o:=ToLower,"car"
if Contains(T(s),"javascript")||!HasPrefix(T(s),"java"){o+="pet"}
return o}
Rust, 97 bytes
let s=if Regex::new(r"^javascript|^!java$").unwrap().is_match("javascript"){"carpet"}else{"car"};
I'm pretty sure that there is a shorter solution but it's my first try :)
C (only calling puts), 131 bytes
f(int*s){char r[]="carpet";~*s&'AVAJ'||(r[3]=0);for(;*s&255;*(int*)&s+=1)~*s&'AVAJ'||~s[1]&'IRCS'||~s[2]&'TP'||(r[3]='p');puts(r);}
It does have its problems, but it passes all of the testcases provided :)
g(int* s)
{
char r[] = "carpet";
~*s&'AVAJ' || (r[3]=0);
for(;*s & 255; *(int*)&s +=1)
~*s&'AVAJ' || ~s[1]&'IRCS' || ~s[2]&'TP' || (r[3]='p');
puts(r);
}
Imaginary bonus points if your answer uses Java, Javascript, or Regex
well... no thanks
Common Lisp, 131 125 bytes
(lambda(s)(format()"car~@[pet~]"(or(<(length s)4)(not(#1=string-equal"java"(subseq s 0 4)))(search"javascript"s :test'#1#))))
Size reduced thanks to the #n= “trick” of Common Lisp.
Explanation
(lambda (s) ; anonymous function
(format ; use of format string to produce the result
() ; the result is a string
"car~@[pet~]" ; print "car", then print "pet" when:
(or (< (length s) 4) ; the string is less then 4 characters or
(not (string-equal "java" (subseq s 0 4))) ; does not start with java or
(search "javascript" s :test 'string-equal)))) ; contains javascript
C (tcc), 144 136 bytes
a;f(s){char*t=s;for(;*t;a=!strncmp(s,"java",4))*t=tolower(*t++);for(t=s;*t;)s=strncmp(t++,"javascript",10)&&s;puts(a*s?"car":"carpet");}
Unrolled:
a;
f(s)
{
char *t = s;
for (; *t; a = !strncmp(s, "java", 4))
*t = tolower(*t++);
for (t=s; *t;)
s = strncmp(t++, "javascript", 10) && s;
puts(a*s ? "car" :"carpet");
}
Dart VM, 104 bytes 102 bytes
main(p){p=p[0].toLowerCase();print("car${p.indexOf('java')==0&&p.indexOf('javascript')<0?'':'pet'}");}
Explanation:
Degolfed:
main(p)
{
p = p[0].toLowerCase();
print("car${p.indexOf('java') == 0 && p.indexOf('javascript') < 0 ? '' : 'pet'}");
}
We have our usual main function
We replace p with p[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 print pet 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 append pet.
Java (OpenJDK 8), 92 82 72 58 57 bytes
s->s.matches("(?i)(?!.*javascript)java.*")?"car":"carpet"
1 byte saved thanks to @Nevay!
Java/JavaScript Polyglot, 108 107 106 bytes
//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
Run as Java
//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
Note: don't trust the highlight as it's incorrect. The real Java, properly interpreted looks like below because \u000A is interpreted in the very first step of the compilation as \n, de facto ending the comment that started with the line comment (//).
//
s->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
Run as JavaScript
//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
Credits to @CowsQuak for the JS version.
let f=
//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
var a=["java","javafx","javabeans","java-stream","java-script","java-8","java.util.scanner","javascript","java-avascript","javascript-events","facebook-javascript-sdk","javajavascript","jquery","python","rx-java","java-api-for-javascript","not-java"];
for(var s of a) console.log(s.padStart(a.reduce((x,y)=>x.length>y.length?x:y).length) + "=>" + f(s));
How many imaginary bonus points for this answer?
-1 byte thanks to @Nevay in the Java answer.
Perl, 98 84 62 Bytes
sub a{"car".($_[0]=~/javascript/i||$_[0]!~/^java/i?'pet':'');}
Thanks to bytepusher
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:
say/^java/i&&!/javascript/i?'car':'carpet'
Lua, 96 bytes
function(x)return x:lower():match"^java"and not x:lower():match"javascript"and"car"or"carpet"end
Batch, 91 bytes
@set/ps=
@set t=%s:~0,4%
@if "%t:java=%%s:javascript=%"=="%s%" (echo car)else echo carpet
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.
Perl, 36 bytes
say/^java(?!script)/i?"car":"carpet"
Run it as such:
perl -nE 'say/^java(?!script)/i?"car":"carpet"' java.txt
Python 2, 69 bytes
f=input().lower().find
print'car'+'pet'*(f('java')!=~f('javascript'))
Currently 1 byte longer than the shortest Python 2 solution.
Python 3, 95 bytes
g=lambda s:(lambda r:'car' if r[:4]=='java' and 'javascript' not in r else 'carpet')(s.lower())
Yeah, it could be shorter but I wanted to try using a nested lambda!
Python 2, 68 bytes
k=input().lower();print'car'+'pet'*(k[:4]!='java'or'javascript'in k)
Excel VBA, 76 Bytes
Anonymous VBE immediate window function that takes input from range [A1] and outputs is car/carpet status to the VBE immediate window
Does not use RegExp
?"car"IIf(InStr(1,[A1],"Java",1)*(InStr(1,[A1],"JavaScript",1)=0),"","pet")
Excel, 84 bytes
="car"&IF(AND(ISERR(SEARCH("javascript",A1)),IFERROR(SEARCH("java",A1),2)=1),,"pet")
JavaScript, 50 49 bytes
Saved 1 byte thanks to @ValueInk by rearranging the regex
a=>/javascript|^(?!java)/i.test(a)?"car":"carpet"
Test snippet
let f=
a=>/javascript|^(?!java)/i.test(a)?"carpet":"car"
var a=["java","javafx","javabeans","java-stream","java-script","java-8","java.util.scanner","java-avascript","javascript","javascript-events","facebook-javascript-sdk","javajavascript","jquery","python","rx-java","java-api-for-javascript","not-java"];
for(var s of a) console.log(s.padStart(a.reduce((x,y)=>x.length>y.length?x:y).length) + "=>" + f(s));
Retina, 44 37 bytes
Ai`^(?!.*javascript)java
.+
pet
^
car
Thanks to @MartinEnder for golfing off 7 bytes!
JAISBaL, 36 bytes
℠℘(?!.*javascript)^java.*}͵Ucar½Upet
Verbose/explanation:
# \# enable verbose parsing #\
tolower \# [0] convert the top value of the stack to lowercase #\
split (?!.*javascript)^java.*} \# [1] split the top value of the stack by (?!.*javascript)^java.*} #\
arraylength \# [2] push the length of the array onto the stack #\
print3 car \# [3] print car #\
!if \# [4] if the top value on the stack is falsy, skip the next statement #\
print3 pet \# [5] print pet #\
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 stolen borrowed from @Cows Quack's answer.
EXCEL Google Sheets, 89 86 Bytes
Saved 3 bytes thanks to Taylor Scott
=LEFT("carpet",6-3*ISERR(SEARCH("javascript",A1))+3*ISERR(IF(SEARCH("java",A1)=1,1,1/0
Takes an input on A1
Explanation
=LEFT("carpet",6-3*ISERR(SEARCH("javascript",A1))+3*ISERR(IF(SEARCH("java",A1)=1,1,1/0)))
SEARCH("javascript",A1) #Case-Insensitive Find, returns error if not found
ISERR( #Returns string true if error, False if not
3*ISERR( #Forces TRUE/False as integer, multiplies by 3
IF(SEARCH("java",A1)=1,1,1/0) #If java found, returns integer. if 1, java begins string
#so returns 1, which will be turned into 0 by iserr.
#Else returns 1/0, which will be turned into 1 by iserr.
LEFT( #Returns digits from the left, based upon count.
Mathematica, 82 bytes
regex
If[#~StringMatchQ~RegularExpression@"(?i)(?!.*javascript)^java.*","Car","Carpet"]&
C#, 80 78 bytes
s=>(s=s.ToLower()).StartsWith("java")&!s.Contains("javascript")?"car":"carpet"