这是设计这个功能的最佳方式吗?

发布于 2024-11-16 00:14:19 字数 2039 浏览 0 评论 0原文

我叫迈克尔·布特罗斯,目前是一名高中生。明年我将进入滑铁卢大学,作为他们的计算机科学课程的入门读物,我已经完成并完成了一些作业。他们使用的语言是Scheme,我一直在学习它。我有 PHP 和 Ruby 背景,所以我担心我从这些语言中学到的一些习惯会被错误地应用到我正在使用的方案中。这是作业中的问题(顺便说一下,可以在线获取):

编写一个名为 sub-time 的函数,该函数使用表示时间的字符串 start-time 和 自然数 mins ,表示之前的分钟数。该函数产生一个 表示给定时间开始时间之前的 mins 分钟的字符串。

消耗的字符串格式如下:两位数字,后跟“hours”, 接下来是两位数字,最后是“分钟”。消耗时间以24小时为单位 格式。如果小时数或分钟数小于 10,则该数字将包含前导 0。 生成的字符串必须完全采用以下格式:一个数字,后跟“hours”, 接下来是数字,然后是“分钟”。请注意,生成的字符串没有 小于 10 的数字前面加 0。产生的时间可能代表一个时间 前一天。

例如,
•(子时间“03小时15分钟”0)产生
“3小时15分钟”,
•(子时间“13小时05分钟”845)产生
“23 小时 0 分钟”,以及
•(子时间“13小时05分钟”2881)已制作
“13 小时 4 分钟”

内置的Scheme函数string->number和number->string可能很有用。

完全按照描述生成字符串非常重要,否则自动测试将失败。特别是,所有字符都必须是小写,并且必须是单个 字符串所有部分之间的空格。

请勿在解决方案中使用任何 cond 表达式。

(老实说,直到现在我才注意到最后一行,而且我的解决方案确实使用了条件表达式。有什么方法可以避免它们吗?)

这是我的解决方案:

;; Assignment 1 Question 4
(define (convert-to-string hours minutes)
  (string-append (number->string hours) " hours " (number->string minutes) " minutes"))

(define (subtract_hours start_hours sub_hours minutes)
  (let ((hours (- start_hours (modulo sub_hours 24))))
    (if (< hours 0)
        (convert-to-string (+ 24 hours) minutes)
        (convert-to-string hours minutes))))

(define (subtract_minutes start_hours start_minutes sub_hours sub_minutes)
  (let ((minutes (- start_minutes sub_minutes)))
    (if (< minutes 0)
        (subtract_hours start_hours (+ 1 sub_hours) (+ 60 minutes))
        (subtract_hours start_hours sub_hours minutes))))

(define (sub-time start-time mins)
  (let ((start_hours (string->number (substring start-time 0 2))) 
        (start_minutes (string->number (substring start-time 9 11)))
        (sub_hours (quotient mins 60))
        (sub_minutes (modulo mins 60)))        
    (subtract_minutes start_hours start_minutes sub_hours sub_minutes)))

我对Scheme非常陌生,尽管我的解决方案有效(根据我有限的测试),我想知道一些经验丰富的退伍军人会说些什么。感谢您抽出时间!

My name is Michael Boutros and I'm currently a senior in high school. Next year I'll be attending the University of Waterloo and as a primer to their CS courses, I've been going through and doing some of the assignments. The language they use is Scheme and I've been learning it as I go along. I come from a PHP and Ruby background, so I'm worried that some of my habits learned from those languages are being applied incorrectly to what I'm doing with scheme. Here's the question from the assignment (available online, by the way):

Write a function called sub-time that consumes a string start-time representing a time, and a
natural number mins which represents the number of minutes before. The function produces a
string representing mins minutes before the given time start-time.

The string consumed will be formatted as follows: a two digit number, followed by “ hours ”,
followed by a two digit number, followed by “ minutes”. The time consumed is in 24-hour
format. If the number of hours or minutes is less than 10, then the number will include a leading 0.
The string produced must be in exactly the following format: a number, followed by “ hours ”,
followed by a number, followed by “ minutes”. Note that the string produced does not have a
leading 0 before numbers less than 10. It is possible that the time produced could represent a time
on a previous day.

For example,
• (sub-time “03 hours 15 minutes” 0) produces
“3 hours 15 minutes”,
• (sub-time “13 hours 05 minutes” 845) produces
“23 hours 0 minutes”, and
• (sub-time “13 hours 05 minutes” 2881) produced
“13 hours 4 minutes”

The built-in Scheme functions string->number and number->string may be useful.

It is very important that you produce the string exactly as described, otherwise the autotests will fail. In particular, all characters must be in lower case and there must be single
spaces between all parts of the string.

Do not use any cond expressions in your solution.

(Honestly, I didn't even notice the last line until just now, and my solution does use conditional expressions. Is there any way to avoid them?)

Here is my solution:

;; Assignment 1 Question 4
(define (convert-to-string hours minutes)
  (string-append (number->string hours) " hours " (number->string minutes) " minutes"))

(define (subtract_hours start_hours sub_hours minutes)
  (let ((hours (- start_hours (modulo sub_hours 24))))
    (if (< hours 0)
        (convert-to-string (+ 24 hours) minutes)
        (convert-to-string hours minutes))))

(define (subtract_minutes start_hours start_minutes sub_hours sub_minutes)
  (let ((minutes (- start_minutes sub_minutes)))
    (if (< minutes 0)
        (subtract_hours start_hours (+ 1 sub_hours) (+ 60 minutes))
        (subtract_hours start_hours sub_hours minutes))))

(define (sub-time start-time mins)
  (let ((start_hours (string->number (substring start-time 0 2))) 
        (start_minutes (string->number (substring start-time 9 11)))
        (sub_hours (quotient mins 60))
        (sub_minutes (modulo mins 60)))        
    (subtract_minutes start_hours start_minutes sub_hours sub_minutes)))

I'm incredibly new to Scheme and although my solution works (from my limited testing), I'm wondering what some seasoned veterans have to say. Thanks for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-11-23 00:14:19

是的,您可以在没有任何条件的情况下编写此代码。考虑将小时+分钟转换为分钟数,然后进行减法,然后将结果转换回小时+分钟。

Yes, you can write this code without any conditionals. Think about converting the hours+minutes to the number of minutes, then doing the subtraction, then converting the result back to hours+minutes.

陪我终i 2024-11-23 00:14:19

我尝试使用分钟转换的想法来回答这个问题,并想出了一些似乎有效的方法。是否允许使用条件似乎还有待争论,所以我谨慎行事,没有使用任何条件。

不幸的是,我对Scheme一无所知,但希望这段代码足够普通,您应该能够进行转换。

int offset; //the mins we are going to subtract
int startTimeInMins = inputHours * 60;
startTimeInMins += inputMins;

int offsetDayRolled = offset mod 1440;
int newTime = startTimeInMins - offsetDayRolled;

newTime += 1440;
newTime = newTime mod 1440;

int newHours = newTime / 60;
int newMins = newTime mod 60;

I had a go at doing the answer using the minute conversion idea and come up with something that seems to work. It seems up for debate whether we are allowed to used conditionals or not, so I played it safe and did not use any.

Unfortunately, I don't know anything about Scheme but hopefully this code is vanilla enough that you should be able to convert.

int offset; //the mins we are going to subtract
int startTimeInMins = inputHours * 60;
startTimeInMins += inputMins;

int offsetDayRolled = offset mod 1440;
int newTime = startTimeInMins - offsetDayRolled;

newTime += 1440;
newTime = newTime mod 1440;

int newHours = newTime / 60;
int newMins = newTime mod 60;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文