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将本次周赛t3t4AC的java代码交给chatgpt改写为scala代码,只需要把“class”改为“object”提交就能过题。

  • 以后简单的业务开发,可能交给专门写prompt的人员了。(x

Code


(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))) )
; )

Racket while循环的更多相关文章

  1. scheme 教程 #lang racket

    scheme 教程 #lang racket 来源  https://blog.csdn.net/yemeishenme/article/details/51471037 原文: https://le ...

  2. racket学习-call/cc (let/cc)

    Drracket continuation 文中使用let/cc代替call/cc Racket文档中,let/cc说明为: (let/cc k body ...+) Equivalent to (c ...

  3. 【.net 深呼吸】细说CodeDom(8):分支与循环

    有人会问,为啥 CodeDom 不会生成 switch 语句,为啥没生成 while 语句之类.要注意,CodeDom只关心代码逻辑,而不是语法,语法是给写代码的人用的.如果用.net的“反编译”工具 ...

  4. Recurrent Neural Network系列1--RNN(循环神经网络)概述

    作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 本文翻译自 RECURRENT NEURAL NETWORKS T ...

  5. Python学习--04条件控制与循环结构

    Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...

  6. 模仿Linux内核kfifo实现的循环缓存

    想实现个循环缓冲区(Circular Buffer),搜了些资料多数是基于循环队列的实现方式.使用一个变量存放缓冲区中的数据长度或者空出来一个空间来判断缓冲区是否满了.偶然间看到分析Linux内核的循 ...

  7. 【JS基础】循环

    for 循环的语法: for (语句 1; 语句 2; 语句 3) { 被执行的代码块 } 语句 1 在循环(代码块)开始前执行 语句 2 定义运行循环(代码块)的条件 语句 3 在循环(代码块)已被 ...

  8. 【python之路4】循环语句之while

    1.while 循环语句 #!/usr/bin/env python # -*- coding:utf-8 -*- import time bol = True while bol: print '1 ...

  9. To Java程序员:切勿用普通for循环遍历LinkedList

    ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...

  10. JavaScript单线程和浏览器事件循环简述

    JavaScript单线程 在上篇博客<Promise的前世今生和妙用技巧>的开篇中,我们曾简述了JavaScript的单线程机制和浏览器的事件模型.应很多网友的回复,在这篇文章中将继续展 ...

随机推荐

  1. iOS用runtime给一个类动态添加方法 ---class_addMethod

    先介绍下class_addMethod这个fangfa   /**   * Adds a new method to a class with a given name and implementat ...

  2. 通过n个线程顺序打印26个英文字母

    通过n个线程顺序打印26个英文字母,例如 n=3 则输出: thread0: a thread1: b thread2: c thread0: d 方案一:轮询 多个线程不断轮询是否是该线程执行任务. ...

  3. PHP Redis - 事务

    Redis 事务可以一次执行多个命令, 并有两个重要的保证: ① 事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命令请求所打断. ② ...

  4. linux一些指令

    一.           文件操作命令 查看当前目录下的文件或文件夹 详细查看ls –l 每列代表不同的意义: 第一列drwxr-xr-x  表示权限, n  其中第一个字符d表示 目录(可能有b-块 ...

  5. Spring系列之验证-14

    目录 Java Bean 验证 Bean 验证概述 配置 Bean 验证提供程序 注入验证器 配置一个`DataBinder` Spring MVC 3 验证 Java Bean 验证 Bean 验证 ...

  6. c#动态添加属性名,且属性名也是动态的

    参考博客:https://www.cnblogs.com/wangyinhuo/articles/3385697.html C#4.0使用DYNAMIC 动态添加属性 dynamic p = new ...

  7. 5-3 掌握 egg.js + 云 mongodb

    1 egg.js 1.1 初始化 初始化和项目启动方法 # 初始化 $ mkdir egg-example && cd egg-example $ npm init egg --typ ...

  8. Dubbo调用 Mybatis 实体类一对多时,报错

    添加fetchType="eager"属性 ,急加载 作为笔记,供个人参考

  9. flutter 环境配置以及我的第一个flutter程序(Hello World)

    电脑配置: 操作系统: Windows 7 或更高版本 (64-bit) 磁盘空间: 400 MB (不包括Android Studio的磁盘空间). Windows下所需安装有: 1.Flutter ...

  10. C语言初级阶段7——指针1

    C语言初级阶段7--指针1 地址与指针 1.地址:数据在内存中的存储位置编号,是一个常量. 2.指针:指针的本质就是地址. 指针变量的定义和声明 1.指针变量:存储的数据是地址. 2.定义方法:类型* ...