码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Go语言学习笔记-A Tour of Go 练习笔记-rot13Reader


    Exercise: rot13Reader

    练习题目:

    A common pattern is an io.Reader that wraps another io.Reader, modifying the stream in some way.

    For example, the gzip.NewReader function takes an io.Reader (a stream of compressed data) and returns a *gzip.Reader that also implements io.Reader (a stream of the decompressed data).

    Implement a rot13Reader that implements io.Reader and reads from an io.Reader, modifying the stream by applying the rot13 substitution cipher to all alphabetical characters.

    The rot13Reader type is provided for you. Make it an io.Reader by implementing its Read method.

    练习程序:

    1. package main
    2. import (
    3. "io"
    4. "os"
    5. "strings"
    6. )
    7. type rot13Reader struct {
    8. r io.Reader
    9. }
    10. func rot13(x byte) byte {
    11. switch {
    12. case x >= 65 && x <= 77:
    13. fallthrough
    14. case x >= 97 && x <= 109:
    15. x = x + 13
    16. case x >= 78 && x <= 90:
    17. fallthrough
    18. case x >= 110 && x <= 122:
    19. x = x - 13
    20. }
    21. return x
    22. }
    23. func (rot rot13Reader) Read(a []byte) (int, error) {
    24. n, err := rot.r.Read(a)
    25. for key, value := range(a) {
    26. a[key] = rot13(value)
    27. }
    28. return n, err
    29. }
    30. func main() {
    31. s := strings.NewReader("Lbh penpxrq gur pbqr!")
    32. r := rot13Reader{s}
    33. io.Copy(os.Stdout, &r)
    34. }

    运行结果:

    You cracked the code!

    学习笔记:

    该题目是对于GO中Read方法的进阶练习,GO中常用的一种方式是用一个io.Reader去封装另一个io.Reader,这样可以实现很多功能,比如可以先通过里层的io.Reader从加密的文本中先读取加密字符,然后在外层的io.Reader的Read方法中实现解密并输出,这样就完成了一种直接读取解密文本的能力。本题目就是类似的场景,使用的加密算法是比较简单的ROT13字符替换加密法。

  • 相关阅读:
    CentOS7.9离线安装Docker环境
    js for循环设置循环变量和循环体内部是两个单独作用域
    day08-注册功能、前端登录注册页面复制、前端登录功能、前端注册功能
    GB28181学习(十六)——基于jrtplib实现tcp被动和主动收流
    【reverse】新160个CrackMe之154-cpp_crackme1——MFC+纯算法逆向
    AI自动绘画生成器,AI自动绘画工具使用教程
    python cookbook 学习笔记(持续更新)
    C语言实现字符串比较
    22_ue4进阶末日生存游戏开发[EQS]
    LeetCode刷题
  • 原文地址:https://blog.csdn.net/sxmatch/article/details/128131640
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号