Problem: 1805. 字符串中不同整数的数目
函数式编程?那是啥?有set!和hash-set!好用吗
开始那一堆是在实现while break continue.按照racket文档里说的,你(require dyoo-while-loop)也行。
?我require不了,leetcode没加载这个collection ,你应该去这个仓库把代码直接拉过来
简单的语法还是问gptchat吧。不然你和我一样分不清{make-hash,make-hasheq,make-hasheqv,make-hashalw}的区别。
(string=? "0" (substring word p1 (+ p1 1))) 判断字符串某个下标的字符是不是字符'0',你应该这么写。其他写法可能行。
chatgpt基本上不会写racket程序,超过5行就别指望简单改改能运行起来;但是chatgpt具备将java代码改写成scala代码的能力,我把class改成object就过了这道leetcode题目
chatgpt将热门语言彼此做改写没什么问题的。2023-03-05将本次周赛t3t4AC的java代码交给chatgpt改写为scala代码,只需要把“class”改为“object”提交就能过题。
以后简单的业务开发,可能交给专门写prompt的人员了。(x

(require (for-syntax racket/base)
racket/stxparam)
(provide while break continue)
;; The following is adapted from:
;;
;; http://matt.might.net/articles/implementing-exceptions/
;;
;; with a few adjustments so that break and continue are
;; hygienic, and the extent of the break and continue are
;; around the body.
;;
(define-syntax-parameter break
(lambda (stx)
(raise-syntax-error #f "Used outside the context of a while body" stx)))
(define-syntax-parameter continue
(lambda (stx)
(raise-syntax-error #f "Used outside the context of a while body" stx)))
;; This enforces the use of break and continue:
;; you have to use them with parens, or else they should
;; complain at compile time.
(define-for-syntax (force-use-with-parens b)
(lambda (stx)
(syntax-case stx ()
[(_)
(with-syntax ([b b])
(syntax/loc stx
(b)))]
[(kw arg arg-rest ...)
(raise-syntax-error #f
(format "Must be directly used without arguments [e.g: (~a)]"
(syntax->datum #'kw))
stx
#'arg)]
[_
(identifier? stx)
(raise-syntax-error #f
(format "Must be directly used [e.g: (~a)]"
(syntax->datum stx))
stx)])))
(define-syntax (while stx)
(syntax-case stx ()
[(_)
(raise-syntax-error #f "missing test and body" stx)]
[(_ cond)
(raise-syntax-error #f "missing body" stx)]
[(_ cond body ...)
(syntax/loc stx
(let/ec fresh-break
(let loop ()
(when cond
(let/ec fresh-continue
(syntax-parameterize
([break (force-use-with-parens #'fresh-break)]
[continue (force-use-with-parens #'fresh-continue)])
(begin body ...)))
(loop)))))]))
;; By the way: it's a bit unclear what should happen if
;; someone tries using break and continue within the
;; context of a nested while loop's test... e.g.
;; (while true
;; (while (begin (break))
;; 'huh?)
;; 'what?)
;;
;; This particular implementation is implemented such that
;; the call to break will exit the outer loop. It is an odd
;; thing to think about, isn't it? :)
;;
;; It should be easy to syntactically restrict if this really
;; becomes an issue, by syntax-parameterizing error productions
;; in the context of evaluating the _cond_ition.
(define/contract (num-different-integers word)
(-> string? exact-integer?)
(define s (make-hash)) ;键不允许重复
(define n (string-length word))
(define p1 0)
(define p2 -1)
(while #t
(while( and (< p1 n) (not (char-numeric? (string-ref word p1))) )
(set! p1 (+ p1 1)))
; (displayln (format "save1 p1= ~a p2= ~a" p1 p2))
(when (>= p1 n) (break)) ;建议when. if cond都不好使
(set! p2 p1)
; (displayln (format "save2 p1= ~a p2= ~a" p1 p2))
(while( and (< p2 n) (char-numeric? (string-ref word p2)) )
(set! p2 (+ p2 1))
)
; (displayln (format "save3 p1= ~a p2= ~a" p1 p2))
(while( and (> (- p2 p1) 1) (string=? "0" (substring word p1 (+ p1 1))) )
(set! p1 (+ p1 1))
)
( hash-set! s (substring word p1 p2) 1 )
(set! p1 p2)
)
(displayln (format "s= ~a" s)) ;看看这个哈希表长啥样
(hash-count s)
)
;下面是草稿
; (while (<= p1 5)
; (set! p1 (+ p1 1))
; ; (displayln p1)
; (displayln (string=? "0" (substring word p1 (+ p1 1))) )
; )
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我是Ruby的新手,有些闭包逻辑让我感到困惑。考虑这段代码:array=[]foriin(1..5)array[5,5,5,5,5]这对我来说很有意义,因为i被绑定(bind)在循环之外,所以每次循环都会捕获相同的变量。使用每个block可以解决这个问题对我来说也很有意义:array=[](1..5).each{|i|array[1,2,3,4,5]...因为现在每次通过时都单独声明i。但现在我迷路了:为什么我不能通过引入一个中间变量来修复它?array=[]foriin1..5j=iarray[5,5,5,5,5]因为j每次循环都是新的,我认为每次循环都会捕获不同的变量。例如,这绝对
假设我有一个没有特定顺序的随机数数组。假设这些是参加马拉松比赛的人的ID#,他们按照完成的顺序添加到数组中,例如:race1=[8,102,67,58,91,16,27]race2=[51,31,7,15,99,58,22]这是一个简化且有些做作的示例,但我认为它传达了基本思想。现在有几个问题:首先,我如何获得特定条目之前和之后的ID?假设我正在查看运行者58,我想知道谁在他之前和之后完成了比赛。race1,runner58:previousfinisher=67,nextfinisher=91race2,runner58:previousfinisher=99,nextfinishe
defreverse(ary)result=[]forresult[0,0]inaryendresultendassert_equal["baz","bar","foo"],reverse(["foo","bar","baz"])这行得通,我想了解原因。有什么解释吗? 最佳答案 如果我使用each而不是for/in重写它,它看起来像这样:defreverse(ary)result=[]#forresult[0,0]inaryary.eachdo|item|result[0,0]=itemendresultendforainb基本上就
在EloquentRuby(第21页,第一版,第六次打印)一书中,作者(RussOlsen)提倡使用each方法而不是for循环,这与我在其他地方读到的所有内容一致。但是作者还继续说,这样做的一个原因是for循环实际上调用了each方法,所以为什么不直接删掉中间人并使用each?所以我想知道这实际上是如何工作的。为了调查,我确实在github上的Ruby存储库上进行了搜索,但发现很难确定我在哪里/如何看到它的实际效果。重述问题:我如何证明Rubyfor循环实际上是使用each方法实现的? 最佳答案 您可以通过编写一个实现每个的类来展
我想从0到2循环@a:0,1,2,0,1,2。defset_aif@a==2@a=0else@a=@a+1endend也许有更好的方法? 最佳答案 (0..2).cycle(3){|x|putsx}#=>0,1,2,0,1,2,0,1,2item=[0,1,2].cycle.eachitem.next#=>0item.next#=>1item.next#=>2item.next#=>0... 关于ruby-循环遍历数组的元素,我们在StackOverflow上找到一个类似的问题:
我必须编写一个Ruby方法:遍历数组,如果其中一个元素符合特定条件则执行Foo。如果没有数组元素符合条件,则执行Bar操作。在任何其他语言中,我会在进入循环之前设置一个bool变量,并在执行Foo时切换它。该变量的值会告诉我是否需要Bar。但这感觉不像Rubyish那样不优雅。谁能提出更好的方法?编辑一些非常好的答案,但由于我本应提及的细节,它们不太有效。Foo所做的事情是对符合条件的数组元素完成的。此外,保证最多有一个元素匹配条件。 最佳答案 是否有任何项目匹配?如果是,则做一些不涉及匹配项目的事情。ifitems.any?{|i
我希望遍历类似于Loopinginaspiral的矩阵但是从外向内循环,而不是从内向外循环。任何人都可以帮助我为任何大小的矩阵执行此操作的好方法,最好是在Ruby中?例子:在3x4矩阵中,我想从[0,0]开始向右移动,然后在到达[3,0]时向下移动,在[3,2]向左移动等等。[0,0][1,0][2,0][3,0][0,1][1,1][2,1][3,1][0,2][1,2][2,2][3,2]移动顺序如下图所示:01239101148765输出将是:[0,0],[1,0],[2,0],[3,0],[3,1],[3,2],[2,2],[1,2],[0,2],[0,1],[1,1],[2,