มันเป็นคำที่แปลก?


29

ท้าทาย

ให้คำเดียวเป็นอินพุตให้พิจารณาว่าคำนั้นเป็นคี่หรือคู่

คำพูดที่แปลกและแม้แต่

สมมติกฎทั่วไป:

odd + odd = even
even + odd = odd
odd + even = odd
even + even = even

ในตัวอักษรตัวอักษรแปลกคือ:

aeiou

และตัวอักษรคู่คือ:

bcdfghjklmnpqrstvwxyz

เช่นเดียวกับตัวพิมพ์ใหญ่ ( AEIOUเป็นเลขคี่และBCDFGHJKLMNPQRSTVWXYZสม่ำเสมอ)

จากนั้นคุณ 'เพิ่ม' ตัวอักษรแต่ละตัวในคำด้วยกัน ตัวอย่างเช่นคำcatsนี้เทียบเท่ากับ:

even + odd + even + even

ซึ่งทำให้ง่ายต่อการ:

odd + even

ซึ่งช่วยให้ง่ายต่อการ:

odd

ดังนั้นคำว่าcatsแปลก

ตัวอย่าง

Input:  trees
Output: even

Input:  brush
Output: odd

Input:  CAts
Output: odd

Input:  Savoie
Output: even

Input:  rhythm
Output: even

กฎระเบียบ

การป้อนข้อมูลทั้งหมดจะเป็นคำเดียวซึ่งจะมีเพียงตัวอักษรตามตัวอักษร

หากคำนั้นแปลกให้ส่งค่าความจริงออกมา หากคำนั้นเป็นเลขคู่ให้ส่งออกค่าเท็จ

การชนะ

รหัสที่สั้นที่สุดในหน่วยไบต์ชนะ


1
คุณสามารถเพิ่มตัวอย่างของคำโดยไม่มีตัวอักษรแปลก ๆ
Hedi

@Hedi ฉันได้เพิ่มหนึ่งจังหวะ
Beta Decay

7
อดีตCuseคุณ Odd Word ™ ได้รับการจดเครื่องหมายการค้าโดย JLee แล้ว นี่คือการใช้คำที่ไม่ได้รับอนุญาต : P
Deusovi

2
นี่คือการขอร้องสำหรับการส่ง regex บริสุทธิ์
Rohan Jhunjhunwala

2
อินพุตรับประกันว่ามีเพียงตัวอักษรตามตัวอักษรเท่านั้น?
DJMcMayhem

คำตอบ:



18

EXCEL, 79 ไบต์:

=MOD(SUMPRODUCT(LEN(A1)-LEN(SUBSTITUTE(LOWER(A1),{"a","e","i","o","u"},""))),2)

input:
This function can be placed anywhere EXCEPT A1
Put your word in question into A1.

Output: 0 if even, 1 if odd.


13

JavaScript (ES6), 34 41 33 32 bytes

Saved 1 bytes thanks to Arnauld:

s=>~s.split(/[aeiou]/i).length&1
  • Odd word : returns 1
  • Even words : returns 0


Previous solutions:

33 bytes thanks to Arnauld:

s=>s.split(/[aeiou]/i).length&1^1
  • Odd word : returns 1
  • Even words : returns 0

Another way without bitwise operators:

s=>++s.split(/[aeiou]/i).length%2

41 bytes:

(s,a=s.match(/[aeiou]/ig))=>a&&a.length%2
  • Odd word : returns 1
  • Even words with odd letters : returns 0
  • Even words with no odd letters : returns null

42 bytes to return 0 instead of null:

(s,a=s.match(/[aeiou]/ig))=>a?a.length%2:0

34 bytes, breaks on words with no odd letters:

f=s=>s.match(/[aeiou]/ig).length%2

Saved 2 bytes thanks to Shaun H

s=>s.match(/[aeiou]/ig).length%2

1
This method breaks when word has no vowels. That said: f= isn't needed, and calling exec on the regex object is shorter. s=>/[aeiou]/ig.exec(s).length%2
Shaun H

I don't have the same result with exec with the g flag.
Hedi

dammit brain yeah ignore that, f= still isn't needed though
Shaun H

Could you do s=>s.split(/[aeiou]/i).length&1^1?
Arnauld

1
s=>~s.split(/[aeiou]/i).length&1 is actually one byte shorter.
Arnauld

8

Brain-Flak 206 196 192 178 + 3 = 181 bytes

Try it Online!

([]<{({}[((((((()()())){}){}){}){}){}()]){({}[({}())]){({}[({})]){({}[({}()())]){({}[({})]){({}<>)(<>)}}}}}{}{}}><>[[]]<>()()){(({}[<>(())<>()()])){{}({}())((<>)<>)}{}}{}<>({}<>)  

This requires the -c flag to run in ASCII mode adding an extra 3 bytes to the length of the program.

Ungolfed

([]<
{({}[(((((()()()){}){}){}){}){}()])
 {
  ({}[()()()()])
  {
   ({}[()()()()])
   {
    ({}[(()()()){}])
    {
     ({}[(()()()){}])
     {
      ({}<>)
      (<>)
     }
    }
   }
  }
 }
 {}
}
><>[[]]<>)
(<(()()<>)>)<>{({}[()])<>(({}()[({})])){{}(<({}({}))>)}{}<>}{}<>({}<{}><>)

Explanation

First store the stack height for future purposes

([]<...>

Then while the stack is not empty (assumes that none of the characters is zero)

{...}

Subtract ninety seven (and store 3 for later optimizations)

({}[((((((()()())){}){}){}){}){}()])

If it is not zero (i.e. not a)

{...}

Subtract 4 (and store 4 for later optimizations)

({}[({}())])

If it is not zero (i.e. not e)

{...}

Subtract 4 (and store 4 for later optimizations)

({}[({})])

If it is not zero (i.e. not i)

{...}

Subtract 6 (and store 6 for later optimizations)

({}[({}()())])

If it is not zero (i.e. not o)

{...}

Subtract 6 (store 6 because the program expects one later)

({}[({})])

If it is not zero (i.e. not u)

{...}

Move the remainder to the other stack and put a zero on the active stack to escape all of the ifs

({}<>)(<>)

Once all of the ifs have been escaped remove the zero and the six

{}{}

Once all the characters have been processed subtract the height of the offset from the originally stored height.

...<>[[]]<>)

Mod by two

{(({}[<>(())<>()()])){{}({}())((<>)<>)}{}}{}<>({}<>) 

I think -c is only +1 byte since ever Perl answer also only adds 1 byte / flag.
ThreeFx

1
@ThreeFx That's because perl -pe'code' is only one byte longer than perl -e'code'.
Dennis

8

C, 42 bytes

f(char*s){return*s&&2130466>>*s&1^f(s+1);}

This works with GCC 4.x on a x86-64 CPU. Results may vary with different setups.

Test it on repl.it.

At the cost of 5 more bytes, undefined behavior can be avoided, so the code should work as long as ints are at least 32 bits wide.

f(char*s){return*s&&2130466>>(*s&31)&1^f(s+1);}

How it works

Modulo 32, the character codes of all odd letters are 1, 5, 9, 15, and 21. 2130466 is the 32-bit integer that has set bits at these positions and unset bits at all others.

When f is called on a string, it first checks if the first character of the string is a null byte (string terminator). If it is, *s yields 0 and f returns 0. Otherwise, *s yield the character code of a letter and the right argument of the logical AND (&&) is executed.

For >>, GCC generates a shift instruction. On a x86-64 CPU, the corresponding instruction for a 32-bit integer ignores all but the lower 5 bits of the right argument, which avoids reducing *s modulo 32. The right shift and the following bitwise AND with 1 extracts the bit of 2130466 that corresponds to the letter, which will be 1 if and only if the letter is odd.

Afterwards, we increment the pointer s (effectively discarding the first letter), call f recursively on the beheaded string, and take the bitwise XOR of the result from above and the result of the recursive call.


Great bit wise work!
Keyu Gan

erees Return 0 in Ideone, is it right?
RosLuP

@RosLuP No, that's not correct. It works on my computer and on repl.it though (possibly because the version of GCC is quite different).
Dennis

Yeah, it's definitely the compiler. With clang 3.7, it works on Ideone as well.
Dennis

7

sed 44 (42 + 1 for -n) 43

-1 thanks to Neil

s/[aeiou][^aeiou]*[aeiou]//gi
/[aeiou]/Ico

Prints o for odd and nothing for even


s/[aeiou][^aeiou]*[aeiou]//gi might save you a byte, if I've counted correctly.
Neil

@Neil Yep! I wish sed could do non-greedy search.
Riley



6

Brain-Flak, 524, 446, 422 bytes

{(<((((()()()()){}){}){}<>)>)<>{({}[()])<>(({}[({})]())){{}(<({}({}))>)}{}<>}{}<>([(((({}<{}<>>))))]()){(<{}>)<>({}[()])<>}<>({}())<>{}([{}]()()()()()){(<{}>)<>({}[()])<>}<>({}())<>{}(((()()())){}{}[{}]){(<{}>)<>({}[()])<>}<>({}())<>{}(((()()()()())){}{}[{}]){(<{}>)<>({}[()])<>}<>({}())<>{}((((()()()){}())){}{}[{}]){(<{}>)<>({}[()])<>}<>({}())<>{}}(<(()())>)<>{({}[()])<>(({}[({})]())){{}(<({}({}))>)}{}<>}{}<>({}<{}<>>)

Try it online!

Ungolfed, more readable version:

{((((()()()()){}){}){})(<({}<>)>)<>{({}[()])<>(({}()[({})])){{}(<({}({}))>)}{}<>}{}<>({}<{}><>)((((({})))))
(())
({}[{}]){(<{}>)<>({}[()])<>}<>({}())<>{}
(()()()()())
({}[{}]){(<{}>)<>({}[()])<>}<>({}())<>{}
(()()()()()()()()())
({}[{}]){(<{}>)<>({}[()])<>}<>({}())<>{}
(()()()()()()()()()()()()()()())
({}[{}]){(<{}>)<>({}[()])<>}<>({}())<>{}
(()()()()()()()()()()()()()()()()()()()()())
({}[{}])
{(<{}>)<>({}[()])<>}<>({}())<>{}}<>(()())(<({}<>)>)<>{({}[()])<>(({}()[({})])){{}(<({}({}))>)}{}<>}{}<>({}<{}><>){{}([()])
(<><>)}({}{}())

The Ungolfed version doesn't seem to work (can't fit TIO link in comment ;_;)
Wheat Wizard

6
"more readable" 'nough said
Rohan Jhunjhunwala

6

Jelly, 13 12 11 bytes

-1 byte thanks to @Luis Mendo (use to replace %2)
-1 byte thanks to @Dennis (use a string compression)

Œlf“¡ẎṢɱ»LḂ

All test cases are at TryItOnline

How?

Œlf“¡ẎṢɱ»LḂ - Main link takes an argument - s
Œl          - lowercase(s)
   “¡ẎṢɱ»   - string of lowercase vowels (compression using the words a and eoui)
  f         - filter - keep only the vowels
         L  - length - the number of vowels
          Ḃ - Bit (modulo 2)

Non-competing, 5 bytes (since I just added the function Øc)

fØcLḂ

Test cases also at TryItOnline

Same as above, but Øc yields the Latin alphabet's vowels, 'AEIOUaeiou'


1
I think you can replace %2 by
Luis Mendo

7
euoi is a cry of impassioned rapture in ancient Bacchic revels, so you can use the dictionary and get the vowels as “¡ẎṢɱ».
Dennis

@Dennis - LOL of course!
Jonathan Allan

2
@Dennis How exactly does that work? Is it just base-compression to get the index of a word in a giant dictionary? Where does the 'a' come from?
DJMcMayhem

2
@DJMcMayhem it's a base 250 compression using a dictionary (which I believe was just taken from Dennis's computer) with separation between short (less than 6 char) and long words. Some code to automate the process of making compressed strings was written by Lynn. The unused 6 bytes are Jelly's string identifying characters “”«»‘’ (there is also which is for a two-char string, but that is used within compressed strings).
Jonathan Allan


4

Python 3, 53 Bytes

This can probably be golfed further:

lambda n:[x in 'aeiou' for x in n.lower()].count(1)&1

Remove spaces between in and 'aeiou' and for, and use sum to save 8 bytes: lambda n:sum(x in'aeiou'for x in n.lower())&1 (although as you can see from DJMcMayhem's post using all ten vowels is also shorter)
Jonathan Allan

1
Thanks for the tips! I noticed the extra spaces a little bit after posting, but in all honesty, @DJMcMayhem and I had identical approaches with his being the best version I can imagine for a Python solution. I didn't know about the sum() command before this golf so once again I learned something! Have a nice day :)
L. Steer


4

C 52 bytes

h(o){o=strpbrk(o,"aeiouAEIOU");return o?1^h(o+1):0;}

the main and the result:

main()
{int   k;
 char *a[]={"trees","brush","CAts","Savoie","rhythm", 0};

 for(k=0;a[k];++k)
     printf("[%s]=%s\n", a[k], h(a[k])?"odd":"even");
}

/*
91
[trees]=even
[brush]=odd
[CAts]=odd
[Savoie]=even
[rhythm]=even

*/

i try to use int as pointers but it not compile where the use of inirection *... the solution printed first here was wrong...
RosLuP

h(o){return~-o?1^h(1+strpbrk(o,"aeiouAEIOU")):1;} saves 3 bytes.
Dennis

s;h(o){s=~-o?1^h(1+strpbrk(o,"aeiouAEIOU")):1;} the result is in the global variable s...
RosLuP

Unfortunately, that's not allowed. Functions have to be reusable, and this will break if you use it more than once. Also, saving the output in a variable is not allowed, unless you take the location as user input.
Dennis

I think in this case reuse is possible becausr the start value of s now is not important...ok ok now i think something can go wrong... Thanks
RosLuP



3

Vim, 32, 31, 29 keystrokes

:s/[^aeiou]//gi
C<C-r>=len(@")%2<cr>

Since the V interpreter is backwards compatible, you can try it online! right here.

One Three bytes saved thanks to m-chrzan!


1
Can you do s/.../gi instead of s/\c.../g?
m-chrzan

@m-chrzan Woah, awesome tip. Thanks!
DJMcMayhem

Also, '<C-r>"' -> @".
m-chrzan

1
I found out about @" 15 minutes ago. The best vim tip I have is to use :help foo and / the documentation :P.
m-chrzan

2
Well, since we're talking about golf, :h foo is shorter. :P
DJMcMayhem

3

Java 7, 88

boolean f(char[]s){int x=0;for(char c:s)if("aeiouAEIOU".indexOf(c)>=0)++x;return x%2>0;}

Ungolfed:

  boolean f(char[] s) {
    int x = 0;
    for (char c : s) {
      if ("aeiouAEIOU".indexOf(c) >= 0) {
        ++x;
      }
    }
    return x % 2 > 0;
  }

3

dimwit, 14 bytes (non-competing)

ar[aeiou]}et}T

I thought this would be a fun, simple challenge to start with for a new language.

Explanation

  • a - push a new array to the matrix
  • r[aeiou]} - count occurrences of all values matching the regex "[aeiou]" in the first array (since the first array contains the input), ignoring case, and push that value to the end of the last array.
  • e - if the last number in the last array is even (which we set to the number of occurrences), perform the next operations up until a closing bracket ("}")
  • t - stop execution, clear the matrix, and set the first value to be false
  • } - end of e code block
  • T - stop execution, clear the matrix, and set the first value to be true

Try it online!

Use the Input field to enter the word.

I'll soon add documentation...


2

PowerShell v2+, 45 42 bytes

($args[0]-replace'[^aeiouAEIOU]').Length%2

Takes input $args[0], sends it through -replace to remove all non-vowel characters, takes the resulting .length, and %2 to check whether it's odd/even.

Examples

PS C:\Tools\Scripts\golfing> 'trees','brush','CAts','Savoie','rhythm'|%{"$_ --> "+(.\is-it-an-odd-word.ps1 $_)}
trees --> 0
brush --> 1
CAts --> 1
Savoie --> 0
rhythm --> 0

2

J, 20 bytes

2|+/@e.&'aeiouAEOIU'

Straight-forward approach

Explanation

2|+/@e.&'aeiouAEOIU'  Input: string S
     e.&'aeiouAEOIU'  Test each char in S for membership in 'aeiouAEOIU'
  +/@                 Sum those values
2|                    Take it modulo 2 and return

Haha, I just posted a J answer (now deleted) 1 byte longer than this. Nice job!
Conor O'Brien

Where is the command for "take input"?
RosLuP

@RosLuP This is a verb (function) that takes a single argument as input. J uses tacit programming so commands are chained together and pass values implicitly
miles

2

Japt, 7 bytes

1&Uè"%v

Test it online! Outputs 1 for odd, 0 for even.

How it works

         // Implicit: U = input string
  Uè     // Count the number of matches of the following regex in the input:
    "%v  //   /[AEIOUaeiou]/g
1&       // Take only the first bit (convert 1, 3, 5, etc. to 1, and others to 0)
         // Implicit output

2

Octave, 34 bytes

@(s)mod(nnz(~(s'-'aeiouAEIOU')),2)


s'-'aeiouAEIOU'    % Creates a 2D-matrix where each of the odd letters are 
                   % subtracted from the string s
~(s'-'aeiouAEIOU') % Negates that array, so the all zero elements become 1
nnz( .... )        % Counts all the non-zero elements (the odd letters)
mod(nnz( ....),2   % Takes this sum modulus 2

This is 6 bytes shorter than the traditional approach using ismember, @(s)mod(sum(ismember(s,'aeiouAEIOU')),2), and two bytes shorter than the regex approach: @(s)mod(nnz(regexpi(s,'[aeiou]')),2).

Test it here.





2

C# 64 62 56 50 Bytes

s=>1>s.Split("aeiouAEIOU".ToCharArray()).Length%2;
  • We are already using linq, so Contains saves 2 bytes over IndexOf
  • Using the method overload of Count saves 6 bytes
  • Thanks to @Milk for suggesting a neat method and saving 6 more bytes

An anonymous function that takes a string and counts the odd letters then returns true if there is an odd number of them or false if there is not.

This new solution splits the string on any of the characters in the given char array. The mechanics of this flip the meaning of the %2 result; 0 is now odd and 1 even hence the 1>.

Try it online here!


It's only 50 bytes to use string.Split() to count the vowels and you don't need LINQ. s=>1>s.Split("aeiouAEIOU".ToCharArray()).Length%2;
milk

@milk Thanks for that, very neat solution.
JustinM - Reinstate Monica



โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.