Ruby的新手,我正在尝试接受方法中的多个splat参数.我想我理解为什么它给我编译错误,但我不知道如何解决它.任何有关如何在参数中使用多个splats的帮助都会有所帮助.提前感谢您的任何指导.
def find_max_expenses(salary, save_prcnt, *pre_ret_g_rates, *post_ret_g_rates, epsilon)
years = pre_ret_g_rates.count
savings = nest_egg_variable(salary, save_prcnt, pre_ret_g_rates)
savings = savings[-1]
low = 0
high = savings
expenses = (low + high) / 2
# can use the [-1] at the end is equivalent to the code below
remaining_money = post_retirement(savings, post_ret_g_rates, expenses) #[-1]
remaining_money = remaining_money[-1]
while remaining_money > epsilon # the value we want to stay above
if remaining_money > 0
low = expenses
else
high = expenses
end
expenses = …Run Code Online (Sandbox Code Playgroud) 我有一个ac#函数,我作为一个更大的算法的一部分,我设计但是这个功能很奇怪.它使用相同的参数计算多次运行中的适应度的不同结果.我没有看到问题出在哪里.任何开明的建议都会受到欢迎.
private readonly Dictionary<int,decimal>_cache = new Dictionary<int, decimal>(); // lookup cache
private void CalculateFitness(TimeTable timeTable)
{
const int points = 1;
var exams = timeTable.Exams.ToList();
var combinations = exams.Select(x => exams.Where(y => exams.IndexOf(y) > exams.IndexOf(x))
.Select(z => new List<Exam> { x, z }))
.SelectMany(x => x);
var clash = combinations.Where(touple => touple[0].Period.Id == touple[1].Period.Id && touple[0].Date == touple[1].Date && touple[0].Students.Intersect(touple[1].Students).Any());
var clCount = clash.Sum(touple => touple[0].Students.Intersect(touple[1].Students).Count());
var score = clCount == 0 ? timeTable.Exams.Count : timeTable.Exams.Count - clCount;
if (_cache.ContainsKey(score)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试开发一个具有函数的类,该函数可以将项目的向量作为其参数.
如果我使用int类型的矢量或其他原语,我可以让它工作正常,但我无法使用对象向量.
例如:
在我的头文件中:
int influenceParticles(vector<Particle> particles);
Run Code Online (Sandbox Code Playgroud)
这就是我所追求的,但不会编译(错误陈述是"'Particle' was not declared in this scope").
particle.h文件已包含在此头文件的顶部.
澄清
这是给我错误的.h文件
#ifndef _PARTICLE_ATTRACTOR
#define _PARTICLE_ATTRACTOR
#include "ofMain.h"
#include "particle.h"
class ParticleAttractor {
//private
public:
ParticleAttractor(int posX, int posY); //constructor (void)
int influenceParticles(vector<Particle> particles);
};
#endif
Run Code Online (Sandbox Code Playgroud) 当尝试使用C++从Stroustrup的编程原理和实践编译示例程序时出现了这个问题.我在第12章,他开始使用FLTK.我在图形和GUI支持代码中遇到编译器错误.特别是Graph.h第140和141行:
error: invalid use of template-name 'Vector' without an argument list
error: ISO C++ forbids declaration of 'parameter' with no type
template<class T> class Vector_ref {
vector<T*> v;
vector<T*> owned;
public:
Vector_ref() {}
Vector_ref(T& a) { push_back(a); }
Vector_ref(T& a, T& b);
Vector_ref(T& a, T& b, T& c);
Vector_ref(T* a, T* b = 0, T* c = 0, T* d = 0)
{
if (a) push_back(a);
if (b) push_back(b);
if (c) push_back(c);
if (d) push_back(d);
}
~Vector_ref() { for (int …Run Code Online (Sandbox Code Playgroud) 什么是[函数]参数?它们用于什么?
我最近开始学习Python; 我是编程的新手,我为这个基本问题道歉.
在每个Python教程中,我都会讨论参数.我已经找到了这个问题的答案,并找到了很多答案,但对我来说,这些答案有点难以理解.我可能只是缺少一些概念背景.
所以...当我定义一个函数时,括号中的东西用于什么?例:
def hi( This is the part that i dont get):
print 'hi'
Run Code Online (Sandbox Code Playgroud)
编辑:
与此相关的两个后续问题后来在这里被关闭和合并,因此部分答案的部分脱离背景特征.
后续问题是:[转述]
我在cocos2D中使用一个调用方法并将BOOL作为参数传递的动作.
我收到警告:"传递参数3'actionWithTarget:selector:data:'使用此行生成没有强制转换的整数的指针":
id actionCharacterReaction = [CCCallFuncND actionWithTarget:self selector:@selector(characterReaction : data:) data:flipChar];
Run Code Online (Sandbox Code Playgroud)
我试过了:
id actionCharacterReaction = [CCCallFuncND actionWithTarget:self selector:@selector(characterReaction : data:) data:(BOOL)flipChar];
Run Code Online (Sandbox Code Playgroud)
我的方法看起来像这样:
-(void) characterReaction:(id)sender data:(BOOL)flipChar {
*code stuff inside*
}
Run Code Online (Sandbox Code Playgroud)
它似乎仍然很好.我对这个警告感到恼火.有任何想法吗?
我是Objective C的新手.我试图在我的实现文件中用参数编写一个void函数,但xcode不喜欢这样.我做了一些研究,看来我做不到.
这就是我基本上要做的事情:
-(void)findMilesLeft(float x, float y)
{
float gallons = x;
float miles = y;
}
Run Code Online (Sandbox Code Playgroud)
我会在另一个void函数中调用这个findMilesLeft-我相信我会把它称为:
-(void)changeSUV
{
[self findMilesLeft(20, 100)];
}
Run Code Online (Sandbox Code Playgroud)
显然我已经尝试过这种语法,它对我不起作用...有什么办法可以正确执行吗?
这里的计算显然没有,但我认为这个想法很清楚.
谢谢,约翰
在Ruby中必须有一种更有效的方法.我有一个方法列表,可以在多个站点中搜索相同的内容(标题,价格),但根据每个商店中的代码略有不同.例如:
def store1_get_title
def store1_get_price
def store2_get_title
def store2_get_price
def store3_get_title
def store3_get_price
Run Code Online (Sandbox Code Playgroud)
当调用所有这些函数时,我只想要一个带有"namespace"参数的泛型调用来调用这些方法中的任何一个,而不必输入所有这些,例如:
for get_all_stores().each do |store|
store::get_title
store::get_price
end
Run Code Online (Sandbox Code Playgroud)
...会像我想的那样调用store1_get_title,store1_get_price,store2_get_title,store2_get_price.有这样的事情或更好的方法吗?
希望有道理.感谢您的任何意见!
编辑:这些任务是在rake任务代码中.
我已经搜索了我遇到的问题,但是看到它的不同变化对我没什么帮助(我对Python很新;把它作为大学编程入门的一部分).
问题:我不断得到一个TypeError来讨论参数的数量,但是我(我迄今为止对Python的知识有限)并没有看到什么是错的.
我有以下功能:
def Grid():
borderSet = 20
spaceSize = 50
totalSpaces = 10
boardX = borderSet + (totalSpaces * spaceSize) + (10 * borderSet)
boardY = borderSet + (totalSpaces * spaceSize) + borderSet
board = GraphWin("Hunt the Wumpus", boardX, boardY)
for r in range(totalSpaces):
for c in range(totalSpaces):
gridTile = Rectangle(Point(borderSet+ r*spaceSize, borderSet+c*spaceSize), Point(borderSet+(r+1)*spaceSize, borderSet+(c+1)*spaceSize))
gridTile.setWidth(2)
gridTile.draw(board)
gridTileText = Text(Point(((gridTile.getP1().getX() + gridTile.getP2().getX()) / 2), ((gridTile.getP1().getY() + gridTile.getP2().getY()) / 2)), False)
gridTileText.draw(board)
ctr = DrawCharacter(borderSet, spaceSize)
ctr.draw(board)
a, w, p, …Run Code Online (Sandbox Code Playgroud) 我的计划如下:
use strict;
use warnings;
use Getopt::Long;
my @letters;
my @words;
GetOptions(
"letters=s{2}" => \@letters,
"words=s{,}" => \@words
);
print "Letters: " . join(", ", @letters) . "\n";
print "Words: " . join(", ", @words) . "\n";
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序时,我得到如下输出:
perl getopts.pl --letters a --words he she it
Letters: a, --words
Words:
Run Code Online (Sandbox Code Playgroud)
--words被读作为--letters参数本身的一部分.我期望GetOptions在这种情况下抛出错误消息.如何完成这项工作.