avatar
a*c
1
Q. Find a vowel product sum of an ASCII string.
> There are are 26 English upper and lower-case letters. Consider the
> encoding where vowels are mapped to numbers as follows: (a=1, e=5,
> i=9, o=15, u=21, y=25 or 0). A vowel sequence is a maximal substring
> of consecutive vowels. A vowel product is the product of the vowel
> encoding of all vowels in a vowel sequence. The vowel product sum
> is the sum of the vowel product of all vowel sequences in the
> string.
> A letter in [AEIOUaeiou] is always a vowel. 'y' or 'Y' is a vowel if
> it is preceded by a consonant; otherwise, it is a consonant. An
> ASCII character is either a vowel, a consonant, or a
> non-letter. Treat all upper-case letters the same as lower-case
> letters.
> Examples:
> "Google Guy" has vowel sequences of "oo", "e", and "u". Its score is
> 15 * 15 + 5 + 21 = 251.
> "Yyyyy" has vowel sequences of "y" and "y". Its score is 25 + 25 = 50.
> "myopia" has vowel sequences of "yo" and "ia". Its score is 25 * 15
> + 9 * 1 = 384.
> "Quietly" has vowel sequences of "uie" and "y". Its score is
> 21 * 9 * 5 + 25 = 970.
> "I'm Feeling Yucky!" has vowel sequences of "I", "ee", "i", "u", and
> "y". Its score 9 + 5 * 5 + 9 + 21 + 25 = 89. The first 'Y' in
> "Yucky" is preceded by a space (non-letter), so it is not considered
> a vowel.
avatar
J*9
2
rules too complicated
start with the simple rules
then modify one by one to get the full solution
avatar
a*c
3
Yeah, and he only give me 20mins to solve

【在 J**9 的大作中提到】
: rules too complicated
: start with the simple rules
: then modify one by one to get the full solution

avatar
w*3
4
题目表述的很复杂,实际不是很难吧。基本就等于是计算一个只有+和*的运算式,那些
字母的条件就是把一个string替换成等价的四则运算的规则吧。
avatar
x*i
5
感觉对y的处理比较复杂. 我纯新手...写了一个代码, 望斧正
然后对于大小写的处理, 我就是创建一个新数组, 然后把所有的都转换成小写字母.
我运行了题目里给出的所有字符串, 结果都是正确的, 然后test了一个空字符串, 返回
为0. 求问还需要做什么样的test啊? 感觉很多代码写出来自己都不知道对不对, 因为
没有完善的test方案, 求指点...新手跪谢了
#define VOWELY 0
#define CONSOY 1
int vowelProduct(const char* newstr){
int length = strlen(newstr);
const char* consolant = "bcdfghjklmnpqrstvwxz";
const char* vowels = "aeiou";
int i;
int yFlag;
int score=0;
char* current;
char* yTest;
int point[26] = {0};
int tempScore=0;
char* str = (char*)malloc(strlen(newstr));
memcpy(str, newstr, strlen(newstr));
for(i=0;iif(str[i]>='A' && str[i]<='Z')
str[i] = str[i] -'A'+'a'; //convert all to small letters
}
point[0]=1;
point[4]=5;
point[8]=9;
point[14]=15;
point[20]=21;
point[24]=25;
if(*str=='y'){
str++;
yFlag=CONSOY;
}
while(*str){
if(*str!='y'){
current = strchr(vowels, *str);
if(current==NULL){
score+=tempScore; //not a vowel
tempScore=0;
}
else{ //a vowel of aeiou
if(tempScore==0)
tempScore=1;
tempScore *= point[(int)*current-'a'];
}
}
else{ //This is a 'y' and it's not at the first place
yTest = strchr(consolant, *(--str));
current = strchr(vowels, *str);
if((yTest==NULL&&*(str)!='y')||(*str=='y')&&yFlag==VOWELY||
current!=NULL){ yFlag=CONSOY;
score+=tempScore;
tempScore=0;
}
else if(yTest!=NULL||(*(str)=='y'&&yFlag==CONSOY)){
if(tempScore==0)tempScore=1;
tempScore *= 25;
yFlag=VOWELY;
}
str++;
}
str++;
}
return score+tempScore;
}
avatar
a*c
6
Boolean checkBasicVowel(char c) {
if (c == ‘a’ || c ==’e’ || c == ‘i’ || c ==’o’ ||
c == ‘u’ || c == ‘A’ || c ==’E’ || c == ‘I’ ||
c ==’O’ ||c == ‘U’)
return True;
}
// check whether it is vowel
Boolean checkVowel(String s, int index){
char c = s.charAt(index);
if (checkBasicVowel(c))
return True;

if (index == 0)
return False;
if (c == ‘y’ || c == ‘Y’) {
if(!checkVowel(s, index - 1))
return True;
return False;
}
return False;
}
// get the value of char
int getValue(char c) {
// TODO
int r = c - ‘a’ + 1;
if (r >= 0)
return r;
return c - ‘A’ + 1;
}
int getScore(String s) {
Boolean cont = False;
int result = 0, lastvalue = 0;
for(int i = 0; i < s.length(); i++) {
if (checkVowel(s, i)) {
if(cont) {
result -= lastvalue;
lastvalue *= getValue(s.charAt(i));
result += lastvalue
} else {
result += getValue(s.charAt(i));
lastvalue = getValue(s.charAt(i));
}
cont = True;
} else {
//int v = getValue(s.charAt(i));
cont = False;
lastvalue = 0;
}
}
return result;
}
Test cases:
1. null
2. “”
3. “good”
4. “yell”
5. “goooooooooodyyyyyy kooghgy”
这是我在面试时候写的,请指正!完了之后他还问了写sql的问题,还有如何备份数据
的问题
avatar
c*t
7
谢分享! 测过"I'm Feeling Yucky!" 吗?

【在 a**c 的大作中提到】
: Boolean checkBasicVowel(char c) {
: if (c == ‘a’ || c ==’e’ || c == ‘i’ || c ==’o’ ||
: c == ‘u’ || c == ‘A’ || c ==’E’ || c == ‘I’ ||
: c ==’O’ ||c == ‘U’)
: return True;
: }
: // check whether it is vowel
: Boolean checkVowel(String s, int index){
: char c = s.charAt(index);
: if (checkBasicVowel(c))

avatar
t*a
8
用函数式编程写了一个。要20分钟内一次写对做不到,楼主挺厉害的。
(defn fold [vowel ch]
(let [alphabet (set (map char (range 97 123)))]
(when (contains? alphabet ch)
(case ch
\a 1
\e 5
\i 9
\o 15
\u 21
\y (if (and (not (nil? vowel)) (zero? vowel))
25
0)
0))))
(defn vowel-seq [s]
(let [sl (vec (clojure.string/lower-case s))]
(remove nil? (rest (reductions fold (cons 1 sl))))))
(defn vowel-prod-sum [s]
(let [p (partition-by #(> % 0) (vowel-seq s))]
[(reduce + (map #(apply * %) p)) p]))
(vowel-prod-sum "Google Guy") ; [251 ((0) (15 15) (0 0) (5) (0 0) (21) (0))]
(vowel-prod-sum "Yyyyy") ; [50 ((0) (25) (0) (25) (0))]
(vowel-prod-sum "myopia") ; [384 ((0) (25 15) (0) (9 1))]
(vowel-prod-sum "Quietly") ; [970 ((0) (21 9 5) (0 0) (25))]
(vowel-prod-sum "I'm Feeling Yucky!") ; [89 ((9) (0 0) (5 5) (0) (9) (0 0 0)
(21) (0 0) (25))]
avatar
J*9
9
This is another question that seems easy but hard to code precisely.
Here's my version in C:
///1) Convert the string into all lower cases either in place or into
another buffer;
///2) First pass to check which ones are vowels;
///3) Second pass to sum up the products.
///4) Space O(n); Time O(n)
/** convert a string to lowercase in place */
char* hkStringTolower(char *str)
{
if(!str) return NULL;
char *s=str;
while(*s)
{
*s = tolower(*s);
s++;
}
return str;
}
static inline int mapVowelToNum(char c)
{
return (int)(c - 'a' + 1);
}
int hkStringFindVowelProduct(char *s)
{
if (!s || !(*s)) return 0;
const char *vowels = "aeiou";
int rtn = 0;
char *p = hkStringTolower(s);
int len = strlen(p);
int i=0;
bool isVowels[len];
for(i=0; i{
isVowels[i] = false;
if(strchr(vowels, (int)p[i]))
isVowels[i] = true;
else if(p[i]=='y')
{
if (i>=1)
isVowels[i] = ((!isVowels[i-1])&&(isalpha(p[i-1])));
}
}
i=0;
while(i{
int product = 1;
bool found = false;
while(isVowels[i])
{
found = true;
int map = mapVowelToNum(p[i]);
printf("%c=%d\n", p[i], map);
product *= map;
i++;
if (i==len)
break;
}
if (found)
rtn += product;
else if (ii++;
}

return rtn;
}
str='Google Guy'
o=15
o=15
e=5
u=21
VowelProduct=251
str='Yyyyy'
y=25
y=25
VowelProduct=50
str='myopia'
y=25
o=15
i=9
a=1
VowelProduct=384
str='Quietly'
u=21
i=9
e=5
y=25
VowelProduct=970
str='I'm Feeling Yucky!'
i=9
e=5
e=5
i=9
u=21
y=25
VowelProduct=89
avatar
p*2
10
我也做了一下
sum=(str)->
b=false #not consonant
encode=(c)->
isletter=(c)->
code=c.charCodeAt 0
code>='a'.charCodeAt 0 and code<='z'.charCodeAt 0
table={'a':1, 'e':5, 'i':9, 'o':15, 'u':21, 'y':25}
c=c.toLowerCase()
code=0
if not isletter(c)
b=false
else
if c is 'y'
code=table[c] if b
else
code= if table[c]? then table[c] else 0
b=code==0
code
res=0
prod=0
for c in str
code=encode(c)
if code is 0
res+=prod
prod=0
else
prod=if prod==0 then code else prod*code
res+prod
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。