欢迎访问yhm138的博客园博客, 你可以通过 [RSS] 的方式持续关注博客更新

MyAvatar

yhm138

HelloWorld!

Racket while循环

Problem: 1805. 字符串中不同整数的数目

lc题解地址

https://leetcode.cn/problems/number-of-different-integers-in-a-string/solutions/2009554/racket-shuang-zhi-zhen-by-yhm138_-hz47/

思路

  • 函数式编程?那是啥?有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将本次周赛t3t4的AC的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)))  )    
;   )
posted @ 2022-12-06 23:12  yhm138  阅读(32)  评论(0编辑  收藏  举报