Skip to content

Problem 8: Remove Digits at Even Positions (100pts)

Problem

Write a function that takes a number, removes the digits in even positions (the 2nd, 4th, 6th, … from left to right), and returns the number formed by the remaining digits.

Warning:

You should not use data structures like lists or strings, or converting from other data structures to int using int(...) in this problem.

Violating this requirement may result in a loss of scores in online judge testing.

编写一个函数,接受一个数字作为参数,移除处于偶数位置的数字(从左到右数第 2 位、第 4 位、第 6 位等),并返回由剩余数字组成的新数字。

Warning

在本问题中,您不应使用列表或字符串等数据结构,也不应使用 int(...) 将其他数据结构转换回整数。

违反此要求可能导致在线判题测试失分。

def remove_even_position(n):
    """Removes the digits of n in even positions (the 2nd, 4th, 6th, … from left to right),
    and returns the number formed by the remaining digits.

    >>> remove_even_position(0)
    0
    >>> remove_even_position(10)
    1
    >>> remove_even_position(123)
    13
    >>> remove_even_position(123456)
    135
    """
    "*** YOUR CODE HERE ***"

Hints

  • 本来可以用 str 切片逃课的但是被制裁了...

  • 可以尝试获取所有奇数位的数(怎么获取?)然后拼在一起(怎么拼?)

  • 可以先获取n的位数(math.log(n, 10) 是一种选择?)也可以直接想想怎么写 while 的停止条件

Solutions