从右边开始寻找整数的第k位
从右边开始寻找整数的第k位
Implement match_k, which takes in an integer k and returns a function that takes in a variable x and returns True if all the digits in x that are k apart are the same.
For example, match_k(2) returns a one argument function that takes in x and checks if digits that are 2 away in x are the same.
match_k(2)(1010) has the value of x = 1010 and digits 1, 0, 1, 0 going from left to right. 1 == 1 and 0 == 0, so the match_k(2)(1010) results in True.
match_k(2)(2010) has the value of x = 2010 and digits 2, 0, 1, 0 going from left to right. 2 != 1 and 0 == 0, so the match_k(2)(2010) results in False.
Important: You may not use strings or indexing for this problem.
Floor dividing by powers of 10 gets rid of the rightmost digits.
def match_k(k):
"""Returns a function that checks if digits k apart match.
>>> match_k(2)(1010)
True
>>> match_k(2)(2010)
False
>>> match_k(1)(1010)
False
>>> match_k(1)(1)
True
>>> match_k(1)(2111111111111111)
False
>>> match_k(3)(123123)
True
>>> match_k(2)(123123)
False
"""
def check(x):
while x // (10 ** k) > 0:
if (x % 10) != (x // (10 ** k)) % 10:
return False
x //= 10
return True
return check
分析:
- 判断最后一位与右边数第k位数字是否相同:
(x % 10) != (x // (10 ** k)) % 10 - 如果不相同,则这个数肯定不符合题目要求,直接返回
False - 如果相同,则将比较位置转为左手边的下一个数字:
x //= 10
从右边开始寻找整数的第k位的更多相关文章
- A + B,末k位不相同
题目描述 读入两个小于10000的正整数A和B,计算A+B.需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1. 输入描述: 测试输入包含若干测试用例,每个测试用例占一行,格式为& ...
- 找出整数中第k大的数
一 问题描述: 找出 m 个整数中第 k(0<k<m+1)大的整数. 二 举例: 假设有 12 个整数:data[1, 4, -1, -4, 9, 8, 0, 3, -8, 11, 2 ...
- [经典算法题]寻找数组中第K大的数的方法总结
[经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26 字体:[大 中 小] 打印复制链接我要评论 今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...
- 编写函数求整形数组a中存储的m个不重复的整数的第k大的整数(其中m>=1,1<=k<=m)很简单的一个思路是酱紫的:管他辣么多干啥,上来一把排序然后直接得答案
/** * @author:(LiberHome) * @date:Created in 2019/2/28 20:38 * @description: * @version:$ *//*编写函数求整 ...
- 整数划分为k份
题目 将整数n分成k份,且每份不能为空,任意两个方案不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有多少种不同的分法. 输入 ...
- 寻找区间内第k小的数
sort排序 这是最直接暴力的方法,时间复杂度为\(O(nlog_n)\) 直接排序,输出第k小的值即可 #include <iostream> #include <algorith ...
- 线性基 - 寻找异或第K大
XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A ...
- Algorithm --> n位数去掉k位后找最小数
去掉K位求取最小数 一个n位的数,去掉其中的k位,怎样使留下来的(n-k)位数按原来的前后顺序组成的数最小 例如 8314925去掉4个数,留下125最小,注意有前后顺序要求,要是没有顺序当然是123 ...
- 取n的第k位
实例二:取n的第k位 方法:a>> k & 1 某值a右移K位后与整数“1”进行与运算.即把需要第几位就右移几位. 例子: 0000 1000 ------8右移3位 0000 0 ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
随机推荐
- KingbaseES Json 系列一:Json构造函数
KingbaseES Json 系列一--Json构造函数(JSON,ROW_TO_JSON,TO_JSON,TO_JSONB) JSON 数据类型是用来存储 JSON(JavaScript Obje ...
- 一个可以让你有更多时间摸鱼的WPF控件(一)
前言 我们平时在开发软件的过程中,有这样一类比较常见的功能,它没什么技术含量,开发起来也没有什么成就感,但是你又不得不花大量的时间来处理它,它就是对数据的增删改查.当我们每增加一个需求就需要对应若干个 ...
- gRPC入门学习之旅(四)
gRPC入门学习之旅(一) gRPC入门学习之旅(二) gRPC入门学习之旅(三) 实现定义的服务 9.在"解决方案资源管理器"中,使用鼠标左键选中"Services&q ...
- 【已解决】aconda3 创建和切换jupyter Kernel(安装好了tensorflow在jupyter中无法使用)
如下图:在这里更换python环境内核(如果你把tensorflow安装在了一个新建的虚拟环境) 1. 创建新的环境(或者是直接激活进入已经安装了tensorflow的环境) conda create ...
- python整理1992、2009国家标准学科分类及代码数据并存入MySQL数据库
文件内容 处理结果 代码 1 import pandas as pd 2 import pymysql 3 4 5 def get_subject_1992(): 6 res={} 7 the_for ...
- #Dijkstra#洛谷 4943 密室
题目 分析 考虑答案只可能是分别到或者哈利一个人到两个房间, 那么在罗恩的时候先不建不可走的边,等到哈利走的时候再建边 代码 #include <cstdio> #include < ...
- 构筑智能未来的开源 .Net AI知识库/智能体项目
在这个信息爆炸的时代,我们如何快速准确地从汪洋大海的数据中抽取真正有价值的知识呢?AntSK,一个基于.NET开发的人工智能知识库和智能体项目,似乎给出了一个新颖的答案.今天,就让我们一起深入了解An ...
- JS实现文件转base64
核心: function file2base64(){ fileAddress = document.getElementById("fileImage").files[0]; f ...
- C++调用Python-4:调用Python函数,传参数字
# mytest.py def myadd(a, b): print("this is test python print add function") return a+b #i ...
- MogDB 操作系统优化指南
MogDB 操作系统优化指南 本文出处:https://www.modb.pro/db/413280 在性能调优过程中,可以根据实际业务情况修改关键操作系统(OS)配置参数,以提升 MogDB 数据库 ...