我试图找到n个有趣多边形的区域,其中(n = 1,A = 1,n = 2,A = 5,n = 3,A = 13,n = 4,A = 25,依此类推)。因此,n-interesting多边形的公式是(n-1) - 有趣多边形+(n-1)* 4的区域。运行程序时,隐藏的测试显示代码错误。我无法弄清楚我的代码有什么问题。
def shapeArea(n):
if n == 0:
return 0
if n == 1:
return 1
for i in range(2,n+1):
return (shapeArea(n-1)+(n-1)*4)
答案 0 :(得分:6)
既然已经有编码示例了,我来解释一下为什么公式是n * n + (n-1) * (n-1)
答案 1 :(得分:2)
我找到了没有递归的公式。测试结果很好。
public interface ApiListener {
void success(String strApiName, Object response);
void error(String strApiName, String error);
void failure(String strApiName, String message);
}
答案 2 :(得分:1)
JavaScript问题的最简单解决方案:
function shapeArea(n) {
if(n<0){
return false
}
return (n*n)+((n-1)*(n-1))
}
console.log(shapeArea(1))
答案 3 :(得分:0)
与其他方法类似,我发现使用for循环的解决方案。
def shapeArea(n):
return sum([( num * 4 ) for num in range(1,n)]) + 1
答案 4 :(得分:0)
嘿CodeSignal用户; p, 我认为您编写“ for”循环的最后一部分是狡猾的。如果您已经在使用递归,为什么需要一个“ for”循环。尽管如此,我还是不使用递归来做到这一点:
def shapeArea(n):
if n == 1:
return 1
return n**2 + (n-1)**2
答案 5 :(得分:0)
通过所有测试,无性能问题
def IncreasingSequence(sequence):
for x in range(0, len(sequence) - 1):
if sequence[y] >= sequence[x + 1]:
alt1 = sequence.copy()
alt2 = sequence.copy()
alt1.pop(x)
alt2.pop(x+1)
return (False, alt1, alt2)
return (True, sequence, sequence)
def almostIncreasingSequence(sequence):
boo, nl1, nl2 = IncreasingSequence(sequence)
if boo == False:
boo1, ll, ll1 = IncreasingSequence(nl1)
if boo1 == False:
boo2, l1, l2 =IncreasingSequence(nl2)
return boo2
return True
答案 6 :(得分:0)
//For C# use this code::
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return Convert.ToInt32(Math.Pow((2*n - 1),2) - 2 * n * (n - 1));
//Math.Pow is used to calculate a number raise to the power of some other number
答案 7 :(得分:0)
def shapeArea(n):
sum=0
i=1
while(n>1):
sum=sum+2*i
n=n-1
i=2+i
sum=sum+i
return sum
尝试计算行方平方的总和(两次 2*i)并在最后添加中间行
答案 8 :(得分:0)
一个易于理解的 ruby 解决方案,无需递归:
def shapeArea(n)
total = 0
(1..n-1).each do |column|
total += column + (column-1)
end
(total*2) + (n+(n-1))
end
(1..n-1)
循环计算边,正方形的数量总是 column + (column - 1)
;(total*2)
的总和,并添加中心列 (n+(n-1))
。答案 9 :(得分:0)
这是一个计算给定n的多边形面积的公式
date open close high low
2019-01-01 00:00:00 3700 3800 3806 3646
2019-01-02 00:00:00 3700 3857 3880 3646
2019-01-03 00:00:00 3700 3766 3880 3646
2019-01-04 00:00:00 3700 3791 3880 3646
2019-01-05 00:00:00 3700 3772 3880 3646
2019-01-06 00:00:00 3700 3988 4023 3646
2019-01-07 00:00:00 3700 3972 4023 3646
输出
import pandas as pd
import yfinance as yf
#not working
def resample_active_week(df):
df2 = pd.DataFrame()
# high is the max from Jan1 to current day
df2['high'] = df.groupby(df.index.isocalendar().week)['high'].cummax()
# low is the min from Jan1 to current day
df2['low'] = df.groupby(df.index.isocalendar().week)['low'].cummin()
#close
df2['close'] = df['close']
# open is based on the open of the current week
df2['open'] = df.groupby(df.index.isocalendar().week)['open'].head(1)
df2=df2.fillna(method='ffill')
return df2
#not working
def resample_active_month(df):
df2 = pd.DataFrame()
# high is the max from Jan1 to current day
df2['high'] = df.groupby(df.index.month)['high'].cummax()
# low is the min from Jan1 to current day
df2['low'] = df.groupby(df.index.month)['low'].cummin()
#close
df2['close'] = df['close']
# open is based on the open of the current month
df2['open'] = df.groupby(df.index.month)['open'].head(1)
df2=df2.fillna(method='ffill')
return df2
#not working
def resample_active_quarter(df):
df2 = pd.DataFrame()
# high is the max from Jan1 to current day
df2['high'] = df.groupby(df.index.quarter)['high'].cummax()
# low is the min from Jan1 to current day
df2['low'] = df.groupby(df.index.quarter)['low'].cummin()
#close
df2['close'] = df['close']
# open is based on the open of the current quarter
df2['open'] = df.groupby(df.index.quarter)['open'].head(1)
df2=df2.fillna(method='ffill')
return df2
#working
def resample_active_year(df):
df2 = pd.DataFrame()
# high is the max from Jan1 to current day
df2['high'] = df.groupby(df.index.year)['high'].cummax()
# low is the min from Jan1 to current day
df2['low'] = df.groupby(df.index.year)['low'].cummin()
#close
df2['close'] = df['close']
# open is based on the open of the current year
df2['open'] = df.groupby(df.index.year)['open'].head(1)
df2=df2.fillna(method='ffill')
return df2
df = yf.download(tickers='BTC-USD', period = 'max', interval = '1d',auto_adjust = True)
df.rename(columns={'Open':'open', 'High':'high','Low':'low','Close':'close'}, inplace=True)
df = df.drop(['Volume'],axis=1)
df2 = resample_active_week(df)
df3 = resample_active_month(df)
df4 = resample_active_quarter(df)
df5 = resample_active_year(df)
with pd.ExcelWriter('ResampleOut.xlsx', engine="openpyxl", mode="w") as writer:
df.to_excel(writer, sheet_name='df_original')
df2.to_excel(writer, sheet_name='df2_week')
df3.to_excel(writer, sheet_name='df3_month')
df4.to_excel(writer, sheet_name='df4_quarter')
df5.to_excel(writer, sheet_name='df5_year')
答案 10 :(得分:0)
您对以下解决方案有何看法?
function shapeArea(n) {
return ((n-1) * (n*2)) + 1 ;
}
答案 11 :(得分:0)
def shapeArea(n):
if n == 1:
return 1
square_side = n+n-1
outer_square_area = square_side**2
white_pieces = 4*(1/2)*n*(n+1)
area = outer_square - white_pieces
return area
解决问题的不同方法:
如果您注意到,每个 n 个有趣的多边形都可以包含在一个边长为 2n-1 的周围正方形中。可以用这个“外方格”减去缺失空格的面积。
想出一个白色碎片的公式是很棘手的,因为你必须在 4 边的每一边上添加这些奇怪的阶梯状碎片。这些奇怪的部分的面积实际上可以使用连续整数或1/2*N(N+1)
的公式计算(对于这个问题N=n-1)
这很容易看出,对于 n=2,较大的周围正方形边长为 2+(2-1)=3,因此总面积将为 9 - 4 = 5。
要更好地了解与如何计算白色区域的联系,请参阅 the visualization behind the formula。注意计算这些三角形块的面积与将 1...n 中的整数相加很相似
答案 12 :(得分:-1)
JavaScript解决方案
通过从完整正方形的面积中减去以得到实心正方形的面积来计算空正方形的面积
全角边=(2n-1)
空平方数= 4 *(直到n-1的所有数字的总和)= 4 *(n *(n-1))/ 2
function shapeArea(n) {
if(n === 1)
return 1
else
return Math.pow((2*n - 1),2) - 2 * n * (n - 1)
}