分享 challenge #1 - #6 update 06/03

oth · 2017年05月22日 · 最后由 oasis_he 回复于 2017年07月18日 · 6756 次阅读

前面的敌人, 看起来值得一战.
尊敬的朋友, 今天你健身了吗?

歧视有很多, 种族, 性别, 年龄, 地域, 学历, 健康, 性取向...
做熟练下面的几百 challenges, 就精通了编程语言基础

电梯 challenge #2
电梯 challenge #3
电梯 challenge #4
电梯 challenge #5
电梯 challenge #6

学习编程的方法有很多, 也因人而异, 有人适合看书, 有人适合看视频教程, 有人天生就很有天赋.

解决 challenges 是掌握编程语言的方法之一, 这种方法可以锻炼你的思维, 更加熟悉编程语言,

很多 challenges 都是在工作中产生的, 在你独自闯荡江湖之前, 需要掌握解决这些问题的方法.

www.reddit.com, 对标百度贴吧

是大洋对岸老百姓喜闻乐见的地方. 换句话说, 这些 challenges 并不是

去 flag 等 tops 公司所要考察的, 也不需要过人的智力和学历, 请有一点耐心和坚持.

题目源自 dailyprogrammer 我搬过来, 有时间我会写写

请在回帖里写下你的代码, 如果你想得到修改建议

由于文化背景不同, 可以掠过那不熟悉的题目

Challenge 1 [easy]

Create a program that will ask the users name and username, age. have it tell them the information back, in the format
写一程序, 询问用户姓名, 年龄, username. 并返回这些信息, 如下:

Your name is ...,
you are ... years old,
your username is ...

have the program log this information in a file to be accessed later.
记录这些信息到一个 file 以备访问.

共收到 23 条回复


puts '>> Please input your name:'
name = gets.chomp
puts '>> Please input your age:'
age = gets.chomp
puts '>> Please input your username'
username = gets.chomp

puts ">> Your name is #{name}." + \
     " You are #{age} years old" + \
     " and username is #{username}"
oth 量产型炮灰工程师 中提及了此贴 05月22日 20:57

challenge 1 [intermediate]

create a program that will allow you to enter events organizable by hour.
新建一程序, 允许你输入 events , 并且可以按时间排序
There must be menu options of some form, and you must be able to easily edit, add, and delete events without directly changing the source code.
(note that by menu i dont necessarily mean gui. as long as you can easily access the different options and receive prompts and instructions telling you how to use the program, it will probably be fine)
(请注意,菜单不一定是 gui, 只要您可以轻松访问不同的选项,并收到提示和说明告, 诉您如何使用程序,这样体验会好很多)

根据上面描述, 在写程序之前, 要翻译成这样:

  1. Create a menu driven program
  2. Using the menu drive program allow a user to add/delete items
  3. The menu should be based on an events calendar where users enter the events by hour
  4. No events should be hard-coded.

这种逐条的语义翻译, 是一项必备技能, 拿到任务后第一件事情就是列 1234..


challenge 1 hard

we all know the classic "guessing game" with higher or lower prompts.
经典的 “猜测游戏” 具有 更高 或 更低 的提示
lets do a role reversal; you create a program that will guess numbers between 1-100, and respond appropriately based on whether users say that the number is too high or too low.
让角色逆转, 您将创建一个可以在 1-100 之间猜测数字的程序, 并根据用户是否认为数字太高或太低,进行适当的响应
Try to make a program that can guess your number based on user input and great code!


oth 回复


NUMBERS = (1..100).to_a
QUIT = 'q'

def make_a_guess(number, guess)
  if guess > number
    puts 'too high'
  elsif guess < number
    puts 'too low'

  guess == number

def start_round(round)
  system "clear"
  puts "round #{round}"
  number = NUMBERS.shuffle!.first

  while true
    puts "please make a guess(1~100, quit: #{QUIT}):"

    guess = gets.chomp!

    return if guess == QUIT

    unless guess =~ /\A\d+\Z/
      puts 'oops! not a number'

    break if make_a_guess(number, guess.to_i)

  start_round(round + 1)


oth 回复


puts 'The answer is ' + (1..100).to_a.bsearch { |x|
  print ">> Is it #{x}? [too high/too low/yes(default)]: "
  (input = gets.chomp) == 'too low' ? 1 : input == 'too high' ? -1 : 0
dsh0416 回复


puts 'The answer is ' + (1..100).to_a.bsearch { |x| (input = [(print ">> Is it #{x}? [too high/too low/yes(default)]: "), gets.chomp][1]) == 'too low' ? 1 : input == 'too high' ? -1 : 0 }.to_s
a  = rand(0..100) 
b = -1
while (a != b)
    puts '>> Please input  number:'
    b = gets.chomp.to_i
    if b>a
        puts "too high"
    if b<a
        puts "too low"
def guess_number down = 1, up = 100, current_number = nil
  current_number = rand(100) + 1 if current_number.nil?
  puts "I guess the number is #{current_number} [smaller/bigger/correct]"
  result = gets.chomp
  case result
  when 'bigger'
    up = current_number
    current_number = (down + current_number) / 2
  when 'smaller'
    down = current_number
    current_number = (up + current_number) / 2
  when 'correct'
    puts "The correct number is #{current_number}"
    puts 'Please enter the result of bigger or smaller'
  guess_number down, up, current_number
11楼 已删除

前面的敌人, 看起来值得一战.
尊敬的朋友, 今天你健身了吗?

challenge 2 easy

Hello, coders! An important part of programming is being able to apply your programs, so your challenge for today is to create a calculator application that has use in your life.

Hello, coders! 编程的一个重要部分是能够应用您的程序,所以您今天的挑战是创建一个在您的生活中使用的计算器。

It might be an interest calculator, or it might be something that you can use in the classroom.


For example, if you were in physics class, you might want to make a F = M * A calc.

例如,在物理课,你可能想要一个 F = M * A 计算。

challenge 2 intermediate

create a short text adventure that will call the user by their name.


The text adventure should use standard text adventure commands ("l, n, s, e, i, etc.").

文本冒险应使用标准文本冒险命令(“l,n,s,e,i” 等)。

for extra credit, make sure the program doesn't fault, quit, glitch, fail, or loop no matter what is put in, even empty text or spaces. These will be tested rigorously!

为了额外的信用,请确保程序没有故障,退出,故障,失败或循环,无论放入什么,甚至空的文本或空格。 这些将被严格测试!

For super extra credit, code it in C

challenge 2 hard

Your mission is to create a stopwatch program. this program should have start, stop, and lap options, and it should write out to a file to be viewed later.

您的任务是创建一个秒表程序。 这个程序应该有 start,stop 和 lap 选项,它应该把结果写入一个文件以供将来查看

jasl 将本帖设为了精华贴 05月24日 22:13

天哪, 你真高!

challenge 3 easy

Welcome to cipher day! write a program that can encrypt texts with an alphabetical caesar cipher. This cipher can ignore numbers, symbols, and whitespace. 编写一个可以使用字母凯撒密码加密文本的程序,该密码可以忽略数字,符号和空格。 for extra credit, add a "decrypt" function to your program!

challenge 3 intermediate

Welcome to cipher day! Create a program that can take a piece of text and encrypt it with an alphabetical substitution cipher. This can ignore white space, numbers, and symbols.

创建一个程序,可以拿一段文本,并按字母顺序替换密码, 进行加密。 可以忽略空白,数字和符号。

for extra credit, make it encrypt whitespace, numbers, and symbols! for extra extra credit, decode someone elses cipher!

challenge 3 hard

Welcome to cipher day! For this challenge, you need to write a program that will take the scrambled words from this post, and compare them against THIS WORD LIST to unscramble them. For bonus points, sort the words by length when you are finished. Post your programs and/or subroutines!

需要编写一个程序,这个程序将从下面获取加密的单词,并将它们与这个 WORD LIST 进行比较来解读它们。 对于奖励,当完成时,按长度排序单词。 发布您的程序和/或子程序!

Here are your words to de-scramble:




mkeart unscrambled is market
sleewa unscrambled is weasel
edcudls unscrambled is cuddles
iragoge unscrambled is georgia
usrlsle unscrambled is russell
nalraoci unscrambled is carolina
nsdeuto unscrambled is notused
amrhat unscrambled is martha
inknsy unscrambled is skinny
iferkna unscrambled is frankie

考察 WORD LIST 中的内容

challenge 4 easy

You're challenge for today is to create a random password generator!


For extra credit, allow the user to specify the amount of passwords to generate.


For even more extra credit, allow the user to specify the length of the strings he wants to generate!


challenge 4 intermediate

create a calculator program that will take an input, following normal calculator input (5*5+4) and give an answer (29). This calculator should use all four operators.

创建一个计算器程序,将按照正常的计算器输入(5 * 5 + 4)进行输入,并给出答案(29)。 这个计算器应该使用所有四个运算符。

For extra credit, add other operators (6(4+3), 3 ** 3, etc.)

challenge 4 hard

today, your challenge is to create a program that will take a series of numbers (5, 3, 15), and find how those numbers can add, subtract, multiply, or divide in various ways to relate to eachother.


This string of numbers should result in 5 * 3 = 15, or 15 /3 = 5, or 15/5 = 3. When you are done, test your numbers with the following strings:

这个数字字符串应该得到结果 5 * 3 = 15 或 15/3 = 5 或 15/5 = 3 完成后,使用以下字符串测试您的数字:

4, 2, 8
6, 2, 12
6, 2, 3
9, 12, 108
4, 16, 64

For extra credit, have the program list all possible combinations.


for even more extra credit, allow the program to deal with strings of greater than three numbers. For example, an input of (3, 5, 5, 3) would be 3 * 5 = 15, 15/5 = 3. When you are finished, test them with the following strings.


2, 4, 6, 3
1, 1, 2, 3
4, 4, 3, 4
8, 4, 3, 6
9, 3, 1, 7

challenge 5 easy

Your challenge for today is to create a program which is password protected, and wont open unless the correct user and password is given.


For extra credit, have the user and password in a seperate .txt file.

额外的加分,将用户和密码放在单独的.text 文件中。

for even more extra credit, break into your own program :)

challenge 5 intermediate

Your challenge today is to write a program that can find the number of anagrams within a .txt file.

您今天的挑战是编写一个程序,可以找到 a.txt 文件中的字谜的数量。

For example, "snap" would be an anagram of "pans", and "skate" would be an anagram of "stake".

challenge 5 hard

Arrr, me mateys! Yer' challenge fer' today be a tough one.

It be gettin awfully borein' on the high seas, so yer' job be to create a pirate based fightin' game! This game oughter' be turn based, and you oughter' be able to pick yer attacks every turn.

The best game'll be winnin' some custom flair, and all the rest o' ya will be walkin the plank!

oth 回复

challenge 4 intermediate 对新手来说相当难,我给个大致思路吧。

方法一:扫描表达式,把所有 * / 都算了,然后重新扫描算 + -,遇到括号递归执行


challenge 3 hard 最快速的做法是字符排序然后哈希,复杂度可以做到 O(m+n) m 是 word list 的长度,n 是查询的数量。如果需要输出排序还需要加一个 O(n*p) p 是字符串的长度

challenge 6 easy

You're challenge for today is to create a program that can calculate pi accurately to at least 30 decimal places.

你今天的挑战是创建一个可以精确计算 pi, 至少 30 位小数的程序。

Try not to cheat :)

challenge 6 intermediate

create a program that can remove all duplicate strings from a .txt. file. for example, "bdbdb" -> "bd"

创建一个可以从.txt 中删除所有重复字符串的程序。 文件。 例如,“bdbdb” - >“bd”

remove duplicate substrings. Ex: aaajtestBlaBlatestBlaBla ---> aaajtestBlaBla

another example: aaatestBlaBlatestBlaBla aaathisBlaBlathisBlaBla aaathatBlaBlathatBlaBla aaagoodBlaBlagoodBlaBla aaagood1BlaBla123good1BlaBla123

output desired: aaatestBlaBla aaathisBlaBla aaathatBlaBla aaagoodBlaBla aaagood1BlaBla123

challenge 6 hard

create a AI that will play NIM


oth 回复

Nim Game 的 AI 如果无关输赢的话那就是乱写也可以。因为 Nim Game 从一开始就可以证明是必赢或者必输的。所以一个好的 AI 可以在必输时直接投降,必赢时不会输。

oth 我觉的 Ruby China 对新手不太友好 中提及了此贴 07月07日 23:29

@dsh0416 我觉得 AI 不会知道堆中含棋子的个数,

AI 与 Player 轮流取棋子

  1. 取走棋子, 取走几个是不确定的
  2. 取走棋子的个数可以小于剩余棋子个数
  3. 若大于剩余个数, 即不成功提取, 成为输家
dsh0416 回复

challenge 4 intermediate 我觉可以简单处理,

eval "1+1"
oth 回复

用 eval 相当于没写。。。背后的字符串解析完全没有碰到,而且还会遇到注入的问题,在非动态语言上也运行不起来。

关于 nim game 的定义建议看英文的维基百科,中文的没有翻译完整。

尼姆游戏开始前就需要知道每个堆的数量 nx。有一个特殊的变形,尼姆堆只有一个堆的时候,通常还需要限制最多能取 m 个子。这个变形是最好证明的,我可以给一个例子,当 n0 = 30,而 m = 4 的时候,后手必胜。因为 n0 是 5 的整倍数,当先手每次取 x 的时候,后手取 (5-x) 个。那么最后这个堆一定只剩下 5 个子,且是先手行动,这时候无论先手拿几个子,后手都可以把剩下的子全拿完。关于一个堆的所有数量都可以被很容易地证明。结论是 n0 是 (m+1) 的整倍数时,后手必胜,反之,先手必胜。你可以试一下。

关于多个堆比较麻烦一些,用逻辑异或处理一下即可。英文维基百科给出了完整的证明。你看中文维基也写了 Nim Game 是一个无偏博弈。无偏博弈的特点之一就是完全信息博弈,双方都能完全看到局势,不可能存在「不会知道堆中含棋子的个数」的问题。

oth 回复

对于 challenge 6 的 easy 来说,其实也挺不 easy 的。由于题目要求精确到小数点后 30 位,基本超出了浮点数的精确范围。如果不考虑这点的话,可能无论什么方法都很难做对。

为了正确做对这道题,你需要定义一个自己的数据结构能够处理更多位的小数,比较方便的是定义成定点数,理论上定义个 100 位,做一个 30 位精确的题目应该是足够了。然后你需要实现这个数字的加减乘除运算。一旦这点做好,就有很多方法可以做对,无论是割圆法还是蒙特卡洛迭代,或者定义一个大正方形然后遍历每个点,应该都能算对。

如果要收敛得比较快,那么比较常用的是梅钦公式。不过梅钦公式中有个 arctan,这对于才实现了加减乘除的自定义数据结构来说很不方便。但可以用泰勒级数展开这个 arctan,从而把问题转换成一个迭代问题 。现代常用的计算圆周率的方法一般都是梅钦公式的改进,称为梅钦类公式。

除了这些方法以外,另一个难点是,如何证明自己的程序已经收敛到前 30 位完全正确了。证明自己是对的这一点也挺有意思,特别是我们假设 30 位的圆周率还没有被人类算出来,我们需要自行证明自己是对的,而不是拿一个正确答案来比较。这一点对于梅钦公式来说比较容易,对于蒙特卡洛方法来说则很难。

@dsh0416 我倾向于更加简单, 更加简化的理解和解决.

dailyprogramer 栏目中, 如果题目标注 easy, 请让题目解决起来像 easy 一样.


  • challenge 4 intermediate 可以使用 eval 解决
  • challenge 6 easy 可以使用 BigDecimal 解决. BigDecimal provides arbitrary-precision floating point decimal arithmetic.

answer for challenge 6 easy:

require "bigdecimal/math"


=> "0.31415926535897932364198143965603e1"

This output is more than 30 decimal places, We need fromat it.

sprintf("%.30f", BigMath.PI(1))


=> "3.141592653589793115997963468544"

check it...

sprintf("%.30f", BigMath.PI(1)).length


=> 32

All above is easy, isn't it.

准备学 ruby 的前端,还在下 ubuntu。先随便写写

challenge #1 easy

// Create a program that will ask the users name and username, age. have it tell them the information back, in the format
const fs = require('fs')
const readline = require('readline')
let name, age, username, text
let rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
function puts(str, callback) {
    rl.question(str + '\n', (answer) => {
puts('your name?', (answer) => {
    name = answer
    puts('your age?', (answer) => {
        age = answer
        puts('your username?', (answer) => {
            username = answer
            text = ` Your name is ${name}\n You are ${age} old \n Your username is ${username}`
            fs.writeFile('./test.txt', text, function (err) {
                if (err) {
                    throw err;
rl.on("close", () => {

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册