我试图在 1 到 1000000 之间的字符串数组中查找重复值。
但是,使用我拥有的代码,我得到的输出是所有条目都加倍了。
例如,如果我有[1,2,3,4,3,4],它会给我输出 3 4 3 4 而不是 3 4。
这是我的代码:
array = [gets]
if array.uniq.length == array.length
puts "array does not contain duplicates"
else
puts "array does contain duplicates"
print array.select{ |x| array.count(x) > 1}
end
Run Code Online (Sandbox Code Playgroud)
另外,每次测试代码时,我都必须将数组定义为array = [1,2,3,4,5,3,5]. 可以puts工作,但当我使用数组 [gets] 时它不会打印。
有人可以帮助我如何解决这两个问题吗?
我目前正在开发HTML,并且正在使用Bootstrap使之更加适合移动设备。但是,bootstrap字体很烂,我希望覆盖它,并用一个更好看的字体替换它。我正在发布HTML文件的标题。看起来像这样:
<head>
<title> Connotate Engineering </title>
<meta name=viewport content='width=700'>
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="ConnotateEngineering.css">
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"
rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>
</head>
Run Code Online (Sandbox Code Playgroud)
这就是我的CSS的样子:
body{
font-family: 'Arial';
font-size: 12px;
}
#images{
font-family: 'Arial';
text-align:left;
font-style: regular;
letter-spacing: 0;
line-height: 1.5em;
text-decoration: none;
word-spacing: 0;
}
#images a{
margin:0px 20px;
display:inline-block;
height: 100%;
text-decoration:none;
color:black;
font-family: 'Arial', serif;
}
.caption {
width: 200px;
text-align: center;
}
@media screen and (max-width:480px) {
#sidebar { display: none;}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Ruby 中调用字符串中的第一个重复字符。我已经使用gets定义了一个输入字符串。
如何调用字符串中的第一个重复字符?
到目前为止,这是我的代码。
string = "#{gets}"
print string
Run Code Online (Sandbox Code Playgroud)
我如何从这个字符串中调用一个字符?
编辑1:
这是我现在拥有的代码,我的输出出现在我面前 没有重复 26 次。我认为我的 if 语句写错了。
string "abcade"
puts string
for i in ('a'..'z')
if string =~ /(.)\1/
puts string.chars.group_by{|c| c}.find{|el| el[1].size >1}[0]
else
puts "no duplicates"
end
end
Run Code Online (Sandbox Code Playgroud)
我的第二个 puts 语句有效,但使用 for 和 if 循环,无论字符串是什么,它都不会返回 26 次重复项。