尝试迭代和数组以及任何10或更高的数字,将这些数字拆分并将它们添加到一起,例如:10 > "1" "0" > 1
。
我能够遍历数组并实现它。但是,它会返回nil
而不是digits < 9
。
def over_ten_sum
#splits the numbers over 10 into seperate digit and sums them
square_odd.map do |num|
if num > 9
num.to_s.chars.each_slice(2).map { |num_1, num_2| num_1.to_i + num_2.to_i }
end
end
end
值为[6, 4, 10, 2, 14, 7, 8, 4, 6, 7, 18, 4]
时返回:
=> [nil, nil, [1], nil, [5], nil, nil, nil, nil, nil, [9], nil]
我正在尝试输出
[6, 4, 1, 2, 5, 7, 8, 6, 7, 9, 4]
这里没有看到断开连接。提前感谢您的任何见解。
答案 0 :(得分:5)
假设你要写
[1, 2, 3].map { |n| }
#=> [nil, nil, nil]
如果nil
未在块中分配值map
,nil
会返回n
n
,则会返回[1, 2, 3].map { |n| 2*n if n > 1 }
#=> [nil, 4, 6]
个数组。类似地,
nil
这与OP的代码问题非常相似。如果在返回的数组中不想要[1, 2, 3].map { |n| n > 1 ? 2*n : n }
#=> [1, 4, 6]
,则只需将数组的每个元素映射为非零值:
num.to_s.chars.each_slice(2).map { |num_1, num_2| num_1.to_i + num_2.to_i }
现在让我们来看看
行num = 34
如果[7]
,则返回7
,除了num = 134
在数组中的事实之外,它是正确的。另一方面,如果[4, 4]
表达式返回[1+3, 4]
(即num[0].to_i + num[1].to_i
),我不希望这是想要的。但是,如果数字总是有两位数,则上述表达式与:
def over_nine_sum(arr)
arr.map { |n| n > 9 ? n.to_s.each_char.reduce(0) { |t,s| t + s.to_i } : n }
end
over_nine_sum [12, 5, 71, 3]
#=> [3, 5, 8, 3]
这简单得多。 1 为了使它更通用,你需要编写类似下面的 2 :
inject
请参阅Enumerable#reduce(又名0-9
)。
@JörgWMittag注意到(见注释)单位数字(def sum_digits(arr)
arr.map { |n| n.to_s.each_char.reduce(0) { |t,s| t + s.to_i } }
end
sum_digits [12, 5, 71, 3]
#=> [3, 5, 8, 3]
)的数字总和与数字本身相同,因此不需要以不同方式处理这些数字。因此我们可以写
def sum_digits(arr)
arr.map { |n| n.digits.sum }
end
正如@ steenslag在评论中建议的那样,这可以简化为
sum_digits
使用方法Integer#digits和Array#sum(两者都是Ruby v2.4中的新方法)。
在n = 34
时考虑上述n.to_s.each_char.reduce(0) { |t,s| t + s.to_i }
#=> 34.to_s.each_char.reduce(0) { |t,s| t + s.to_i }
#=> "34".each_char.reduce(0) { |t,s| t + s.to_i }
的第一个版本的步骤:
reduce
现在t
将块变量"34"
(返回的“备忘录”)初始化为零,并将s
的第一个数字传递给块并将其分配给块变量t = 0
s = "3"
:
t + s.to_i
#=> 0 + "3".to_i
#=> 3
块计算是:
t
这是s = "4"
t + s.to_i
#=> 3 + "4".to_i
#=> 3 + 4
#=> 7
的更新值。接着,
square_odd
1。另一个问题是,如果n.to_s.each_char.reduce(0)...
是局部变量,Ruby在评估它时会引发“未定义的变量或方法”异常。
2。 n.to_s.chars.reduce(0)...
优于chars
,因为each_char
返回临时数组,而{
-KiTWPuI_TYt0b_Qi03Y: {
amount: 0,
category: "Baby",
content: "I love cricket",
date: "2017-03-01",
title: "Cricket"
},
-Kid7fghtlxkyrOChQPk: {
amount: "111",
category: "Book",
content: "updated content",
date: "2017-04-01",
title: "Cycling"
},
d9e7545c-90a3-4a57-97ab-ea3bf9171023: {
amount: "12",
category: "Baby",
content: "COnten1",
date: "2017-01-01",
title: "Title2"
}
}
返回枚举数。
答案 1 :(得分:3)
删除def over_ten_sum
#splits the numbers over 10 into seperate digit and sums them
square_odd.map do |num|
num.to_s.chars.each_slice(2).map { |num_1, num_2| num_1.to_i + num_2.to_i }
end.flatten
end
#=> [6, 4, 1, 2, 5, 7, 8, 4, 6, 7, 9, 4]
:
if num > 9
出了什么问题? nil
遗漏了所有其他号码,并且没有返回任何内容,所以每次都有def over_ten_sum
#splits the numbers over 10 into seperate digit and sums them
square_odd.map do |num|
if num > 9
num.to_s.chars.each_slice(2).map { |num_1, num_2| num_1.to_i + num_2.to_i }
else
num
end
end.flatten
end
#=> [6, 4, 1, 2, 5, 7, 8, 4, 6, 7, 9, 4]
。为了更清楚,请检查以下代码:
else
正如您所看到的,结果是相同的,因为num
发送class ShirtOrder
{
friend ostream& operator<<(ostream &os, const ShirtOrder &rhsObj);
private:
// the data members below are required (you may change identifiers)
// there is no need for additional data members
int orderYear, orderMonth, orderDay;
char printMethod; // 's', 'i', or 'e'
string message;
int mediums; // number of medium shirts ordered
int larges; // number of large shirts ordered
int xls; // number of XL shirts ordered
string shirtColor;
string inkColor;
string orderID;
string region;
string nameAndEmail;
ShirtOrder* next; // new for P5
public:
ShirtOrder(); // default constructor
ShirtOrder(int orderYear, int orderMonth, int orderDay,
char printMethod, string message, int mediums, int larges, int xls,
string shirtColor, string inkColor, string orderID,
string region, string nameAndEmail,
ShirtOrder *soPtr = NULL);
ShirtOrder(const ShirtOrder &otherObj);
~ShirtOrder(); // destructor // New for P5 (may not be in-line)
ShirtOrder* getNext() const {return next;} // New for P5 (may be in-line)
void setNext(ShirtOrder* soPtr) {next = soPtr;} // New for P5 (may be in-line)
double getBlankCost() const;
double getPrintingCost() const;
double getTotalCost() const;
int getLetterCount() const;
char getPrintMethod() const {return printMethod;}
int getOrderYear() const {return orderYear;}
int getOrderMonth() const {return orderMonth;}
int getOrderDay() const {return orderDay;}
int getMediums() const {return mediums;}
int getLarges() const {return larges;}
int getXls() const{return xls;}
string getShirtColor() const {return shirtColor;}
string getInkColor() const {return inkColor;}
string getOrderID() const {return orderID;}
string getRegion() const {return region;}
string getNameAndEmail() const {return nameAndEmail;}
string getMessage() const {return message;}
void setOrderYear (int orderYear) {this->orderYear = orderYear;}
void setOrderMonth (int orderMonth) {this->orderMonth = orderMonth;}
void setOrderDay (int orderDay) {this->orderDay = orderDay;}
void setPrintMethod (char printMethod) {this->printMethod = printMethod;}
void setMessage (string message) {this->message = message; }
void setMediums (int mediums) {this->mediums = mediums;}
void setLarges (int larges) {this->larges = larges;}
void setXls (int xls) {this->xls = xls;}
void setShirtColor (string shirtColor) {this->shirtColor = shirtColor;}
void setInkColor (string inkColor) {this->inkColor = inkColor;}
void setOrderID (string orderID) {this->orderID = orderID;}
void setRegion (string region) {this->region = region;}
void setNameAndEmail (string nameAndEmail) {this->nameAndEmail = nameAndEmail;}
};
时它不会大于9。