首页 \ 问答 \ 如何从range()函数获取最后一个数字?(How do I get the last number from the range() function?)

如何从range()函数获取最后一个数字?(How do I get the last number from the range() function?)

有没有办法从range()函数中获取最后一个数字? 我需要在前20个术语中得到Fibonacci序列中的最后一个数字,还是应该使用列表而不是range()


Is there a way to get the last number from the range() function? I need to get the last number in a Fibonacci sequence for first 20 terms or should I use a list instead of range()?


原文:https://stackoverflow.com/questions/780057
更新时间:2022-04-20 13:04

最满意答案

我找到了解决方案。 我的完整规则太复杂了,所以我发布了简化代码来解释我的解决方案。

首先,我们有一个规则创建一个文件夹,并在同一个命令中使用$(shell ...)

all:
    mkdir -p $(PREFIX) && \
    cd $(PREFIX) && \
    cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ')/./loaders.cache
    touch $@
    @echo [DONE]

如果之前没有创建文件夹,这可能会失败。 在这种情况下, $(shell ...)将展开为空字符串。 cat命令将失败。 我们在这里看到,这不是一个扩展问题。 这是一个计时问题。

解决方案是将此规则分为两部分:

all_pass1:
    mkdir -p $(PREFIX) && \
    touch $@
    @echo [DONE]

all_pass2: all_pass1
    cd $(PREFIX) && \
    cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ')/./loaders.cache
    touch $@
    @echo [DONE]

好的,我有一个解决方案。 但我想知道为什么我的第一个makefile工作,第二个开始第一次失败。 这就是我发布这个问题的原因。 这在我看来仍然像一些GNU魔法。

对我来说,它已经解决了,但如果您认为有更好的解决方案,请随时发表评论或提出更好的解决方案。


I found a solution. My full rule is too complex so I post simplified code to explain my solution.

First we have a rule that creates a folder and uses $(shell ...) in the same command:

all:
    mkdir -p $(PREFIX) && \
    cd $(PREFIX) && \
    cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ')/./loaders.cache
    touch $@
    @echo [DONE]

This can fail, if the folder was not created before. In that case $(shell ...) will expand to an empty string. And the cat command will fail. We see here that this is not an expansion problem. It's a timing problem.

The solution is to break this rule into two parts:

all_pass1:
    mkdir -p $(PREFIX) && \
    touch $@
    @echo [DONE]

all_pass2: all_pass1
    cd $(PREFIX) && \
    cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ')/./loaders.cache
    touch $@
    @echo [DONE]

Ok I have a solution. But I wonder why my first makefile worked and the second that started the first fail. And that's the reason why I posted this problem. That still seems to me like some GNU make magic.

For me it's solved, but feel free to comment or present a better solution, if you think there is a better one.

相关问答

更多
  • 你可以给像makefile.win和makefile.nix这样的文件提供明智的名字并使用它们: make -f makefile.win make -f makefile.nix 或者有一个包含以下内容的Makefile: win: make -f makefile.win nix: make -f makefile.nix 并使用make win或make nix You can give sensible names to the files like makefile.win and ...
  • 百分比字符正在通过与词干中的通配符相匹配而被读取,并正在被词干匹配替换。 如果您检查您的示例的make -p输出,您会看到解析的目标行如下所示: %.a: $(patsubst %.c,%.o,$($*_SRCS)) 就制作而言,它只是一组非常奇怪的图案化目标(或类似的东西)。 如果您以类似的方式从make解析中跳过百分比字符,那么您可以得到您想要的工作: pc := % $$(patsubst $$(pc).c,$$(pc).o,$$($$*_SRCS)) 对于附加信息替换引用 (例如$(foo_SR ...
  • 简而言之 ,该语法不能按照您希望的方式工作。 在GNU make语法中做正确的方法是使用模式规则: public%.o: public%.c hashtable.h $(CC) $(CFLAGS) -c $< public%: public%.o $(CC) -o $@ $< 长答案 :这个: public*.o: public*.c hashtable.h 并不意味着你想要它的意思。 假设您在构建开始时有几个文件public01.c等,并且没有文件public01.o ...
  • 因为您的目标文件显然是从.cpp文件构建的。 您没有明确的规则来构建.cpp文件中的.o文件,因此Make使用隐式规则 $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c 。 Because your object files are apparently built from .cpp files. You have no explicit rule for building .o files from .cpp files, so Make uses the implicit rule ...
  • 我找到了解决方案。 我的完整规则太复杂了,所以我发布了简化代码来解释我的解决方案。 首先,我们有一个规则创建一个文件夹,并在同一个命令中使用$(shell ...) : all: mkdir -p $(PREFIX) && \ cd $(PREFIX) && \ cat $(PREFIX)/../$(INPUT) > $(shell cat $(PREFIX)/../$(INPUT) | awk -F "=" '/LoaderDir/ {print $$2}' | tr -d ' ' ...
  • 只需在源代码树中添加make-3.81源代码即可。 您的主Makefile首先应使用任何可用的make编译make-3.81 ,然后使用make-3.81编译其他所有内容。 Just add make-3.81 sources in your source tree. Your main Makefile first should compile make-3.81 using whatever make is available and then use make-3.81 for compiling e ...
  • 它可能是这样的: floppy: picoc command to make floppy picoc: make -C directory/of/picoc all .PHONY : picoc 这更适用于指南然后完全解决方案,因为我不确定我是否理解您的项目结构。 It could be something along the lines: floppy: picoc command to make floppy picoc: make -C directory/of ...
  • 任何方式,我告诉你一些事情,希望可以帮助你: all: @$(MAKE) -C subfolder 或者如果你想使用* .mk作为你的子makefile,你可以写: all: @$(MAKE) -C busfolder -f somename.mk 我向你展示我的例子,我的DIR看起来像: TOPDIR-- Makefile | |-- debug | |-- debug.c | |-- debug.h | |-- debug.mk | |-- instrument.c ...
  • 我不知道为什么$$<以这种方式扩展。 您可以使用$$*来改变配置,但是: %.result: jobs/% $$(strip $$(call get-job,jobs/$$*)) echo $^ 您需要添加strip调用,以便将call结果中嵌入的换行符转换为空格。 I don't know why $$< expands that way. You can make your configuration work by using $$* instead, though: %.resul ...
  • 该手册具有误导性,可能是由于拼写错误。 这两句话中的第一句: $$ <变量计算为此目标的第一个规则中的第一个先决条件。 $$ ^和$$ +计算已经为同一目标出现的规则的所有先决条件列表($$ +重复,$$ ^没有)。 应该像第二个一样,通过附加“已经出现的”字样来证明。 您可以通过运行两个makefile来验证这是否正确描述了该行为: 制作1 .SECONDEXPANSION: foo: foo.1 $$< @echo $+ 输出是: foo.1 显示$$<已扩展为空字符串。 然后: 制作2 . ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的