# Categrory · Coding

## Details

Let be `n` an integer prime with `10` e.g. `7`.

`1/7 = 0.142857 142857 142857 ...`.

We see that the decimal part has a cycle: `142857`. The length of this cycle is `6`. In the same way:

`1/11 = 0.09 09 09 ...`. Cycle length is `2`.

Given an integer n (n > 1), the function cycle(n) returns the length of the cycle if n and 10 are coprimes, otherwise returns -1.

Exemples:

``````cycle(5) = -1
cycle(13) = 6 -> 0.076923 076923 0769
cycle(21) = 6 -> 0.047619 047619 0476
cycle(27) = 3 -> 0.037 037 037 037 0370
cycle(33) = 2 -> 0.03 03 03 03 03 03 03 03
cycle(37) = 3 -> 0.027 027 027 027 027 0
cycle(94) = -1

cycle(22) = -1 since 1/22 ~ 0.0 45 45 45 45 ...
``````

Note

• Translators are welcome for all languages.

## Details

Your friend recently has shown you some chess puzzles he found somewhere. It’s not your ordinary chess: but the mysterious, distinct relative of chess, the legendary 象棋, also known as Xiangqi/Chinese chess! As a Chinese, you know that your friend doesn’t know anything in Chinese besides recognizing a few sigils (for example, he doesn’t know what is `七步擒士` or `雙馬飲泉`), so it’s probably likely that the “puzzles” he got is actually bogus, and the pieces are not placed properly. However, you don’t want to actually teach him Xiangqi either (yet), so the best tactic you could come up with is to write a validator program and let your friend use it instead of using you.

You will be given an ASCII board. Xiangqi board is 9 tiles wide and 10 tiles high, with the pieces placed as follows:

``````Example (the starting Xiangqi board):

｜Ｘ｜
砲　＋－＋　砲

－－－－－－－－－
－－－－－－－－－

炮　＋－＋　炮
｜Ｘ｜

The bottom and top half corresponds to red and black pieces, respectively.
Note that red and black have different characters to distinguish the same piece of both sides.
``````

Your function, `chessValidator`, should determine whether the pieces in argument `board` are placed legally. Unlike the chess you’re familiar with, there are restrictions as to where the various chess pieces can be at:

## 要点

• 除法，求余，因子
• `Number.MAX_SAFE_INTEGER`
• 二分法求方程根

## Details

Your task is determine lowest number base system in which the input `n` (base 10), expressed in this number base system, is all `1` in its digit. See an example:

‘7’ in base 2 is ‘111’ - fits! answer is 2

‘21’ in base 2 is ‘10101’ - contains ‘0’ does not fit ‘21’ in base 3 is ‘210’ - contains ‘0’ and ‘2’ does not fit ‘21’ in base 4 is ‘111’ - contains only ‘1’ it fits! answer is 4

`n` is always less than `Number.MAX_SAFE_INTEGER`.

## Born to be chained | 4kyu

• 对于 Object 的遍历；

## Details

Function composition is a powerful technique. For example:

``````function sum(x, y) {
return x + y;
}

function double(x) {
return sum(x, x);
}

function minus (x, y) {
return x - y;
}

return sum(x, 1);
}

double(sum(2, 3)); // 10
``````

But in complex expressions, composition may be difficult to understand. For example:

``````double(double(addOne(sum(7, minus(sum(5, sum(4, 5)), 4))))); // 72
``````

In this kata, we will implement a function that allows us to perform this by applying a fluid style:

``````c.sum(4, 5).sum(5).minus(4).sum(7).addOne().double().double().execute(); // 72
``````

Your job is implement the `chain` function:

``````function chain(fns) {
}

``````

As you can see, this function receives the methods to be chained and returns an object that allows you to call the chained methods. The result is obtained by calling the `execute`method.

Chained functions receive an arbitrary number of arguments. The first function in the chain receives all its arguments. In the other functions, the first argument is the result of the previous function and then it only receives the remainder arguments (second, third, etc.). The tests always pass the appropriate arguments and you do not have to worry about checking this.

Note that the chain can be reused (the internal state is not stored):

## 要点

• `__str__()` 的几种定义方法：如 `"%s" % value``'{}'.format()`
• A `and` B `and` C
• `if``else` 单行语句
• `for``in` 单行语句
• `join()` 函数
• `str()` 函数与 `__str__()` 方法

## Details

A `Tree` consists of a root, which is of type `Node`, and possibly a left subtree of type `Tree` and possibly a right subtree of type `Tree`. If the left subtree is present, then all its nodes are less than the parent tree’s root and if the right tree is present, then all its nodes are greater than the parent tree’s root. In this kata, classes `Tree` and `Node` have been provided. However, the methods `__eq__`, `__ne__`, and `__str__` are missing from the `Tree` class. Your job is to provide the implementation of these methods. The example test cases should provide enough information to implement these methods correctly.

As an illustrative example, here is the string representation of a tree that has two nodes, ‘B’ at the root and ‘C’ at the root of the right subtree. The left subtree is missing and the right subtree is a leaf, i.e., has no subtrees:

``````'[_ B [C]]'
``````

This tree is obtained by evaluating the following expression:

``````Tree(Node('B'), None, Tree(Node('C'))))
``````

Notice in particular that when one subtree, but not both, is missing, an underscore is in its place, a single space separates the root node from the subtrees, and when both subtrees are missing, the root node is enclosed in brackets.

## Details

As part of a broader functionality you need to develop an argument mapper.

The function receives a function object as first parameter and an unknown number of arguments [zero to many]. You have to return an associative array that maps the name of an argument and its related value.

The usage is:

``````function func1(arg1, arg2) { ... }

var map = createArgumentMap(func1,'valueOfArg1', 'valueOfArg2');
console.log(map['arg1']);  // writes 'valueOfArg1'
console.log(map['arg2']);  // writes 'valueOfArg2'
``````

The passed values are in the same order as they appear in the function object.

Invalid inputs, e.g. non-function objects, or wrong number of arguments, are not considered.

Hajime!

## 要点

• `decorator` 装饰器用法
• `try``except``else` control flow
• `getattr()``setattr()`
• `__dict__.update()`

## Details

Implement the functools.wraps decorator, which is used to preserve the name and docstring of a decorated function. Your decorator must not modify the behavior of the decorated function. Here’s an example :

``````def identity(func):
@wraps(func)
def wrapper(*args, **kwargs):
"""Wraps func"""
return func(*args, **kwargs)
return wrapper

@identity
def return_one():
"""Return one"""
return 1

return_one.__name__ == 'return_one' # If wraps hadn't been used, __name__ would be equal to 'wrapper'
return_one.__doc__ == 'Return one' # If wraps hadn't been used, __doc__ would be equal to 'Wraps func'
``````

Note: of course, you may not use the functools module for this kata.

## Details

Write a function `defaultArguments`. It takes a function as an argument, along with an object containing default values for that function’s arguments, and returns another function which defaults to the right values.

You cannot assume that the function’s arguments have any particular names.

You should be able to call `defaultArguments` repeatedly to change the defaults.

``````function add(a,b) { return a+b;};

add_ = defaultArguments(add_,{c:3}); // doesn't do anything, since c isn't an argument
``````

HINT: This problem requires using `Fuction.prototype.toString()` in order to extract a function’s argument list