- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Use reduce to compute the area of the largest rectangle in an array of rectangles. Each rectangle has width and height properties, and if r is a rectangle, the area is
r.width * r.height
If there are no rectangles, return 0, so the base case is 0. In the online instruction examples, reduce() was used to form the sum over a set of items. In this case you will want to take the maximum over a set of items (using JavaScript’s Math.max(a, b) function.)
You must use a function named area to compute and return the area of a rectangle. It takes one parameter which is a rectangle object.
You must also define a function to pass into reduce, analogous to the sum_value function in “The Real Reduce” sub-lessons within the “Knapsack: Representation” lesson. You must name this function biggest.
下面的代码是类(class)练习的解决方案。
To complete this exercise, you should modify the code in the window below. Make the following changes:
- Define area(rect) to compute and return the area of a rectangle object.
- Define biggest to be the third parameter to reduce (already defined).
- Finish the definition of compute using a call to reduce.
<html>
<head>
<title>Using Reduce</title>
<link rel="stylesheet" href="../css/exercise.css"></link>
<script>
function reduce(a, base, f) {
var result = base;
for (var i = 0; i < a.length; i++)
result = f(result, a[i]);
return result;
}
// these are the rectangles to consider
r1 = {width: 9, height: 34}; // area is 306
r2 = {width: 10, height: 31}; // area is 310
r3 = {width: 11, height: 28}; // area is 308
r4 = {width: 12, height: 25}; // area is 300
r5 = {width: 13, height: 24}; // area is 312
r6 = {width: 14, height: 22}; // area is 308
r7 = {width: 15, height: 20}; // 300
r8 = {width: 16, height: 19}; // 304
r9 = {width: 17, height: 18}; // 306
r10 = {width: 18, height: 17}; // 306
r11 = {width: 19, height: 16}; // 304
r12 = {width: 20, height: 15}; // 300
r13 = {width: 22, height: 14}; // 308
r14 = {width: 24, height: 12}; // 288
r15 = {width: 28, height: 11}; // 308
// form an array of all the rectangles
rectangles = [r1, r2, r3, r4, r5, r6, r7, r8,
r9, r10, r11, r12, r13, r14, r15];
// define the function named area here:
function area(r){
return r.width * r.height;
}
// define the function named biggest here:
function biggest(biggestYet, newRect){
biggestYet = Math.max (biggestYet, newRect)
return biggestYet;
}
// complete the definition using a call to reduce:
function compute() {
// return -23; // replace with code to return the area of
// the largest rectangle in rectangles
var biggestYet = 0;
for (x = 0 ; x < rectangles.length ; x++)
{
var rArea = area ( rectangles[x]);
console.log ( "area "+ x+ "," + rArea );
biggestYet = biggest (biggestYet, rArea);
console.log ( "biggest "+ x +","+ biggestYet);
}
return biggestYet;
}
</script>
</head>
<body>
<!-- the output is displayed using HTML -->
<p>The area of the largest rectangle is:
<!-- the ? will be replaced with the answer -->
<div id = "answer">?</div></p>
<br>
<!-- a button runs compute and puts the answer into the HTML -->
<button id = "computeButton"
onclick = "x = compute();
<!-- find the document element named 'answer' -->
where = document.getElementById('answer');
<!-- insert result x as text into the HTML -->
where.innerHTML = x.toString();">Run compute()
to compute the largest rectangle.</button>
</body>
</html>
最佳答案
您应该使用名为 reduce 的方法。您遇到的问题是因为 onclick 处理程序。你需要再调查一下。这是一个可能的解决方案:
function reduce(a, base, f) {
var result = base;
for (var i = 0; i < a.length; i++)
result = f(result, a[i]);
return result;
};
function area(rectangle) {
return rectangle.width * rectangle.height;
};
function biggest(result, rectangle) {
return Math.max(result, area(rectangle));
};
function compute(rectangles) {
var x = reduce(rectangles, 0, biggest);
var where = document.getElementById('answer');
where.innerHTML = x.toString();
};
var r1 = {width: 9, height: 34}; // area is 306
var r2 = {width: 10, height: 31}; // area is 310
var r3 = {width: 11, height: 28}; // area is 308
var r4 = {width: 12, height: 25}; // area is 300
var r5 = {width: 13, height: 24}; // area is 312
var r6 = {width: 14, height: 22}; // area is 308
var r7 = {width: 15, height: 20}; // 300
var r8 = {width: 16, height: 19}; // 304
var r9 = {width: 17, height: 18}; // 306
var r10 = {width: 18, height: 17}; // 306
var r11 = {width: 19, height: 16}; // 304
var r12 = {width: 20, height: 15}; // 300
var r13 = {width: 22, height: 14}; // 308
var r14 = {width: 24, height: 12}; // 288
var r15 = {width: 28, height: 11}; // 308
// form an array of all the rectangles
var rectangles = [r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15];
document.getElementById('computeButton').onclick = function() {
compute(rectangles);
};
<!-- the output is displayed using HTML -->
<p>The area of the largest rectangle is:
<!-- the ? will be replaced with the answer -->
<div id = "answer">?</div></p>
<br>
<!-- a button runs compute and puts the answer into the HTML -->
<button id = "computeButton">Run compute()
to compute the largest rectangle.</button>
或者整个程序作为一个线程:
Math.max.apply(Math, rectangles.map(area));
关于javascript - 背包表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041236/
Byte byte1=10; Short short1=20; Integer integer=30; 在上面的代码中自动装箱成功在这里查看下面的代码,我正在明确地进行 casitng,因为它默认将
这里有几个相关的问题。 根据标题,如果我们将变量类型指定为 long 或 float、double,为什么它是一个要求?编译器不会在编译时评估变量的类型吗? Java 将所有整型文字视为 int -
我最近一直在使用一些 bash 脚本,并且一直在浏览手册页。根据我收集到的信息,$(( )) 是否表示 expr 而 [ ] 是否表示 test? 对于 $(( )): echo $(( 5 + 3
我有 UILabel,其中显示了 int 值,我希望如果值以千为单位,例如 1000,那么标签应该在 2000 年及以后显示 1k 和 2k。如何实现? 最佳答案 这个怎么样? int myNum =
我正在自学 verilog 并尝试编写失败模型。我在指定部分遇到了以下 ck->q 延迟弧的建模,但无法理解它到底是做什么的。 (posege CK => (Q : 1'b1))=(0, 0); 谁能
考虑这样一个句子: John Smith travelled to Washington. 在美好的一天,名称标记者会将“约翰·史密斯”识别为一个人,将“华盛顿”识别为一个地方。然而,如果没有其他证据
有没有办法通过某种元处理器或预处理器告诉 JavaScript 单词 AND 等于 && 而单词 OR 等于 ||和 <> 等同于 !===? 也许将 THEN 等同于 { 结束到 不要! 最佳答案
我正在处理一个非常大的图,它有 5 亿个节点,节点的平均度为 100。所以它是一种稀疏图。我还必须存储每条边的权重。我目前正在使用两个 vector ,如下所示 // V could be 100 m
我想使用 Python 表示一组整数范围,其中可以动态修改该集合并测试其是否包含在内。具体来说,我想将其应用于文件中的地址范围或行号。 我可以定义我关心的地址范围: 200 - 400 450 -
>>> x = -4 >>> print("{} {:b}".format(x, x)) -4 -100 >>> mask = 0xFFFFFFFF >>> print("{} {:b}".forma
虽然代码不多,但简单明了 复制代码 代码如下: preg_match('/^(?!string)/', 'aa') === true 这个用来验证一个字符串是否是非'string'开头的,
我正在尝试创建一些 SQLAlchemy 模型,并且正在努力解决如何将 timedelta 正确应用于特定列的问题。 timedelta(以天为单位指定)作为整数存储在单独的表 (Shifts) 中,
“Range: bytes=0-” header 是什么意思?是整个文件吗?我尝试发回 0 个字节但没有成功,当我发送整个文件时它可以正常工作,但我在流式上下文中不止一次收到此请求,它看起来不正确。
要创建时间序列的 SAX 表示,您首先需要计算数据的 PAA(分段聚合近似),然后将答案映射到符号表。但是,在计算 PAA 之前,您需要对数据进行标准化。 我正在对数据进行标准化,但我不知道之后如何计
假设我有一个 RESTful、超文本驱动的服务来模拟冰淇淋店。为了帮助更好地管理我的商店,我希望能够显示每日报告,列出所售每种冰淇淋的数量和美元值(value)。 这种报告功能似乎可以作为名为 Dai
我需要以 RDF 格式表示句子。 换句话说,“约翰喜欢可乐”将自动表示为: Subject : John Predicate : Likes Object : Coke 有谁知道我应该从哪里开始?是否
我即将编写一个解析器,将文本文件逐行读取到不同类型的结构中,并将这些结构提供给回调(观察者或访问者 - 尚不确定)。 文本文件包含 MT-940 数据 - SWIFT 银行对帐单。 这些行由一个指定类
我主要是一名 C++ 开发人员,但我经常编写 Python 脚本。我目前正在为游戏编写骰子模拟器,但我不确定在 Python 中解决我的问题的最佳方法。 一共有三种玩家技能,每个玩家一强、中一、弱一。
在过去的 5 个小时里,我一直在寻找答案。尽管我找到了很多答案,但它们并没有以任何方式提供帮助。 我基本上要寻找的是任何 32 位无符号整数的按位异或运算符的数学、算术唯一表示。 尽管这听起来很简单,
我需要将依赖项存储在 DAG 中。 (我们正在细粒度地规划新的学校类(class)) 我们正在使用 rails 3 注意事项 宽于深 很大 我估计每个节点有 5-10 个链接。随着系统的增长,这将增加
我是一名优秀的程序员,十分优秀!