SRM456 Div2 Easy(250) AppleWord
長さnのアップルワードは apn-3le。
#include <string> #include <ctype.h> using namespace std; class AppleWord { public: int minRep( string word ); }; int AppleWord::minRep( string word ) { int n = (int)word.length(); if ( n < 5 ) return -1; int c = 0; for ( int i=0; i<n; i++ ) { char w = tolower( word[i] ); if ( i == 0 && w != 'a' || 0<i&&i<n-2 && w != 'p' || i == n-2 && w != 'l' || i == n-1 && w != 'e' ) c++; } return c; }
Python。word.lower()とappleの異なる文字の個数、を綺麗に書く方法は無いものだろうか。
class AppleWord: def minRep(s,word): n = len(word) if n >= 5: apple = "a"+"p"*(n-3)+"le" return len([i for i in range(n) if word[i].lower()!=apple[i]]) else: return -1 if __name__ == "__main__": a = AppleWord() print a.minRep("TopCoder")