[Algorithm] Huawei Pen test--Pinyin and English conversion

Source: Internet
Author: User

Title Description:

Write pinyin and English conversion function, if the input number is in English, turn into pinyin, if it is pinyin, turn into English, such as input onezerodoublethree, then output Yilingsansan.

Pinyin, English see the following table:

Yi Er San Si Wu Liu Qi Ba Jiu Ling

One, three four Five Six Seven Eight Nine Zero

Note: Each word is capitalized, with two consecutive identical digits, and the input can be preceded by a double, and the output cannot be added double.

Input Description:

A string with the first letter capitalized, the character input range is 0,1,2,3,4,5,6,7,8,9 English word or Chinese pinyin.

Output Description:

If the input is an English word, output pinyin. If the input is pinyin, output the English word. Cannot mix up, input illegal, output error.

Example:

Input:

Onetwodoublethree

Output:

Yiersansan

Idea: First, the word breaker, according to the uppercase letter string word. Then, match and replace, focusing on double. Finally, various illegal situations are dealt with.

Code:

 Packagecom;Importjava.util.ArrayList;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Scanner;ImportJava.util.Set; Public classTest {StaticString[] 中文版 = {"Zero", "one", "one", "three", "four", "Five", "Six", "Seven", "Eight", "Nine"}; Staticstring[] Pinyin = {"Ling", "Yi", "Er", "San", "Si", "Wu", "Liu", "Qi", "Ba", "Jiu"}; StaticList<string> Elist =NewArraylist<string>(); Staticlist<string> plist =NewArraylist<string>(); Static {         for(inti = 0; i < english.length; i++) {Elist.add (english[i]);        Plist.add (Pinyin[i]); }    }     Public Static voidMain (string[] args) {intflag = 1;//represents the normal condition, the output error for 0 delegatesScanner Scanner =NewScanner (system.in); String s=Scanner.nextline (); /*** If the input is all lowercase letters directly error*/        if(S.matches ("[A-z]{1,}") ) {flag= 0; } String S1= S.replaceall ("[A-Z]", "$");//Regular Replacement Note "$" preceded by a spacestring[] split = S1.split (""); String[] Split2=NewString[split.length-1]; System.arraycopy (Split,1, split2, 0, split2.length);//because split splits out an empty character (first), remove it now.                Try {             for(inti = 0; i < split2.length; i++) {                if(Elist.contains (Split2[i])) {intindex =Elist.indexof (Split2[i]); Split2[i]=Split2[i].replace (Split2[i], Plist.get (index)); }Else if(Plist.contains (Split2[i])) {intindex =Plist.indexof (Split2[i]); Split2[i]=Split2[i].replace (Split2[i], Elist.get (index)); }Else if(Split2[i].equals ("Double") ) {Split2[i]= Split2[i + 1]; if(Elist.contains (Split2[i])) {intindex =Elist.indexof (Split2[i]); Split2[i]=Split2[i].replace (Split2[i], Plist.get (index)); }Else if(Plist.contains (Split2[i])) {intindex =Plist.indexof (Split2[i]); Split2[i]=Split2[i].replace (Split2[i], Elist.get (index)); }Else{                        /*** The situation of doubledouble appears*/Flag= 0; }                }Else{flag= 0; }            }        } Catch(Exception e) {/*** Exception is thrown because there is a case where only double is present*/Flag= 0; }                /*** This is determined by the number of sets, because the last can only appear in either English or pinyin, if the mixture appears in the wrong*/Set<Integer> set =NewHashset<integer>();  for(inti = 0; i < split2.length; i++) {Set.add (judge (Split2[i)); }                if(Set.size () > 1) {flag= 0; } StringBuilder SB=NewStringBuilder (""); if(Flag = = 1){             for(intj = 0; J < Split2.length; J + +) {sb.append (split2[j]); }        }Else{sb.append ("ERROR");    } System.out.println (Sb.tostring ()); }        /*** Determine if a string is an English number or a phonetic number or nothing@paramString *@returnThe English number returns 0, the phonetic number returns 1, otherwise returns-1*/     Public Static intjudge (String string) {intret =-1; if(Elist.contains (String)) {ret= 0; }Else if(Elist.contains (String)) {ret= 1; }        returnret; }}

$1,$2 in regular expression ... is the contents of the parentheses represented in the first parenthesis, and the 2nd parenthesis is the/gai ([\w]+?). The Over ([\d]+)/Match gainover123 $1= brackets in the n $2= 2nd brackets in the 123,$0 represent all.

[Algorithm] Huawei Pen test--Pinyin and English conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.