你好,我有一个问题,我正在努力解决。
定义一个名为clean_slate的方法,该方法将采用表示记录的String和另一个表示名称作为参数的String。该方法返回一个表示原始记录的字符串,但该名称的每个实例都被替换为两倍于名称包含字符的X' s。如果传递了全名,它还将替换仅使用第一个或最后一个名称与全名包含字符的X的实例。您的方法不应区分大小写。假设输入始终是String。如果找不到姓名,则返回原始记录。
到目前为止,我有:
def clean_slate(record, name)
name = name.downcase
split_name = name.downcase.split(" ") #array
change_name = split_name.map do |letter|
letter = "X" * 2 * name.length
end
record_words = record.downcase.split(" ") #array
new_record = record_words.map do |word|
if word.include?(split_name[0] || split_name[1])
word = change_name
else
word
end
end
new_record.join(" ")
end
clean_slate("Megan Fox is a talented actress. megan knows sulfoxide is a chemical compound containing a sulfinyl (SO) functional group attached to two carbon atoms. It is a polar functional group. Ms. fox has many talents.", "Megan Fox")
我将返回record
,但我没有在record
编辑中获得姓氏。有谁知道如何解决这个问题?
"XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX fox is a talented actress. XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX knows sulfoxide is a chemical compound containing a sulfinyl (so) functional group attached to two carbon atoms. it is a polar functional group. ms. fox has many talents."
答案 0 :(得分:0)
在record.split.map
内,您需要检查传递的record.downcase
是否包含split
的第一个或最后一个值,但同时检查word
是否new_record = record.split.map do |word|
if record.downcase.include?(split_name[0] || split_name[1]) &&
word.downcase == split_name[0] || word.downcase == split_name[1]
...
在块中等于这两个中的任何一个,所以它将类似于:
map
此外,如果您使用split_name
而不是X
来分配word = change_name
的数量,那么它会返回一个数组,当您执行['XXXXX']
时,它需要word = change_name.first
数组中的值,如change_name.first
(例如),因此您需要访问内部唯一的一个值:
def clean_slate(record, name)
name = name.downcase
split_name = name.downcase.split(" ")
change_name = split_name.map do |letter|
letter = 'X' * 2 * name.length
end
new_record = record.split.map do |word|
if record.downcase.include?(split_name[0] || split_name[1]) &&
word.downcase == split_name[0] || word.downcase == split_name[1]
change_name.first
else
word
end
end
new_record.join(" ")
end
由于不需要初始化未在块内使用的变量,因此只需split(" ")
。
这样就可以了:
split
但它有一些你可以改进的东西:
letter = 'X' * 2 * name.length
可以缩短为'XX' * name.length
change_name = 'XX' * name.length
可以是change_name.first
和
change_name
无需使用地图,所以
join(" ")
更改为if
record.map
更喜欢单引号而不是双引号
您需要一种方法来简化[0]
内的[-1]
语句,但您仍然不知道该名称是否只有两个单词才能使用def clean_slate(record, name)
record.gsub(/\b#{name.split.join('|')}\b/i, 'XX' * name.size)
end
和{{ 1}}。
您可以查看String#gsub
。
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={1,2,3,4,5,6};
int num,i;
for(i=6;i>0;i--)
{
a[i]=a[i-1];
}
printf("enter any no, ");
scanf("%d",&num); // input 12
a[0]=num;
for(i=0;i<=6;i++)
{
printf("\n\n%d",a[i]);
}
getch();
}
答案 1 :(得分:0)
这是实现这一目标的更简洁方法。只需将record
中的每个单词与name
进行比较,然后替换X
。
def clean_slate record, name
names = name.downcase.split
new_record = []
record.split.each do |word|
new_record << word
if names.include?(word.downcase)
new_record[-1] = "X" * 2 * name.length
end
end
new_record.join(' ')
end
结果:
XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX is a talented actress. XXXXXXXXXXXXXXXXXX knows sulfoxide is a chemical compound containing a sulfinyl (SO) functional group attached to two carbon atoms. It is a polar functional group. Ms. XXXXXXXXXXXXXXXXXX has many talents.
答案 2 :(得分:0)
如果我理解你的任务,我认为你可以做类似的解决方案:
def clean_slate(record, name)
name = name.split.map(&:downcase)
record = record.split
record.map do |word|
if name.include? word.downcase
word = 'X' * 2 * name.join.length
else
word
end
end.join(' ')
end
#> clean_slate("Megan Fox is a talented actress. megan knows sulfoxide is a chemical compound containing a sulfinyl (SO) functional group attached to two carbon atoms. It is a polar functional group. Ms. fox has many talents.", "Megan Fox")
#=> "XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX is a talented actress. XXXXXXXXXXXXXXXX knows sulfoxide is a chemical compound containing a sulfinyl (SO) functional group attached to two carbon atoms. It is a polar functional group. Ms. XXXXXXXXXXXXXXXX has many talents."