Code Monkey home page Code Monkey logo

fe-knowledge's People

Contributors

wuyongyu avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

fe-knowledge's Issues

扑克牌问题

背景

手中有一堆扑克牌, 不知道它的顺序。
第一步, 从牌顶拿出一张牌, 放到桌子上。
第二步, 从牌顶再拿一张牌, 放在手上牌的底部。
第三步, 重复第一步、第二步的操作, 直到手中所有的牌都放到了桌子上。
最后, 桌子上牌的顺序是:(牌底部)1,2,3,4,5,6,7,8,9,10,11,12,13(牌顶部)
请问, 刚开始拿在手里的牌的顺序是什么?


思路:

首先,这扑克牌问题主要考察逆向思维,就是先告诉我们结果,然后让我们顺藤摸瓜,一步一步到达开始。

写个简单例子:

var a = [1, 2, 3, 4, 5];
function f(a){
    let b = [],
	c = [...a],
	len = a.length;
    for(let i = 0; i < len; i++){
	b.push(c.pop());
	c.unshift(c.pop());
    }
    return b;
}
f(a);  // b ==> [5, 3, 1, 2, 4];

这个例子是按照正向思维实现的,可是题目的要求,只给到我们b的的值,a的值是不知道的,有点像数学里面的求解函数。因此,我们只需要调整for循环里面的取值顺序,就能轻松解开。

var a = [5, 3, 1, 2, 4];
function f(a){
    let b = [],
	c = [...a],
	len = a.length;
    for(let i = 0; i < len; i++){
	b.push(c.pop());
	b.push(b.shift());
    }
    b.unshift(b.pop());
    return b;
}
f(a);  // b ==> [1, 2, 3, 4, 5]

最后规范一下命名,增强可读性。

/**
 * Input 拿出牌的顺序 1,2,3,4,5,6,7,8,9,10,11,12,13
 * Output 牌堆原来的顺序
 */
function getCardsOrder(input){
    let output = [],
        copyInput = [...input],
        len = input.length;
    for(let i = 0; i < len; i++){
        output.push(copyInput.pop());
        output.push(output.shift());
    }
    output.unshift(output.pop());
    return output;
}

JavaScript函数和React源码

// 斐波那契函数

function fibonacci(num){
    if(num === 1 || num === 2){
        return 1;
    }
    return fibonacci(num - 1) + fibonacci(num - 2);
}

===

(0.8 + 0.7 + 0.6 + 0.9) / 4 // 0.75
(0.6 + 0.8 + 0.7 + 0.9) / 4 // 0.7499999999999999

(6 + 8 + 7 + 9) / 4 // 7.5
(8 + 7 + 6 + 9) / 4 // 7.5

在字典中最好使用整数值

Commit Message Emoji

commit-message-emoji

Commit type Emoji
Initial commit 🎉 :tada:
Version tag 🔖 :bookmark:
New feature :sparkles:
Bugfix 🐛 :bug:
Metadata 📇 :card_index:
Documentation 📚 :books:
Documenting source code 💡 :bulb:
Performance 🐎 :racehorse:
Cosmetic 💄 :lipstick:
Tests 🚨 :rotating_light:
Adding a test :white_check_mark:
General update :zap:
Improve format/structure 🎨 :art:
Refactor code 🔨 :hammer:
Removing code/files 🔥 :fire:
Continuous Integration 💚 :green_heart:
Security 🔒 :lock:
Upgrading dependencies ⬆️ :arrow_up:
Downgrading dependencies ⬇️ :arrow_down:
Lint 👕 :shirt:
Translation 👽 :alien:
Text 📝 :pencil:
Critical hotfix 🚑 :ambulance:
Deploying stuff 🚀 :rocket:
Fixing on MacOS 🍎 :apple:
Fixing on Linux 🐧 :penguin:
Fixing on Windows 🏁 :checkered_flag:
Work in progress 🚧 :construction:
Adding CI build system 👷 :construction_worker:
Analytics or tracking code 📈 :chart_with_upwards_trend:
Removing a dependency :heavy_minus_sign:
Adding a dependency :heavy_plus_sign:
Docker 🐳 :whale:
Configuration files 🔧 :wrench:
Package.json in JS 📦 :package:
Merging branches 🔀 :twisted_rightwards_arrows:
Bad code / need improv. 💩 :hankey:
Reverting changes :rewind:
Breaking changes 💥 :boom:
Code review changes 👌 :ok_hand:
Accessibility :wheelchair:
Move/rename repository 🚚 :truck:
Other Be creative

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.