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

oth · May 22, 2017 · Last by oasis_he replied at July 18, 2017 · 7992 hits
Topic has been selected as the excellent topic by the admin.

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

歧视有很多,种族,性别,年龄,地域,学历,健康,性取向...
软件行业天然的抹平歧视
做熟练下面的几百 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 以备访问。

easy

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 in 量产型炮灰工程师 mention this topic. 22 May 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!
尝试制作这个程序,可以根据用户输入猜测你的数字!

这些还真是锻炼基本逻辑的题,推荐白板练习

Reply to 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'
  end

  guess == number
end

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'
      next
    end

    break if make_a_guess(number, guess.to_i)
  end

  start_round(round + 1)
end

start_round(1)

Reply to 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
}.to_s
Reply to 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"
    else
    end
    if b<a
        puts "too low"
    else
    end
end
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}"
    return
  else
    puts 'Please enter the result of bigger or smaller'
  end
  guess_number down, up, current_number
end
11 Floor has deleted

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


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 mark as excellent topic. 24 May 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
sleewa
edcudls
iragoge
usrlsle
nalraoci
nsdeuto
amrhat
inknsy
iferkna

提示:

结果是

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.

今天,您的挑战是创建一个将采用一系列数字(5,3,15)的程序,并找出通过各种方式添加,减去,乘法或除法。

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!


Reply to oth

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

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

方法二:处理成抽象语法树(AST)以支持更多符号的自由添加

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

尼姆游戏(英语:Nim),又译为拈,是一种两个人玩的回合制数学战略游戏。游戏者轮流从一堆棋子(或者任何道具)中取走一个或者多个,最后不能再取的就是输家。当指定相应数量时,一堆这样的棋子称作一个尼姆堆。
斯普莱格(R.P.Sprague)和格隆第(P.M.Grundy)独立地证明了一切无偏博弈(从任何一个局势出发,双方可以采取完全相同的行动,也就是说棋盘上没有颜色的区分)都等价于一个特定大小的尼姆堆。但这里的尼姆堆包含的棋子数量可以是无穷的。事实上,它可以是任何序数。


Reply to oth

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

oth in 我觉的 Ruby China 对新手不太友好 mention this topic. 07 Jul 23:29

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

AI 与 Player 轮流取棋子

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

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

eval "1+1"
Reply to oth

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

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

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

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

Reply to 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"
BigMath.PI(1).to_s

output:

=> "0.31415926535897932364198143965603e1"

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

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

output:

=> "3.141592653589793115997963468544"

check it...

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

output:

=> 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) => {
        callback(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;
                }
                console.log(text)
                rl.close()
            })
        })
    })
})
rl.on("close", () => {
    process.exit(0)
})

You need to Sign in before reply, if you don't have an account, please Sign up first.