A bit hacky solution in Creative category for Striped Words by qria
import re checkio=lambda t:sum(any(all('@' Oct. 9, 2013 Comments: qria on Oct. 9, 2013, 4:34 a.m. <p>Some explanation </p> <pre class='brush: python'>'@'<c </pre> <p>evaluates to False if c is a number</p> <pre class='brush: python'>i&1 </pre> <p>means i%2 ( I don't know why I wrote it like this... i%2 gives the same result...)</p> <p>j^, (c in 'aeiouyAEIOUY')^ just toggles the i&2 if it's True </p> <p>I wrote 'aeiouyAEIOUY' because it's shorter than writing '.upper()'</p> <p>Everything else should be relatively straightforward.</p> <p>Also, I found a interesting thing while doing this</p> <pre class='brush: python'>'a'in 'aeiou' == True </pre> <p>evaluates to False</p> <p>I have no idea why...</p> qria on Oct. 9, 2013, 4:47 a.m. <pre class='brush: python'>>>> False == False == False True >>> False == False == True False </pre> <p>WHAT??? Python!! I TRUSTED YOU!!</p> qria on Oct. 9, 2013, 4:53 a.m. <p>Ok found out why python does that. Apparently python considers == operator as a comparison, meaning that 'False == False == False' is evaluated to 'False == False and False== False'</p> <p>Similarly ('a' in 'aeiou' == True) is the same as ('a' in 'aeiou' and 'aeiou' == True)</p> bryukh on Oct. 10, 2013, 1:38 a.m. <p>Thanks. It was something new for me.</p> veky on Oct. 9, 2013, 4:08 p.m. <p>The above is standard gotcha (in and == are same priority, chained), but here I must say I'm surprised. <em>What</em> did you expect (trust:) Python to reply to these?</p> veky on Oct. 10, 2013, 4:17 a.m. <p>I now (probably) see what you mean. C has poisoned so many people's minds. We (at least <em>should</em>) intuitively know what a==b==c means. Especially when (I hope C hasn't poisoned you here) you <em>do</em> know what a < b < c means. Nobody in his right mind would argue it is (a < b) < c.</p> <p>Of course, if you <em>want</em> (a==b)==c, just say so. And if you want a==(b==c) (it <em>could</em> be different:), say so. a==b==c is something else. [<em>Even in C</em>, if somebody wrote a==b==c without parentheses, I'd be pretty angry.:]</p> bryukh on Oct. 10, 2013, 4:30 a.m. <blockquote> <p>C has poisoned so many people's minds.</p> </blockquote> <p>+5. It was my first serious language :-)</p> <p>Thanks for this explanation.</p> veky on Oct. 10, 2013, 4:46 a.m. <p>Mine was BASIC (and it is equally serious as C, I can argue that as long as you want:-P). And already then people said BASIC scars you for life. Maybe. But C scars you much, much deeper.</p> <p>There are so many braindead decisions that made sense on PDP-11 (where C was developed), and practically nowhere else, and now are propagated through myths and legends about how processors really work.</p> <p>Also, C was made (not designed, just cobbled together of various pieces of B) for writing operating systems. It could be argued it is good for this job even today (especially if you write for some obscure machine). But how many operating systems have you written?</p> <p>Ok, you're bryukh, you write everything:-), so maybe you wrote one or even two. But 99.99% of the time, you write something entirely inappropriate for C. Don't (this is an appeal to you as much as to everybody else). Use the right tool for the job.</p> bryukh on Oct. 10, 2013, 5:07 a.m. <p>Oh, i remember BASIC -- i learned it in the school. But it was not seriously. </p> <blockquote> <p>But how many operating systems have you written?</p> </blockquote> <p>no one :-) I didn't choose - it was our teacher decision in the institute. Later i chose C++.</p> bryukh on Oct. 10, 2013, 5:09 a.m. <blockquote> <p>Use the right tool for the job.</p> </blockquote> <p>I agree with you. For me now it's Python, Javascript and little Java.</p> veky on Oct. 9, 2013, 4:10 p.m. <p>Cool. :-) Expecially three way ^. :-)</p> nickie on Nov. 3, 2013, 10:20 p.m. <p>Oh my... It turns out you can make it even shorter if you try to take out the cryptic bitwise stuff:</p> <pre class='brush: python'>import re checkio=lambda t:sum(len(set((c in 'AEIOUY')==i%2 for i,c in enumerate(w.upper())))==1 for w in re.findall(r"\w\w+",t)) </pre> <p>I'm finding this much easier to understand:</p> <ol> <li><p>re.findall gives you all the words that contain only letters and more than one.</p></li> <li><p>upper() converts each word to uppercase.</p></li> <li><p>enumerate gives you each letter of the word (c), together with its index (i) in the word.</p></li> <li><p>The comparison with i%2 gives you either all ones or all zeroes if and only if your word is striped. Otherwise, it gives you both ones and zeroes. To determine if a word is striped, you can put them all in a set and see if its cardinality is one.</p></li> </ol> <p>I'm writing it here for all ordinary people (like me) who may want to understand this and don't like this "make it shorter" game:</p> <pre class='brush: python'>def checkio(text): return sum(len(set( (c in 'AEIOUY') == i%2 for i, c in enumerate(w.upper()) ) ) == 1 for w in re.findall(r"\w\w+",t) ) </pre> qria on Nov. 4, 2013, 3:51 a.m. <p>Thanks for commenting :) I usually don't do much code golfing, but I think it's a fun game to play. I originally solved it with much more readability, but someone challenged me to make it shorter, so.... yeah. I have no idea why this is voted the highest.</p> <p>By the way, you might want to add a logic to check if a word contains a number. Since the question explicitly states that the words including a number is not considered a word, your solution fails the test at</p> <pre class='brush: python'>assert checkio("1st 2a ab3er root rate") == 1 # second test of Extra2 </pre> <p>just insert </p> <pre class='brush: python'>c not in '0123456789' </pre> <p>or </p> <pre class='brush: python'>'@'<c </pre> <p>and it'll work.</p> <p>But replacing complex any(all( ~ ) for j in (0, 1)) with simple len(set( ))==1 was a cool idea, and you probably still beat me. So I like your solution :)</p> nickie on Nov. 4, 2013, 6:25 a.m. <p>You're right. I thought \w was only for alpha but it was alphanumeric. I'm fixing it here (still 9 characters shorter, if that says anything):</p> <pre class='brush: python'>import re checkio=lambda t:sum(len(set((c in 'AEIOUY')==i%2 for i,c in enumerate(w)))==1 for w in re.findall(r"\b[A-Z]{2,}\b",t.upper())) </pre> <p>Fixed the explanation too:</p> <ol> <li><p>upper() converts all letters to uppercase.</p></li> <li><p>re.findall gives you all the words that contain only letters and more than one. \b stands for the empty string at the beginning or the end of a word.</p></li> <li><p>enumerate gives you each letter of the word (c), together with its index (i) in the word.</p></li> <li><p>The comparison with i%2 gives you either all ones or all zeroes if and only if your word is striped. Otherwise, it gives you both ones and zeroes. To determine if a word is striped, you can put them all in a set and see if its cardinality is one.</p></li> </ol> <p>I'm writing it here for all ordinary people (like me) who may want to understand this and don't like this "make it shorter" game:</p> <pre class='brush: python'>def checkio(text): return sum(len(set( (letter in 'AEIOUY') == index % 2 for index, letter in enumerate(word) ) ) == 1 for word in re.findall(r"\b[A-Z]{2,}\b", text.upper()) ) </pre> marshall.zheng on Dec. 13, 2013, 2:26 p.m. <p>amazing!</p> PaulBrown on Dec. 19, 2013, 5:01 a.m. <p>Good "code golf" but not very readable.</p> dorothyhs on Jan. 19, 2014, 8:34 p.m. <p>You guys are too good at this. I will catch up.</p> Columpio on Feb. 9, 2014, 7:03 a.m. <p>Using re is shitty trick! Can ya do it in python (not perl) way?</p> ForcePush on April 13, 2014, 4:08 p.m. <p>any(all())? wtf?</p>
Oct. 9, 2013
Comments: