我正在尝试从我正在经历的算法书中创建python实现.虽然我确信python可能内置了这些函数,但我认为稍微学习一下这个语言会是一个很好的练习.
给出的算法是为数值数组创建插入排序循环.这让我能够正常工作.然后我尝试修改它以执行反向排序(最大数字到最小数字).输出几乎就在那里,但我不确定它出了什么问题.
首先,增加数量的种类:
sort_this = [31,41,59,26,41,58]
print sort_this
for j in range(1,len(sort_this)):
key = sort_this[j]
i = j - 1
while i >= 0 and sort_this[i] > key:
sort_this[i + 1] = sort_this[i]
i -= 1
sort_this[i + 1] = key
print sort_this
Run Code Online (Sandbox Code Playgroud)
现在,反向排序不起作用:
sort_this = [5,2,4,6,1,3]
print sort_this
for j in range(len(sort_this)-2, 0, -1):
key = sort_this[j]
i = j + 1
while i < len(sort_this) and sort_this[i] > key:
sort_this[i - 1] = sort_this[i]
i += 1
print …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 d3js 库在时间轴上绘制一条垂直线。x 轴是 2015 年。我希望垂直线代表今天(今天总是当天)。我遇到的问题是弄清楚如何准确地将日期作为坐标提供以便正确绘制图形。这是代码的jsfiddle。
var margin = {top: 10, right: 10, bottom: 30, left: 10},
width = 1200 - margin.left - margin.right,
height = 800 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([new Date(2015, 0, 1), new Date(2015, 11, 31)])
.range([0, width]);
var y = d3.scale.linear()
.domain([0,1000])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.months)
.tickSize(30, 0)
.tickFormat(d3.time.format("%B"));
var xAxisMinor = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.months)
.tickSize(-height)
.tickFormat(d3.time.format("%B"));
var xAxisMinorTicks = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.weeks)
.tickSize(-height) …
Run Code Online (Sandbox Code Playgroud)