Skip to content

30秒代码

ts
function anagrams(str: string): string[] {
  if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str]
  return str
    .split(``)
    .reduce(
      (acc, letter, i) =>
        acc.concat(
          anagrams(str.slice(0, i) + str.slice(i + 1)).map(
            (val) => letter + val,
          ),
        ),
      [],
    )
}
console.log(anagrams(`abcd`))
export { anagrams }
function anagrams(str: string): string[] {
  if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str]
  return str
    .split(``)
    .reduce(
      (acc, letter, i) =>
        acc.concat(
          anagrams(str.slice(0, i) + str.slice(i + 1)).map(
            (val) => letter + val,
          ),
        ),
      [],
    )
}
console.log(anagrams(`abcd`))
export { anagrams }
ts
/*
 * @Author: frozen521
 * @Date:   2019-03-15 20:57:33
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 20:57:43
 */

/**
 * @Author: yanni
 * @Description:给定一个 key 和一个 set 作为参数,给定上下文时调用它们。主要用于函数组合。使用闭包以存储的参数调用存储的 key 。
 * @Date: 21:36 2019/3/15
 * @Modified By:
 * @Params:
 */

const call =
  (key, ...args) =>
  (context) =>
    context[key](...args)
Promise.resolve([1, 2, 3])
  .then(call(`map`, (x) => 2 * x))
  .then(console.log) //[ 2, 4, 6 ]
const map = call.bind(null, `map`)
Promise.resolve([1, 2, 3])
  .then(map((x) => 2 * x))
  .then(console.log) //[ 2, 4, 6 ]
export {}
/*
 * @Author: frozen521
 * @Date:   2019-03-15 20:57:33
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 20:57:43
 */

/**
 * @Author: yanni
 * @Description:给定一个 key 和一个 set 作为参数,给定上下文时调用它们。主要用于函数组合。使用闭包以存储的参数调用存储的 key 。
 * @Date: 21:36 2019/3/15
 * @Modified By:
 * @Params:
 */

const call =
  (key, ...args) =>
  (context) =>
    context[key](...args)
Promise.resolve([1, 2, 3])
  .then(call(`map`, (x) => 2 * x))
  .then(console.log) //[ 2, 4, 6 ]
const map = call.bind(null, `map`)
Promise.resolve([1, 2, 3])
  .then(map((x) => 2 * x))
  .then(console.log) //[ 2, 4, 6 ]
export {}
ts
/*
 * @Author: frozen521
 * @Date:   2019-03-15 21:44:31
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 21:45:56
 */
const capitalize = ([first, ...rest]: [string, string[]], lowerRest = false) =>
  first.toUpperCase() +
  (lowerRest ? rest.join(``).toLowerCase() : rest.join(``))

console.log(capitalize(`goob`))

const m = `letitgo`

function say([f, ...r]) {
  console.log(`f is${f}`)
  console.log(r)
}
console.log(say(m))
export { capitalize, say }
/*
 * @Author: frozen521
 * @Date:   2019-03-15 21:44:31
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 21:45:56
 */
const capitalize = ([first, ...rest]: [string, string[]], lowerRest = false) =>
  first.toUpperCase() +
  (lowerRest ? rest.join(``).toLowerCase() : rest.join(``))

console.log(capitalize(`goob`))

const m = `letitgo`

function say([f, ...r]) {
  console.log(`f is${f}`)
  console.log(r)
}
console.log(say(m))
export { capitalize, say }
ts
/*
 * @Author: frozen521
 * @Date:   2019-03-15 20:59:26
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 20:59:40
 */

const collectInto =
  (fn) =>
  (...args) =>
    fn(args)
/*
 * @Author: frozen521
 * @Date:   2019-03-15 20:59:26
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 20:59:40
 */

const collectInto =
  (fn) =>
  (...args) =>
    fn(args)
ts
export const colorize = (...args: string[]) => ({
  black: `\x1b[30m${args.join(` `)}`,
  red: `\x1b[31m${args.join(` `)}`,
  green: `\x1b[32m${args.join(` `)}`,
  yellow: `\x1b[33m${args.join(` `)}`,
  blue: `\x1b[34m${args.join(` `)}`,
  magenta: `\x1b[35m${args.join(` `)}`,
  cyan: `\x1b[36m${args.join(` `)}`,
  white: `\x1b[37m${args.join(` `)}`,
  bgBlack: `\x1b[40m${args.join(` `)}\x1b[0m`,
  bgRed: `\x1b[41m${args.join(` `)}\x1b[0m`,
  bgGreen: `\x1b[42m${args.join(` `)}\x1b[0m`,
  bgYellow: `\x1b[43m${args.join(` `)}\x1b[0m`,
  bgBlue: `\x1b[44m${args.join(` `)}\x1b[0m`,
  bgMagenta: `\x1b[45m${args.join(` `)}\x1b[0m`,
  bgCyan: `\x1b[46m${args.join(` `)}\x1b[0m`,
  bgWhite: `\x1b[47m${args.join(` `)}\x1b[0m`,
})
console.log(colorize(`foo`).red) // 'foo' (red letters)
console.log(colorize(`foo`, `bar`).bgBlue) // 'foo bar' (blue background)
console.log(colorize(colorize(`foo`).yellow, colorize(`foo`).green).bgWhite)
export const colorize = (...args: string[]) => ({
  black: `\x1b[30m${args.join(` `)}`,
  red: `\x1b[31m${args.join(` `)}`,
  green: `\x1b[32m${args.join(` `)}`,
  yellow: `\x1b[33m${args.join(` `)}`,
  blue: `\x1b[34m${args.join(` `)}`,
  magenta: `\x1b[35m${args.join(` `)}`,
  cyan: `\x1b[36m${args.join(` `)}`,
  white: `\x1b[37m${args.join(` `)}`,
  bgBlack: `\x1b[40m${args.join(` `)}\x1b[0m`,
  bgRed: `\x1b[41m${args.join(` `)}\x1b[0m`,
  bgGreen: `\x1b[42m${args.join(` `)}\x1b[0m`,
  bgYellow: `\x1b[43m${args.join(` `)}\x1b[0m`,
  bgBlue: `\x1b[44m${args.join(` `)}\x1b[0m`,
  bgMagenta: `\x1b[45m${args.join(` `)}\x1b[0m`,
  bgCyan: `\x1b[46m${args.join(` `)}\x1b[0m`,
  bgWhite: `\x1b[47m${args.join(` `)}\x1b[0m`,
})
console.log(colorize(`foo`).red) // 'foo' (red letters)
console.log(colorize(`foo`, `bar`).bgBlue) // 'foo bar' (blue background)
console.log(colorize(colorize(`foo`).yellow, colorize(`foo`).green).bgWhite)
ts
/*
 * @Author: frozen521
 * @Date:   2019-03-15 21:06:10
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 21:07:21
 */
const compact = (arr: any[]) => arr.filter(Boolean)
console.log(compact([0, 1, false, 2, ``, 3, `a`, `e` * 23, NaN, `s`, 34]))
/*
 * @Author: frozen521
 * @Date:   2019-03-15 21:06:10
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 21:07:21
 */
const compact = (arr: any[]) => arr.filter(Boolean)
console.log(compact([0, 1, false, 2, ``, 3, `a`, `e` * 23, NaN, `s`, 34]))
ts
/*
 * @Author: frozen521
 * @Date:   2019-03-15 21:18:39
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-16 10:27:14
 */
let n = readline()
let allmoney = 1024 - n
let m64 = parseInt(allmoney / 64)
let m64rest = allmoney - 64 * m64
let m16 = parseInt(m64rest / 16)
let m16rest = m64rest - 16 * m16
let m4 = parseInt(m16rest / 4)
let m4rest = m16rest - 4 * m4
print(m64 + m16 + m4 + m4rest)
export {}
/*
 * @Author: frozen521
 * @Date:   2019-03-15 21:18:39
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-16 10:27:14
 */
let n = readline()
let allmoney = 1024 - n
let m64 = parseInt(allmoney / 64)
let m64rest = allmoney - 64 * m64
let m16 = parseInt(m64rest / 16)
let m16rest = m64rest - 16 * m16
let m4 = parseInt(m16rest / 4)
let m4rest = m16rest - 4 * m4
print(m64 + m16 + m4 + m4rest)
export {}
ts
/*
 * @Author: frozen521
 * @Date:   2019-03-15 21:21:21
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 21:25:15
 */

/**
 * 返回数组中所有 val 的索引。 如果 val 从不出现,则返回 [] 。

使用 Array.forEach() 循环元素和 Array.push() 来存储匹配元素的索引。 返回索引数组。
 * @Author    yangzq
 * @DateTime  2019-03-15
 * @copyright [copyright]
 * @license   [license]
 * @version   [version]
 * @param     {number[]}    arr [description]
 * @param     {[type]}    val [description]
 * @return    {[type]}        [description]
 */
const indexofall = (arr, val) => {
  const indices = []
  arr.forEach((el, i) => {
    el === val && indices.push(i)
  })
  console.log(indices)
}
indexofall([1, 2, 3, 1, 2, 3], 1)
/*
 * @Author: frozen521
 * @Date:   2019-03-15 21:21:21
 * @Last Modified by:   frozen521
 * @Last Modified time: 2019-03-15 21:25:15
 */

/**
 * 返回数组中所有 val 的索引。 如果 val 从不出现,则返回 [] 。

使用 Array.forEach() 循环元素和 Array.push() 来存储匹配元素的索引。 返回索引数组。
 * @Author    yangzq
 * @DateTime  2019-03-15
 * @copyright [copyright]
 * @license   [license]
 * @version   [version]
 * @param     {number[]}    arr [description]
 * @param     {[type]}    val [description]
 * @return    {[type]}        [description]
 */
const indexofall = (arr, val) => {
  const indices = []
  arr.forEach((el, i) => {
    el === val && indices.push(i)
  })
  console.log(indices)
}
indexofall([1, 2, 3, 1, 2, 3], 1)

Released under the MIT License.