Tag · Python

1/n- Cycle | 6kyu

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.

Task

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.

Binary Search Trees | 5kyu

要点

  • __str__() 的几种定义方法:如 "%s" % value'{}'.format()
  • A and B and C
  • ifelse 单行语句
  • forin 单行语句
  • 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.

分类

标签