002. ocr

关卡地址#

解决方案:#

思路:#

这一关就不贴图了,直接看图片下提示:

recognize the characters. maybe they are in the book, but MAYBE they are in the page source.

很明显,玄机在源代码中。在源代码中有这样的提示:

find rare characters in the mess below:

所以,从一堆混乱的字符中找到稀有的字符吧!

代码:#

import helper
msg=helper.readFile("../../Data/002/msg.txt")

dic={}

for ch in msg:
    if ch in dic:
        continue
    else:
        dic[ch]=msg.count(ch)

# print(dic)

outstr=[]
for key,value in dic.items():
    if value < 10:
        outstr.append(key)

print(''.join(outstr))
$msg=(Get-Content (Resolve-Path "../../Data/002/msg.txt").Path -Raw).Replace("`r`n","")

# powershell 中 hashtable 遍历的顺序与添加的顺序不一致
# $dic=@{}
$dic=New-Object 'System.Collections.Generic.Dictionary[string,int]'
for ($i = 0; $i -lt $msg.Length; $i++) {
    $ch=$msg[$i]
    if ($dic.ContainsKey($ch)) {
        $dic[$ch]+=1
    } else {
        $dic[$ch]=0
    }  
}

[string]$outstr=""
foreach ($key in $dic.Keys) {
    if ($dic[$key] -le 10) {
        $outstr+=$key
    }
}

Write-Output $outstr
package main

import (
	"fmt"
	"strings"
)

func (c *Challenge) Challenge002()  {
	msg:=ReadFile("../../Data/002/msg.txt")

	// go语言 遍历map时返回值是无序的,相同keys每次构建map时顺序都会变化,构建后多次遍历结果一致。
	dic:=map [rune] int {}
	outstr:=""
	// // 记录字符顺序
	// chars:=""

	// for _,ch := range msg {
	// 	_,exists:=dic[ch]
	// 	if (exists) {
	// 		continue
	// 	} else {
	// 		s:=string(ch)
	// 		chars+=s
	// 		dic[ch]=strings.Count(msg,s)
	// 	}
	// }

	// for _,ch := range chars {
	// 	value,exists:=dic[ch]
	// 	if (exists) {
	// 		if (value < 10) {
	// 			outstr+=string(ch)
	// 		}
	// 	}
	// }

	for _,ch := range msg {
		_,exists:=dic[ch]
		if (exists) {
			continue
		} else {
			s:=string(ch)
			count:=strings.Count(msg,s)
			dic[ch]=count
			if (count < 10) {
				outstr+=s
			}
		}
	}

	fmt.Println(outstr)
}

最终结果: equality#

下一关地址#

010. what are you looking at?

关卡地址#

解决方案:#

思路:#

图片中的牛说:“你瞅啥?”,然后你说:“瞅你咋地,不光瞅你还打你呢”。(皮一下很开心😝)。然后(点击它)会得到一个sequence.txt文件,内容如下:

a = [1, 11, 21, 1211, 111221,

len(a[30]) = ?

规律是这样的:

  1. 第一个元素: 1
  2. 第二个元素: 11 (表示1个1)
  3. 第三个元素: 21 (表示2个1)
  4. 第四个元素: 1211 (表示1个2,1个1)
  5. 第五个元素: 111221 (表示1个1,1个2,2个1)

可以看出,这是一个简单的字符统计程序

代码:#

def getNext(instr):
    count=0
    curch=instr[0]
    outstr=[]
    for ch in instr:
        if ch != curch:
            outstr.append(str(count)+curch)
            curch=ch
            count=1
        else:
            count+=1
    outstr.append(str(count)+curch)
    return ''.join(outstr)

a=['1']
for i in range(31):
    a.append(getNext(a[i]))
print(len(a[30]))
function GetNext {
    param (
        [string]
        $instr
    )
    
    $count=0
    $curch=$instr[0]
    $outstr=New-Object System.Text.StringBuilder

    for ($i = 0; $i -lt $instr.Length; $i++) {
        $ch=$instr[$i]
        if ($ch -ne $curch) {
            # StringBuilder.Append的值会作为函数的返回值返回,所以要在前面加[void]或$null=
            [void]$outstr.AppendFormat("{0}{1}", $count, $curch)
            $curch=$ch
            $count=1
        } else {
            $count++
        }
    }
    [void]$outstr.AppendFormat("{0}{1}", $count, $curch)

    return $outstr.ToString()
}

$a=New-Object System.Collections.Generic.List[string]
$a.Add("1")
for ($i = 0; $i -lt 31; $i++) {
    $next=GetNext($a[$i])
    $a.Add($next)
}
Write-Output $a[30].Length
package main

import(
	"fmt"
	"strings"
)

func (c *Challenge) Challenge010() {
	a:=[]string {"1"}
	for i := 0; i < 31; i++ {
		a=append(a, getNext(a[i]))
	}
	fmt.Println(len(a[30]))
}

func getNext(instr string) string {
	count:=0
	curch:=instr[0]
	var outstr strings.Builder

	for i := 0; i < len(instr); i++ {
		ch:=instr[i]
		if ch != curch {
			outstr.WriteString(fmt.Sprintf("%d%s",count,string(curch)))
			curch=ch
			count=1
		} else {
			count++
		}
	}
	outstr.WriteString(fmt.Sprintf("%d%s",count,string(curch)))

	return outstr.String()
}

最终结果: 5808#

下一关地址#

❤️ 如果这篇文章对你有帮助,欢迎赞助支持我继续维护 ❤️

☕ Support me ⚡ 爱发电赞助