我目前正在编写猪拉丁语代码,它要求忽略所有非字母,即保留在单词的同一位置。例如,汤姆将是om'sTay。所以我使用以下方法删除了所有非字母字符:
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++){
try {
JSONObject jsonObject = response.getJSONObject(i);
Riders riders = new Riders();
riders.setName(jsonObject.getString("Full_Name"));
riders.setClub(jsonObject.getString("Club"));
riders.setFinishPosition(jsonObject.getString("Finish_Position"));
ridersList.add(riders);
} catch (JSONException e){
e.printStackTrace();
progressDialog.dismiss();
}
// You need to call notifyDataSetChanged to see the effect in your RecyclerView.
adapter.notifyDataSetChanged();
}
}
猪拉丁代码,我想把非字母带回来。
不确定如何将非字母带回更改单词的同一位置。
答案 0 :(得分:0)
In[2]: def pig_latinize(word):
...: cluster_dex = 0
...: vowels = set('AEIOUaeiou')
...: for i, char in enumerate(word, start=1):
...: if not char.isalpha() or char in vowels:
...: break
...: cluster_dex = i
...: if cluster_dex == 0:
...: suffix = 'hay'
...: elif cluster_dex == len(word):
...: cluster_dex = 1
...: suffix = 'way'
...: else:
...: suffix = 'ay'
...: return '{}{}{}'.format(word[cluster_dex:], word[:cluster_dex], suffix)
...:
...:
...: def pig_latin(words):
...: return ' '.join(pig_latinize(word) for word in words.split())
...:
In[3]: pig_latin("Tom's") == "om'sTay"
Out[3]: True
In[4]: pig_latin('the ap!ple is green') == 'ethay ap!plehay ishay eengray'
Out[4]: True
In[5]: pig_latin("Kate's care") == "ate'sKay arecay"
Out[5]: True
In[6]: pig_latin('myth') == 'ythmway'
Out[6]: True
In[7]: pig_latin('cry') == 'rycway'
Out[7]: True
编辑:
这显示了将这些功能从您给出的描述中转换为类的可能方法
>>> class PigLatin(object):
... VOWELS = set('AEIOUaeiou')
...
... def __init__(self, to_convert):
... self.convert_words(to_convert)
...
... def convert_words(self, to_convert):
... if not isinstance(to_convert, list):
... # if to_convert is a string, split into separate words
... to_convert = to_convert.split()
... self.pig_latin = ' '.join(
... self.p_latin_converter(word) for word in to_convert)
...
... def p_latin_converter(self, word):
... cluster_dex = 0
... for i, char in enumerate(word, start=1):
... if not char.isalpha() or char in self.VOWELS:
... break
... cluster_dex = i
... if cluster_dex == 0:
... suffix = 'hay'
... elif cluster_dex == len(word):
... cluster_dex = 1
... suffix = 'way'
... else:
... suffix = 'ay'
... return '{}{}{}'.format(word[cluster_dex:], word[:cluster_dex], suffix)
...
# *** An object of this method initializes the string it wants to
# convert when it is created. ***
>>> p = PigLatin("Tom's") # initialize with a sting
# pig_latin attribute is then set with the converted text
>>> p.pig_latin == "om'sTay"
True
# *** Further, the class should have a method that allows the string to be
# modified to perform another conversion. ***
>>> p.convert_words('the ap!ple is green') # modify the pig_latin attribute
>>> p.pig_latin == 'ethay ap!plehay ishay eengray'
True
# *** build a pigLatin_class that works with strings and lists and has a
# method to convert a string into pig latin. ***
>>> p.convert_words(['myth', 'cry']) # use a list of strings
>>> p.pig_latin == 'ythmway rycway'
True
# *** The set of VOWELS can be defined as a static attribute of the class. ***
>>> p.VOWELS
{'E', 'e', 'A', 'O', 'a', 'i', 'u', 'I', 'U', 'o'}
# *** The class pigLatin should be based on the basic object datatype. ***
>>> isinstance(p, object) # inherits from object "class PigLatin(object):"
True
# *** method pLatin_converter can be used. ***
>>> p.pig_latin # current pig_latin attribute
'ythmway rycway'
# returns a word without modifying the pig_latin attribute
>>> p.p_latin_converter("Kate's care")
"ate's careKay"
# show that the pig_latin attribute wasn't modified after the above call
>>> p.pig_latin
'ythmway rycway'