我正在学习 Coursera 上的“算法简介”课程,我已经看到了处理 Big-Theta、Big-Omega 和 Big-O 符号的视频。视频结尾测验提出了以下问题:
Q: Which of the following functions is O(N^3)?
a) 11N + 15lgN + 100
b) (N^2)/3
c) 25,000*(N^3)
d) All of the above
Run Code Online (Sandbox Code Playgroud)
我回答“c”并被告知我的答案不正确,而正确答案实际上是“d”。课程提供的解释没有多大帮助:
Recall that big-Oh notation provides only an upper bound on the growth
rate of a function as N gets large. In this course, we primarily use
tilde notation because it more accurately describes the function—it
provides both an upper and lower bound on the function as well as the …Run Code Online (Sandbox Code Playgroud) 我有一个继承自ApplicationMailer的Mailer类,后者继承自ActionMailer :: Base.Ruby版本是ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15].
Mailer类如下所示:
class PurchaseOrderStatusMailer < ApplicationMailer
CONTACTS = {
JC: ['me@example.com'],
RM: ['me@example.com']
}
def daily_report_email(facility)
@facility = facility
ingredient_items = LineItem.ingredient.by_facility(facility)
@delivered_count = ingredient_items.by_date([7.days.ago, 7.days.from_now]).delivered.count
@partial_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).partial.count
@late_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).late.count
@expected_count = ingredient_items.by_date([Date.today, 7.days.from_now]).expected.count
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}")
end
end
Run Code Online (Sandbox Code Playgroud)
ApplicationMailer如下所示:
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'notify@example.com'
def facility_email(facility)
emails = Rails.application.config_for(:emails)
(emails[facility] + emails["DEFAULT"]).flatten
end
end
Run Code Online (Sandbox Code Playgroud)
该视图位于 …
我正在阅读 RBENV 版本管理器的源代码,并且在命令的规范文件中遇到了此测试exec:
@test "supports ruby -S <cmd>" {
export RBENV_VERSION="2.0"
# emulate `ruby -S' behavior
create_executable "ruby" <<SH
#!$BASH
if [[ \$1 == "-S"* ]]; then
found="\$(PATH="\${RUBYPATH:-\$PATH}" which \$2)"
# assert that the found executable has ruby for shebang
if head -n1 "\$found" | grep ruby >/dev/null; then
\$BASH "\$found"
else
echo "ruby: no Ruby script found in input (LoadError)" >&2
exit 1
fi
else
echo 'ruby 2.0 (rbenv test)'
fi
SH
create_executable "rake" <<SH
#!/usr/bin/env …Run Code Online (Sandbox Code Playgroud) 例如,这两个测试:
it "fails to create given a bad player list" do
Team.new("Random name", bad_players).should raise_error
end
Run Code Online (Sandbox Code Playgroud)
和
it "fails to create given a bad player list" do
expect {Team.new("Random name", bad_players)}.to raise_error
end
Run Code Online (Sandbox Code Playgroud)
它们返回不同的输出(第一个失败,而第二个失败).根据Team模型的内容,我希望传递或失败.
完整的RSpec代码是:
require_relative 'team'
describe "Team" do
it "has a name" do
Team.new("Random name").should respond_to(:name)
end
it "has a list of players" do
Team.new("Random name").players.should be_kind_of Array
end
it "is favored if it has a celebrity on it"
it "complains if there is a bad word in …Run Code Online (Sandbox Code Playgroud) 在我的Rails应用程序中,我在页面顶部有以下链接:
<ul>
<li><%= link_to 'Group Chairperson', '#group_chair' %></li>
<li><%= link_to 'Group Treasurer', '#group_treasurer' %></li>
<li><%= link_to 'Group Secretary', '#group_secretary' %></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
在页面的下方,我有以下表格行选择器:
<tr id="group_chair">
Run Code Online (Sandbox Code Playgroud)
单击"组主席"链接会使浏览器按预期向下滚动到正确的表行.现在我想制作一个测试此功能的规范.我尝试了以下方法:
require 'spec_helper'
describe "Group officer duties page" do
before { visit group_officer_duties_path }
it "should scroll down to the right section" do
click_link "Group Chairperson"
expect(current_url).to eq "http://www.example.com/group_officer_duties#group_chair"
end
end
Run Code Online (Sandbox Code Playgroud)
我希望这个测试能够通过,因为这是我点击右侧链接后浏览器的URL栏所显示的内容.但是,我实际上得到了这个:
Failures:
1) Group officer duties page should scroll down to the right section
Failure/Error: expect(current_url).to eq "http://www.example.com/group_officer_duties#group_chair"
expected: "http://www.example.com/group_officer_duties#group_chair"
got: "http://www.example.com/group_officer_duties"
(compared using ==) …Run Code Online (Sandbox Code Playgroud) 我正在尝试以独立模式安装HBase v1.1.2(与Hadoop v2.7.2),并且正在尽可能地关注Apache HBase文档,但是配置似乎有些棘手。不知道这是否与我使用自制软件安装HBase相对,而不是通过其镜像站点之一直接下载有关。我的目标是达到可以执行创建表,列出现有表等的目的。
我已经安装了Java 1.7.0,并且已经运行“ brew install hbase”来安装hbase。在Homebrew的hbase目录中,我打开了/libexec/conf/hbase-env.sh,在第27行看到以下内容:
export JAVA_HOME="$(/usr/libexec/java_home)"
Run Code Online (Sandbox Code Playgroud)
当我/usr/libexec/java_home在命令行中执行时,它输出/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home。因此,似乎设置了有效的$ JAVA_HOME env变量。
在/libexec/conf/hbase-site.xml配置文件中,我设置了以下键/值对:
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///home/richiethomas/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/richiethomas/zookeeper</value>
</property>
</configuration>
Run Code Online (Sandbox Code Playgroud)
换句话说,我已经复制了HBase文档中显示的配置,只是将“ testuser”替换为“ richiethomas”,因为这是我在命令行中使用的用户名。
然后在Homebrew hbase目录中,运行`./bin/start-hbase.sh'。我看到以下内容:
starting master, logging to /usr/local/Cellar/hbase/1.1.2/libexec/bin/../logs/hbase-richiethomas-master-MacBook-Pro-5.local.out
然后,我跑步hbase shell,看到以下内容:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/local/Cellar/hbase/1.1.2/libexec/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/local/Cellar/hadoop/2.7.2/libexec/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2017-01-03 17:27:52,056 …Run Code Online (Sandbox Code Playgroud) 我正在阅读 Ruby 的“man”页面,我看到以下“-S”标志的描述:
-S Makes Ruby use the PATH environment variable to search for script, unless its name begins with a slash. This is used to emulate #! on
machines that don't support it, in the following manner:
#! /usr/local/bin/ruby
# This line makes the next one a comment in Ruby \
exec /usr/local/bin/ruby -S $0 $*
On some systems $0 does not always contain the full pathname, so you need the -S switch to tell Ruby to search for …Run Code Online (Sandbox Code Playgroud) 我目前正在关注Big Nerd Ranch的Objective-C指南,其中一个例子如下:
int main(int argc, const char * argv[])
{
int i = 17;
printf("i stores its value at %p\n", &i); return 0;
}
// output => i stores its value at 0xbffff738
int main(int argc, const char * argv[])
{
int i = 17;
printf("i stores its value at %p\n", &i);
printf("this function starts at %p\n", main); return 0;
}
// output => i stores its value at 0xbffff738
// this function starts at 0x100000ed0
Run Code Online (Sandbox Code Playgroud)
我尝试在main前面使用"&"符号,我得到相同的结果 - 0x100000ed0.但是当我从"i"前面删除&符时,我只看到0x11而不是0xbffff738. …
我正在处理的应用程序有一个信用响应对象,其中包含布尔"已批准"字段.我试图在Objective C中注销这个值,但由于没有布尔值的格式说明符,我不得不求助于以下内容:
NSLog("%s", [response approved] ? @"TRUE" : @"FALSE");
Run Code Online (Sandbox Code Playgroud)
虽然不可能,但我更愿意做以下事情:
NSLog("%b", [response approved]);
Run Code Online (Sandbox Code Playgroud)
...其中"%b"是布尔值的格式说明符.
在做了一些研究之后,似乎一致的共识是C和Objective-C都没有相当于"%b"的说明符,并且大多数开发者最终都会自己滚动(类似上面的选项#1).
显然Dennis Ritchie&Co.知道他们写C时他们在做什么,我怀疑这个丢失的格式说明符是一个意外.我很想知道这个决定背后的理由,所以我可以向我的团队解释(他们也很好奇).
编辑: 下面的一些答案表明它可能是一个本地化问题,即"TRUE"和"FALSE"太具有英语特色.但这不是所有语言都面临的两难选择吗?即不只是C和Objective-C?除其他外,Java和Ruby能够实现"True"和"False"布尔值.不知道为什么这些langs的作者没有同样的选择.
此外,如果本地化是问题,我希望它也会影响语言的其他方面.例如,使用受保护的关键字.C使用英语关键词如"include","define","return","void"等,而非英语使用者解析这些关键词比"true"或"false"这样的关键词更难.
我的同事最近使用Homebrew在他的Macbook上安装了Node Version Manager,并运行了安装脚本末尾建议的两个命令:
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Run Code Online (Sandbox Code Playgroud)
在安装发生的终端窗口中一切正常,但是如果他打开一个新的终端窗口甚至是一个新的选项卡,他必须重新安装NVM.
我的命令行经验水平相对较低(我不知道我不知道什么),所以我不知道从哪里开始诊断问题.欢迎提出建议.
我正在尝试做一个简单的数学问题,但我不断收到此错误消息.怎么了?我正在使用cloud9 ide.
/home/ubuntu/workspace/Sphere.cpp:在函数'int main()'中:/ home /ubuntu/workspace/scc.c:20:63:错误:类型'int'和'const char [15]的无效操作数]'到二元'运算符<<'cout <<"圆的面积是:"<< 3.14*米^ 2 <<"米平方"<< endl;
这是整个代码:
#include <iostream>
using namespace std;
int main() {
// Declare the radius
int meters;
cout << "Please enter the radius in meters: ";
cin >> meters;
// Calculate Diameter
cout << "The diameter of the circle is: " << meters*2 << "m" << endl;
//Calculate Area
double PI;
PI = 3.14;
cout << "The area of the circle is: " << 3.14*meters^2 << "meters squared" << …Run Code Online (Sandbox Code Playgroud) 我有以下 C 函数:
void proc(long a1, long *a1p,
int a2, int *a2p,
short a3, short *a3p,
char a4, char *a4p)
{
*a1p += a1;
*a2p += a2;
*a3p += a3;
*a4p += a4;
}
Run Code Online (Sandbox Code Playgroud)
使用Godbolt,我已将其转换为 x86_64 程序集(为简单起见,我使用该-Og标志来最小化优化)。它产生以下组件:
proc:
movq 16(%rsp), %rax
addq %rdi, (%rsi)
addl %edx, (%rcx)
addw %r8w, (%r9)
movl 8(%rsp), %edx
addb %dl, (%rax)
ret
Run Code Online (Sandbox Code Playgroud)
我对汇编的第一行感到困惑:movq 16(%rsp), %rax. 我知道%rax寄存器用于存储返回值。但是该proc过程没有返回值。所以我很好奇为什么在此处使用该寄存器,而不是 %r9其他一些不用于返回值的寄存器。
相对于其他指令,我也对这条指令的位置感到困惑。它首先出现,远在%rax任何需要其目标寄存器之前(实际上,直到最后一步才需要该寄存器)。它也出现在 之前addq %rdi, (%rsi) …
我已经读过,用Ruby编程时irb的目的是帮助实时测试代码.我之前学过HTML和CSS,那时我只需在浏览器中打开页面并保存/刷新页面就可以测试我的代码.irb是否与Ruby执行类似的功能?如果是这样,我是否认为在irb中测试Ruby代码(而不是浏览器)的好处是,irb可以更容易地将Ruby中的错误与Javascript/HTML /等中的错误分开?看来Ruby程序迟早会在浏览器中运行(理论上),在我看来,程序员似乎希望测试条件尽可能接近真实用例环境.