假设有一维数组,满足如下条件,需要按每个数字大小(包括小数数字),重新分布到新的一维数组中,以整数为单位,对于数组中的每一个数字,如果不是整数,将其分布在新数组中的位于此数字的向下取整的数字,和向上取整的数字之间
新的数组的数据可用于chart图的按照时间显示的x轴上
the number in array must be:
# 1. sorted by ascending order
# 2. each number are not equal each other
# 3. each number >= 0
# max length = the last number in tenorsInyears + the counter of non-integer in tenorsInyears??
- python版
# -*- coding: utf-8 -*-
import math
"""
"""
def main():
print("we are in %s", __name__)
## real number
tenorsInyears = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
# tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
## Many numbers less than 1 at the beginner
# tenorsInyears = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
# tenorsInyears = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
## Many decimal numbers at the center
# tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
# tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 40]
## Many decimal numbers at the last
tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9]
tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11]
tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11.1]
tenorsInyears = [1, 1.5, 2, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11, 11.1]
tenorsInyears = [0.1, 2.1, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10]
tenorsInyears = [1, 2.1, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10]
## Many group decimal numbers, Which locate between the two consecutive numbers, Such as 4~5, 10~11
tenorsInyears = [1, 1.5, 2.0, 2.3, 3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 5, 6, 7,
10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
## the beginner number is > 1
tenorsInyears = [1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
tenorsInyears = [3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14]
tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14, 99]
tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 5, 7, 9, 11, 13, 15, 17]
tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.81, 5, 7, 10, 14, 19, 25, 32, 40]
tenorsInyears = [4.1, 4.2, 4.3, 7, 9.5, 11.6, 14.7, 17.8, 21.9, 23, 23.9, 26, 28, 30, 33, 37, 42, 48]
tenorsInyears = [4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 8]
## the last number is not an integer
tenorsInyears = [0.1, 0.2, 1, 2.1, 2.9, 3, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 7.1, 7.2, 8, 8.1]
## the beginner number and the last number all are not integer
tenorsInyears = [0.1, 0.2, 1, 2.1, 2.9, 3, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5,
4.6, 4.7, 4.8, 4.9, 5, 7.1, 7.2, 8, 8.1, 8.2, 8.3, 8.4, 8.5]
## all number are not integer
tenorsInyears = [0.1, 2.1, 3.0, 6.2, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10.1, 40.1]
tenorsInyears = [0.1, 2.1, 3.1, 4.2, 5.2, 6.3, 7.4, 8.5, 9.6, 10.7, 11.8, 19.9, 20.1, 40.1]
# the number in array must be:
# 1. sorted by ascending order
# 2. each number are not equal each other
# 3. each number >= 0
# max length = the last number in tenorsInyears + the counter of non-integer in tenorsInyears??
value2 = []
value3 = []
for i in range(1, 100):
value2.append(str(i) + "Y")
value3.append(str(i))
counterOfNonIntegerArr = getCounterOfNonIntegerArr(tenorsInyears)
maxLength = math.floor(tenorsInyears[-1]) + counterOfNonIntegerArr
value4 = [""] * maxLength
value5 = [""] * maxLength
print(tenorsInyears)
# if counterOfNonIntegerArr == 0:
# # all element are integer number
# for i in range(0, len(tenorsInyears)):
# item = tenorsInyears[i]
# index = item - 1
# value4[index] = value2[i]
# value5[index] = item
# else:
index = 1
for i in range(0, len(tenorsInyears)):
item = tenorsInyears[i]
itemCeil = math.ceil(item)
if i == 0:
index = itemCeil - 1
value4[index] = value2[i]
value5[index] = item
else:
lastNumber = tenorsInyears[i - 1]
difference = itemCeil - math.ceil(lastNumber)
if difference == 0:
index = index + 1
value4[index] = value2[i]
value5[index] = item
elif difference == 1:
if isNotInteger(lastNumber):
index = index + 2
value4[index] = value2[i]
value5[index] = item
else:
index = index + 1
value4[index] = value2[i]
value5[index] = item
else:
if isNotInteger(lastNumber):
index = index + difference + 1
value4[index] = value2[i]
value5[index] = item
else:
index = index + difference
value4[index] = value2[i]
value5[index] = item
print(value5)
def getCounterOfNonIntegerArr(arr):
counterNonInteger = 0
for i in range(0, len(arr)):
if isNotInteger(arr[i]):
counterNonInteger += 1
return counterNonInteger
## the number is not an integer
## Such as 2.1, 3.1, etc are not integer
## and 2.0, 3.0, etc are let it as be integer here
def isNotInteger(number):
return not isinstance(number, int) and math.ceil(number) != number
if __name__ == '__main__':
main()
一些测试数据
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', '', '', '', '', '', '', '', '', 20, '', '', '', '', '', '', '', '', '', 30, '', '', '', '', '', '', '', '', '', 40]
[1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
[1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', '', '', '', '', '', '', '', '', 20, '', '', '', '', '', '', '', '', '', 30, '', '', '', '', '', '', '', '', '', 40]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', 12, '', 14]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', '', '', '', '', '', '', '', '', 20, '', '', '', '', '', '', '', '', '', 30, '', '', '', '', '', '', '', '', '', 40]
[3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14]
['', '', 3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, '', '', 7, 8, 9, 10, '', 12, '', 14]
[1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
['', 1.5, 2, 3, 4, 5, 6, 7, '', '', 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, '', 12, '', 14]
[1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
[1, 1.5, 2, 3, 4, 5, 6, 7, '', '', 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, '', 12, '', 14]
- Javascript版
function main() {
real number
let tenorsInyears = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
// tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
Many numbers less than 1 at the beginner
// tenorsInyears = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
// tenorsInyears = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
Many decimal numbers at the center
// tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
// tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 40]
Many decimal numbers at the last
// tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9]
// tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11]
// tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11.1]
// tenorsInyears = [1, 1.5, 2, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11, 11.1]
// tenorsInyears = [0.1, 2.1, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10]
// tenorsInyears = [1, 2.1, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10]
Many group decimal numbers, Which locate between the two consecutive numbers, Such as 4~5, 10~11
// tenorsInyears = [1, 1.5, 2.0, 2.3, 3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 5, 6, 7,
// 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
the beginner number is > 1
// tenorsInyears = [1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
// tenorsInyears = [3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14]
// tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14, 99]
// tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 5, 7, 9, 11, 13, 15, 17]
// tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.81, 5, 7, 10, 14, 19, 25, 32, 40]
// tenorsInyears = [4.1, 4.2, 4.3, 7, 9.5, 11.6, 14.7, 17.8, 21.9, 23, 23.9, 26, 28, 30, 33, 37, 42, 48]
// tenorsInyears = [4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 8]
the last number is not an integer
// tenorsInyears = [0.1, 0.2, 1, 2.1, 2.9, 3, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 7.1, 7.2, 8, 8.1]
the beginner number and the last number all are not integer
// tenorsInyears = [0.1, 0.2, 1, 2.1, 2.9, 3, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5,
// 4.6, 4.7, 4.8, 4.9, 5, 7.1, 7.2, 8, 8.1, 8.2, 8.3, 8.4, 8.5]
all number are not integer
// tenorsInyears = [0.1, 2.1, 3.0, 6.2, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10.1, 40.1]
// tenorsInyears = [0.1, 2.1, 3.1, 4.2, 5.2, 6.3, 7.4, 8.5, 9.6, 10.7, 11.8, 19.9, 20.1, 40.1]
// the number in array must be:
// 1. sorted by ascending order
// 2. each number are not equal each other
// 3. each number >= 0
// max length = the last number in tenorsInyears + the counter of non-integer in tenorsInyears??
let value2 = [];
let value3 = [];
for (let i = 1; i < 100; i++) {
value2.push(i + "Y");
value3.push(i);
}
const counterOfNonIntegerArr = getCounterOfNonIntegerArr(tenorsInyears);
const maxLength = Math.floor(tenorsInyears[tenorsInyears.length - 1]) + counterOfNonIntegerArr;
value4 = []
value5 = []
for (let i = 0; i < maxLength; i++) {
value4.push('');
value5.push('');
}
console.log(tenorsInyears);
// if counterOfNonIntegerArr == 0:
// // all element are integer number
// for i in range(0, len(tenorsInyears)):
// item = tenorsInyears[i]
// index = item - 1
// value4[index] = value2[i]
// value5[index] = item
// else:
let index = 1;
for (let i = 0; i < tenorsInyears.length; i++) {
let item = tenorsInyears[i];
let itemCeil = Math.ceil(item);
if (i == 0) {
index = itemCeil - 1;
value4[index] = value2[i];
value5[index] = item;
} else {
let lastNumber = tenorsInyears[i - 1];
difference = itemCeil - Math.ceil(lastNumber);
if (difference === 0) {
index = index + 1;
value4[index] = value2[i];
value5[index] = item;
} else if (difference === 1) {
if (isNotInteger(lastNumber)) {
index = index + 2;
value4[index] = value2[i];
value5[index] = item;
} else {
index = index + 1;
value4[index] = value2[i];
value5[index] = item;
}
} else {
if (isNotInteger(lastNumber)) {
index = index + difference + 1;
value4[index] = value2[i];
value5[index] = item;
} else {
index = index + difference;
value4[index] = value2[i];
value5[index] = item;
}
}
}
}
console.log(value5);
}
function getCounterOfNonIntegerArr(arr) {
counterNonInteger = 0;
for (let i = 0; i < arr.length; i++) {
if (isNotInteger(arr[i])) {
counterNonInteger += 1;
}
}
return counterNonInteger;
}
the number is not an integer
Such as 2.1, 3.1, etc are not integer
and 2.0, 3.0, etc are let it as be integer here
function isNotInteger(number) {
// return not isinstance(number, int) and Math.ceil(number) != number
return Math.round(number) !== number;
}
main();